@@ -85,15 +85,24 @@ class PublishPluginCommand extends PluginCommand {
85
85
final Print _print;
86
86
final Stdin _stdin;
87
87
// The directory of the actual package that we are publishing.
88
- Directory _packageDir;
89
88
StreamSubscription <String > _stdinSubscription;
90
89
91
90
@override
92
91
Future <Null > run () async {
93
92
checkSharding ();
93
+ final String package = argResults[_packageOption];
94
+ if (package == null ) {
95
+ _print (
96
+ 'Must specify a package to publish. See `plugin_tools help publish-plugin`.' );
97
+ throw ToolExit (1 );
98
+ }
99
+
94
100
_print ('Checking local repo...' );
95
- _packageDir = _checkPackageDir ();
96
- await _checkGitStatus ();
101
+ if (! await GitDir .isGitDir (packagesDir.path)) {
102
+ _print ('$packagesDir is not a valid Git repository.' );
103
+ throw ToolExit (1 );
104
+ }
105
+
97
106
final bool shouldPushTag = argResults[_pushTagsOption];
98
107
final String remote = argResults[_remoteOption];
99
108
String remoteUrl;
@@ -102,60 +111,68 @@ class PublishPluginCommand extends PluginCommand {
102
111
}
103
112
_print ('Local repo is ready!' );
104
113
105
- await _publish ();
106
- _print ('Package published!' );
107
- if (! argResults[_tagReleaseOption]) {
108
- return await _finishSuccesfully ();
114
+ final Directory packageDir = _getPackageDir (package);
115
+ await _publishPlugin (packageDir: packageDir);
116
+ if (argResults[_tagReleaseOption] as bool ) {
117
+ await _tagRelease (
118
+ packageDir: packageDir,
119
+ remote: remote,
120
+ remoteUrl: remoteUrl,
121
+ shouldPushTag: shouldPushTag);
109
122
}
123
+ await _finishSuccesfully ();
124
+ }
110
125
111
- _print ('Tagging release...' );
112
- final String tag = _getTag ();
126
+ Future <void > _publishPlugin ({@required Directory packageDir}) async {
127
+ await _checkGitStatus (packageDir);
128
+ await _publish (packageDir);
129
+ _print ('Package published!' );
130
+ }
131
+
132
+ Future <void > _tagRelease (
133
+ {@required Directory packageDir,
134
+ @required String remote,
135
+ @required String remoteUrl,
136
+ @required bool shouldPushTag}) async {
137
+ final String tag = _getTag (packageDir);
138
+ _print ('Tagging release $tag ...' );
113
139
await processRunner.runAndExitOnError ('git' , < String > ['tag' , tag],
114
- workingDir: _packageDir );
140
+ workingDir: packageDir );
115
141
if (! shouldPushTag) {
116
- return await _finishSuccesfully () ;
142
+ return ;
117
143
}
118
144
119
145
_print ('Pushing tag to $remote ...' );
120
146
await _pushTagToRemote (remote: remote, tag: tag, remoteUrl: remoteUrl);
121
- await _finishSuccesfully ();
122
147
}
123
148
124
149
Future <void > _finishSuccesfully () async {
125
150
await _stdinSubscription.cancel ();
126
151
_print ('Done!' );
127
152
}
128
153
129
- Directory _checkPackageDir () {
130
- final String package = argResults[_packageOption];
131
- if (package == null ) {
132
- _print (
133
- 'Must specify a package to publish. See `plugin_tools help publish-plugin`.' );
134
- throw ToolExit (1 );
135
- }
136
- final Directory _packageDir = packagesDir.childDirectory (package);
137
- if (! _packageDir.existsSync ()) {
138
- _print ('${_packageDir .absolute .path } does not exist.' );
154
+ // Returns the packageDirectory based on the package name.
155
+ // Throws ToolExit if the `package` doesn't exist.
156
+ Directory _getPackageDir (String package) {
157
+ final Directory packageDir = packagesDir.childDirectory (package);
158
+ if (! packageDir.existsSync ()) {
159
+ _print ('${packageDir .absolute .path } does not exist.' );
139
160
throw ToolExit (1 );
140
161
}
141
- return _packageDir ;
162
+ return packageDir ;
142
163
}
143
164
144
- Future <void > _checkGitStatus () async {
145
- if (! await GitDir .isGitDir (packagesDir.path)) {
146
- _print ('$packagesDir is not a valid Git repository.' );
147
- throw ToolExit (1 );
148
- }
149
-
165
+ Future <void > _checkGitStatus (Directory packageDir) async {
150
166
final ProcessResult statusResult = await processRunner.runAndExitOnError (
151
167
'git' ,
152
168
< String > [
153
169
'status' ,
154
170
'--porcelain' ,
155
171
'--ignored' ,
156
- _packageDir .absolute.path
172
+ packageDir .absolute.path
157
173
],
158
- workingDir: _packageDir);
174
+ workingDir: packageDir);
175
+
159
176
final String statusOutput = statusResult.stdout;
160
177
if (statusOutput.isNotEmpty) {
161
178
_print (
@@ -169,17 +186,17 @@ class PublishPluginCommand extends PluginCommand {
169
186
Future <String > _verifyRemote (String remote) async {
170
187
final ProcessResult remoteInfo = await processRunner.runAndExitOnError (
171
188
'git' , < String > ['remote' , 'get-url' , remote],
172
- workingDir: _packageDir );
189
+ workingDir: packagesDir );
173
190
return remoteInfo.stdout;
174
191
}
175
192
176
- Future <void > _publish () async {
193
+ Future <void > _publish (Directory packageDir ) async {
177
194
final List <String > publishFlags = argResults[_pubFlagsOption];
178
195
_print (
179
- 'Running `pub publish ${publishFlags .join (' ' )}` in ${_packageDir .absolute .path }...\n ' );
196
+ 'Running `pub publish ${publishFlags .join (' ' )}` in ${packageDir .absolute .path }...\n ' );
180
197
final Process publish = await processRunner.start (
181
198
'flutter' , < String > ['pub' , 'publish' ] + publishFlags,
182
- workingDirectory: _packageDir );
199
+ workingDirectory: packageDir );
183
200
publish.stdout
184
201
.transform (utf8.decoder)
185
202
.listen ((String data) => _print (data));
@@ -196,9 +213,9 @@ class PublishPluginCommand extends PluginCommand {
196
213
}
197
214
}
198
215
199
- String _getTag () {
216
+ String _getTag (Directory packageDir ) {
200
217
final File pubspecFile =
201
- fileSystem.file (p.join (_packageDir .path, 'pubspec.yaml' ));
218
+ fileSystem.file (p.join (packageDir .path, 'pubspec.yaml' ));
202
219
final YamlMap pubspecYaml = loadYaml (pubspecFile.readAsStringSync ());
203
220
final String name = pubspecYaml['name' ];
204
221
final String version = pubspecYaml['version' ];
@@ -220,7 +237,6 @@ class PublishPluginCommand extends PluginCommand {
220
237
_print ('Tag push canceled.' );
221
238
throw ToolExit (1 );
222
239
}
223
-
224
240
await processRunner.runAndExitOnError ('git' , < String > ['push' , remote, tag],
225
241
workingDir: packagesDir);
226
242
}
0 commit comments