Skip to content

Commit

Permalink
feat: add replaceVersionInContent helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 18, 2023
1 parent 9fd0bd9 commit 3249658
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
67 changes: 66 additions & 1 deletion lib/helpers/regexes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
/** source: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string */
/** Official SemVer RegEx {@link https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string source} */
const semVerRegexLine = new RegExp(
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/,
);
const semVerRegex = new RegExp(semVerRegexLine.source.replace(/[\^$]/g, ""));
const semVerRegexGroup = new RegExp("(" + semVerRegex.source + ")");

/**
* RegExes of all supported version formats in mix.exs
*
* see {@link https://github.com/Talent-Ideal/semantic-release-hex/#supported-version-formats Supported version formats}
*/
export const versionRegexesArray = [
composeSemVerRegex(/\bversion:\s*"/, /"/),
composeSemVerRegex(/@version\s+"/, /"/),
];

/**
* Union RegEx of {@link versionRegexesArray} (for match only, don't use for capturing)
*/
export const versionRegex = new RegExp(
versionRegexesArray.map((r) => r.source).join("|"),
);

/**
* Equivalent of {@link versionRegexesArray} but with special capturing groups for replacing (see {@link composedRegexToReplaceGroups})
*/
const versionRegexsReplaceGroupsArray = versionRegexesArray.map(
composedRegexToReplaceGroups,
);

/**
* Replaces the version part of a version RegEx match
*
* @param {string} content
* @param {string} version
* @returns {string}
*/
export function replaceVersionInContent(content, version) {
let match;
for (let regex of versionRegexsReplaceGroupsArray) {
match = RegExp(regex).exec(content);
if (match) {
console.log(regex.source);
console.log(match);
break;
}
}

if (!match) throw new Error(`No match for: ${content}`);

const [, g1, , g3] = match;

return `${g1}${version}${g3}`;
}

/**
* Compose a RegEx by injecting the SemVer regex between `head` and `tail`
*
Expand All @@ -24,3 +64,28 @@ export const versionRegex = new RegExp(
function composeSemVerRegex(head, tail) {
return new RegExp(head.source + semVerRegexGroup.source + tail.source);
}

/**
* Returns a RegEx that matches the same content as `composedRegex`,
* but with only 3 capturing groups (for replacing):
* - g1: everything before version string
* - g2: version string
* - g3: everything after version string
*
* @param {RegExp} composedRegex
* @returns {RegExp} a RegEx composed with `composeSemVerRegex`
*/
function composedRegexToReplaceGroups(composedRegex) {
return new RegExp(
"(.*" +
composedRegex.source.replace(
semVerRegexGroup.source,
")(" +
// replace all capturing groups from the inner semVerRegex by non-capturing ones
semVerRegex.source.replace(/\((?!\?:)/g, "(?:") +
")(",
) +
".*)",
"s",
);
}
26 changes: 25 additions & 1 deletion lib/helpers/regexes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ import {
invalidSemVers,
validSemVers,
} from "../../tests/fixtures/regexes.fixture";
import { versionRegex } from "./regexes";
import { replaceVersionInContent, versionRegex } from "./regexes";

describe("replaceVersionInContent", () => {
it("should replace the version part of a match", () => {
expect(replaceVersionInContent(`version: "0.0.0-dev"`, "1.0.0")).toBe(
`version: "1.0.0"`,
);
expect(replaceVersionInContent(`@version "0.0.0-dev"`, "1.0.0")).toBe(
`@version "1.0.0"`,
);
});

it("should preserve indentation and newline", () => {
expect(
replaceVersionInContent(`\n version: "0.0.0-dev" \n`, "1.0.0"),
).toBe(`\n version: "1.0.0" \n`);
expect(
replaceVersionInContent(`\n @version "0.0.0-dev" \n`, "1.0.0"),
).toBe(`\n @version "1.0.0" \n`);
});

it("should throw if no match is found", () => {
expect(() => replaceVersionInContent("", "1.0.0")).toThrow();
});
});

describe("versionRegex", () => {
it("should match valid values", () => {
Expand Down
4 changes: 1 addition & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ export async function verifyConditions(_, context) {
"ENOVERSION",
);
}

verified = true;
}

export async function prepare(pluginConfig, context) {
prepared = true;
const { cwd, nextRelease } = context;
}

0 comments on commit 3249658

Please sign in to comment.