| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library command_lish; | 5 library command_lish; |
| 6 | 6 |
| 7 import 'dart:async'; |
| 7 import 'dart:io'; | 8 import 'dart:io'; |
| 8 import 'dart:json'; | 9 import 'dart:json'; |
| 9 import 'dart:uri'; | 10 import 'dart:uri'; |
| 10 | 11 |
| 11 import '../../pkg/args/lib/args.dart'; | 12 import '../../pkg/args/lib/args.dart'; |
| 12 import '../../pkg/http/lib/http.dart' as http; | 13 import '../../pkg/http/lib/http.dart' as http; |
| 13 import '../../pkg/path/lib/path.dart' as path; | 14 import '../../pkg/path/lib/path.dart' as path; |
| 14 import 'directory_tree.dart'; | 15 import 'directory_tree.dart'; |
| 15 import 'git.dart' as git; | 16 import 'git.dart' as git; |
| 16 import 'http.dart'; | 17 import 'http.dart'; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 37 | 38 |
| 38 /// The URL of the server to which to upload the package. | 39 /// The URL of the server to which to upload the package. |
| 39 Uri get server => new Uri.fromString(commandOptions['server']); | 40 Uri get server => new Uri.fromString(commandOptions['server']); |
| 40 | 41 |
| 41 Future _publish(packageBytes) { | 42 Future _publish(packageBytes) { |
| 42 var cloudStorageUrl; | 43 var cloudStorageUrl; |
| 43 return oauth2.withClient(cache, (client) { | 44 return oauth2.withClient(cache, (client) { |
| 44 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We | 45 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We |
| 45 // should report that error and exit. | 46 // should report that error and exit. |
| 46 var newUri = server.resolve("/packages/versions/new.json"); | 47 var newUri = server.resolve("/packages/versions/new.json"); |
| 47 return client.get(newUri).chain((response) { | 48 return client.get(newUri).then((response) { |
| 48 var parameters = parseJsonResponse(response); | 49 var parameters = parseJsonResponse(response); |
| 49 | 50 |
| 50 var url = _expectField(parameters, 'url', response); | 51 var url = _expectField(parameters, 'url', response); |
| 51 if (url is! String) invalidServerResponse(response); | 52 if (url is! String) invalidServerResponse(response); |
| 52 cloudStorageUrl = new Uri.fromString(url); | 53 cloudStorageUrl = new Uri.fromString(url); |
| 53 var request = new http.MultipartRequest('POST', cloudStorageUrl); | 54 var request = new http.MultipartRequest('POST', cloudStorageUrl); |
| 54 | 55 |
| 55 var fields = _expectField(parameters, 'fields', response); | 56 var fields = _expectField(parameters, 'fields', response); |
| 56 if (fields is! Map) invalidServerResponse(response); | 57 if (fields is! Map) invalidServerResponse(response); |
| 57 fields.forEach((key, value) { | 58 fields.forEach((key, value) { |
| 58 if (value is! String) invalidServerResponse(response); | 59 if (value is! String) invalidServerResponse(response); |
| 59 request.fields[key] = value; | 60 request.fields[key] = value; |
| 60 }); | 61 }); |
| 61 | 62 |
| 62 request.followRedirects = false; | 63 request.followRedirects = false; |
| 63 request.files.add(new http.MultipartFile.fromBytes( | 64 request.files.add(new http.MultipartFile.fromBytes( |
| 64 'file', packageBytes, filename: 'package.tar.gz')); | 65 'file', packageBytes, filename: 'package.tar.gz')); |
| 65 return client.send(request); | 66 return client.send(request); |
| 66 }).chain(http.Response.fromStream).then((response) { | 67 }).then(http.Response.fromStream).then((response) { |
| 67 var location = response.headers['location']; | 68 var location = response.headers['location']; |
| 68 if (location == null) throw new PubHttpException(response); | 69 if (location == null) throw new PubHttpException(response); |
| 69 return location; | 70 return location; |
| 70 }).then((location) => client.get(location)) | 71 }).then((location) => client.get(location)) |
| 71 .then(handleJsonSuccess); | 72 .then(handleJsonSuccess); |
| 72 }).transformException((e) { | 73 }).catchError((e) { |
| 73 if (e is! PubHttpException) throw e; | 74 if (e is! PubHttpException) throw e; |
| 74 var url = e.response.request.url; | 75 var url = e.response.request.url; |
| 75 if (url.toString() == cloudStorageUrl.toString()) { | 76 if (url.toString() == cloudStorageUrl.toString()) { |
| 76 // TODO(nweiz): the response may have XML-formatted information about | 77 // TODO(nweiz): the response may have XML-formatted information about |
| 77 // the error. Try to parse that out once we have an easily-accessible | 78 // the error. Try to parse that out once we have an easily-accessible |
| 78 // XML parser. | 79 // XML parser. |
| 79 throw 'Failed to upload the package.'; | 80 throw 'Failed to upload the package.'; |
| 80 } else if (url.origin == server.origin) { | 81 } else if (url.origin == server.origin) { |
| 81 handleJsonError(e.response); | 82 handleJsonError(e.response); |
| 82 } | 83 } |
| 83 }); | 84 }); |
| 84 } | 85 } |
| 85 | 86 |
| 86 Future onRun() { | 87 Future onRun() { |
| 87 var files; | 88 var files; |
| 88 return _filesToPublish.transform((f) { | 89 return _filesToPublish.then((f) { |
| 89 files = f; | 90 files = f; |
| 90 log.fine('Archiving and publishing ${entrypoint.root}.'); | 91 log.fine('Archiving and publishing ${entrypoint.root}.'); |
| 91 return createTarGz(files, baseDir: entrypoint.root.dir); | 92 return createTarGz(files, baseDir: entrypoint.root.dir); |
| 92 }).chain(consumeInputStream).chain((packageBytes) { | 93 }).then(consumeInputStream).then((packageBytes) { |
| 93 // Show the package contents so the user can verify they look OK. | 94 // Show the package contents so the user can verify they look OK. |
| 94 var package = entrypoint.root; | 95 var package = entrypoint.root; |
| 95 log.message( | 96 log.message( |
| 96 'Publishing "${package.name}" ${package.version}:\n' | 97 'Publishing "${package.name}" ${package.version}:\n' |
| 97 '${generateTree(files)}'); | 98 '${generateTree(files)}'); |
| 98 | 99 |
| 99 // Validate the package. | 100 // Validate the package. |
| 100 return _validate().chain((_) => _publish(packageBytes)); | 101 return _validate().then((_) => _publish(packageBytes)); |
| 101 }); | 102 }); |
| 102 } | 103 } |
| 103 | 104 |
| 104 /// The basenames of files that are automatically excluded from archives. | 105 /// The basenames of files that are automatically excluded from archives. |
| 105 final _BLACKLISTED_FILES = const ['pubspec.lock']; | 106 final _BLACKLISTED_FILES = const ['pubspec.lock']; |
| 106 | 107 |
| 107 /// The basenames of directories that are automatically excluded from | 108 /// The basenames of directories that are automatically excluded from |
| 108 /// archives. | 109 /// archives. |
| 109 final _BLACKLISTED_DIRECTORIES = const ['packages']; | 110 final _BLACKLISTED_DIRECTORIES = const ['packages']; |
| 110 | 111 |
| 111 /// Returns a list of files that should be included in the published package. | 112 /// Returns a list of files that should be included in the published package. |
| 112 /// If this is a Git repository, this will respect .gitignore; otherwise, it | 113 /// If this is a Git repository, this will respect .gitignore; otherwise, it |
| 113 /// will return all non-hidden files. | 114 /// will return all non-hidden files. |
| 114 Future<List<String>> get _filesToPublish { | 115 Future<List<String>> get _filesToPublish { |
| 115 var rootDir = entrypoint.root.dir; | 116 var rootDir = entrypoint.root.dir; |
| 116 | 117 |
| 117 return Futures.wait([ | 118 return Futures.wait([ |
| 118 dirExists(join(rootDir, '.git')), | 119 dirExists(join(rootDir, '.git')), |
| 119 git.isInstalled | 120 git.isInstalled |
| 120 ]).chain((results) { | 121 ]).then((results) { |
| 121 if (results[0] && results[1]) { | 122 if (results[0] && results[1]) { |
| 122 // List all files that aren't gitignored, including those not checked | 123 // List all files that aren't gitignored, including those not checked |
| 123 // in to Git. | 124 // in to Git. |
| 124 return git.run(["ls-files", "--cached", "--others", | 125 return git.run(["ls-files", "--cached", "--others", |
| 125 "--exclude-standard"]); | 126 "--exclude-standard"]); |
| 126 } | 127 } |
| 127 | 128 |
| 128 return listDir(rootDir, recursive: true).chain((entries) { | 129 return listDir(rootDir, recursive: true).then((entries) { |
| 129 return Futures.wait(entries.mappedBy((entry) { | 130 return Futures.wait(entries.mappedBy((entry) { |
| 130 return fileExists(entry).then((isFile) { | 131 return fileExists(entry).then((isFile) { |
| 131 // Skip directories. | 132 // Skip directories. |
| 132 if (!isFile) return null; | 133 if (!isFile) return null; |
| 133 | 134 |
| 134 // TODO(rnystrom): Making these relative will break archive | 135 // TODO(rnystrom): Making these relative will break archive |
| 135 // creation if the cwd is ever *not* the package root directory. | 136 // creation if the cwd is ever *not* the package root directory. |
| 136 // Should instead only make these relative right before generating | 137 // Should instead only make these relative right before generating |
| 137 // the tree display (which is what really needs them to be). | 138 // the tree display (which is what really needs them to be). |
| 138 // Make it relative to the package root. | 139 // Make it relative to the package root. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 151 | 152 |
| 152 /// Returns the value associated with [key] in [map]. Throws a user-friendly | 153 /// Returns the value associated with [key] in [map]. Throws a user-friendly |
| 153 /// error if [map] doens't contain [key]. | 154 /// error if [map] doens't contain [key]. |
| 154 _expectField(Map map, String key, http.Response response) { | 155 _expectField(Map map, String key, http.Response response) { |
| 155 if (map.containsKey(key)) return map[key]; | 156 if (map.containsKey(key)) return map[key]; |
| 156 invalidServerResponse(response); | 157 invalidServerResponse(response); |
| 157 } | 158 } |
| 158 | 159 |
| 159 /// Validates the package. Throws an exception if it's invalid. | 160 /// Validates the package. Throws an exception if it's invalid. |
| 160 Future _validate() { | 161 Future _validate() { |
| 161 return Validator.runAll(entrypoint).chain((pair) { | 162 return Validator.runAll(entrypoint).then((pair) { |
| 162 var errors = pair.first; | 163 var errors = pair.first; |
| 163 var warnings = pair.last; | 164 var warnings = pair.last; |
| 164 | 165 |
| 165 if (!errors.isEmpty) { | 166 if (!errors.isEmpty) { |
| 166 throw "Sorry, your package is missing " | 167 throw "Sorry, your package is missing " |
| 167 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " | 168 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " |
| 168 "and can't be published yet.\nFor more information, see: " | 169 "and can't be published yet.\nFor more information, see: " |
| 169 "http://pub.dartlang.org/doc/pub-lish.html.\n"; | 170 "http://pub.dartlang.org/doc/pub-lish.html.\n"; |
| 170 } | 171 } |
| 171 | 172 |
| 172 var message = 'Looks great! Are you ready to upload your package'; | 173 var message = 'Looks great! Are you ready to upload your package'; |
| 173 | 174 |
| 174 if (!warnings.isEmpty) { | 175 if (!warnings.isEmpty) { |
| 175 var s = warnings.length == 1 ? '' : 's'; | 176 var s = warnings.length == 1 ? '' : 's'; |
| 176 message = "Package has ${warnings.length} warning$s. Upload anyway"; | 177 message = "Package has ${warnings.length} warning$s. Upload anyway"; |
| 177 } | 178 } |
| 178 | 179 |
| 179 return confirm(message).then((confirmed) { | 180 return confirm(message).then((confirmed) { |
| 180 if (!confirmed) throw "Package upload canceled."; | 181 if (!confirmed) throw "Package upload canceled."; |
| 181 }); | 182 }); |
| 182 }); | 183 }); |
| 183 } | 184 } |
| 184 } | 185 } |
| OLD | NEW |