12
12
import java .util .Collections ;
13
13
import java .util .Comparator ;
14
14
import java .util .HashMap ;
15
+ import java .util .LinkedHashMap ;
15
16
import java .util .List ;
16
17
import java .util .Map ;
17
18
import java .util .Set ;
18
19
import java .util .stream .Collectors ;
19
20
import java .util .stream .Stream ;
20
21
import org .json .JSONArray ;
22
+ import org .json .JSONException ;
21
23
import org .json .JSONObject ;
24
+ import org .yaml .snakeyaml .DumperOptions ;
25
+ import org .yaml .snakeyaml .Yaml ;
22
26
23
27
public final class ModulesInfoCreator {
24
28
@@ -48,11 +52,8 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
48
52
moduleInfos .add (moduleInfo );
49
53
}
50
54
51
- final JSONArray modulesJson = new JSONArray ();
52
- for (ModuleInfo moduleInfo : moduleInfos ) {
53
- modulesJson .put (moduleInfo .toJSON ());
54
- }
55
- System .out .println (modulesJson .toString (2 ));
55
+ // printAsJson(moduleInfos);
56
+ printAsYaml (moduleInfos );
56
57
57
58
System .out .println ("Total modules: " + moduleInfos .size ());
58
59
System .out .println ("Total functions: " + moduleInfos .stream ()
@@ -65,6 +66,26 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
65
66
);
66
67
}
67
68
69
+ private static void printAsJson (List <ModuleInfo > moduleInfos ) throws JSONException {
70
+ final JSONArray modulesJson = new JSONArray ();
71
+ for (ModuleInfo moduleInfo : moduleInfos ) {
72
+ modulesJson .put (new JSONObject (moduleInfo .info ()));
73
+ }
74
+ System .out .println (modulesJson .toString (2 ));
75
+ }
76
+
77
+ private static void printAsYaml (List <ModuleInfo > moduleInfos ) {
78
+ DumperOptions options = new DumperOptions ();
79
+ options .setIndent (2 );
80
+ options .setDefaultFlowStyle (DumperOptions .FlowStyle .BLOCK );
81
+
82
+ final List <Map <String , Object >> infos = new ArrayList <>();
83
+ for (ModuleInfo moduleInfo : moduleInfos ) {
84
+ infos .add (moduleInfo .info ());
85
+ }
86
+ System .out .println (new Yaml (options ).dump (infos ));
87
+ }
88
+
68
89
private static List <String > listValues (Class moduleClass ) {
69
90
return Arrays .stream (moduleClass .getDeclaredClasses ())
70
91
.filter (clazz -> getAllInterfaces (clazz ).stream ().anyMatch (i -> i .equals (Value .class )))
@@ -91,14 +112,27 @@ public ModuleInfo(String name) {
91
112
types = new ArrayList <>();
92
113
}
93
114
94
- public JSONArray constantsJSON () {
95
- final JSONArray result = new JSONArray ();
115
+ public List <Map <String , Object >> functions () {
116
+ return functions .stream ().sorted ()
117
+ .map (f -> {
118
+ final Map <String , Object > function = new LinkedHashMap <>();
119
+ function .put ("name" , f );
120
+ function .put ("args" , "" );
121
+ function .put ("desc" , "" );
122
+ function .put ("desc_ru" , "" );
123
+ return function ;
124
+ })
125
+ .collect (Collectors .toList ());
126
+ }
127
+
128
+ public List <Map <String , Object >> constants () {
129
+ final List <Map <String , Object >> result = new ArrayList <>();
96
130
constants .entrySet ().stream ()
97
131
.sorted (Comparator .comparing (e -> e .getKey ()))
98
132
.forEach (entry -> {
99
133
final Value value = entry .getValue ();
100
134
101
- final JSONObject constant = new JSONObject ();
135
+ final Map < String , Object > constant = new LinkedHashMap <> ();
102
136
constant .put ("name" , entry .getKey ());
103
137
constant .put ("type" , value .type ());
104
138
constant .put ("typeName" , Types .typeToString (value .type ()));
@@ -112,26 +146,27 @@ public JSONArray constantsJSON() {
112
146
} else {
113
147
constant .put ("value" , value .asString ());
114
148
}
115
- result .put (constant );
149
+ result .add (constant );
116
150
});
117
151
return result ;
118
152
}
119
153
120
- public JSONObject toJSON () {
121
- final JSONObject json = new JSONObject ();
122
- json .put ("name" , name );
123
- json .put ("functions" , functions .stream ().sorted ().toArray ());
124
- json .put ("constants" , constantsJSON ());
154
+ public Map <String , Object > info () {
155
+ final Map <String , Object > result = new LinkedHashMap <>();
156
+ result .put ("name" , name );
157
+ result .put ("scope" , "both" );
158
+ result .put ("constants" , constants ());
159
+ result .put ("functions" , functions ());
125
160
if (!types .isEmpty ()) {
126
- json .put ("types" , types .stream ().sorted ()
161
+ result .put ("types" , types .stream ().sorted ()
127
162
.map (s -> {
128
- final JSONObject type = new JSONObject ();
163
+ final Map < String , String > type = new HashMap <> ();
129
164
type .put ("name" , s );
130
165
return type ;
131
166
})
132
167
.toArray ());
133
168
}
134
- return json ;
169
+ return result ;
135
170
}
136
171
}
137
172
}
0 commit comments