Skip to content

Commit

Permalink
feat: Update version calculation logic and build number incrementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lsaudon committed Jul 4, 2024
1 parent 745685d commit 00e7e85
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
40 changes: 35 additions & 5 deletions lib/src/commands/release/release_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,41 @@ class ReleaseCommand extends Command<int> {
) {
final highestVersionType =
versionTypes.reduce((final a, final b) => a.index > b.index ? a : b);
return switch (highestVersionType) {
SemanticVersionType.major => currentVersion.nextMajor,
SemanticVersionType.minor => currentVersion.nextMinor,
SemanticVersionType.patch => currentVersion.nextPatch,
};
final version = _calculateNextVersion(currentVersion, highestVersionType);
return _incrementBuildNumberIfNeeded(version, currentVersion);
}

Version _calculateNextVersion(
final Version currentVersion,
final SemanticVersionType highestVersionType,
) {
switch (highestVersionType) {
case SemanticVersionType.major:
return currentVersion.nextMajor;
case SemanticVersionType.minor:
return currentVersion.nextMinor;
case SemanticVersionType.patch:
return currentVersion.nextPatch;
}
}

Version _incrementBuildNumberIfNeeded(
final Version version,
final Version currentVersion,
) {
final firstOrNull = currentVersion.build.firstOrNull ?? '';
final buildNumber = int.tryParse(firstOrNull.toString());

if (buildNumber == null) {
return version;
}

return Version(
version.major,
version.minor,
version.patch,
build: '${buildNumber + 1}',
);
}

String generate({
Expand Down
16 changes: 8 additions & 8 deletions test/src/commands/release_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class _MockProcessManager extends Mock implements ProcessManager {}

// Constants for Test Data
const changelogContent = '''
## 🔖 [1.3.0]
## 🔖 [1.3.0+2]
### 🐛 Bug Fixes
Expand Down Expand Up @@ -60,7 +60,7 @@ const changelogContent = '''
- removed testing framework
''';
const String changelogContentFromJsonFile = '''
## 🔖 [1.3.0]
## 🔖 [1.3.0+2]
### 🐛 Corrections de bugs
Expand Down Expand Up @@ -110,13 +110,13 @@ Run "$executableName help" to see global options.''';

const String pubspecContent = '''
name: example
version: 1.2.3
version: 1.2.3+1
environment:
sdk: ">=3.0.0 <4.0.0"''';

const String updatedPubspecContent = '''
name: example
version: 1.3.0
version: 1.3.0+2
environment:
sdk: ">=3.0.0 <4.0.0"''';

Expand Down Expand Up @@ -169,13 +169,13 @@ void _mockProcessResultsForNoTag(final ProcessManager processManager) {

// Helper Function to Mock Process Results for Latest Tag Scenario
void _mockProcessResultsForLatestTag(final ProcessManager processManager) {
final latestTagResult = ProcessResult(0, 0, 'v1.2.3', '');
final latestTagResult = ProcessResult(0, 0, 'v1.2.3+1', '');

when(() => processManager.run('git describe --tags --abbrev=0'.split(' ')))
.thenAnswer((final _) async => latestTagResult);
when(
() => processManager
.run('git log v1.2.3..HEAD --grep=^.*: --pretty=%s'.split(' ')),
.run('git log v1.2.3+1..HEAD --grep=^.*: --pretty=%s'.split(' ')),
).thenAnswer((final _) async => commitResult);
}

Expand All @@ -189,7 +189,7 @@ void main() {
setUp(() {
logger = _MockLogger();
processManager = _MockProcessManager();
const versionTag = 'v1.3.0';
const versionTag = 'v1.3.0+2';
const versionBranch = 'releascribe-$versionTag';
when(
() => processManager.run(['git', 'checkout', '-b', versionBranch]),
Expand Down Expand Up @@ -239,7 +239,7 @@ void main() {
final changelogFime =
await fileSystem.file('CHANGELOG.md').readAsString();
expect(changelogFime, changeLogExpected);
const versionTag = 'v1.3.0';
const versionTag = 'v1.3.0+2';
const versionBranch = 'releascribe-$versionTag';
for (final c in [
['git', 'checkout', '-b', versionBranch],
Expand Down

0 comments on commit 00e7e85

Please sign in to comment.