Skip to content

Commit

Permalink
minor fixes (#66)
Browse files Browse the repository at this point in the history
* minor fixes

* trim the prefix
  • Loading branch information
justinmchase authored May 14, 2024
1 parent 0ed8927 commit 1e5b6ff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
18 changes: 9 additions & 9 deletions src/hooks/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function patchCsproj(file: string, version: SemVer) {
}

async function patchPomXml(file: string, version: SemVer) {
const { def } = semverFormats(version);
const { original } = semverFormats(version);
const contents = await Deno.readTextFile(file);
const document = xml.parse(contents, {
captureSpacesBetweenElements: true,
Expand All @@ -94,7 +94,7 @@ async function patchPomXml(file: string, version: SemVer) {
} else if (el.type === "element" && el.name === "version") {
const value = el.elements[0];
if (value.type === "text") {
value.text = def;
value.text = original;
isVersionSet = true;
break;
}
Expand All @@ -116,24 +116,24 @@ async function patchPomXml(file: string, version: SemVer) {
}

async function patchPackageJson(file: string, version: SemVer) {
const { def } = semverFormats(version);
const { original } = semverFormats(version);
const contents = await Deno.readTextFile(file);
const edits = JSONC.modify(contents, ["version"], def, {});
const edits = JSONC.modify(contents, ["version"], original, {});
const result = JSONC.applyEdits(contents, edits);
await Deno.writeTextFile(file, result);
}

async function patchPackageLockJson(packageJsonPath: string, version: SemVer) {
const { def } = semverFormats(version);
const { original } = semverFormats(version);
const dir = path.dirname(packageJsonPath);
const packageLockJsonPath = path.resolve(dir, "package-lock.json");
if (await exists(packageLockJsonPath)) {
const contents = await Deno.readTextFile(packageLockJsonPath);
const versionEdits = JSONC.modify(contents, ["version"], def, {});
const versionEdits = JSONC.modify(contents, ["version"], original, {});
const moduleVersionEdits = JSONC.modify(
contents,
["packages", "", "version"],
def,
original,
{},
);
const edits = [...versionEdits, ...moduleVersionEdits];
Expand All @@ -143,11 +143,11 @@ async function patchPackageLockJson(packageJsonPath: string, version: SemVer) {
}

async function patchChartYaml(file: string, version: SemVer) {
const { def } = semverFormats(version);
const { original } = semverFormats(version);
const contents = await Deno.readTextFile(file);
const result = contents.replace(
/^version:\s*(.*)$/m,
`version: ${def}`,
`version: ${original}`,
);
await Deno.writeTextFile(file, result);
}
20 changes: 11 additions & 9 deletions src/util/variant.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { format, SemVer } from "../../deps/semver.ts";

export enum FormatKind {
Default = "def",
Original = "original",
Dotnet = "dotnet",
Docker = "docker",
Major = "major",
MajorMinor = "major_minor",
}

export function semverFormats(semver: SemVer, prefix = "") {
export function semverFormats(semver: SemVer): Record<FormatKind, string> {
const formattedVersion = format(semver);
const kebabVersion = formattedVersion.replace(/[+]/g, "-");
const cleanedPrefix = prefix.trim();
return {
def: `${cleanedPrefix}${formattedVersion}`,
dotnet: `${cleanedPrefix}${kebabVersion}`,
docker: `${cleanedPrefix}${kebabVersion}`,
major: `${cleanedPrefix}${semver.major}`,
original: formattedVersion,
docker: kebabVersion,
dotnet: kebabVersion,
major: `${semver.major}`,
major_minor: `${semver.major}.${semver.minor}`,
};
}

export function semverFormatByKey(
semver: SemVer,
prefix = "",
variantKey: FormatKind = FormatKind.Default,
variantKey: FormatKind = FormatKind.Original,
) {
return semverFormats(semver, prefix)[variantKey];
const format = semverFormats(semver)[variantKey];
return `${prefix.trim()}${format}`;
}
11 changes: 2 additions & 9 deletions src/util/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Deno.test({
assertSpyCall(appendTextFile, 1, {
args: [
"/test/output",
"sv_major=1\n",
"major=1\n",
{ create: true, append: true },
],
});
Expand Down Expand Up @@ -199,20 +199,13 @@ Deno.test({
],
});
assertSpyCall(appendTextFile, 6, {
args: [
"/test/output",
"def=1.2.3-pre.0+1\n",
{ create: true, append: true },
],
});
assertSpyCall(appendTextFile, 7, {
args: [
"/test/output",
"dotnet=1.2.3-pre.0-1\n",
{ create: true, append: true },
],
});
assertSpyCall(appendTextFile, 8, {
assertSpyCall(appendTextFile, 7, {
args: [
"/test/output",
"docker=1.2.3-pre.0-1\n",
Expand Down
16 changes: 8 additions & 8 deletions src/util/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ export async function printVersion(
context: IContext,
semver: SemVer,
full = false,
prefix = "",
) {
const formatted = format(semver);
const sv_major = semver.major;
const { minor, patch, prerelease, build } = semver;
const { major, minor, patch, prerelease, build } = semver;
const pre = prerelease.join(".");
const b = build.join(".");
const other = semverFormats(semver, prefix);
const { dotnet, docker } = semverFormats(semver);
await writeGithubOutput(context, {
version: formatted,
sv_major,
major,
minor,
patch,
prerelease: pre,
build: b,
...other,
dotnet,
docker,
});
if (full) {
console.log(JSON.stringify({
version: formatted,
sv_major,
major,
minor,
patch,
prerelease: pre,
build: b,
...other,
dotnet,
docker,
}));
} else {
console.log(formatted);
Expand Down

0 comments on commit 1e5b6ff

Please sign in to comment.