-
Notifications
You must be signed in to change notification settings - Fork 6
/
Help.java
422 lines (369 loc) · 10.7 KB
/
Help.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 biz.aQute.gogo.commands.provider;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Formatter;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.felix.service.command.CommandSession;
import org.apache.felix.service.command.Descriptor;
import org.apache.felix.service.command.Parameter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import aQute.lib.converter.Converter;
import aQute.lib.exceptions.Exceptions;
import aQute.lib.justif.Justif;
import aQute.libg.glob.Glob;
import biz.aQute.gogo.commands.dtoformatter.DTOFormatter;
@SuppressWarnings("deprecation")
public class Help {
private final BundleContext context;
public Help(BundleContext bc, DTOFormatter formatter) {
context = bc;
formatter.build(Scope.class)
.inspect()
.field("name")
.field("description")
.format("commands", Scope::commands)
.line()
.field("name")
.format("commands", Scope::commands)
.part()
.as(Scope::toString);
formatter.build(Command.class)
.inspect()
.as(Command::inspect)
.line()
.as(Command::line)
.part()
.as(Command::toString);
}
@Descriptor("clears the screen")
public void clear(CommandSession s) {
for (int i = 0; i < 100; i++) {
s.getConsole()
.println();
}
}
@Descriptor("Format in box form or turn it off")
public boolean box(boolean on) {
return DTOFormatter.boxes = on;
}
static public class Scope implements Comparable<Scope> {
public String name;
public String description = "";
public final Map<String, Command> commands = new TreeMap<>();
Scope(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
public String commands() {
int size = commands.size();
try (Formatter f = new Formatter()) {
int column = 0;
for (Command command : commands.values()) {
f.format("%-20s ", command.name);
if (column++ == 4) {
f.format("\n");
column = 0;
}
}
return f.toString();
}
}
@Override
public int compareTo(Scope o) {
return name.compareTo(o.name);
}
}
static public class Command {
String name;
List<Method> methods = new ArrayList<>();
Properties properties;
Command(String name) {
this.name = name;
}
public String inspect() {
Justif j = new Justif(100, tabs(8));
Formatter f = j.formatter();
f.format("COMMAND\n\t1%s", name);
String title = getTitle();
if (title != null) {
f.format("\t3-- %s\n", fixup(title));
}
f.format("\n\u2007\n");
String topDescription = fixup(getDescription());
if (topDescription != null) {
f.format("DESCRIPTION\n");
f.format("\t1%s\n", topDescription);
}
f.format("\u2007\nSYNOPSIS %s\n", name);
for (Method m : methods) {
f.format("\t1%s", name);
String description = Help.getDescription(m);
if (description != null) {
f.format("\t6%s", description);
}
f.format("\n");
java.lang.reflect.Parameter[] parameters = m.getParameters();
Descriptor mdescription = m.getAnnotation(Descriptor.class);
for (java.lang.reflect.Parameter p : parameters) {
if (p.getType() == CommandSession.class)
continue;
Parameter ann = p.getAnnotation(Parameter.class);
Descriptor d = p.getAnnotation(Descriptor.class);
if (ann != null) {
f.format("\t1 [");
for (String name : ann.names()) {
f.format("%s", name);
break;
}
if (Parameter.UNSPECIFIED.equals(ann.presentValue())) {
// not a flag
f.format(" %s:%s", p.getName(), type(p.getParameterizedType()));
} else {
f.format(" %s", p.getName());
}
f.format("]");
f.format("\t4%s", ann.absentValue());
} else {
if (p.isVarArgs())
f.format("\t1 %s...:%s", p.getName(), type(p.getType()
.getComponentType()));
else
f.format("\t1 %s:%s", p.getName(), type(p.getParameterizedType()));
}
if (d != null) {
f.format("\t6- %s", d.value());
}
f.format("\n");
}
f.format("\n\n");
}
String example = getExample();
if (example != null) {
f.format("EXAMPLE\n\t1%s\n", example);
}
String see = getSee();
if (see != null) {
f.format("SEE\n\t1%s\n", see);
}
f.format("\n");
return j.toString();
}
private String getExample() {
return getProperties().getProperty(name + ".example");
}
private String getSee() {
return getProperties().getProperty(name + ".see");
}
private String getDescription() {
return getProperties().getProperty(name + ".description");
}
private String getTitle() {
return getProperties().getProperty(name + ".title", Help.getDescription(methods.get(0)));
}
private Properties getProperties() {
if (properties == null) {
List<Class<?>> collect = methods.stream()
.map(Method::getDeclaringClass)
.filter(Objects::nonNull)
.collect(Collectors.toList());
properties = getResource("help.properties", collect);
}
return properties;
}
private String type(Type type) {
if (type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (clazz.isPrimitive())
return clazz.getName();
if (clazz.isArray()) {
return type(clazz.getComponentType()) + "[]";
}
return clazz.getSimpleName()
.toLowerCase();
} else if (type instanceof ParameterizedType) {
}
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
String raw = type(ptype.getRawType());
StringBuilder sb = new StringBuilder();
sb.append(raw)
.append("<");
for (Type t : ptype.getActualTypeArguments()) {
sb.append(type(t));
}
sb.append(">");
return sb.toString();
}
return type.toString()
.toLowerCase();
}
public String line() {
return inspect();
}
}
static public class Argument {
public String name;
public String description;
public Type type;
}
@Descriptor("Displays help for each available scopes")
public Collection<Scope> help() throws Exception {
return getCommands().values();
}
@Descriptor("Displays help for a command")
public List<Command> help(
// @formatter:off
@Parameter(names= {"-s","--scope"}, absentValue="*")
@Descriptor("Limit to matching scopes")
Glob scope,
@Descriptor("Glob for the command name")
Glob command
// @formatter:on
) throws Exception {
return getCommands().entrySet()
.stream()
.filter(e -> scope.matches(e.getKey()))
.flatMap(e -> e.getValue().commands.entrySet()
.stream())
.filter(e -> command.matches(e.getKey()))
.map(Entry::getValue)
.collect(Collectors.toList());
}
private Map<String, Scope> getCommands() throws Exception {
Map<String, Scope> scopes = new TreeMap<>();
ServiceReference<?>[] refs = null;
try {
refs = context.getAllServiceReferences(null, "(osgi.command.scope=*)");
} catch (InvalidSyntaxException ex) {
throw Exceptions.duck(ex);
}
for (ServiceReference<?> ref : refs) {
Object svc = context.getService(ref);
if (svc != null) {
try {
String description = getDescription(svc.getClass());
String name = (String) ref.getProperty("osgi.command.scope");
Scope scope = scopes.computeIfAbsent(name, Scope::new);
scope.description = concat(scope.description, description);
Object ofunc = ref.getProperty("osgi.command.function");
String[] funcs = Converter.cnv(String[].class, ofunc);
for (String func : funcs) {
scope.commands.computeIfAbsent(func, Command::new);
}
Method[] methods = svc.getClass()
.getMethods();
for (Method method : methods) {
for (String func : funcs) {
if (matches(func, method)) {
Command command = scope.commands.get(func);
command.methods.add(method);
}
}
}
} finally {
context.ungetService(ref);
}
}
}
return scopes;
}
private boolean matches(String func, Method method) {
func = func.toLowerCase();
String name = method.getName()
.toLowerCase();
if (func.equals(name)) {
return true;
}
if (name.startsWith("_"))
name = name.substring(1);
if (name.startsWith("get") || name.startsWith("set"))
name = name.substring(3);
else if (name.startsWith("is"))
name = name.substring(2);
return func.equals(name);
}
private String concat(String description, String description2) {
if (description == null)
return description2;
if (description2 == null)
return description;
return description.concat("\n")
.concat(description2);
}
private static String getDescription(AnnotatedElement class1) {
Descriptor descriptor = class1.getAnnotation(Descriptor.class);
if (descriptor == null)
return null;
return descriptor.value();
}
private static int[] tabs(int width) {
int[] tabs = new int[100];
for (int i = 0; i < 100; i++) {
tabs[i] = i * width;
}
return tabs;
}
private static Properties getResource(String path, Class<?> clazz) {
try (InputStream in = clazz.getResourceAsStream(path)) {
if (in == null)
return null;
try (InputStreamReader rd = new InputStreamReader(in)) {
Properties p = new Properties();
p.load(rd);
return p;
}
} catch (Exception e) {
return null;
}
}
private static Properties getResource(String name, Collection<Class<?>> clazz) {
return clazz.stream()
.map(c -> getResource(name, c))
.filter(Objects::nonNull)
.findFirst()
.orElse(new Properties());
}
private static String fixup(String s) {
if (s == null) {
return null;
}
// the boxing causes empty lines to be stripped
return s.replaceAll("\n\n", "\n\u2007\n")
.replaceAll("\n", "\f");
}
}