aboutsummaryrefslogtreecommitdiffstats
path: root/config-class-plugin/src/main/java/com/yahoo/vespa/ConfigGenMojo.java
blob: c1c8a2e837344437e3d3485fe6540ade43b9c450 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa;

import com.yahoo.config.codegen.MakeConfig;
import com.yahoo.config.codegen.MakeConfigProperties;
import com.yahoo.config.codegen.PropertyException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static com.yahoo.config.codegen.DefParser.DEFAULT_PACKAGE_PREFIX;


/**
 * Goal which generates config classes from def-files.
 */
@Mojo(name = "config-gen", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
public class ConfigGenMojo extends AbstractMojo {
    @Parameter( defaultValue = "${project}", readonly = true )
    private MavenProject project;

    /**
     * Generate source to here.
     */
    @Parameter(property = "plugin.configuration.outputDirectory",
            defaultValue = "${project.build.directory}/generated-sources/vespa-configgen-plugin")
    private File outputDirectory;

	/**
	 * Location of def files to generate source from, comma separated list of directories.
     */
    @Parameter(property = "plugin.configuration.defFilesDirectories",
            defaultValue = "src/main/resources/configdefinitions")
    private String defFilesDirectories;

    /**
     * Set to 'false' to create pure data config classes without any vespa framework code
     */
    @Parameter(property = "plugin.configuration.useFramework", defaultValue = "true")
    private Boolean useFramework;

    /**
     * Set to 'false' to allow generation of config classes that have the default namespace 'config'.
     */
    @Parameter(property = "plugin.configuration.requireNamespace", defaultValue = "true")
    private Boolean requireNamespace;

    /**
     * Package prefix of generated configs. The resulting package name will be packagePrefix.namespace if specified.
     */
    @Parameter(property = "plugin.configuration.packagePrefix", defaultValue = DEFAULT_PACKAGE_PREFIX)
    private String packagePrefix;

    /**
     * If true, the config sources are only intended for use during testing.
     *
     */
    @Parameter(property = "plugin.configuration.testConfig", defaultValue = "false")
    private boolean testConfig;

    /**
     * Returns List of all def-files in all defFilesDirectories, including path.
     * @return The list of def-files.
     */
    private List<String> getDefFileNames() {
        List<String> defFileNames = new ArrayList<>();

        String[] dirNames = defFilesDirectories.split(",");
        List<File> dirs = new ArrayList<>();
        for (String dirName : dirNames) {
            File dir = new File(project.getBasedir(), dirName.trim());
            if (dir.isDirectory() && dir.canRead()) {
                dirs.add(dir);
            }
        }
        for (File dir : dirs) {
            String[] dirFiles = dir.list(new FilenameFilter () {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".def");
                }
            });
            for (String filename : dirFiles) {
                defFileNames.add(dir.toString() + File.separator + filename);
            }
        }
        return defFileNames;
    }

    public void execute()
        throws MojoExecutionException
    {
        List<String> defFileNames = getDefFileNames();

        // Silent failure when there are no def-files to process...
        if (defFileNames.size() == 0) {
            return;
        }

        String configSpec = String.join(",", defFileNames);

        boolean generateSources;
        // optionally create the output directory
        File f = outputDirectory;
        if (!f.exists() ) {
            f.mkdirs();
            generateSources = true;
            getLog().debug("Output dir does not exist");
        } else {
            getLog().debug("Output dir exists");
            generateSources = isSomeGeneratedFileStale(outputDirectory, defFileNames);
        }

        if (generateSources) {
            getLog().debug("Will generate config class files");
            try {
                MakeConfigProperties config = new MakeConfigProperties(outputDirectory.toString(),
                                                                       configSpec,
                                                                       null,
                                                                       null,
                                                                       null,
                                                                       useFramework.toString(),
                                                                       packagePrefix);
                if (!MakeConfig.makeConfig(config)) {
                    throw new MojoExecutionException("Failed to generate config for: " + configSpec);
                }
            } catch (IOException | PropertyException e) {
                throw new MojoExecutionException("Failed to generate config for: " + configSpec, e);
            }
        } else {
            getLog().debug("No changes, will not generate config class files");
        }

        // We have created files, so add the output directory to the compile source root
        addSourceRoot(outputDirectory.toString());
    }

    private boolean isSomeGeneratedFileStale(File outputDirectory, List<String> defFileNames) {
        long oldestGeneratedModifiedTime = walk(outputDirectory.toPath())
                    .filter(Files::isRegularFile)
                    .map(Path::toFile)
                    .peek(f -> getLog().debug("Checking generated file " + f))
                    .mapToLong(File::lastModified)
                    .min()
                    .orElse(Long.MAX_VALUE);

        long lastModifiedSource = defFileNames.stream()
                .peek(sourceFile -> getLog().debug("Checking source file " + sourceFile))
                .map(File::new)
                .mapToLong(File::lastModified)
                .max()
                .orElse(0L);

        getLog().debug("lastModifiedSource: " + lastModifiedSource + ", oldestTGeneratedModified: " + oldestGeneratedModifiedTime);
        return lastModifiedSource > oldestGeneratedModifiedTime;
    }

    private static Stream<Path> walk(Path path) {
        try {
            return Files.walk(path);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private void addSourceRoot(String outputDirectory) {
        if (testConfig) {
            project.addTestCompileSourceRoot(outputDirectory);
        } else {
            project.addCompileSourceRoot(outputDirectory);
        }
    }
}