Skip to content

Commit

Permalink
build(rules_task): Use custom semantic release plugin to release rule…
Browse files Browse the repository at this point in the history
…s_task (#587)

* Remove monorepo

* Use commit paths plugin
  • Loading branch information
mvgijssel authored Oct 24, 2023
1 parent 716ad9f commit 97e58a2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 760 deletions.
7 changes: 6 additions & 1 deletion rules/rules_task/.releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"name": "master"
}
],
"extends": "semantic-release-monorepo",
"tagFormat": "rules_task-v${version}",
"extends": [
"./commit-paths.js"
],
"commitPaths": [
"rules/rules_task/*"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
Expand Down
95 changes: 95 additions & 0 deletions rules/rules_task/commit-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const gitLogParser = require("git-log-parser");
const oldParse = gitLogParser.parse;
const { execSync } = require("child_process");
const { wrap } = require("module");

let commitPaths = null;

// Copied from https://github.com/pmowrer/semantic-release-plugin-decorators/blob/master/src/wrapStep.js
// This executes the "stepName" method of all plugins and afterwards executes the "wrapFn" method
const wrapStep = (
stepName,
wrapFn,
{ defaultReturn = undefined, wrapperName = "" } = {}
) => {
const wrapperFn = async function (globalPluginConfig, context) {
const {
options: { plugins },
} = context;

for (const pluginDefinition of plugins) {
const [pluginName, pluginConfig] = Array.isArray(pluginDefinition)
? pluginDefinition
: [pluginDefinition, {}];

if (!pluginName) {
// Still needed ?
context.logger.log(`Falsy plugin name at index "${index}"`);
continue;
} else if (typeof pluginName !== "string") {
throw new Error(
`${
wrapperName ? wrapperName : "semantic-release-plugin-decorators"
}: Incorrect plugin name type. Expected string but was ${JSON.stringify(
pluginName
)}.`
);
}

const plugin = await import(pluginName);
const step = plugin && plugin[stepName];

if (!step) {
context.logger.log(
`Plugin "${pluginName}" does not provide step "${stepName}"`
);
continue;
}

context.logger.log(`Start step "${stepName}" of plugin "${pluginName}"`);

await step(globalPluginConfig, context);

context.logger.log(
`Completed step "${stepName}" of plugin "${pluginName}"`
);
}

return await wrapFn(globalPluginConfig, context);
};

Object.defineProperty(wrapperFn, "name", { value: wrapperName });

return wrapperFn;
};

// Wrapping the verifyConditions step to get the config so we can globally set the commitPaths for the git-log-parser
const verifyConditions = wrapStep(
"verifyConditions",
(globalPluginConfig) => {
commitPaths = globalPluginConfig["commitPaths"];
},
{
wrapperName: "get-commit-paths",
}
);

gitLogParser.parse = function (config, options) {
// get the git root and change the cwd so all the commitPaths are relative to the git root
const newCwd = execSync("git rev-parse --show-toplevel", {
encoding: "utf-8",
}).trim();
const configValue = config["_"];
const newConfig = {
_: [configValue, "--", ...commitPaths],
};
const newOptions = {
...options,
cwd: newCwd,
};
return oldParse(newConfig, newOptions);
};

module.exports = {
verifyConditions,
};
13 changes: 6 additions & 7 deletions rules/rules_task/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
"author": "",
"license": "ISC",
"dependencies": {
"@semantic-release/commit-analyzer": "9.0.2",
"@semantic-release/exec": "^6.0.3",
"@semantic-release/github": "8.1.0",
"@semantic-release/release-notes-generator": "10.0.3",
"conventional-changelog-conventionalcommits": "^7.0.2",
"semantic-release": "^22.0.5",
"semantic-release-monorepo": "^7.0.5"
"@semantic-release/commit-analyzer": "11.0.0",
"@semantic-release/exec": "6.0.3",
"@semantic-release/github": "9.2.1",
"@semantic-release/release-notes-generator": "12.0.0",
"conventional-changelog-conventionalcommits": "7.0.2",
"semantic-release": "22.0.5"
}
}
Loading

0 comments on commit 97e58a2

Please sign in to comment.