-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e3a9d7
commit 98a15bd
Showing
3 changed files
with
109 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -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('.')); | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -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; |
This file contains 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