OLD | NEW |
---|---|
1 import "dart:io"; | 1 import "dart:io"; |
2 import "dart:async"; | 2 import "dart:async"; |
3 | 3 |
4 import '../discovery_api_dart_client_generator/lib/generator.dart'; | 4 import '../discovery_api_dart_client_generator/lib/generator.dart'; |
5 import 'package:google_discovery_v1_api/discovery_v1_api_client.dart'; | 5 import 'package:google_discovery_v1_api/discovery_v1_api_client.dart'; |
6 import 'package:yaml/yaml.dart'; | 6 import 'package:yaml/yaml.dart'; |
7 | 7 |
8 class Package { | 8 class Package { |
9 final String name; | 9 final String name; |
10 final List<String> apis; | 10 final List<String> apis; |
11 final Pubspec pubspec; | 11 final Pubspec pubspec; |
12 final String readme; | 12 final String readme; |
13 final String license; | 13 final String license; |
14 | 14 |
15 Package(this.name, this.apis, this.pubspec, this.readme, this.license); | 15 Package(this.name, this.apis, this.pubspec, this.readme, this.license); |
16 } | 16 } |
17 | 17 |
18 String generateReadme(String readmeFile, List<DirectoryListItems> items) { | 18 /** |
19 var sb = new StringBuffer(); | 19 * Configuration of a set of pacakges generated from a set of APIs exposed by |
kustermann
2014/08/25 09:47:29
-> packages
Søren Gjesse
2014/08/26 13:44:12
Done.
| |
20 if (readmeFile != null) { | 20 * A Discovery Service. |
kustermann
2014/08/25 09:47:29
A -> a
Søren Gjesse
2014/08/26 13:44:13
Done.
| |
21 sb.write(new File(readmeFile).readAsStringSync()); | 21 */ |
22 } | 22 class DiscoveryPackagesConfiguration { |
23 sb.writeln(''' | 23 final Map<String, Package> packages = {}; |
24 final List<String> skippedApis = []; | |
25 final _knownApis = new Set(); | |
24 | 26 |
25 ## Available Google APIs | 27 /// APIs which are returned by the |
28 final excessApis = new Set(); | |
29 final missingApis = new Set(); | |
kustermann
2014/08/25 09:47:29
Put generic types here and make the variables type
Søren Gjesse
2014/08/26 13:44:13
Done.
| |
26 | 30 |
27 The following is a list of APIs that are currently available inside this | 31 /** |
28 package. | 32 * Create a new discovery packages configuration. |
29 '''); | 33 * |
30 for (DirectoryListItems item in items) { | 34 * [config] is the path to the YAML configuration file. |
31 sb.write("#### "); | 35 * |
32 if (item.icons != null && item.icons.x16 != null) { | 36 * [allApis] is the list of all supported APIs returned by the Discovery |
33 sb.write(" "); | 37 * Service. |
34 } | 38 * |
35 sb..writeln('${item.title} - ${item.name} ${item.version}') | 39 * The format of a YAML document describing a number of packages looks |
36 ..writeln() | 40 * like this: |
37 ..writeln('${item.description}') | 41 * |
38 ..writeln(); | 42 * packages: |
39 if (item.documentationLink != null) { | 43 * - googleapis: |
40 sb.writeln( | 44 * version: 0.1.0 |
41 'Official API documentation: ${item.documentationLink}'); | 45 * author: Dart Team <misc@dartlang.org> |
42 sb.writeln(); | 46 * homepage: http://www.dartlang.org |
43 } | 47 * readme: resources/README.md |
44 } | 48 * license: resources/LICENSE |
45 return sb.toString(); | 49 * apis: |
46 } | 50 * - analytics:v3 |
47 | 51 * - bigquery:v2 |
48 main() { | 52 * - googleapis_beta: |
49 listFromYaml(value) => value != null ? value : []; | 53 * version: 0.1.0 |
50 | 54 * author: Dart Team <misc@dartlang.org> |
51 // Set up paths to directories 'discovery' and 'generated'. | 55 * homepage: http://www.dartlang.org |
52 var discoveryDocsDir = Platform.script.resolve('discovery').path; | 56 * readme: resources/README.md |
53 var generatedApisDir = Platform.script.resolve('generated').path; | 57 * license: resources/LICENSE |
54 | 58 * apis: |
55 // Read the configuration. | 59 * - datastore:v1beta2 |
56 var configFile = Platform.script.resolve('config.yaml').path; | 60 * - dns:v1beta1 |
57 var config = loadYaml(new File(configFile).readAsStringSync()); | 61 * skipped_apis: |
58 | 62 * - adexchangebuyer:v1 |
59 listAllApis().then((List<DirectoryListItems> items) { | 63 * |
60 var pkgs = config['packages']; | 64 * Each package to build is listed under the key `packages:`. |
61 var packages = {}; | 65 * |
66 * The key `skipped_apis` is used to list APIs returned buy the Discovery | |
67 * Service but is not part of any generated packages. | |
68 * | |
69 * The file names for the content of readme and license files are resolved | |
70 * relative to the configuration file. | |
71 */ | |
72 DiscoveryPackagesConfiguration( | |
73 String configFile, List<DirectoryListItems> allApis) { | |
74 var configYaml = new File(configFile).readAsStringSync(); | |
75 var yaml = loadYaml(configYaml); | |
76 var configPackages = yaml['packages']; | |
62 var supportedApis = []; | 77 var supportedApis = []; |
63 pkgs.forEach((package) { | 78 configPackages.forEach((package) { |
64 package.forEach((name, values) { | 79 package.forEach((name, values) { |
65 var apis = listFromYaml(values['apis']); | 80 var apis = _listFromYaml(values['apis']); |
66 var version = | 81 var version = |
67 values['version'] != null ? values['version'] : '0.1.0-dev'; | 82 values['version'] != null ? values['version'] : '0.1.0-dev'; |
68 var author = values['author']; | 83 var author = values['author']; |
69 var homepage = values['homepage']; | 84 var homepage = values['homepage']; |
70 var readmeFile; | 85 var readmeFile; |
71 if (values['readme'] != null) { | 86 if (values['readme'] != null) { |
72 readmeFile = Platform.script.resolve(values['readme']).path; | 87 readmeFile = new Uri.file(configFile).resolve(values['readme']).path; |
73 } | 88 } |
74 var licenseFile; | 89 var licenseFile; |
75 if (values['license'] != null) { | 90 if (values['license'] != null) { |
76 licenseFile = Platform.script.resolve(values['license']).path; | 91 licenseFile = new Uri.file(configFile).resolve(values['license']).path ; |
kustermann
2014/08/25 09:47:28
long line :)
Søren Gjesse
2014/08/26 13:44:13
Done.
| |
77 } | 92 } |
78 | 93 |
79 | |
80 // Generate package description. | 94 // Generate package description. |
81 var apiDescriptions = []; | 95 var apiDescriptions = []; |
82 var sb = new StringBuffer() | 96 var sb = new StringBuffer() |
83 ..write('"Auto-generated client libraries for accessing ' | 97 ..write('"Auto-generated client libraries for accessing ' |
84 'the following APIs:\\n'); | 98 'the following APIs:\\n'); |
85 items.forEach((DirectoryListItems apiDescription) { | 99 allApis.forEach((DirectoryListItems apiDescription) { |
86 if (apis.contains(apiDescription.id)) { | 100 if (apis.contains(apiDescription.id)) { |
87 sb..writeln('') | 101 sb..writeln('') |
88 ..write(' ') | 102 ..write(' ') |
89 ..write(apiDescription.id) | 103 ..write(apiDescription.id) |
90 ..write(' - ') | 104 ..write(' - ') |
91 ..write(apiDescription.description) | 105 ..write(apiDescription.description) |
92 ..write('\\n'); | 106 ..write('\\n'); |
93 apiDescriptions.add(apiDescription); | 107 apiDescriptions.add(apiDescription); |
94 } | 108 } |
95 }); | 109 }); |
96 sb.write('"'); | 110 sb.write('"'); |
97 | 111 |
98 // Generate the README.md file content. | 112 // Generate the README.md file content. |
99 var readme = generateReadme(readmeFile, apiDescriptions); | 113 var readme = _generateReadme(readmeFile, apiDescriptions); |
100 | 114 |
101 // Read the LICENSE | 115 // Read the LICENSE |
102 var license = new File(licenseFile).readAsStringSync(); | 116 var license = new File(licenseFile).readAsStringSync(); |
103 | 117 |
104 // Create package description with pubspec.yaml information. | 118 // Create package description with pubspec.yaml information. |
105 var pubspec = new Pubspec( | 119 var pubspec = new Pubspec( |
106 name, version, sb.toString(), author: author, homepage: homepage); | 120 name, version, sb.toString(), author: author, homepage: homepage); |
107 packages[name] = new Package(name, apis, pubspec, readme, license); | 121 packages[name] = new Package(name, apis, pubspec, readme, license); |
108 }); | 122 }); |
kustermann
2014/08/25 09:47:28
Maybe split this up into three private static func
Søren Gjesse
2014/08/26 13:44:13
Done.
| |
109 }); | 123 }); |
110 | 124 |
125 // Read the skipped APIs. | |
126 skippedApis.addAll(_listFromYaml(yaml['skipped_apis'])); | |
127 | |
111 // Check that all APIs are mentioned in the configuration. | 128 // Check that all APIs are mentioned in the configuration. |
112 var skippedApis = listFromYaml(config['skipped_apis']); | |
113 packages.forEach((_, package) => supportedApis.addAll(package.apis)); | 129 packages.forEach((_, package) => supportedApis.addAll(package.apis)); |
114 var knownApis = new Set(); | 130 _knownApis.addAll(supportedApis); |
115 knownApis.addAll(supportedApis); | 131 _knownApis.addAll(skippedApis); |
116 knownApis.addAll(skippedApis); | 132 excessApis.addAll(_knownApis); |
117 var excessApis = new Set.from(knownApis); | 133 allApis.forEach((item) { |
118 | 134 if (!_knownApis.contains(item.id)) missingApis.add(item.id); |
119 var missingApis = []; | |
120 items.forEach((item) { | |
121 if (!knownApis.contains(item.id)) missingApis.add(item.id); | |
122 excessApis.remove(item.id); | 135 excessApis.remove(item.id); |
123 }); | 136 }); |
137 } | |
124 | 138 |
125 if (missingApis.isNotEmpty) { | 139 /** |
126 print('WARNING: No configuration for the following APIs:'); | 140 * Generate packages from the configuration. |
127 missingApis.forEach((id) => print('- $id')); | 141 * |
128 } | 142 * [discoveryDocsDir] is the directory where all the downloaded discovery |
129 | 143 * documents are stored. |
130 if (excessApis.isNotEmpty) { | 144 * |
131 print('WARNING: The following APIs does not exist:'); | 145 * [generatedApisDir] is the directory where the packages are generated. |
132 excessApis.forEach((id) => print('- $id')); | 146 * Each package is generated in a sub-directory. |
133 } | 147 */ |
134 | 148 Future generate(String discoveryDocsDir, String generatedApisDir) { |
135 // Delete downloaded discovery documents. | 149 // Delete all downloaded discovery documents. |
136 var dir = new Directory(discoveryDocsDir); | 150 var dir = new Directory(discoveryDocsDir); |
137 if (dir.existsSync()) dir.deleteSync(recursive: true); | 151 if (dir.existsSync()) dir.deleteSync(recursive: true); |
138 | 152 |
139 // Download the discovery documents for the packages to build. | 153 // Download the discovery documents for the packages to build. |
140 var futures = []; | 154 var futures = []; |
141 packages.forEach((name, package) { | 155 packages.forEach((name, package) { |
142 futures.add(downloadDiscoveryDocuments('$discoveryDocsDir/$name', | 156 futures.add(downloadDiscoveryDocuments('$discoveryDocsDir/$name', |
143 ids: package.apis)); | 157 ids: package.apis)); |
144 }); | 158 }); |
145 | 159 |
146 Future.wait(futures).then((_) { | 160 return Future.wait(futures).then((_) { |
147 packages.forEach((name, package) { | 161 packages.forEach((name, package) { |
148 print('Generating library $name'); | |
149 generateAllLibraries('$discoveryDocsDir/$name', | 162 generateAllLibraries('$discoveryDocsDir/$name', |
150 '$generatedApisDir/$name', | 163 '$generatedApisDir/$name', |
151 package.pubspec); | 164 package.pubspec); |
152 new File('$generatedApisDir/$name/README.md') | 165 new File('$generatedApisDir/$name/README.md') |
153 .writeAsStringSync(package.readme); | 166 .writeAsStringSync(package.readme); |
154 if (package.license != null) { | 167 if (package.license != null) { |
155 new File('$generatedApisDir/$name/LICENSE') | 168 new File('$generatedApisDir/$name/LICENSE') |
156 .writeAsStringSync(package.license); | 169 .writeAsStringSync(package.license); |
157 } | 170 } |
158 }); | 171 }); |
159 print('DONE'); | |
160 }); | 172 }); |
173 } | |
174 | |
175 // Return empty list for YAML null value. | |
176 _listFromYaml(value) => value != null ? value : []; | |
177 | |
178 String _generateReadme(String readmeFile, List<DirectoryListItems> items) { | |
179 var sb = new StringBuffer(); | |
180 if (readmeFile != null) { | |
181 sb.write(new File(readmeFile).readAsStringSync()); | |
182 } | |
183 sb.writeln(''' | |
184 | |
185 ## Available Google APIs | |
186 | |
187 The following is a list of APIs that are currently available inside this | |
188 package. | |
189 '''); | |
190 for (DirectoryListItems item in items) { | |
191 sb.write("#### "); | |
192 if (item.icons != null && item.icons.x16 != null) { | |
193 sb.write(" "); | |
194 } | |
195 sb..writeln('${item.title} - ${item.name} ${item.version}') | |
196 ..writeln() | |
197 ..writeln('${item.description}') | |
198 ..writeln(); | |
199 if (item.documentationLink != null) { | |
200 sb.writeln( | |
201 'Official API documentation: ${item.documentationLink}'); | |
202 sb.writeln(); | |
203 } | |
204 } | |
205 return sb.toString(); | |
206 } | |
207 } | |
208 | |
209 main() { | |
210 listAllApis().then((List<DirectoryListItems> items) { | |
211 var configuration = new DiscoveryPackagesConfiguration( | |
212 Platform.script.resolve('config.yaml').path, items); | |
213 | |
214 // Print warnings for APIs not mentioned. | |
215 if (configuration.missingApis.isNotEmpty) { | |
216 print('WARNING: No configuration for the following APIs:'); | |
217 configuration.missingApis.forEach((id) => print('- $id')); | |
218 } | |
219 if (configuration.excessApis.isNotEmpty) { | |
220 print('WARNING: The following APIs does not exist:'); | |
221 configuration.excessApis.forEach((id) => print('- $id')); | |
222 } | |
223 | |
224 // Generate the packages. | |
225 configuration.generate(Platform.script.resolve('discovery').path, | |
226 Platform.script.resolve('generated').path) | |
227 .then((_) => print('Done!')); | |
161 }); | 228 }); |
162 } | 229 } |
OLD | NEW |