-
Notifications
You must be signed in to change notification settings - Fork 169
/
Run.java
143 lines (124 loc) · 5.04 KB
/
Run.java
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
/*
* This file is part of GumTree.
*
* GumTree is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GumTree is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2011-2015 Jean-Rémy Falleri <jr.falleri@gmail.com>
* Copyright 2011-2015 Floréal Morandat <florealm@gmail.com>
*/
package com.github.gumtreediff.client;
import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.gen.Registry;
import com.github.gumtreediff.gen.TreeGenerator;
import org.reflections.Reflections;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class Run {
public static class Options implements Option.Context {
@Override
public Option[] values() {
return new Option[]{
new Option("-c", "Set global property (-c property value). "
+ "Properties do not need to be prefixed by gumtree.", 2) {
@Override
protected void process(String name, String[] args) {
String key = args[0].startsWith("gumtree.") ? args[0] : "gumtree." + args[0];
System.setProperty(key, args[1]);
}
},
new Option.Verbose(),
new Help(this)
};
}
}
public static void initGenerators() {
Reflections reflections = new Reflections("com.github.gumtreediff.gen");
reflections.getSubTypesOf(TreeGenerator.class).forEach(
gen -> {
com.github.gumtreediff.gen.Register a =
gen.getAnnotation(com.github.gumtreediff.gen.Register.class);
if (a != null)
Generators.getInstance().install(gen, a);
});
}
public static void initClients() {
Reflections reflections = new Reflections("com.github.gumtreediff.client");
reflections.getSubTypesOf(Client.class).forEach(
cli -> {
com.github.gumtreediff.client.Register a =
cli.getAnnotation(com.github.gumtreediff.client.Register.class);
if (a != null)
Clients.getInstance().install(cli, a);
});
}
static {
initGenerators();
}
public static void startClient(String name, Registry.Factory<? extends Client> client, String[] args) {
try {
Client inst = client.newInstance(new Object[]{args});
try {
inst.run();
} catch (Exception e) {
System.err.printf("** Error while running client %s: %s\n", name, e);
}
} catch (InvocationTargetException e) {
System.err.printf("** Error while parsing option for %s:\n%s\n", name, e.getCause());
} catch (InstantiationException | IllegalAccessException e) {
System.err.printf("Can't instantiate client: '%s'\n%s\n", name, e);
e.printStackTrace();
}
}
public static void main(String[] args) {
Options opts = new Options();
args = Option.processCommandLine(args, opts);
initClients();
Registry.Factory<? extends Client> client;
if (args.length == 0) {
System.err.println("** No command given.");
displayHelp(System.err, opts);
} else if ((client = Clients.getInstance().getFactory(args[0])) == null) {
System.err.printf("** Unknown sub-command '%s'.\n", args[0]);
displayHelp(System.err, opts);
} else {
String[] a = new String[args.length - 1];
System.arraycopy(args, 1, a, 0, a.length);
startClient(args[0], client, a);
}
}
public static void displayHelp(PrintStream out, Option.Context ctx) {
out.println("Available Options:");
Option.displayOptions(out, ctx);
out.println("");
listCommand(out);
}
public static void listCommand(PrintStream out) {
out.println("Available Commands:");
for (Registry.Entry cmd: Clients.getInstance().getEntries())
out.println("* " + cmd);
}
static class Help extends Option.Help {
public Help(Context ctx) {
super(ctx);
}
@Override
public void process(String name, String[] args) {
displayHelp(System.out, context);
System.exit(0);
}
}
}