@@ -85,15 +85,24 @@ class PublishPluginCommand extends PluginCommand {
8585 final Print _print;
8686 final Stdin _stdin;
8787 // The directory of the actual package that we are publishing.
88- Directory _packageDir;
8988 StreamSubscription <String > _stdinSubscription;
9089
9190 @override
9291 Future <Null > run () async {
9392 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+
94100 _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+
97106 final bool shouldPushTag = argResults[_pushTagsOption];
98107 final String remote = argResults[_remoteOption];
99108 String remoteUrl;
@@ -102,60 +111,68 @@ class PublishPluginCommand extends PluginCommand {
102111 }
103112 _print ('Local repo is ready!' );
104113
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);
109122 }
123+ await _finishSuccesfully ();
124+ }
110125
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 ...' );
113139 await processRunner.runAndExitOnError ('git' , < String > ['tag' , tag],
114- workingDir: _packageDir );
140+ workingDir: packageDir );
115141 if (! shouldPushTag) {
116- return await _finishSuccesfully () ;
142+ return ;
117143 }
118144
119145 _print ('Pushing tag to $remote ...' );
120146 await _pushTagToRemote (remote: remote, tag: tag, remoteUrl: remoteUrl);
121- await _finishSuccesfully ();
122147 }
123148
124149 Future <void > _finishSuccesfully () async {
125150 await _stdinSubscription.cancel ();
126151 _print ('Done!' );
127152 }
128153
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.' );
139160 throw ToolExit (1 );
140161 }
141- return _packageDir ;
162+ return packageDir ;
142163 }
143164
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 {
150166 final ProcessResult statusResult = await processRunner.runAndExitOnError (
151167 'git' ,
152168 < String > [
153169 'status' ,
154170 '--porcelain' ,
155171 '--ignored' ,
156- _packageDir .absolute.path
172+ packageDir .absolute.path
157173 ],
158- workingDir: _packageDir);
174+ workingDir: packageDir);
175+
159176 final String statusOutput = statusResult.stdout;
160177 if (statusOutput.isNotEmpty) {
161178 _print (
@@ -169,17 +186,17 @@ class PublishPluginCommand extends PluginCommand {
169186 Future <String > _verifyRemote (String remote) async {
170187 final ProcessResult remoteInfo = await processRunner.runAndExitOnError (
171188 'git' , < String > ['remote' , 'get-url' , remote],
172- workingDir: _packageDir );
189+ workingDir: packagesDir );
173190 return remoteInfo.stdout;
174191 }
175192
176- Future <void > _publish () async {
193+ Future <void > _publish (Directory packageDir ) async {
177194 final List <String > publishFlags = argResults[_pubFlagsOption];
178195 _print (
179- 'Running `pub publish ${publishFlags .join (' ' )}` in ${_packageDir .absolute .path }...\n ' );
196+ 'Running `pub publish ${publishFlags .join (' ' )}` in ${packageDir .absolute .path }...\n ' );
180197 final Process publish = await processRunner.start (
181198 'flutter' , < String > ['pub' , 'publish' ] + publishFlags,
182- workingDirectory: _packageDir );
199+ workingDirectory: packageDir );
183200 publish.stdout
184201 .transform (utf8.decoder)
185202 .listen ((String data) => _print (data));
@@ -196,9 +213,9 @@ class PublishPluginCommand extends PluginCommand {
196213 }
197214 }
198215
199- String _getTag () {
216+ String _getTag (Directory packageDir ) {
200217 final File pubspecFile =
201- fileSystem.file (p.join (_packageDir .path, 'pubspec.yaml' ));
218+ fileSystem.file (p.join (packageDir .path, 'pubspec.yaml' ));
202219 final YamlMap pubspecYaml = loadYaml (pubspecFile.readAsStringSync ());
203220 final String name = pubspecYaml['name' ];
204221 final String version = pubspecYaml['version' ];
@@ -220,7 +237,6 @@ class PublishPluginCommand extends PluginCommand {
220237 _print ('Tag push canceled.' );
221238 throw ToolExit (1 );
222239 }
223-
224240 await processRunner.runAndExitOnError ('git' , < String > ['push' , remote, tag],
225241 workingDir: packagesDir);
226242 }
0 commit comments