17236617fSMaros Marsalek/*
2f5e34776SJan Srnicek * Copyright (c) 2017 Cisco and/or its affiliates.
37236617fSMaros Marsalek *
47236617fSMaros Marsalek * Licensed under the Apache License, Version 2.0 (the "License");
57236617fSMaros Marsalek * you may not use this file except in compliance with the License.
67236617fSMaros Marsalek * You may obtain a copy of the License at:
77236617fSMaros Marsalek *
87236617fSMaros Marsalek *     http://www.apache.org/licenses/LICENSE-2.0
97236617fSMaros Marsalek *
107236617fSMaros Marsalek * Unless required by applicable law or agreed to in writing, software
117236617fSMaros Marsalek * distributed under the License is distributed on an "AS IS" BASIS,
127236617fSMaros Marsalek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137236617fSMaros Marsalek * See the License for the specific language governing permissions and
147236617fSMaros Marsalek * limitations under the License.
157236617fSMaros Marsalek */
167236617fSMaros Marsalek
177236617fSMaros Marsalekpackage io.fd.honeycomb.common.scripts
187236617fSMaros Marsalek
197236617fSMaros Marsalekimport groovy.text.SimpleTemplateEngine
207236617fSMaros Marsalek
217236617fSMaros Marsalekimport java.nio.file.Files
227236617fSMaros Marsalekimport java.nio.file.Paths
237236617fSMaros Marsalek
247236617fSMaros Marsalek/**
257236617fSMaros Marsalek * Check/generate and repair Readme.adoc for a honeycomb module.
267236617fSMaros Marsalek */
277236617fSMaros Marsalekclass ReadmeGenerator {
287236617fSMaros Marsalek
29f5e34776SJan Srnicek    static final def DEFAULT_README = ReadmeGenerator.getResource("/readme/readmeDefaultContent").text
307236617fSMaros Marsalek
317236617fSMaros Marsalek    static final def ADOC_FOLDER = "asciidoc"
327236617fSMaros Marsalek    static final def README = "Readme"
337236617fSMaros Marsalek    static final def README_FILE = "${README}.adoc"
347236617fSMaros Marsalek    static final def README_HTML = "${README}.html"
357236617fSMaros Marsalek    static final def SITE_FOLDER = "site"
367236617fSMaros Marsalek    static final def INDEX_HTML = "index.html"
377236617fSMaros Marsalek
387236617fSMaros Marsalek    public static void checkReadme(project, properties, log) {
397236617fSMaros Marsalek        log.info "Checking ${ADOC_FOLDER}/${README_FILE}"
407236617fSMaros Marsalek        def asciidoc = Paths.get(project.getBasedir().toString(), ADOC_FOLDER)
417236617fSMaros Marsalek        def readme = Paths.get(asciidoc.toString(), README_FILE)
427236617fSMaros Marsalek        if (!Files.exists(readme)) {
437236617fSMaros Marsalek            log.info "Generating ${readme}"
447236617fSMaros Marsalek            Files.createDirectories(asciidoc)
457236617fSMaros Marsalek            Files.createFile(readme)
467236617fSMaros Marsalek            readme.toFile().text = new SimpleTemplateEngine().createTemplate(DEFAULT_README)
477236617fSMaros Marsalek                    .make(["artifactId": project.artifactId])
487236617fSMaros Marsalek                    .toString()
497236617fSMaros Marsalek        }
507236617fSMaros Marsalek    }
517236617fSMaros Marsalek
527236617fSMaros Marsalek    public static void fixSite(project, properties, log) {
537236617fSMaros Marsalek        def index = Paths.get(project.build.directory.toString(), SITE_FOLDER, INDEX_HTML)
547236617fSMaros Marsalek        if (Files.exists(index)) {
557236617fSMaros Marsalek            log.info "Fixing links in generated site"
567236617fSMaros Marsalek            def html = index.toFile().text
577236617fSMaros Marsalek            log.info "Fixing ${ADOC_FOLDER} ${README_HTML} link"
587236617fSMaros Marsalek            index.toFile().text = html.replaceAll("[./]*${README}\\.html", README_HTML)
597236617fSMaros Marsalek        }
607236617fSMaros Marsalek    }
617236617fSMaros Marsalek}
62