aboutsummaryrefslogtreecommitdiff
blob: 0a69442903fc6fcd663458c56058044871352a55 (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package org.gentoo.java.ebuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.gentoo.java.ebuilder.maven.MavenCache;
import org.gentoo.java.ebuilder.maven.MavenEbuilder;
import org.gentoo.java.ebuilder.maven.MavenParser;
import org.gentoo.java.ebuilder.maven.MavenProject;
import org.gentoo.java.ebuilder.portage.PortageParser;

/**
 * Main class.
 *
 * @author fordfrog
 */
public class Main {

    /**
     * Main method for launching the application.
     *
     * @param args command line arguments
     */
    public static void main(final String[] args) {
        @SuppressWarnings("UseOfSystemOutOrSystemErr")
        final Config config = new Config(new PrintWriter(System.out, true),
                new PrintWriter(System.err, true));

        if (args == null || args.length == 0) {
            printUsage(config);
            Runtime.getRuntime().exit(1);
        }

        parseArgs(config, args);
        checkArgs(config);

        if (config.isRefreshCache()) {
            refreshCache(config);
        }

        if (config.isGenerateEbuild()) {
            generateEbuild(config);
        }

        config.getStdoutWriter().println("Finished!");
        config.getStdoutWriter().flush();
        config.getErrorWriter().flush();
    }

    /**
     * Checks whether correct arguments are passed.
     *
     * @param config application configuration
     */
    private static void checkArgs(final Config config) {
        if (config.isRefreshCache()) {
            if (config.getPortageTree() == null) {
                config.setPortageTree(Paths.get("/usr/portage"));
            }

            if (!config.getPortageTree().toFile().exists()) {
                config.getErrorWriter().println("ERROR: Portage tree "
                        + config.getPortageTree() + " does not exist.");
                Runtime.getRuntime().exit(1);
            }
        } else if (config.getPortageTree() != null) {
            config.getErrorWriter().println("WARNING: Portage tree is used "
                    + "only when refreshing cache.");
        }

        if (config.isGenerateEbuild()) {
            if (config.getDownloadUri() == null) {
                config.getErrorWriter().println(
                        "ERROR: --download-uri must be specified.");
                Runtime.getRuntime().exit(1);
            } else if (config.getEbuild() == null) {
                config.getErrorWriter().println(
                        "ERROR: --ebuild must be specified.");
                Runtime.getRuntime().exit(1);
            } else if (!config.getEbuild().getParent().toFile().exists()) {
                config.getErrorWriter().println("ERROR: Ebuild parent "
                        + "directory " + config.getEbuild().getParent()
                        + " does not exist.");
                Runtime.getRuntime().exit(1);
            } else if (config.getKeywords() == null) {
                config.getErrorWriter().println(
                        "ERROR: --keywords must be specified.");
                Runtime.getRuntime().exit(1);
            } else if (config.getWorkdir() == null) {
                config.getErrorWriter().println(
                        "ERROR: --workdir must be specified.");
                Runtime.getRuntime().exit(1);
            } else if (!config.getWorkdir().toFile().exists()) {
                config.getErrorWriter().println("ERROR: Workdir "
                        + config.getWorkdir().toFile().getPath()
                        + " does not exist.");
                Runtime.getRuntime().exit(1);
            } else if (config.getPomFiles().isEmpty()) {
                config.getErrorWriter().println(
                        "ERROR: --pom must be specified at least once.");
                Runtime.getRuntime().exit(1);
            }

            config.getPomFiles().stream().forEach((pomFile) -> {
                final File fullPath
                        = config.getWorkdir().resolve(pomFile).toFile();

                if (!fullPath.exists()) {
                    config.getErrorWriter().println("ERROR: POM file "
                            + fullPath + " does not exist.");
                    Runtime.getRuntime().exit(1);
                }
            });

            if (config.getSlot() == null) {
                config.setSlot("0");
            }
        } else if (config.getDownloadUri() != null) {
            config.getErrorWriter().println("WARNING: Download URI is used "
                    + "only when generating ebuild.");
        } else if (config.getEbuild() != null) {
            config.getErrorWriter().println(
                    "WARNING: Ebuild is used only when "
                    + "generating ebuild.");
        } else if (config.getKeywords() != null) {
            config.getErrorWriter().println("WARNING: Keywords are used only "
                    + "when generating ebuild.");
        } else if (config.getLicense() != null) {
            config.getErrorWriter().println("WARNING: License is used only "
                    + "when generating ebuild.");
        } else if (!config.getPomFiles().isEmpty()) {
            config.getErrorWriter().println("WARNING: pom.xml is used only "
                    + "when generating ebuild.");
        } else if (config.getSlot() != null) {
            config.getErrorWriter().println("WARNING: SLOT is used only when "
                    + "generating ebuild.");
        } else if (config.getWorkdir() != null) {
            config.getErrorWriter().println("WARNING: Workdir is used only "
                    + "when generating ebuild.");
        }

        if (!config.isRefreshCache()
                && !config.getCacheFile().toFile().exists()) {
            config.getErrorWriter().println("ERROR: Cache file does not exist. "
                    + "First you must generate it using --refresh-cache.");
            Runtime.getRuntime().exit(1);
        }
    }

    /**
     * Processed generation of ebuild.
     *
     * @param config application configuration
     */
    private static void generateEbuild(final Config config) {
        final MavenCache mavenCache = new MavenCache();
        mavenCache.loadCache(config);

        final MavenParser mavenParser = new MavenParser();
        final List<MavenProject> mavenProjects
                = mavenParser.parsePomFiles(config, mavenCache);

        final MavenEbuilder mavenEbuilder = new MavenEbuilder();
        mavenEbuilder.generateEbuild(config, mavenProjects, mavenCache);
    }

    /**
     * Parses command line arguments.
     *
     * @param config application configuration container
     * @param args   command line arguments
     */
    @SuppressWarnings("AssignmentToForLoopParameter")
    private static void parseArgs(final Config config, final String[] args) {
        for (int i = 0; i < args.length; i++) {
            final String arg = args[i];

            switch (arg) {
                case "--download-uri":
                case "-u":
                    i++;

                    try {
                        config.setDownloadUri(new URI(args[i]));
                    } catch (final URISyntaxException ex) {
                        config.getErrorWriter().println("ERROR: URI " + args[i]
                                + " is not valid.");
                    }

                    break;
                case "--ebuild":
                case "-e":
                    i++;
                    config.setEbuild(Paths.get(args[i]).toAbsolutePath().
                            normalize());
                    break;
                case "--generate-ebuild":
                case "-g":
                    config.setGenerateEbuild(true);
                    break;
                case "--keywords":
                case "-k":
                    i++;
                    config.setKeywords(args[i]);
                    break;
                case "--license":
                case "-l":
                    i++;
                    config.setLicense(args[i]);
                    break;
                case "--pom":
                case "-p":
                    i++;
                    config.addPomFile(Paths.get(args[i]));
                    break;
                case "--portage-tree":
                case "-t":
                    i++;
                    config.setPortageTree(Paths.get(args[i]).toAbsolutePath().
                            normalize());
                    break;
                case "--cache-file":
                    i++;
                    config.setCacheFile(Paths.get(args[i]).toAbsolutePath().
                            normalize());
                    break;
                case "--refresh-cache":
                case "-c":
                    config.setRefreshCache(true);
                    break;
                case "--slot":
                case "-s":
                    i++;
                    config.setSlot(args[i]);
                    break;
                case "--workdir":
                case "-w":
                    i++;
                    config.setWorkdir(Paths.get(args[i]).toAbsolutePath().
                            normalize());
                    break;
                default:
                    config.getErrorWriter().println("ERROR: Switch '" + args[i]
                            + "' is not supported.");
                    Runtime.getRuntime().exit(1);
            }
        }
    }

    /**
     * Prints application usage information.
     */
    private static void printUsage(final Config config) {
        try (final BufferedReader reader = new BufferedReader(
                new InputStreamReader(Main.class.getResourceAsStream(
                        "/usage.txt")))) {
            reader.lines().forEach((String line) -> {
                config.getStdoutWriter().println(line);
            });
        } catch (final IOException ex) {
            throw new RuntimeException("Failed to read usage from resource", ex);
        }
    }

    /**
     * Processes cache refresh.
     *
     * @param config application configuration
     */
    private static void refreshCache(final Config config) {
        final PortageParser portageParser = new PortageParser();
        portageParser.parseTree(config);
    }
}