Skip to content

Commit

Permalink
feat: update version requirement in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Nov 22, 2023
1 parent ebb7b65 commit 1183ee8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
8 changes: 1 addition & 7 deletions lib/helpers/regexes/mix.regexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ export const mixVersionRegexesArray = [
*/
export const mixVersionRegex = createUnionRegex(mixVersionRegexesArray);

/**
* Equivalent of {@link mixVersionRegexesArray} but with special capturing groups for replacing (see {@link composedRegexsToReplaceGroups})
*/
export const mixVersionRegexesReplaceGroupsArray =
composedRegexsToReplaceGroups(mixVersionRegexesArray);

/**
* Replaces the version string in a mix.exs file content
*
Expand All @@ -71,6 +65,6 @@ export function replaceVersionInMixContent(content, version) {
return replaceSecondGroupInContent(
content,
version,
mixVersionRegexesReplaceGroupsArray,
composedRegexsToReplaceGroups(mixVersionRegexesArray),
);
}
33 changes: 29 additions & 4 deletions lib/helpers/regexes/readme.regexes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { composeSemVerRegex, createUnionRegex } from "./regexes.js";
import {
composeSemVerRegex,
composedRegexToReplaceGroups,
createUnionRegex,
replaceSecondGroupInContent,
} from "./regexes.js";

export function createReadmeVersionRequirementRegexs(projectName) {
const readmeVersionRegexesArray = [
Expand All @@ -8,7 +13,7 @@ export function createReadmeVersionRequirementRegexs(projectName) {
/"/,
true,
),
composeMixDepsSemVerRegex(projectName, /\s*?git.*?tag:\s*"v/, /"/, true),
composeMixDepsSemVerRegex(projectName, /\s*?git.*?tag:\s*"v/, /"/, false),
];

const readmeVersionRegex = createUnionRegex(readmeVersionRegexesArray);
Expand All @@ -23,10 +28,9 @@ export function createReadmeVersionRequirementRegexs(projectName) {
* Composes a RegEx by injecting the SemVer regex between `head` and `tail`,
* but matching only inside a specific mix dependency tuple
*
* @param {string} projectName
* @param {string} projectName the name of the mix app
* @param {RegExp} head
* @param {RegExp} tail
* @param {boolean | null} [optionalPatch] make the patch version optional
* @returns {RegExp}
*/
export function composeMixDepsSemVerRegex(
Expand All @@ -44,3 +48,24 @@ export function composeMixDepsSemVerRegex(
"s",
);
}

/**
* Replaces the version string in a README file content
*
* @param {string} projectName the name of the mix app
* @param {string} content content of the README file
* @param {string} version version to insert in place of the current one
* @returns {string}
*/
export function replaceVersionInReadmeContent(projectName, content, version) {
const { readmeVersionRegexesArray } =
createReadmeVersionRequirementRegexs(projectName);

return replaceSecondGroupInContent(
content,
version,
readmeVersionRegexesArray.map((regex, i) =>
composedRegexToReplaceGroups(regex, i === 0),
),
);
}
2 changes: 1 addition & 1 deletion lib/helpers/regexes/regexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function createUnionRegex(regexes) {
*
* @see {@link composedRegexToReplaceGroups} for the array version
*/
function composedRegexToReplaceGroups(composedRegex, optionalPatch) {
export function composedRegexToReplaceGroups(composedRegex, optionalPatch) {
return new RegExp(
"(.*" +
composedRegex.source.replace(
Expand Down
32 changes: 32 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import fs from "node:fs";
import path from "node:path";
import {
mixVersionRegex,
parseProjectName,
replaceVersionInMixContent,
} from "./helpers/regexes/mix.regexes.js";
import { replaceVersionInReadmeContent } from "./helpers/regexes/readme.regexes.js";

export async function verifyConditions(_, context) {
const { cwd } = context;
Expand All @@ -31,6 +33,10 @@ export async function verifyConditions(_, context) {
}

export async function prepare(_, { cwd, nextRelease: { version }, logger }) {
/**
* Update mix.exs
*/

const projectPath = path.join(cwd, "mix.exs");

const projectContent = fs.readFileSync(projectPath, {
Expand All @@ -45,4 +51,30 @@ export async function prepare(_, { cwd, nextRelease: { version }, logger }) {
logger.log("Write version %s to mix.exs in %s", version, cwd);

fs.writeFileSync(projectPath, updatedProjectContent);

/**
* Update README.md
*/

const readmePath = path.join(cwd, "README.md");

const readmeContent = fs.readFileSync(readmePath, {
encoding: "utf-8",
});

const projectName = parseProjectName(projectContent);

try {
const updatedReadmeContent = replaceVersionInReadmeContent(
projectName,
readmeContent,
version,
);

logger.log("Write version %s to README.md in %s", version, cwd);

fs.writeFileSync(readmePath, updatedReadmeContent);
} catch {
logger.log("No version found in README.md in %s", cwd);
}
}

0 comments on commit 1183ee8

Please sign in to comment.