OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library command_lish; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 import 'dart:json'; | |
10 import 'dart:uri'; | |
11 | |
12 import 'package:args/args.dart'; | |
13 import 'package:http/http.dart' as http; | |
14 import 'package:pathos/path.dart' as path; | |
15 | |
16 import 'directory_tree.dart'; | |
17 import 'exit_codes.dart' as exit_codes; | |
18 import 'git.dart' as git; | |
19 import 'http.dart'; | |
20 import 'io.dart'; | |
21 import 'log.dart' as log; | |
22 import 'oauth2.dart' as oauth2; | |
23 import 'pub.dart'; | |
24 import 'utils.dart'; | |
25 import 'validator.dart'; | |
26 | |
27 /// Handles the `lish` and `publish` pub commands. | |
28 class LishCommand extends PubCommand { | |
29 final description = "Publish the current package to pub.dartlang.org."; | |
30 final usage = "pub publish [options]"; | |
31 final aliases = const ["lish", "lush"]; | |
32 | |
33 ArgParser get commandParser { | |
34 var parser = new ArgParser(); | |
35 // TODO(nweiz): Use HostedSource.defaultUrl as the default value once we use | |
36 // dart:io for HTTPS requests. | |
37 parser.addFlag('dry-run', abbr: 'n', negatable: false, | |
38 help: 'Validate but do not publish the package'); | |
39 parser.addFlag('force', abbr: 'f', negatable: false, | |
40 help: 'Publish without confirmation if there are no errors'); | |
41 parser.addOption('server', defaultsTo: 'https://pub.dartlang.org', | |
42 help: 'The package server to which to upload this package'); | |
43 return parser; | |
44 } | |
45 | |
46 /// The URL of the server to which to upload the package. | |
47 Uri get server => Uri.parse(commandOptions['server']); | |
48 | |
49 /// Whether the publish is just a preview. | |
50 bool get dryRun => commandOptions['dry-run']; | |
51 | |
52 /// Whether the publish requires confirmation. | |
53 bool get force => commandOptions['force']; | |
54 | |
55 Future _publish(packageBytes) { | |
56 var cloudStorageUrl; | |
57 return oauth2.withClient(cache, (client) { | |
58 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We | |
59 // should report that error and exit. | |
60 var newUri = server.resolve("/packages/versions/new.json"); | |
61 return client.get(newUri).then((response) { | |
62 var parameters = parseJsonResponse(response); | |
63 | |
64 var url = _expectField(parameters, 'url', response); | |
65 if (url is! String) invalidServerResponse(response); | |
66 cloudStorageUrl = Uri.parse(url); | |
67 var request = new http.MultipartRequest('POST', cloudStorageUrl); | |
68 | |
69 var fields = _expectField(parameters, 'fields', response); | |
70 if (fields is! Map) invalidServerResponse(response); | |
71 fields.forEach((key, value) { | |
72 if (value is! String) invalidServerResponse(response); | |
73 request.fields[key] = value; | |
74 }); | |
75 | |
76 request.followRedirects = false; | |
77 request.files.add(new http.MultipartFile.fromBytes( | |
78 'file', packageBytes, filename: 'package.tar.gz')); | |
79 return client.send(request); | |
80 }).then(http.Response.fromStream).then((response) { | |
81 var location = response.headers['location']; | |
82 if (location == null) throw new PubHttpException(response); | |
83 return location; | |
84 }).then((location) => client.get(location)) | |
85 .then(handleJsonSuccess); | |
86 }).catchError((error) { | |
87 if (error is! PubHttpException) throw error; | |
88 var url = error.response.request.url; | |
89 if (urisEqual(url, cloudStorageUrl)) { | |
90 // TODO(nweiz): the response may have XML-formatted information about | |
91 // the error. Try to parse that out once we have an easily-accessible | |
92 // XML parser. | |
93 throw new Exception('Failed to upload the package.'); | |
94 } else if (urisEqual(Uri.parse(url.origin), Uri.parse(server.origin))) { | |
95 handleJsonError(error.response); | |
96 } else { | |
97 throw error; | |
98 } | |
99 }); | |
100 } | |
101 | |
102 Future onRun() { | |
103 if (force && dryRun) { | |
104 log.error('Cannot use both --force and --dry-run.'); | |
105 this.printUsage(); | |
106 exit(exit_codes.USAGE); | |
107 } | |
108 | |
109 var packageBytesFuture = _filesToPublish.then((files) { | |
110 log.fine('Archiving and publishing ${entrypoint.root}.'); | |
111 | |
112 // Show the package contents so the user can verify they look OK. | |
113 var package = entrypoint.root; | |
114 log.message( | |
115 'Publishing "${package.name}" ${package.version}:\n' | |
116 '${generateTree(files)}'); | |
117 | |
118 return createTarGz(files, baseDir: entrypoint.root.dir); | |
119 }).then((stream) => stream.toBytes()); | |
120 | |
121 // Validate the package. | |
122 return _validate(packageBytesFuture.then((bytes) => bytes.length)) | |
123 .then((isValid) { | |
124 if (isValid) return packageBytesFuture.then(_publish); | |
125 }); | |
126 } | |
127 | |
128 /// The basenames of files that are automatically excluded from archives. | |
129 final _BLACKLISTED_FILES = const ['pubspec.lock']; | |
130 | |
131 /// The basenames of directories that are automatically excluded from | |
132 /// archives. | |
133 final _BLACKLISTED_DIRS = const ['packages']; | |
134 | |
135 /// Returns a list of files that should be included in the published package. | |
136 /// If this is a Git repository, this will respect .gitignore; otherwise, it | |
137 /// will return all non-hidden files. | |
138 Future<List<String>> get _filesToPublish { | |
139 var rootDir = entrypoint.root.dir; | |
140 | |
141 return git.isInstalled.then((gitInstalled) { | |
142 if (dirExists(path.join(rootDir, '.git')) && gitInstalled) { | |
143 // List all files that aren't gitignored, including those not checked | |
144 // in to Git. | |
145 return git.run(["ls-files", "--cached", "--others", | |
146 "--exclude-standard"]); | |
147 } | |
148 | |
149 return listDir(rootDir, recursive: true) | |
150 .where(fileExists) // Skip directories and broken symlinks. | |
151 .map((entry) => path.relative(entry, from: rootDir)); | |
152 }).then((files) => files.where(_shouldPublish).toList()); | |
153 } | |
154 | |
155 /// Returns `true` if [file] should be published. | |
156 bool _shouldPublish(String file) { | |
157 if (_BLACKLISTED_FILES.contains(path.basename(file))) return false; | |
158 return !path.split(file).any(_BLACKLISTED_DIRS.contains); | |
159 } | |
160 | |
161 /// Returns the value associated with [key] in [map]. Throws a user-friendly | |
162 /// error if [map] doens't contain [key]. | |
163 _expectField(Map map, String key, http.Response response) { | |
164 if (map.containsKey(key)) return map[key]; | |
165 invalidServerResponse(response); | |
166 } | |
167 | |
168 /// Validates the package. Completes to false if the upload should not | |
169 /// proceed. | |
170 Future<bool> _validate(Future<int> packageSize) { | |
171 return Validator.runAll(entrypoint, packageSize).then((pair) { | |
172 var errors = pair.first; | |
173 var warnings = pair.last; | |
174 | |
175 if (!errors.isEmpty) { | |
176 log.error("Sorry, your package is missing " | |
177 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} " | |
178 "and can't be published yet.\nFor more information, see: " | |
179 "http://pub.dartlang.org/doc/pub-lish.html.\n"); | |
180 return false; | |
181 } | |
182 | |
183 if (force) return true; | |
184 | |
185 if (dryRun) { | |
186 var s = warnings.length == 1 ? '' : 's'; | |
187 log.warning("Package has ${warnings.length} warning$s."); | |
188 return false; | |
189 } | |
190 | |
191 var message = 'Looks great! Are you ready to upload your package'; | |
192 | |
193 if (!warnings.isEmpty) { | |
194 var s = warnings.length == 1 ? '' : 's'; | |
195 message = "Package has ${warnings.length} warning$s. Upload anyway"; | |
196 } | |
197 | |
198 return confirm(message).then((confirmed) { | |
199 if (!confirmed) { | |
200 log.error("Package upload canceled."); | |
201 return false; | |
202 } | |
203 return true; | |
204 }); | |
205 }); | |
206 } | |
207 } | |
OLD | NEW |