Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: sdk/lib/_internal/pub/lib/src/command_lish.dart

Issue 18143002: Reorganize some pub libraries. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove some newlines. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
11 import 'package:args/args.dart';
12 import 'package:http/http.dart' as http;
13 import 'package:pathos/path.dart' as path;
14
15 import 'command.dart';
16 import 'directory_tree.dart';
17 import 'exit_codes.dart' as exit_codes;
18 import 'git.dart' as git;
19 import 'hosted_source.dart';
20 import 'http.dart';
21 import 'io.dart';
22 import 'log.dart' as log;
23 import 'oauth2.dart' as oauth2;
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 /// The URL of the server to which to upload the package.
34 Uri get server => Uri.parse(commandOptions['server']);
35
36 /// Whether the publish is just a preview.
37 bool get dryRun => commandOptions['dry-run'];
38
39 /// Whether the publish requires confirmation.
40 bool get force => commandOptions['force'];
41
42 LishCommand() {
43 commandParser.addFlag('dry-run', abbr: 'n', negatable: false,
44 help: 'Validate but do not publish the package.');
45 commandParser.addFlag('force', abbr: 'f', negatable: false,
46 help: 'Publish without confirmation if there are no errors.');
47 commandParser.addOption('server', defaultsTo: HostedSource.DEFAULT_URL,
48 help: 'The package server to which to upload this package.');
49 }
50
51 Future _publish(packageBytes) {
52 var cloudStorageUrl;
53 return oauth2.withClient(cache, (client) {
54 return log.progress('Uploading', () {
55 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We
56 // should report that error and exit.
57 var newUri = server.resolve("/api/packages/versions/new");
58 return client.get(newUri, headers: PUB_API_HEADERS).then((response) {
59 var parameters = parseJsonResponse(response);
60
61 var url = _expectField(parameters, 'url', response);
62 if (url is! String) invalidServerResponse(response);
63 cloudStorageUrl = Uri.parse(url);
64 var request = new http.MultipartRequest('POST', cloudStorageUrl);
65
66 var fields = _expectField(parameters, 'fields', response);
67 if (fields is! Map) invalidServerResponse(response);
68 fields.forEach((key, value) {
69 if (value is! String) invalidServerResponse(response);
70 request.fields[key] = value;
71 });
72
73 request.followRedirects = false;
74 request.files.add(new http.MultipartFile.fromBytes(
75 'file', packageBytes, filename: 'package.tar.gz'));
76 return client.send(request);
77 }).then(http.Response.fromStream).then((response) {
78 var location = response.headers['location'];
79 if (location == null) throw new PubHttpException(response);
80 return location;
81 }).then((location) => client.get(location, headers: PUB_API_HEADERS))
82 .then(handleJsonSuccess);
83 });
84 }).catchError((error) {
85 if (error is! PubHttpException) throw error;
86 var url = error.response.request.url;
87 if (urisEqual(url, cloudStorageUrl)) {
88 // TODO(nweiz): the response may have XML-formatted information about
89 // the error. Try to parse that out once we have an easily-accessible
90 // XML parser.
91 throw new ApplicationException('Failed to upload the package.');
92 } else if (urisEqual(Uri.parse(url.origin), Uri.parse(server.origin))) {
93 handleJsonError(error.response);
94 } else {
95 throw error;
96 }
97 });
98 }
99
100 Future onRun() {
101 if (force && dryRun) {
102 log.error('Cannot use both --force and --dry-run.');
103 this.printUsage();
104 exit(exit_codes.USAGE);
105 }
106
107 var packageBytesFuture = entrypoint.packageFiles().then((files) {
108 log.fine('Archiving and publishing ${entrypoint.root}.');
109
110 // Show the package contents so the user can verify they look OK.
111 var package = entrypoint.root;
112 log.message(
113 'Publishing "${package.name}" ${package.version}:\n'
114 '${generateTree(files, baseDir: entrypoint.root.dir)}');
115
116 return createTarGz(files, baseDir: entrypoint.root.dir);
117 }).then((stream) => stream.toBytes());
118
119 // Validate the package.
120 return _validate(packageBytesFuture.then((bytes) => bytes.length))
121 .then((isValid) {
122 if (isValid) return packageBytesFuture.then(_publish);
123 });
124 }
125
126 /// Returns the value associated with [key] in [map]. Throws a user-friendly
127 /// error if [map] doens't contain [key].
128 _expectField(Map map, String key, http.Response response) {
129 if (map.containsKey(key)) return map[key];
130 invalidServerResponse(response);
131 }
132
133 /// Validates the package. Completes to false if the upload should not
134 /// proceed.
135 Future<bool> _validate(Future<int> packageSize) {
136 return Validator.runAll(entrypoint, packageSize).then((pair) {
137 var errors = pair.first;
138 var warnings = pair.last;
139
140 if (!errors.isEmpty) {
141 log.error("Sorry, your package is missing "
142 "${(errors.length > 1) ? 'some requirements' : 'a requirement'} "
143 "and can't be published yet.\nFor more information, see: "
144 "http://pub.dartlang.org/doc/pub-lish.html.\n");
145 return false;
146 }
147
148 if (force) return true;
149
150 if (dryRun) {
151 var s = warnings.length == 1 ? '' : 's';
152 log.warning("Package has ${warnings.length} warning$s.");
153 return false;
154 }
155
156 var message = 'Looks great! Are you ready to upload your package';
157
158 if (!warnings.isEmpty) {
159 var s = warnings.length == 1 ? '' : 's';
160 message = "Package has ${warnings.length} warning$s. Upload anyway";
161 }
162
163 return confirm(message).then((confirmed) {
164 if (!confirmed) {
165 log.error("Package upload canceled.");
166 return false;
167 }
168 return true;
169 });
170 });
171 }
172 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/pub/lib/src/command_install.dart ('k') | sdk/lib/_internal/pub/lib/src/command_update.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698