-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to update pubspec version
- Loading branch information
1 parent
51a0e13
commit 89c578b
Showing
2 changed files
with
221 additions
and
0 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,79 @@ | ||
import 'dart:io'; | ||
|
||
import 'version.dart'; | ||
|
||
void main(List<String> args) async { | ||
await updatePubspecVersion(); | ||
|
||
// final result = await Process.start( | ||
// 'flutter', | ||
// ['build', 'ipa'], | ||
// runInShell: true, | ||
// ); | ||
|
||
// result.stdout.transform(const Utf8Decoder()).listen((data) { | ||
// print(data); | ||
// }); | ||
|
||
// result.stderr.transform(const Utf8Decoder()).listen((data) { | ||
// print(data); | ||
// }); | ||
|
||
// print(await result.exitCode); | ||
} | ||
|
||
Future<Version?> updatePubspecVersion() async { | ||
final pubspecFile = File('pubspec.yaml'); | ||
|
||
final pubspecContent = await pubspecFile.readAsString(); | ||
|
||
final version = getVersion(pubspecContent); | ||
|
||
if (version == null) { | ||
return null; | ||
} | ||
|
||
final newVersion = version.getNewMinorReleaseVersion().toVersionString(); | ||
|
||
final updatedPubspec = getPubspecWithReplacedVersion( | ||
pubspecContent, | ||
newVersion, | ||
); | ||
|
||
await pubspecFile.writeAsString(updatedPubspec); | ||
|
||
return version; | ||
} | ||
|
||
Version? getVersion(String pubspec) { | ||
final splittedPubspecContent = pubspec.split('\n'); | ||
|
||
for (int line = 0; line < splittedPubspecContent.length; line++) { | ||
final lineContent = splittedPubspecContent[line]; | ||
|
||
if (lineContent.startsWith('version:')) { | ||
final versionString = lineContent.split(':')[1].trim(); | ||
final version = Version.fromString(versionString); | ||
|
||
return version; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
String getPubspecWithReplacedVersion(String pubspec, String version) { | ||
final splittedPubspecContent = pubspec.split('\n'); | ||
|
||
for (int line = 0; line < splittedPubspecContent.length; line++) { | ||
final lineContent = splittedPubspecContent[line]; | ||
|
||
if (lineContent.startsWith('version:')) { | ||
splittedPubspecContent[line] = 'version: $version'; | ||
|
||
return splittedPubspecContent.join('\n'); | ||
} | ||
} | ||
|
||
return splittedPubspecContent.join('\n'); | ||
} |
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,142 @@ | ||
import 'dart:convert'; | ||
|
||
class Version { | ||
final int major; | ||
final int minor; | ||
final int patch; | ||
final int buildNumber; | ||
|
||
const Version({ | ||
required this.major, | ||
required this.minor, | ||
required this.patch, | ||
required this.buildNumber, | ||
}); | ||
|
||
factory Version.major() { | ||
return const Version( | ||
major: 1, | ||
minor: 0, | ||
patch: 0, | ||
buildNumber: 1, | ||
); | ||
} | ||
|
||
factory Version.minor() { | ||
return const Version( | ||
major: 0, | ||
minor: 1, | ||
patch: 0, | ||
buildNumber: 1, | ||
); | ||
} | ||
|
||
factory Version.patch() { | ||
return const Version( | ||
major: 0, | ||
minor: 0, | ||
patch: 1, | ||
buildNumber: 1, | ||
); | ||
} | ||
|
||
Version operator +(Version other) { | ||
return Version( | ||
major: major + other.major, | ||
minor: minor + other.minor, | ||
patch: patch + other.patch, | ||
buildNumber: buildNumber + other.buildNumber, | ||
); | ||
} | ||
|
||
factory Version.fromString(String version) { | ||
final splittedByPlusVersion = version.split('+'); | ||
|
||
final primaryVersion = splittedByPlusVersion[0].split('.'); | ||
final buildNumber = splittedByPlusVersion[1]; | ||
|
||
return Version( | ||
major: int.parse(primaryVersion[0]), | ||
minor: int.parse(primaryVersion[1]), | ||
patch: int.parse(primaryVersion[2]), | ||
buildNumber: int.parse(buildNumber), | ||
); | ||
} | ||
|
||
@override | ||
String toString() { | ||
return 'Version(major: $major, minor: $minor, patch: $patch, buildNumber: $buildNumber)'; | ||
} | ||
|
||
String toVersionString() { | ||
return '$major.$minor.$patch+$buildNumber'; | ||
} | ||
|
||
Version getNewMajorReleaseVersion() { | ||
return this + Version.major(); | ||
} | ||
|
||
Version getNewMinorReleaseVersion() { | ||
return this + Version.minor(); | ||
} | ||
|
||
Version getNewPatchReleaseVersion() { | ||
return this + Version.patch(); | ||
} | ||
|
||
Version copyWith({ | ||
int? major, | ||
int? minor, | ||
int? patch, | ||
int? buildNumber, | ||
}) { | ||
return Version( | ||
major: major ?? this.major, | ||
minor: minor ?? this.minor, | ||
patch: patch ?? this.patch, | ||
buildNumber: buildNumber ?? this.buildNumber, | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'major': major, | ||
'minor': minor, | ||
'patch': patch, | ||
'buildNumber': buildNumber, | ||
}; | ||
} | ||
|
||
factory Version.fromMap(Map<String, dynamic> map) { | ||
return Version( | ||
major: map['major']?.toInt() ?? 0, | ||
minor: map['minor']?.toInt() ?? 0, | ||
patch: map['patch']?.toInt() ?? 0, | ||
buildNumber: map['buildNumber']?.toInt() ?? 0, | ||
); | ||
} | ||
|
||
String toJson() => json.encode(toMap()); | ||
|
||
factory Version.fromJson(String source) => | ||
Version.fromMap(json.decode(source)); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
|
||
return other is Version && | ||
other.major == major && | ||
other.minor == minor && | ||
other.patch == patch && | ||
other.buildNumber == buildNumber; | ||
} | ||
|
||
@override | ||
int get hashCode { | ||
return major.hashCode ^ | ||
minor.hashCode ^ | ||
patch.hashCode ^ | ||
buildNumber.hashCode; | ||
} | ||
} |