| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library googleapis_generator.package_configuration; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:discoveryapis_generator/discoveryapis_generator.dart'; |
| 11 import 'package:yaml/yaml.dart'; |
| 12 |
| 13 import '../googleapis_generator.dart'; |
| 14 |
| 15 class Package { |
| 16 final String name; |
| 17 final List<String> apis; |
| 18 final Pubspec pubspec; |
| 19 final String readme; |
| 20 final String license; |
| 21 final String changelog; |
| 22 |
| 23 Package(this.name, this.apis, this.pubspec, this.readme, this.license, |
| 24 this.changelog); |
| 25 } |
| 26 |
| 27 /** |
| 28 * Configuration of a set of packages generated from a set of APIs exposed by |
| 29 * a Discovery Service. |
| 30 */ |
| 31 class DiscoveryPackagesConfiguration { |
| 32 Map<String, Package> packages = {}; |
| 33 Iterable<String> excessApis; |
| 34 Iterable<String> missingApis; |
| 35 |
| 36 /** |
| 37 * Create a new discovery packages configuration. |
| 38 * |
| 39 * [config] is the path to the YAML configuration file. |
| 40 * |
| 41 * [allApis] is the list of all supported APIs returned by the Discovery |
| 42 * Service. |
| 43 * |
| 44 * The format of a YAML document describing a number of packages looks |
| 45 * like this: |
| 46 * |
| 47 * packages: |
| 48 * - googleapis: |
| 49 * version: 0.1.0 |
| 50 * author: Dart Team <misc@dartlang.org> |
| 51 * homepage: http://www.dartlang.org |
| 52 * readme: resources/README.md |
| 53 * license: resources/LICENSE |
| 54 * apis: |
| 55 * - analytics:v3 |
| 56 * - bigquery:v2 |
| 57 * - googleapis_beta: |
| 58 * version: 0.1.0 |
| 59 * author: Dart Team <misc@dartlang.org> |
| 60 * homepage: http://www.dartlang.org |
| 61 * readme: resources/README.md |
| 62 * license: resources/LICENSE |
| 63 * apis: |
| 64 * - datastore:v1beta2 |
| 65 * - dns:v1beta1 |
| 66 * skipped_apis: |
| 67 * - adexchangebuyer:v1 |
| 68 * |
| 69 * Each package to build is listed under the key `packages:`. |
| 70 * |
| 71 * The key `skipped_apis` is used to list APIs returned buy the Discovery |
| 72 * Service but is not part of any generated packages. |
| 73 * |
| 74 * The file names for the content of readme and license files are resolved |
| 75 * relative to the configuration file. |
| 76 */ |
| 77 DiscoveryPackagesConfiguration( |
| 78 String configFile, List<DirectoryListItems> allApis) { |
| 79 var configYaml = new File(configFile).readAsStringSync(); |
| 80 var yaml = loadYaml(configYaml); |
| 81 packages = _packagesFromYaml(yaml['packages'], configFile, allApis); |
| 82 var knownApis = _calculateKnownApis(packages, |
| 83 _listFromYaml(yaml['skipped_apis'])); |
| 84 missingApis = _calculateMissingApis(knownApis, allApis); |
| 85 excessApis = _calculateExcessApis(knownApis, allApis); |
| 86 } |
| 87 |
| 88 /** |
| 89 * Generate packages from the configuration. |
| 90 * |
| 91 * [discoveryDocsDir] is the directory where all the downloaded discovery |
| 92 * documents are stored. |
| 93 * |
| 94 * [generatedApisDir] is the directory where the packages are generated. |
| 95 * Each package is generated in a sub-directory. |
| 96 */ |
| 97 Future generate(String discoveryDocsDir, String generatedApisDir) { |
| 98 // Delete all downloaded discovery documents. |
| 99 var dir = new Directory(discoveryDocsDir); |
| 100 if (dir.existsSync()) dir.deleteSync(recursive: true); |
| 101 |
| 102 // Download the discovery documents for the packages to build. |
| 103 var futures = []; |
| 104 packages.forEach((name, package) { |
| 105 futures.add(downloadDiscoveryDocuments('$discoveryDocsDir/$name', |
| 106 ids: package.apis)); |
| 107 }); |
| 108 |
| 109 return Future.wait(futures).then((_) { |
| 110 packages.forEach((name, package) { |
| 111 generateAllLibraries('$discoveryDocsDir/$name', |
| 112 '$generatedApisDir/$name', |
| 113 package.pubspec); |
| 114 new File('$generatedApisDir/$name/README.md') |
| 115 .writeAsStringSync(package.readme); |
| 116 if (package.license != null) { |
| 117 new File('$generatedApisDir/$name/LICENSE') |
| 118 .writeAsStringSync(package.license); |
| 119 } |
| 120 if (package.changelog != null) { |
| 121 new File('$generatedApisDir/$name/CHANGELOG.md') |
| 122 .writeAsStringSync(package.changelog); |
| 123 } |
| 124 }); |
| 125 }); |
| 126 } |
| 127 |
| 128 // Return empty list for YAML null value. |
| 129 static List _listFromYaml(value) => value != null ? value : []; |
| 130 |
| 131 static String _generateReadme( |
| 132 String readmeFile, List<DirectoryListItems> items) { |
| 133 var sb = new StringBuffer(); |
| 134 if (readmeFile != null) { |
| 135 sb.write(new File(readmeFile).readAsStringSync()); |
| 136 } |
| 137 sb.writeln(''' |
| 138 |
| 139 ## Available Google APIs |
| 140 |
| 141 The following is a list of APIs that are currently available inside this |
| 142 package. |
| 143 '''); |
| 144 for (DirectoryListItems item in items) { |
| 145 sb.write("#### "); |
| 146 if (item.icons != null && item.icons.x16 != null) { |
| 147 sb.write(" "); |
| 148 } |
| 149 sb..writeln('${item.title} - ${item.name} ${item.version}') |
| 150 ..writeln() |
| 151 ..writeln('${item.description}') |
| 152 ..writeln(); |
| 153 if (item.documentationLink != null) { |
| 154 sb.writeln( |
| 155 'Official API documentation: ${item.documentationLink}'); |
| 156 sb.writeln(); |
| 157 } |
| 158 } |
| 159 return sb.toString(); |
| 160 } |
| 161 |
| 162 static Map<String, Package> _packagesFromYaml( |
| 163 YamlList configPackages, |
| 164 String configFile, |
| 165 List<DirectoryListItems> allApis) { |
| 166 var packages = {}; |
| 167 configPackages.forEach((package) { |
| 168 package.forEach((name, values) { |
| 169 packages[name] = _packageFromYaml(name, values, configFile, allApis); |
| 170 }); |
| 171 }); |
| 172 |
| 173 return packages; |
| 174 } |
| 175 |
| 176 static Package _packageFromYaml(String name, |
| 177 YamlMap values, |
| 178 String configFile, |
| 179 List<DirectoryListItems> allApis) { |
| 180 var apis = _listFromYaml(values['apis']); |
| 181 var version = |
| 182 values['version'] != null ? values['version'] : '0.1.0-dev'; |
| 183 var author = values['author']; |
| 184 var homepage = values['homepage']; |
| 185 |
| 186 var configUri = new Uri.file(configFile); |
| 187 |
| 188 var readmeFile; |
| 189 if (values['readme'] != null) { |
| 190 readmeFile = configUri.resolve(values['readme']).path; |
| 191 } |
| 192 var licenseFile; |
| 193 if (values['license'] != null) { |
| 194 licenseFile = configUri.resolve(values['license']).path; |
| 195 } |
| 196 var changelogFile; |
| 197 if (values['changelog'] != null) { |
| 198 changelogFile = configUri.resolve(values['changelog']).path; |
| 199 } |
| 200 |
| 201 // Generate package description. |
| 202 var apiDescriptions = []; |
| 203 var sb = new StringBuffer() |
| 204 ..write('"Auto-generated client libraries for accessing ' |
| 205 'the following APIs:'); |
| 206 bool first = true; |
| 207 allApis.forEach((DirectoryListItems apiDescription) { |
| 208 if (apis.contains(apiDescription.id)) { |
| 209 if (!first) sb.write(', '); |
| 210 sb.write(apiDescription.id); |
| 211 apiDescriptions.add(apiDescription); |
| 212 first = false; |
| 213 } |
| 214 }); |
| 215 sb.write('"'); |
| 216 |
| 217 // Generate the README.md file content. |
| 218 var readme = _generateReadme(readmeFile, apiDescriptions); |
| 219 |
| 220 // Read the LICENSE |
| 221 var license = new File(licenseFile).readAsStringSync(); |
| 222 |
| 223 // Read CHANGELOG.md |
| 224 var changelog = new File(changelogFile).readAsStringSync(); |
| 225 |
| 226 // Create package description with pubspec.yaml information. |
| 227 var pubspec = new Pubspec( |
| 228 name, version, sb.toString(), author: author, homepage: homepage); |
| 229 return new Package(name, apis, pubspec, readme, license, changelog); |
| 230 } |
| 231 |
| 232 /// The known APIs are the APis mentioned in each package together with |
| 233 /// the APIs explicitly skipped. |
| 234 static Set<String> _calculateKnownApis(Map<String, Package> packages, |
| 235 YamlList skippedApis) { |
| 236 var knownApis = new Set(); |
| 237 knownApis.addAll(skippedApis); |
| 238 packages.forEach((_, package) => knownApis.addAll(package.apis)); |
| 239 return knownApis; |
| 240 } |
| 241 |
| 242 /// The missing APIs are the APIs returned from the Discovery Service |
| 243 /// but not mentioned in the configuration. |
| 244 static Iterable<String> _calculateMissingApis( |
| 245 Iterable<String> knownApis, List<DirectoryListItems> allApis) { |
| 246 return allApis |
| 247 .where((item) => !knownApis.contains(item.id)) |
| 248 .map((item) => item.id); |
| 249 } |
| 250 |
| 251 /// The excess APIs are the APIs mentioned in the configuration but not |
| 252 /// returned from the Discovery Service. |
| 253 static Iterable<String> _calculateExcessApis( |
| 254 Iterable<String> knownApis, List<DirectoryListItems> allApis) { |
| 255 var excessApis = new Set.from(knownApis); |
| 256 allApis.forEach((item) => excessApis.remove(item.id)); |
| 257 return excessApis; |
| 258 } |
| 259 } |
| 260 |
| OLD | NEW |