| 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 { |
| 9 final String name; |
| 10 final List<String> apis; |
| 11 final Pubspec pubspec; |
| 12 final String readme; |
| 13 final String license; |
| 14 |
| 15 Package(this.name, this.apis, this.pubspec, this.readme, this.license); |
| 16 } |
| 17 |
| 18 String generateReadme(String readmeFile, List<DirectoryListItems> items) { |
| 19 var sb = new StringBuffer(); |
| 20 if (readmeFile != null) { |
| 21 sb.write(new File(readmeFile).readAsStringSync()); |
| 22 } |
| 23 sb.writeln(''' |
| 24 |
| 25 ## Available Google APIs |
| 26 |
| 27 The following is a list of APIs that are currently available inside this |
| 28 package. |
| 29 '''); |
| 30 for (DirectoryListItems item in items) { |
| 31 sb.write("#### "); |
| 32 if (item.icons != null && item.icons.x16 != null) { |
| 33 sb.write(" "); |
| 34 } |
| 35 sb..writeln('${item.title} - ${item.name} ${item.version}') |
| 36 ..writeln() |
| 37 ..writeln('${item.description}') |
| 38 ..writeln(); |
| 39 if (item.documentationLink != null) { |
| 40 sb.writeln( |
| 41 'Official API documentation: ${item.documentationLink}'); |
| 42 sb.writeln(); |
| 43 } |
| 44 } |
| 45 return sb.toString(); |
| 46 } |
| 47 |
| 8 main() { | 48 main() { |
| 49 listFromYaml(value) => value != null ? value : []; |
| 50 |
| 51 // Set up paths to directories 'discovery' and 'generated'. |
| 52 var discoveryDocsDir = Platform.script.resolve('discovery').path; |
| 53 var generatedApisDir = Platform.script.resolve('generated').path; |
| 54 |
| 9 // Read the configuration. | 55 // Read the configuration. |
| 10 var configFile = Platform.script.resolve('config.yaml').path; | 56 var configFile = Platform.script.resolve('config.yaml').path; |
| 11 var discoveryDocsDir = Platform.script.resolve('discovery').path; | |
| 12 var config = loadYaml(new File(configFile).readAsStringSync()); | 57 var config = loadYaml(new File(configFile).readAsStringSync()); |
| 13 var pkgs = config['packages']; | 58 |
| 14 var packages = {}; | 59 listAllApis().then((List<DirectoryListItems> items) { |
| 15 var supportedApis = []; | 60 var pkgs = config['packages']; |
| 16 var knownApis = []; | 61 var packages = {}; |
| 17 pkgs.forEach((pkg) { | 62 var supportedApis = []; |
| 18 pkg.forEach((name, values) { | 63 pkgs.forEach((package) { |
| 19 packages[name] = values != null ? values : []; | 64 package.forEach((name, values) { |
| 65 var apis = listFromYaml(values['apis']); |
| 66 var version = |
| 67 values['version'] != null ? values['version'] : '0.1.0-dev'; |
| 68 var author = values['author']; |
| 69 var homepage = values['homepage']; |
| 70 var readmeFile; |
| 71 if (values['readme'] != null) { |
| 72 readmeFile = Platform.script.resolve(values['readme']).path; |
| 73 } |
| 74 var licenseFile; |
| 75 if (values['license'] != null) { |
| 76 licenseFile = Platform.script.resolve(values['license']).path; |
| 77 } |
| 78 |
| 79 |
| 80 // Generate package description. |
| 81 var apiDescriptions = []; |
| 82 var sb = new StringBuffer() |
| 83 ..write('Auto-generated client libraries for accessing ' |
| 84 'the following APIs:'); |
| 85 items.forEach((DirectoryListItems apiDescription) { |
| 86 if (apis.contains(apiDescription.id)) { |
| 87 sb..writeln(' ') |
| 88 ..write(apiDescription.id) |
| 89 ..write(' - ') |
| 90 ..write(apiDescription.description); |
| 91 apiDescriptions.add(apiDescription); |
| 92 } |
| 93 }); |
| 94 |
| 95 // Generate the README.md file content. |
| 96 var readme = generateReadme(readmeFile, apiDescriptions); |
| 97 |
| 98 // Read the LICENSE |
| 99 var license = new File(licenseFile).readAsStringSync(); |
| 100 |
| 101 // Create package description with pubspec.yaml information. |
| 102 var pubspec = new Pubspec( |
| 103 name, version, sb.toString(), author: author, homepage: homepage); |
| 104 packages[name] = new Package(name, apis, pubspec, readme, license); |
| 105 }); |
| 20 }); | 106 }); |
| 21 }); | |
| 22 var skippedApis = config['skipped_apis']; | |
| 23 packages.forEach((_, values) => supportedApis.addAll(values)); | |
| 24 knownApis.addAll(supportedApis); | |
| 25 knownApis.addAll(skippedApis); | |
| 26 | 107 |
| 27 // Check that all APIs are mentioned in the configuration. | 108 // Check that all APIs are mentioned in the configuration. |
| 28 var missingApis = []; | 109 var skippedApis = listFromYaml(config['skipped_apis']); |
| 29 listAllApis().then((List<DirectoryListItems> items) { | 110 packages.forEach((_, package) => supportedApis.addAll(package.apis)); |
| 111 var knownApis = new Set(); |
| 112 knownApis.addAll(supportedApis); |
| 113 knownApis.addAll(skippedApis); |
| 114 var excessApis = new Set.from(knownApis); |
| 115 |
| 116 var missingApis = []; |
| 30 items.forEach((item) { | 117 items.forEach((item) { |
| 31 if (!knownApis.contains(item.id)) missingApis.add(item.id); | 118 if (!knownApis.contains(item.id)) missingApis.add(item.id); |
| 119 excessApis.remove(item.id); |
| 32 }); | 120 }); |
| 33 | 121 |
| 34 if (missingApis.isNotEmpty) { | 122 if (missingApis.isNotEmpty) { |
| 35 print('No configuration for the following APIs;'); | 123 print('WARNING: No configuration for the following APIs:'); |
| 36 missingApis.forEach((id) => print('- $id')); | 124 missingApis.forEach((id) => print('- $id')); |
| 37 return; | 125 } |
| 126 |
| 127 if (excessApis.isNotEmpty) { |
| 128 print('WARNING: The following APIs does not exist:'); |
| 129 excessApis.forEach((id) => print('- $id')); |
| 38 } | 130 } |
| 39 | 131 |
| 40 // Delete downloaded discovery documents. | 132 // Delete downloaded discovery documents. |
| 41 var dir = new Directory(discoveryDocsDir); | 133 var dir = new Directory(discoveryDocsDir); |
| 42 if (dir.existsSync()) dir.deleteSync(recursive: true); | 134 if (dir.existsSync()) dir.deleteSync(recursive: true); |
| 43 | 135 |
| 44 // Download the discovery documents for the packages to build. | 136 // Download the discovery documents for the packages to build. |
| 45 var futures = []; | 137 var futures = []; |
| 46 packages.forEach((name, values) { | 138 packages.forEach((name, package) { |
| 47 futures.add(downloadDiscoveryDocuments('discovery/$name', ids: values)); | 139 futures.add(downloadDiscoveryDocuments('$discoveryDocsDir/$name', |
| 140 ids: package.apis)); |
| 48 }); | 141 }); |
| 49 | 142 |
| 50 Future.wait(futures).then((_) { | 143 Future.wait(futures).then((_) { |
| 51 packages.forEach((name, values) { | 144 packages.forEach((name, package) { |
| 52 print('Generating library $name'); | 145 print('Generating library $name'); |
| 53 generateAllLibraries('discovery/$name', 'generated/$name'); | 146 generateAllLibraries('$discoveryDocsDir/$name', |
| 147 '$generatedApisDir/$name', |
| 148 package.pubspec); |
| 149 new File('$generatedApisDir/$name/README.md') |
| 150 .writeAsStringSync(package.readme); |
| 151 if (package.license != null) { |
| 152 new File('$generatedApisDir/$name/LICENSE') |
| 153 .writeAsStringSync(package.license); |
| 154 } |
| 54 }); | 155 }); |
| 55 print('DONE'); | 156 print('DONE'); |
| 56 }); | 157 }); |
| 57 }); | 158 }); |
| 58 } | 159 } |
| OLD | NEW |