Skip to content

Commit

Permalink
Merge pull request #2467 from intuit/version-file/publish-script-args
Browse files Browse the repository at this point in the history
[Version File] Remove assumption that canary and next should be snapshots
  • Loading branch information
sugarmanz authored Jul 17, 2024
2 parents 0509a79 + ec6d09c commit f4dcb98
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
15 changes: 12 additions & 3 deletions plugins/version-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ yarn add -D @auto-it/version-file
## Options

- versionFile (optional, default="VERSION"): Path to where the version is stored in the repository. It should be a file containing just the semver.
- releaseScript: (optional, default=None): Path to script that runs the publish actions in your repository. If not supplied nothing will be called. If supplied will be called during the `publish`,`canary` and `next` hooks. For the `publish` hook the first parameter passed to the script will be `release` to indicate that a regular release is being called. For `canary` and `next` hooks the first parameter will be `snapshot` to indicate a prerelease version.
- publishScript: (optional, default=None): Path to script that runs the publish actions in your repository. If not supplied nothing will be called. If supplied will be called during the `publish`,`canary` and `next` hooks with the arguments defined in `publishScriptReleaseTypeArgs` for that release type.
- publishScriptReleaseTypeArgs: (optional, default=```{
"publish": ["release"],
"canary": ["snapshot"],
"next": ["snapshot"]
}```): Mapping of arguments to pass to the `publishScript` for each release type (`publish`, `canary`, `next`)

## Usage

Expand All @@ -34,6 +39,10 @@ yarn add -D @auto-it/version-file
```json
{
"plugins": [
"version-file", {"versionFile": "./tools/Version.txt", "releaseScript":"./tools/publish.sh"}
"version-file", {"versionFile": "./tools/Version.txt", "publishScript":"./tools/publish.sh", "publishScriptReleaseTypeArgs": {
"publish": ["release"], // (default)
"canary": ["snapshot"],
"next": ["some", "other", "args"],
}}
]
}
}
64 changes: 63 additions & 1 deletion plugins/version-file/__tests__/version-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,66 @@ describe("Test Release Types", () => {
// Check the right version was written
expect(fs.readFileSync("VERSION", "utf-8")).toStrictEqual("v2.0.0-next.0")
});
});

test("Release type args can be provided to override default args for publish script", async () => {
const prefixRelease: (a: string) => string = (version: string) => {
return `v${version}`;
};

mockFs({
VERSION: `1.0.0`,
});
const plugin = new BazelPlugin({
publishScript: "./tools/release.sh",
publishScriptReleaseTypeArgs: {
publish: ["args", "for", "publish"],
canary: ["different", "canary"],
next: ["next"],
},
});
const hooks = makeHooks();

plugin.apply(({
hooks,
config: { prereleaseBranches: ["next"] },
remote: "origin",
baseBranch: "main",
logger: dummyLog(),
prefixRelease,
getCurrentVersion: () => "1.0.0",
git: {
getLastTagNotInBaseBranch: async () => undefined,
getLatestRelease: () => "1.0.0",
getLatestTagInBranch: () => Promise.resolve("1.0.0"),
},
} as unknown) as Auto);


await hooks.publish.promise({ bump: SEMVER.major });

expect(execPromise).toHaveBeenNthCalledWith(1, "./tools/release.sh", [
"args", "for", "publish"
]);

await hooks.canary.promise({
bump: SEMVER.minor,
canaryIdentifier: "canary.368.1",
});

expect(execPromise).toHaveBeenNthCalledWith(3, "./tools/release.sh", [
"different",
"canary",
]);

await hooks.next.promise(["1.0.0"], {
bump: SEMVER.major,
fullReleaseNotes: "",
releaseNotes: "",
commits: [],
});

expect(execPromise).toHaveBeenNthCalledWith(5, "./tools/release.sh", [
"next",
]);
})
});
34 changes: 29 additions & 5 deletions plugins/version-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@ import { inc, ReleaseType } from "semver";
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

interface ReleaseTypeArgs {
/** Args to use when invoking the publishScript during the publish hook */
publish: string[];
/** Args to use when invoking the publishScript during the canary hook */
canary: string[];
/** Args to use when invoking the publishScript during the next hook */
next: string[];
}

const pluginOptions = t.partial({
/** Path to file (from where auto is executed) where the version is stored */
versionFile: t.string,

/** Optional script that executes release pipeline stages */
publishScript: t.string
publishScript: t.string,

/** Optional publish script args mapping for each release hook, defaults `publish` to ["release"] and the others to ["snapshot"] */
publishScriptReleaseTypeArgs: t.partial({
publish: t.array(t.string),
canary: t.array(t.string),
next: t.array(t.string),
})
});

export type IVersionFilePluginOptions = t.TypeOf<typeof pluginOptions>;
Expand Down Expand Up @@ -55,10 +70,19 @@ export default class VersionFilePlugin implements IPlugin {
/** Release script location */
readonly publishScript: string | undefined

/** */
readonly publishScriptReleaseTypeArgs: ReleaseTypeArgs;

/** Initialize the plugin with it's options */
constructor(options: IVersionFilePluginOptions) {
this.versionFile = options.versionFile ?? "VERSION";
this.publishScript = options.publishScript
this.publishScript = options.publishScript;
this.publishScriptReleaseTypeArgs = {
publish: ['release'],
canary: ['snapshot'],
next: ['snapshot'],
...options.publishScriptReleaseTypeArgs ?? {}
};
}


Expand Down Expand Up @@ -113,7 +137,7 @@ export default class VersionFilePlugin implements IPlugin {
// Call release script if provided
if(this.publishScript){
auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`);
await execPromise(this.publishScript, ["release"])
await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs.publish)
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}
Expand Down Expand Up @@ -141,7 +165,7 @@ export default class VersionFilePlugin implements IPlugin {
// Ship canary release if release script is provided
if(this.publishScript){
auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`);
await execPromise(this.publishScript, ["snapshot"]);
await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs.canary);
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}
Expand Down Expand Up @@ -188,7 +212,7 @@ export default class VersionFilePlugin implements IPlugin {
// ship next release if release script is provided
if(this.publishScript){
auto.logger.log.info(`Calling release script in repo at ${this.publishScript}`);
await execPromise(this.publishScript, ["snapshot"]);
await execPromise(this.publishScript, this.publishScriptReleaseTypeArgs.next);
} else {
auto.logger.log.info("Skipping calling release script in repo since none was provided");
}
Expand Down

0 comments on commit f4dcb98

Please sign in to comment.