Skip to content

Commit

Permalink
Add version-info cli option (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickjmiller authored Sep 23, 2024
1 parent 2e8423a commit 998256d
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.2.0-wip

- Add `--version-info` option. This is off by default.

## 5.1.0

- Support build wasm option for Flutter web.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Arguments:
--[no-]source-branch-info Includes the name of the source branch and SHA
in the commit message
(defaults to on)
--[no-]version-info Includes the pubspec version of the package in
the commit message
--post-build-dart-script Optional Dart script to run after all builds
have completed, but before files are committed
to the repository.
Expand Down
13 changes: 13 additions & 0 deletions lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const _defaultRelease = true;
const _defaultVerbose = false;
const _defaultSourceBranchInfo = true;
const _defaultDryRun = false;
const _defaultVersionInfo = false;
const _defaultWebRenderer = WebRenderer.auto;

const defaultMessage = 'Built <$_directoryFlag>';
Expand Down Expand Up @@ -98,6 +99,15 @@ Flutter: enabled passes `--release`, otherwise passes `--profile`.
@JsonKey(includeToJson: false, includeFromJson: false)
final bool sourceBranchInfoWasParsed;

@CliOption(
defaultsTo: _defaultVersionInfo,
help: 'Includes the pubspec version of the package in the commit message',
)
final bool versionInfo;

@JsonKey(includeToJson: false, includeFromJson: false)
final bool versionInfoWasParsed;

@CliOption(
help: 'Optional Dart script to run after all builds have completed, but '
'before files are committed to the repository.',
Expand Down Expand Up @@ -220,6 +230,8 @@ See the README for details.''',
this.extraArgs,
this.help = false,
this.version = false,
this.versionInfo = _defaultVersionInfo,
this.versionInfoWasParsed = false,
this.rest = const [],
});

Expand Down Expand Up @@ -252,6 +264,7 @@ See the README for details.''',
sourceBranchInfo:
sourceBranchInfoWasParsed ? sourceBranchInfo : other.sourceBranchInfo,
version: version,
versionInfo: versionInfoWasParsed ? versionInfo : other.versionInfo,
verbose: verboseWasParsed ? verbose : other.verbose,
);
}
Expand Down
13 changes: 12 additions & 1 deletion lib/src/options.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/src/peanut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ Directories:
}
}

if (options.versionInfo) {
final version = await getPackageVersion(workingDir);
message = '''
$message
Version: ${version ?? 'Unknown'}''';
}

if (options.sourceBranchInfo) {
final currentBranch = await gitDir.currentBranch();
var commitInfo = currentBranch.sha;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions lib/src/webdev.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ Future<void> checkPubspecLock(String pkgDir) async {
}
}

Future<String?> getPackageVersion(String workingDir) async {
final pubSpec = await _Pubspec.read(workingDir);
return pubSpec.version;
}

const String pubspecFile = 'pubspec.yaml';

class _Pubspec {
final YamlMap? _pubspec;

_Pubspec(this._pubspec);

static Future<_Pubspec> read(String workingDir) async {
final pubSpec =
loadYaml(await File(p.join(workingDir, pubspecFile)).readAsString())
as YamlMap;

return _Pubspec(pubSpec);
}

String? get version => _pubspec?['version'] as String?;
}

class _PubspecLock {
final YamlMap? _packages;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: peanut
version: 5.1.0
version: 5.2.0-wip
description: >-
Update your GitHub gh-pages branch with the compiled output of your Dart web
app. Supports 'pub build' and the new 'pub run build_runner'.
Expand Down
2 changes: 2 additions & 0 deletions test/cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Arguments:
--[no-]source-branch-info Includes the name of the source branch and SHA
in the commit message
(defaults to on)
--[no-]version-info Includes the pubspec version of the package in
the commit message
--post-build-dart-script Optional Dart script to run after all builds
have completed, but before files are committed
to the repository.
Expand Down
2 changes: 2 additions & 0 deletions test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ void _checkDefault(
expect(options.sourceBranchInfoWasParsed, wasParsed);
expect(options.verbose, expected.verbose);
expect(options.verboseWasParsed, wasParsed);
expect(options.versionInfo, expected.versionInfo);
expect(options.versionInfoWasParsed, expected.versionInfoWasParsed);
expect(options.wasm, expected.wasm);

expect(options.version, jsonSkippedDefault ?? expected.version);
Expand Down
33 changes: 33 additions & 0 deletions test/peanut_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,38 @@ Built web
Branch: main
Commit: ${primaryCommit.single.sha}
package:peanut $packageVersion''',
);

await _expectStandardTreeContents(gitDir, ghCommit.treeSha);
});

test('integration with version-info', () async {
await _simplePackage();
await _pubGet();
final gitDir = await _initGitDir();
final primaryCommit = await gitDir.showRef();

await _run(options: const Options(versionInfo: true));

expect(
(await gitDir.branches()).map((br) => br.branchName),
unorderedEquals(['main', 'gh-pages']),
);

final ghBranchRef = await gitDir.branchReference('gh-pages');

final ghCommit = await gitDir.commitFromRevision(ghBranchRef!.sha);
expect(
ghCommit.message,
'''
Built web
Version: 1.0.0
Branch: main
Commit: ${primaryCommit.single.sha}
package:peanut $packageVersion''',
);

Expand Down Expand Up @@ -447,6 +479,7 @@ Future<void> _simplePackage({
'pubspec.yaml',
r'''
name: peanut_test
version: 1.0.0
environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down

0 comments on commit 998256d

Please sign in to comment.