Skip to content

Commit 197cbb7

Browse files
authored
Add support for different version formats when using regexp (#60)
* add support for variant version formats along with regexp * Per PR comments, added VariantKey enum, updated to use key instead of if block, applied deno fmt * added missing variant.ts file * update unit tests
1 parent 4927477 commit 197cbb7

File tree

19 files changed

+61
-17
lines changed

19 files changed

+61
-17
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
tab_width = 2
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
indent_style = space
10+
continuation_indent_size = 4
11+
trim_trailing_whitespace = true

src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { patch, regexp, replace } from "./hooks/mod.ts";
2+
import { VariantKey } from "./util/variant.ts";
23

34
export interface IContext {
45
output?: string;
@@ -16,6 +17,7 @@ export interface IContext {
1617
current: string,
1718
pattern: string,
1819
flags?: string,
20+
variant?: VariantKey,
1921
) => Promise<void>;
2022
};
2123
}

src/hooks/hooks.interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { VariantKey } from "../util/variant.ts";
2+
13
export enum PostHookKind {
24
Replace = "replace",
35
Patch = "patch",
@@ -17,6 +19,7 @@ export type RegExpPostHook = {
1719
file: string;
1820
pattern: string;
1921
flags?: string;
22+
variant?: VariantKey;
2023
};
2124

2225
export type PostHook =

src/hooks/post.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export async function postVersionHook(
4242
current,
4343
hook.pattern,
4444
hook.flags,
45+
hook.variant,
4546
);
4647
break;
4748
default:

src/hooks/regexp.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import { variantByKey } from "../util/version.ts";
2+
import { VariantKey } from "../util/variant.ts";
3+
14
export async function regexp(
25
file: string,
36
current: string,
47
pattern: string,
58
flags?: string,
9+
variant?: VariantKey,
610
) {
711
const regexp = new RegExp(pattern, flags);
812
const contents = await Deno.readTextFile(file);
913
const match = contents.match(regexp);
10-
const updated = contents.replace(regexp, current);
11-
12-
console.log(`replacing [${match?.[0] || ""}] -> ${current} in ${file}`);
14+
const applicableVersion = variantByKey(current, variant);
15+
console.log(
16+
`replacing [${match?.[0] || ""}] -> ${applicableVersion} in ${file}`,
17+
);
18+
const updated = contents.replace(regexp, applicableVersion);
1319
await Deno.writeTextFile(file, updated);
1420
}

src/util/variant.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum VariantKey {
2+
Default = "version_default",
3+
Dotnet = "version_dotnet",
4+
Docker = "version_docker",
5+
}

src/util/version.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Deno.test({
151151
const appendTextFile = stub(
152152
Deno,
153153
"writeTextFile",
154-
resolvesNext(new Array(8)),
154+
resolvesNext(new Array(9)),
155155
);
156156
try {
157157
const version = parse("1.2.3-pre.0+1");
@@ -201,11 +201,18 @@ Deno.test({
201201
assertSpyCall(appendTextFile, 6, {
202202
args: [
203203
"/test/output",
204-
"version_dotnet=1.2.3-pre.0-1\n",
204+
"version_default=1.2.3-pre.0+1\n",
205205
{ create: true, append: true },
206206
],
207207
});
208208
assertSpyCall(appendTextFile, 7, {
209+
args: [
210+
"/test/output",
211+
"version_dotnet=1.2.3-pre.0-1\n",
212+
{ create: true, append: true },
213+
],
214+
});
215+
assertSpyCall(appendTextFile, 8, {
209216
args: [
210217
"/test/output",
211218
"version_docker=1.2.3-pre.0-1\n",

src/util/version.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { path } from "../../deps/std.ts";
22
import { format, parse, SemVer } from "../../deps/semver.ts";
33
import { IContext } from "../context.ts";
4+
import { VariantKey } from "./variant.ts";
45

56
export const DEFAULT_VERSION = parse("0.1.0");
67

@@ -58,11 +59,19 @@ export function variants(version: string) {
5859
const kabobBuild = version.replace(/[+]/g, "-");
5960
// todo: add any other platform specific variants here.
6061
return {
62+
version_default: version,
6163
version_dotnet: kabobBuild,
6264
version_docker: kabobBuild,
6365
};
6466
}
6567

68+
export function variantByKey(
69+
version: string,
70+
variantKey: VariantKey = VariantKey.Default,
71+
) {
72+
return variants(version)[variantKey];
73+
}
74+
6675
/**
6776
* This reads the current version file found at $CWD/VERSION and parses it as a
6877
* valid SemVer. If the file is not found, is empty, or contains an invalid

test/dotnet/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.0
1+
0.4.0-lambda.2+ghi.789

test/dotnet/example.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net7.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<PackageId>example</PackageId>
7-
<Version>0.1.0</Version>
7+
<Version>0.4.0-lambda.2-ghi.789</Version>
88
<Authors>Justin Chase</Authors>
99
<Company>Optum</Company>
1010
<PackageOutputPath>./nupkg</PackageOutputPath>

0 commit comments

Comments
 (0)