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,
})
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,
}) async {
final hasPreviousVersion = (await GitUtils.getVersions(gitRoot.path)).isNotEmpty;
if (baseRef == null && !hasPreviousVersion) {
logger.err('No previous version found. Please tag the first version. e.g. git tag v0.0.1');
exit(1);
}
final effectiveBaseRef = baseRef ?? await GitUtils.getPreviousRef(gitRoot.path);
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),
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);
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 = "v$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("v$nextVersion", gitRoot.path);
logger.info('Tagged version v$nextVersion');
}
return VersionResult(
version: Version.parse(nextVersion),
apiChanges: changes,
changelog: changelog,
badge: badgeContent ?? '',
);
}