1/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package io.fd.honeycomb.common.scripts
18
19import groovy.text.SimpleTemplateEngine
20
21import java.nio.file.Files
22import java.nio.file.Paths
23
24/**
25 * Check/generate and repair Readme.adoc for a honeycomb module.
26 */
27class ReadmeGenerator {
28
29    static final def DEFAULT_README = ReadmeGenerator.getResource("/readme/readmeDefaultContent").text
30
31    static final def ADOC_FOLDER = "asciidoc"
32    static final def README = "Readme"
33    static final def README_FILE = "${README}.adoc"
34    static final def README_HTML = "${README}.html"
35    static final def SITE_FOLDER = "site"
36    static final def INDEX_HTML = "index.html"
37
38    public static void checkReadme(project, properties, log) {
39        log.info "Checking ${ADOC_FOLDER}/${README_FILE}"
40        def asciidoc = Paths.get(project.getBasedir().toString(), ADOC_FOLDER)
41        def readme = Paths.get(asciidoc.toString(), README_FILE)
42        if (!Files.exists(readme)) {
43            log.info "Generating ${readme}"
44            Files.createDirectories(asciidoc)
45            Files.createFile(readme)
46            readme.toFile().text = new SimpleTemplateEngine().createTemplate(DEFAULT_README)
47                    .make(["artifactId": project.artifactId])
48                    .toString()
49        }
50    }
51
52    public static void fixSite(project, properties, log) {
53        def index = Paths.get(project.build.directory.toString(), SITE_FOLDER, INDEX_HTML)
54        if (Files.exists(index)) {
55            log.info "Fixing links in generated site"
56            def html = index.toFile().text
57            log.info "Fixing ${ADOC_FOLDER} ${README_HTML} link"
58            index.toFile().text = html.replaceAll("[./]*${README}\\.html", README_HTML)
59        }
60    }
61}
62