@auto-it/sbt
Version:
Publish Scala projects with sbt
164 lines • 6.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sbtPublish = exports.sbtSetVersion = exports.sbtGetVersion = exports.sbtClient = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@auto-it/core");
const semver_1 = require("semver");
const t = tslib_1.__importStar(require("io-ts"));
const strip_ansi_1 = tslib_1.__importDefault(require("strip-ansi"));
const pluginOptions = t.partial({
setCanaryVersion: t.boolean,
publishCommand: t.string,
});
/** Calls sbt in the client and returns cleaned up logs */
async function sbtClient(input) {
const output = await core_1.execPromise("sbt", ["--client", input]);
const cleanOutput = strip_ansi_1.default(output).replace(/(.*\n)*^>.*$/m, "").trim();
return cleanOutput;
}
exports.sbtClient = sbtClient;
/** Read version from sbt */
async function sbtGetVersion() {
var _a;
// in multi-module projects, we want to get only ThisBuild/version
await sbtClient("set version/aggregate := false");
const output = await sbtClient("print version");
const version = (_a = output.split("\n").shift()) === null || _a === void 0 ? void 0 : _a.trim();
if (!version) {
throw new Error(`Failed to read version from sbt: ${output}`);
}
return version;
}
exports.sbtGetVersion = sbtGetVersion;
/** Set version in sbt to the given value */
async function sbtSetVersion(version) {
return sbtClient(`set ThisBuild / version := \\"${version}\\"`);
}
exports.sbtSetVersion = sbtSetVersion;
/** Run sbt publish */
async function sbtPublish(command) {
return sbtClient(command || "publish");
}
exports.sbtPublish = sbtPublish;
/** Publish Scala projects with sbt */
class SbtPlugin {
/** Initialize the plugin with it's options */
constructor(options) {
/** The name of the plugin */
this.name = "sbt";
this.options = options;
}
/** Tap into auto plugin points. */
apply(auto) {
// exact copy-paste from the git-tag plugin
/** Get the latest tag in the repo, if none then the first commit */
async function getTag() {
try {
return await auto.git.getLatestTagInBranch();
}
catch (error) {
return auto.prefixRelease("0.0.0");
}
}
auto.hooks.validateConfig.tapPromise(this.name, async (name, options) => {
// If it's a string thats valid config
if (name === this.name && typeof options !== "string") {
return core_1.validatePluginConfiguration(this.name, pluginOptions, options);
}
});
// exact copy-paste from the git-tag plugin
auto.hooks.getPreviousVersion.tapPromise(this.name, async () => {
if (!auto.git) {
throw new Error("Can't calculate previous version without Git initialized!");
}
return getTag();
});
// exact copy-paste from the git-tag plugin
auto.hooks.version.tapPromise(this.name, async ({ bump, dryRun, quiet }) => {
if (!auto.git) {
return;
}
const lastTag = await getTag();
const newTag = semver_1.inc(lastTag, bump);
if (dryRun && newTag) {
if (quiet) {
console.log(newTag);
}
else {
auto.logger.log.info(`Would have published: ${newTag}`);
}
return;
}
if (!newTag) {
auto.logger.log.info("No release found, doing nothing");
return;
}
const prefixedTag = auto.prefixRelease(newTag);
auto.logger.log.info(`Tagging new tag: ${lastTag} => ${prefixedTag}`);
await core_1.execPromise("git", [
"tag",
prefixedTag,
"-m",
`"Update version to ${prefixedTag}"`,
]);
auto.logger.verbose.info(`Set version in sbt to "${newTag}"`);
await sbtSetVersion(newTag);
});
auto.hooks.publish.tapPromise(this.name, async () => {
auto.logger.verbose.info("Run sbt publish");
const publishLogs = await sbtPublish(this.options.publishCommand);
auto.logger.verbose.info("Output:\n", publishLogs);
auto.logger.log.info("Pushing new tag to GitHub");
await core_1.execPromise("git", [
"push",
"--atomic",
"--follow-tags",
"--set-upstream",
auto.remote,
core_1.getCurrentBranch() || auto.baseBranch,
]);
});
auto.hooks.canary.tapPromise(this.name, async ({ canaryIdentifier, dryRun, quiet }) => {
if (!auto.git) {
return;
}
/** Construct canary version using Auto-provided suffix */
const constructCanaryVersion = async () => {
const lastTag = await getTag();
const lastVersion = lastTag.replace(/^v/, "");
return `${lastVersion}${canaryIdentifier}-SNAPSHOT`;
};
const canaryVersion = this.options.setCanaryVersion
? await constructCanaryVersion()
: await sbtGetVersion();
auto.logger.log.info(`Canary version: ${canaryVersion}`);
if (dryRun) {
if (quiet) {
console.log(canaryVersion);
}
else {
auto.logger.log.info(`Would have published: ${canaryVersion}`);
}
return;
}
if (this.options.setCanaryVersion) {
auto.logger.verbose.info(`Set version in sbt to "${canaryVersion}"`);
await sbtSetVersion(canaryVersion);
}
auto.logger.verbose.info("Run sbt publish");
const publishLogs = await sbtPublish(this.options.publishCommand);
auto.logger.verbose.info("Output:\n", publishLogs);
auto.logger.verbose.info("Successfully published canary version");
return {
newVersion: canaryVersion,
details: [
"```",
publishLogs,
"```",
].join("\n"),
};
});
}
}
exports.default = SbtPlugin;
//# sourceMappingURL=index.js.map