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:io'; | 7 import 'dart:io'; |
8 import 'dart:json'; | 8 import 'dart:json'; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 help: 'The package server to which to upload this package'); | 29 help: 'The package server to which to upload this package'); |
30 return parser; | 30 return parser; |
31 } | 31 } |
32 | 32 |
33 /// The URL of the server to which to upload the package. | 33 /// The URL of the server to which to upload the package. |
34 Uri get server => new Uri.fromString(commandOptions['server']); | 34 Uri get server => new Uri.fromString(commandOptions['server']); |
35 | 35 |
36 Future onRun() { | 36 Future onRun() { |
37 var cloudStorageUrl; | 37 var cloudStorageUrl; |
38 return oauth2.withClient(cache, (client) { | 38 return oauth2.withClient(cache, (client) { |
39 // TODO(nweiz): Better error-handling. There are a few cases we need to | 39 // TODO(nweiz): Cloud Storage can provide an XML-formatted error. We |
40 // handle better: | 40 // should report that error and exit. |
41 // | |
42 // * The server can tell us we need new credentials (a 401 error). The | |
43 // oauth2 package should throw an AuthorizationException in this case | |
44 // (contingent on issue 6813 and 6275). We should have the user | |
45 // re-authorize the client, then restart the command. We should also do | |
46 // this in case of an ExpirationException. See issue 6950. | |
47 // | |
48 // * Cloud Storage can provide an XML-formatted error. We should report | |
49 // that error and exit. | |
50 return Futures.wait([ | 41 return Futures.wait([ |
51 client.get(server.resolve("/packages/versions/new.json")), | 42 client.get(server.resolve("/packages/versions/new.json")), |
52 _filesToPublish.transform((files) { | 43 _filesToPublish.transform((files) { |
53 return createTarGz(files, baseDir: entrypoint.root.dir); | 44 return createTarGz(files, baseDir: entrypoint.root.dir); |
54 }).chain(consumeInputStream) | 45 }).chain(consumeInputStream) |
55 ]).chain((results) { | 46 ]).chain((results) { |
56 var response = results[0]; | 47 var response = results[0]; |
57 var packageBytes = results[1]; | 48 var packageBytes = results[1]; |
58 var parameters = _parseJson(response); | 49 var parameters = _parseJson(response); |
59 | 50 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 throw 'Failed to upload the package.'; | 87 throw 'Failed to upload the package.'; |
97 } else if (url.origin == server.origin) { | 88 } else if (url.origin == server.origin) { |
98 var errorMap = _parseJson(e.response); | 89 var errorMap = _parseJson(e.response); |
99 if (errorMap['error'] is! Map || | 90 if (errorMap['error'] is! Map || |
100 !errorMap['error'].containsKey('message') || | 91 !errorMap['error'].containsKey('message') || |
101 errorMap['error']['message'] is! String) { | 92 errorMap['error']['message'] is! String) { |
102 _invalidServerResponse(e.response); | 93 _invalidServerResponse(e.response); |
103 } | 94 } |
104 throw errorMap['error']['message']; | 95 throw errorMap['error']['message']; |
105 } | 96 } |
| 97 } else if (e is oauth2.ExpirationException) { |
| 98 printError("Pub's authorization to upload packages has expired and " |
| 99 "can't be automatically refreshed."); |
| 100 return onRun(); |
| 101 } else if (e is oauth2.AuthorizationException) { |
| 102 var message = "OAuth2 authorization failed"; |
| 103 if (e.description != null) message = "$message (${e.description})"; |
| 104 printError("$message."); |
| 105 return oauth2.clearCredentials(cache).chain((_) => onRun()); |
| 106 } else { |
| 107 throw e; |
106 } | 108 } |
107 | |
108 if (e is! oauth2.ExpirationException) throw e; | |
109 | |
110 printError("Pub's authorization to upload packages has expired and can't " | |
111 "be automatically refreshed."); | |
112 return onRun(); | |
113 }); | 109 }); |
114 } | 110 } |
115 | 111 |
116 /// The basenames of files that are automatically excluded from archives. | 112 /// The basenames of files that are automatically excluded from archives. |
117 final _BLACKLISTED_FILES = const ['pubspec.lock']; | 113 final _BLACKLISTED_FILES = const ['pubspec.lock']; |
118 | 114 |
119 /// The basenames of directories that are automatically excluded from | 115 /// The basenames of directories that are automatically excluded from |
120 /// archives. | 116 /// archives. |
121 final _BLACKLISTED_DIRECTORIES = const ['packages']; | 117 final _BLACKLISTED_DIRECTORIES = const ['packages']; |
122 | 118 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 _expectField(Map map, String key, http.Response response) { | 164 _expectField(Map map, String key, http.Response response) { |
169 if (map.containsKey(key)) return map[key]; | 165 if (map.containsKey(key)) return map[key]; |
170 _invalidServerResponse(response); | 166 _invalidServerResponse(response); |
171 } | 167 } |
172 | 168 |
173 /// Throws an error describing an invalid response from the server. | 169 /// Throws an error describing an invalid response from the server. |
174 void _invalidServerResponse(http.Response response) { | 170 void _invalidServerResponse(http.Response response) { |
175 throw 'Invalid server response:\n${response.body}'; | 171 throw 'Invalid server response:\n${response.body}'; |
176 } | 172 } |
177 } | 173 } |
OLD | NEW |