This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[tool] refactor publish plugin command #3779
Merged
fluttergithubbot
merged 7 commits into
flutter:master
from
cyanglaz:publish_plugin_refactor
Apr 5, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,15 +85,24 @@ class PublishPluginCommand extends PluginCommand { | |
| final Print _print; | ||
| final Stdin _stdin; | ||
| // The directory of the actual package that we are publishing. | ||
| Directory _packageDir; | ||
| StreamSubscription<String> _stdinSubscription; | ||
|
|
||
| @override | ||
| Future<Null> run() async { | ||
| checkSharding(); | ||
| final String package = argResults[_packageOption]; | ||
| if (package == null) { | ||
| _print( | ||
| 'Must specify a package to publish. See `plugin_tools help publish-plugin`.'); | ||
| throw ToolExit(1); | ||
| } | ||
|
|
||
| _print('Checking local repo...'); | ||
| _packageDir = _checkPackageDir(); | ||
| await _checkGitStatus(); | ||
| if (!await GitDir.isGitDir(packagesDir.path)) { | ||
| _print('$packagesDir is not a valid Git repository.'); | ||
| throw ToolExit(1); | ||
| } | ||
|
|
||
| final bool shouldPushTag = argResults[_pushTagsOption]; | ||
| final String remote = argResults[_remoteOption]; | ||
| String remoteUrl; | ||
|
|
@@ -102,60 +111,68 @@ class PublishPluginCommand extends PluginCommand { | |
| } | ||
| _print('Local repo is ready!'); | ||
|
|
||
| await _publish(); | ||
| _print('Package published!'); | ||
| if (!argResults[_tagReleaseOption]) { | ||
| return await _finishSuccesfully(); | ||
| final Directory packageDir = _getPackageDir(package); | ||
| await _publishPlugin(packageDir: packageDir); | ||
| if (argResults[_tagReleaseOption] as bool) { | ||
| await _tagRelease( | ||
| packageDir: packageDir, | ||
| remote: remote, | ||
| remoteUrl: remoteUrl, | ||
| shouldPushTag: shouldPushTag); | ||
| } | ||
| await _finishSuccesfully(); | ||
| } | ||
|
|
||
| _print('Tagging release...'); | ||
| final String tag = _getTag(); | ||
| Future<void> _publishPlugin({@required Directory packageDir}) async { | ||
| await _checkGitStatus(packageDir); | ||
| await _publish(packageDir); | ||
| _print('Package published!'); | ||
| } | ||
|
|
||
| Future<void> _tagRelease( | ||
| {@required Directory packageDir, | ||
| @required String remote, | ||
| @required String remoteUrl, | ||
| @required bool shouldPushTag}) async { | ||
| final String tag = _getTag(packageDir); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's flip this and the line above, so the print can include the tag for ease of debugging issues. |
||
| _print('Tagging release $tag...'); | ||
| await processRunner.runAndExitOnError('git', <String>['tag', tag], | ||
| workingDir: _packageDir); | ||
| workingDir: packageDir); | ||
| if (!shouldPushTag) { | ||
| return await _finishSuccesfully(); | ||
| return; | ||
| } | ||
|
|
||
| _print('Pushing tag to $remote...'); | ||
| await _pushTagToRemote(remote: remote, tag: tag, remoteUrl: remoteUrl); | ||
| await _finishSuccesfully(); | ||
| } | ||
|
|
||
| Future<void> _finishSuccesfully() async { | ||
| await _stdinSubscription.cancel(); | ||
| _print('Done!'); | ||
| } | ||
|
|
||
| Directory _checkPackageDir() { | ||
| final String package = argResults[_packageOption]; | ||
| if (package == null) { | ||
| _print( | ||
| 'Must specify a package to publish. See `plugin_tools help publish-plugin`.'); | ||
| throw ToolExit(1); | ||
| } | ||
| final Directory _packageDir = packagesDir.childDirectory(package); | ||
| if (!_packageDir.existsSync()) { | ||
| _print('${_packageDir.absolute.path} does not exist.'); | ||
| // Returns the packageDirectory based on the package name. | ||
| // Throws ToolExit if the `package` doesn't exist. | ||
| Directory _getPackageDir(String package) { | ||
| final Directory packageDir = packagesDir.childDirectory(package); | ||
| if (!packageDir.existsSync()) { | ||
| _print('${packageDir.absolute.path} does not exist.'); | ||
| throw ToolExit(1); | ||
| } | ||
| return _packageDir; | ||
| return packageDir; | ||
| } | ||
|
|
||
| Future<void> _checkGitStatus() async { | ||
| if (!await GitDir.isGitDir(packagesDir.path)) { | ||
| _print('$packagesDir is not a valid Git repository.'); | ||
| throw ToolExit(1); | ||
| } | ||
|
|
||
| Future<void> _checkGitStatus(Directory packageDir) async { | ||
| final ProcessResult statusResult = await processRunner.runAndExitOnError( | ||
| 'git', | ||
| <String>[ | ||
| 'status', | ||
| '--porcelain', | ||
| '--ignored', | ||
| _packageDir.absolute.path | ||
| packageDir.absolute.path | ||
| ], | ||
| workingDir: _packageDir); | ||
| workingDir: packageDir); | ||
|
|
||
| final String statusOutput = statusResult.stdout; | ||
| if (statusOutput.isNotEmpty) { | ||
| _print( | ||
|
|
@@ -169,17 +186,17 @@ class PublishPluginCommand extends PluginCommand { | |
| Future<String> _verifyRemote(String remote) async { | ||
| final ProcessResult remoteInfo = await processRunner.runAndExitOnError( | ||
| 'git', <String>['remote', 'get-url', remote], | ||
| workingDir: _packageDir); | ||
| workingDir: packagesDir); | ||
| return remoteInfo.stdout; | ||
| } | ||
|
|
||
| Future<void> _publish() async { | ||
| Future<void> _publish(Directory packageDir) async { | ||
| final List<String> publishFlags = argResults[_pubFlagsOption]; | ||
| _print( | ||
| 'Running `pub publish ${publishFlags.join(' ')}` in ${_packageDir.absolute.path}...\n'); | ||
| 'Running `pub publish ${publishFlags.join(' ')}` in ${packageDir.absolute.path}...\n'); | ||
| final Process publish = await processRunner.start( | ||
| 'flutter', <String>['pub', 'publish'] + publishFlags, | ||
| workingDirectory: _packageDir); | ||
| workingDirectory: packageDir); | ||
| publish.stdout | ||
| .transform(utf8.decoder) | ||
| .listen((String data) => _print(data)); | ||
|
|
@@ -196,9 +213,9 @@ class PublishPluginCommand extends PluginCommand { | |
| } | ||
| } | ||
|
|
||
| String _getTag() { | ||
| String _getTag(Directory packageDir) { | ||
| final File pubspecFile = | ||
| fileSystem.file(p.join(_packageDir.path, 'pubspec.yaml')); | ||
| fileSystem.file(p.join(packageDir.path, 'pubspec.yaml')); | ||
| final YamlMap pubspecYaml = loadYaml(pubspecFile.readAsStringSync()); | ||
| final String name = pubspecYaml['name']; | ||
| final String version = pubspecYaml['version']; | ||
|
|
@@ -220,7 +237,6 @@ class PublishPluginCommand extends PluginCommand { | |
| _print('Tag push canceled.'); | ||
| throw ToolExit(1); | ||
| } | ||
|
|
||
| await processRunner.runAndExitOnError('git', <String>['push', remote, tag], | ||
| workingDir: packagesDir); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.