forked from intuit/auto
/
index.ts
198 lines (166 loc) · 5.45 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
Auto,
execPromise,
getCurrentBranch,
IPlugin,
validatePluginConfiguration,
} from "@auto-it/core";
import { inc, ReleaseType } from "semver";
import * as t from "io-ts";
import stripAnsi from "strip-ansi";
const pluginOptions = t.partial({
manageVersion: t.boolean,
});
export type ISbtPluginOptions = t.TypeOf<typeof pluginOptions>;
/** Publish Scala projects with sbt */
export default class SbtPlugin implements IPlugin {
/** The name of the plugin */
name = "sbt";
/** The options of the plugin */
readonly options: ISbtPluginOptions;
/** Initialize the plugin with it's options */
constructor(options: ISbtPluginOptions) {
this.options = options;
}
/** Tap into auto plugin points. */
apply(auto: 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");
}
}
/** Calls sbt in the client and returns cleaned up logs */
async function sbtClient(...args: string[]) {
const output = await execPromise("sbt", ["--client", ...args]);
const cleanOutput = stripAnsi(output).replace(/(.*\n)*^>.*$/m, "").trim();
return cleanOutput;
}
/** Read version from sbt */
async function sbtGetVersion() {
// 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 = output.split("\n").shift()?.trim();
if (!version) {
throw new Error(`Failed to read version from sbt: ${output}`);
}
auto.logger.log.info(`Got version from sbt: ${version}`);
return version;
}
/** Set version in sbt to the given value */
async function sbtSetVersion(version: string) {
auto.logger.log.info(`Set version in sbt to "${version}"`);
return sbtClient(`set every version := \\"${version}\\"`);
}
/** Run sbt publish */
async function sbtPublish() {
auto.logger.log.info("Run sbt publish");
const publishLog = await sbtClient("publish");
auto.logger.log.info("Output:\n" + publishLog);
return publishLog;
}
/** Construct canary version using Auto-provided suffix */
async function getCanaryVersion(canaryIdentifier: string) {
const lastTag = await getTag();
const lastVersion = lastTag.replace(/^v/, "");
return `${lastVersion}${canaryIdentifier}-SNAPSHOT`;
}
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 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 = inc(lastTag, bump as ReleaseType);
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 execPromise("git", [
"tag",
prefixedTag,
"-m",
`"Update version to ${prefixedTag}"`,
]);
if (this.options.manageVersion) {
await sbtSetVersion(newTag);
}
},
);
auto.hooks.publish.tapPromise(this.name, async () => {
await sbtPublish();
auto.logger.log.info("Pushing new tag to GitHub");
await execPromise("git", [
"push",
"--follow-tags",
"--set-upstream",
auto.remote,
getCurrentBranch() || auto.baseBranch,
]);
});
auto.hooks.canary.tapPromise(
this.name,
async ({ canaryIdentifier, dryRun, quiet }) => {
if (!auto.git) {
return;
}
const canaryVersion = this.options.manageVersion
? await getCanaryVersion(canaryIdentifier)
: 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.manageVersion) {
await sbtSetVersion(canaryVersion);
}
const publishLogs = await sbtPublish();
auto.logger.verbose.info("Successfully published canary version");
return {
newVersion: canaryVersion,
details: [
"```",
publishLogs,
"```",
].join("\n"),
};
},
);
}
}