summaryrefslogtreecommitdiffstats
path: root/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ModulesListGenerator.groovy
blob: c7a74d20e209f81a291e41ef4facc75081170335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.common.scripts

import groovy.text.SimpleTemplateEngine

import java.nio.file.Paths

/**
 * Generate modules-list file a honeycomb distribution.
 */
class ModulesListGenerator {

    static final def DEFAULT_MODULES_LIST = ModulesListGenerator.getResource("/modules/modulesListDefaultContent").text

    static final def MODULES_LIST_CONTENT_PROPERTY = "distribution.modules"
    static final def MODULES_FOLDER = "modules"
    static final def MODULE_LIST_FILE_SUFFIX = "-module-config"
    static final def SEPARATOR = ","

    public static void generate(project, properties, log) {
        // module configuration file extraction
        // builds project name from group,artifact and version to prevent overwriting
        // while building multiple distribution project
        def artifact = project.artifact
        def projectName = "${artifact.getGroupId()}_${artifact.getArtifactId()}_${artifact.getVersion()}".replace(".","-")

        log.info "Generating list of modules started by distribution ${projectName}"

        def activeModules = properties.getProperty(MODULES_LIST_CONTENT_PROPERTY, DEFAULT_MODULES_LIST)
                .tokenize(SEPARATOR)
                .collect { module -> module.trim() }

        log.info "Project ${projectName} : Found modules ${activeModules}"
        //creates folder modules

        def outputPath = Paths.get(project.build.outputDirectory, StartupScriptGenerator.MINIMAL_RESOURCES_FOLDER, MODULES_FOLDER)
        //creates module folder
        outputPath.toFile().mkdirs()

        def outputFile = Paths.get(outputPath.toString(), "${projectName}${MODULE_LIST_FILE_SUFFIX}").toFile()
        outputFile.createNewFile();
        log.info("Writing module configuration for distribution ${projectName} to ${outputPath}")

        if (activeModules.isEmpty()) {
            outputFile.text = new SimpleTemplateEngine().createTemplate(DEFAULT_MODULES_LIST).make(
                    ["groupId"   : project.groupId,
                     "artifactId": project.artifactId,
                     "version"   : project.version]).toString()
        } else {
            activeModules.add(0, "// Generated from ${project.groupId}/${project.artifactId}/${project.version}")
            outputFile.text = activeModules.join(System.lineSeparator)
        }
    }
}