Skip to content

Commit

Permalink
add version utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 8, 2024
1 parent 2e3a9d7 commit 98a15bd
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 60 deletions.
90 changes: 90 additions & 0 deletions lib/src/version_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:pub_semver/pub_semver.dart';

/// Regex that matches a version number at the beginning of a string.
final _startVersion = RegExp(r'^' // Start at beginning.
r'(\d+).((\d+))?' // Version number.
);

/// Like [_startVersion] but matches the entire string.
final _completeVersion = RegExp('${_startVersion.pattern}\$');

// Handle String with 4 numbers
/// Regex that matches a version number at the beginning of a string.
final _fourNumberStartVersion = RegExp(r'^' // Start at beginning.
r'(\d+).(\d+).(\d+).([0-9A-Za-z-]*)') // Version number.
;

/// Like [_startVersion] but matches the entire string.
final _fourNumberCompleteVersion =
RegExp('${_fourNumberStartVersion.pattern}\$');

/// Add support for version X, X.X not supported in platform version
Version parseVersion(String text) {
try {
return Version.parse(text);
} on FormatException catch (e) {
Match? match = _completeVersion.firstMatch(text);
if (match != null) {
try {
// print(match[0]);
// print(match[1]);
// print(match[2]);
var major = int.parse(match[1]!);
var minor = int.parse(match[2]!);

return Version(major, minor, 0);
} on FormatException catch (_) {
throw e;
}
} else {
match = _fourNumberCompleteVersion.firstMatch(text);
if (match != null) {
try {
// print(match[0]);
// print(match[1]);
// print(match[2]);
var major = int.parse(match[1]!);
var minor = int.parse(match[2]!);
var patch = int.parse(match[3]!);
var build = match[4];

return Version(major, minor, patch, build: build);
} on FormatException catch (_) {
throw e;
}
} else {
throw FormatException('Could not parse "$text".');
}
}
}
}

List<Object> _bumpPreReleaseOrBuild(List<Object> list) {
for (var part in list.reversed.indexed) {
var (index, item) = part;

if (item is int) {
return List.of(list)..[index] = item + 1;
}
}
return [...list, 0];
}

/// Common helper
extension TekartikVersionExt on Version {
/// Remove pre release and build
Version get noPreReleaseOrBuild {
return Version(major, minor, patch);
}

/// Bump the last number in either pre release or build
Version get nextPreReleaseOrBuild {
if (isPreRelease) {
return Version(major, minor, patch,
pre: _bumpPreReleaseOrBuild(preRelease).join('.'));
} else {
return Version(major, minor, patch,
build: _bumpPreReleaseOrBuild(build).join('.'));
}
}
}
61 changes: 1 addition & 60 deletions lib/version_utils.dart
Original file line number Diff line number Diff line change
@@ -1,64 +1,5 @@
library;

import 'package:pub_semver/pub_semver.dart';

export 'package:pub_semver/pub_semver.dart';

/// Regex that matches a version number at the beginning of a string.
final _startVersion = RegExp(r'^' // Start at beginning.
r'(\d+).((\d+))?' // Version number.
);

/// Like [_startVersion] but matches the entire string.
final _completeVersion = RegExp('${_startVersion.pattern}\$');

// Handle String with 4 numbers
/// Regex that matches a version number at the beginning of a string.
final _fourNumberStartVersion = RegExp(r'^' // Start at beginning.
r'(\d+).(\d+).(\d+).([0-9A-Za-z-]*)') // Version number.
;

/// Like [_startVersion] but matches the entire string.
final _fourNumberCompleteVersion =
RegExp('${_fourNumberStartVersion.pattern}\$');

/// Add support for version X, X.X not supported in platform version
Version parseVersion(String text) {
try {
return Version.parse(text);
} on FormatException catch (e) {
Match? match = _completeVersion.firstMatch(text);
if (match != null) {
try {
// print(match[0]);
// print(match[1]);
// print(match[2]);
var major = int.parse(match[1]!);
var minor = int.parse(match[2]!);

return Version(major, minor, 0);
} on FormatException catch (_) {
throw e;
}
} else {
match = _fourNumberCompleteVersion.firstMatch(text);
if (match != null) {
try {
// print(match[0]);
// print(match[1]);
// print(match[2]);
var major = int.parse(match[1]!);
var minor = int.parse(match[2]!);
var patch = int.parse(match[3]!);
var build = match[4];

return Version(major, minor, patch, build: build);
} on FormatException catch (_) {
throw e;
}
} else {
throw FormatException('Could not parse "$text".');
}
}
}
}
export 'src/version_utils.dart' show parseVersion, TekartikVersionExt;
18 changes: 18 additions & 0 deletions test/version_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:test/test.dart';

void main() => defineTests();

Version _vp(String versionText) => Version.parse(versionText);

void defineTests() {
//useVMConfiguration();
group('version', () {
Expand All @@ -27,5 +29,21 @@ void defineTests() {
fail('should not parse');
} on FormatException catch (_) {}
});

test('noPreReleaseOrBuild', () {
var version = _vp('1.2.3+1');
expect(version.noPreReleaseOrBuild, Version(1, 2, 3));
version = _vp('1.2.3-1');
expect(version.noPreReleaseOrBuild, Version(1, 2, 3));
});

test('nextPreReleaseOrBuild', () {
var version = _vp('1.2.3+1');
expect(version.nextPreReleaseOrBuild, Version(1, 2, 3, build: '2'));
version = _vp('1.2.3-1');
expect(version.nextPreReleaseOrBuild, Version(1, 2, 3, pre: '2'));
version = _vp('1.2.3');
expect(version.nextPreReleaseOrBuild, Version(1, 2, 3, build: '0'));
});
});
}

0 comments on commit 98a15bd

Please sign in to comment.