version function
Future<VersionResult>
version(
{ - required Directory gitRoot,
- required Directory dartRoot,
- String? baseRef,
- required bool tag,
- String? newRef,
- required bool isPreRelease,
- required bool commit,
- required bool badge,
- required bool generateChangelog,
- required bool cache,
- required String tagPrefix,
})
Implementation
Future<VersionResult> version({
required Directory gitRoot,
required Directory dartRoot,
String? baseRef,
required bool tag,
String? newRef,
required bool isPreRelease,
required bool commit,
required bool badge,
required bool generateChangelog,
required bool cache,
required String tagPrefix,
}) async {
final hasPreviousVersion = (await GitUtils.getVersions(gitRoot.path, tagPrefix: tagPrefix)).isNotEmpty;
if (baseRef == null && !hasPreviousVersion) {
logger.err('No previous version found. Please tag the first version. e.g. git tag ${tagPrefix}0.0.1');
exit(1);
}
final effectiveBaseRef = baseRef ?? await GitUtils.getPreviousRef(gitRoot.path, tagPrefix: tagPrefix);
final changes = await compare(
baseRef: effectiveBaseRef,
newRef: "HEAD",
dartRoot: dartRoot,
gitRoot: gitRoot,
cache: cache,
);
final basePubSpec = await GitUtils.gitShow(
baseRef ?? await GitUtils.getPreviousRef(gitRoot.path, tagPrefix: tagPrefix),
gitRoot.path,
'pubspec.yaml',
);
final baseVersion = PubspecUtils.getVersion(basePubSpec);
final highestMagnitudeChange = getHighestMagnitude(changes);
logger.info('Highest magnitude change: $highestMagnitudeChange');
final nextVersion = await calculateNextVersion(baseVersion, highestMagnitudeChange, isPreRelease, gitRoot, tagPrefix);
logger.info('Next version: $nextVersion');
await PubspecUtils.setVersion(File(join(dartRoot.path, 'pubspec.yaml')), Version.parse(nextVersion));
String? changelog;
String? badgeContent;
if (generateChangelog) {
String changelogNewRef;
// If we are committing, we use the new version tag as the new ref.
// If we are not committing (e.g. dry run / PR), we use the current commit hash
// so the link points to the specific commit.
if (commit) {
changelogNewRef = "$tagPrefix$nextVersion";
} else {
changelogNewRef = await GitUtils.getCurrentCommitHash(gitRoot.path) ?? "HEAD";
}
final generator = ChangelogGenerator(
apiChanges: changes,
projectRoot: dartRoot,
baseRef: effectiveBaseRef,
newRef: changelogNewRef,
);
await generator.updateChangelogFile();
changelog = await generator.generateChangelogEntry();
logger.info('Generated changelog entry for version $nextVersion');
}
if (badge) {
final badgeContent = await generateVersionBadge(nextVersion);
await File(join(dartRoot.path, 'version_badge.svg')).writeAsString(badgeContent);
logger.info('Generated version badge for version $nextVersion');
}
if (commit) {
await GitUtils.commitVersion(
nextVersion,
gitRoot.path,
commitBadge: badge,
commitChangelog: generateChangelog,
);
logger.info('Committed version $nextVersion');
}
if (tag) {
await GitUtils.gitTag("$tagPrefix$nextVersion", gitRoot.path);
logger.info('Tagged version $tagPrefix$nextVersion');
}
return VersionResult(
version: Version.parse(nextVersion),
apiChanges: changes,
changelog: changelog,
badge: badgeContent ?? '',
);
}