Skip to content

Commit 133485d

Browse files
committed
can now configure totalBits #5
1 parent 44feb50 commit 133485d

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

Tasks/EncodeSemverToInteger/EncodeSemverToInteger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ try {
2323
let preReleaseTagMap: IPrereleaseTagMap = JSON.parse(tl.getInput("PreReleaseTagMap"));
2424
let preReleaseNumberBits = getNumberInput("PreReleaseNumberBits");
2525
let outputVariable = tl.getInput("OutputVariable");
26-
let disableZeroBitAllocation = tl.getBoolInput("disableZeroBitAllocation");
26+
let disableZeroBitAllocation = tl.getBoolInput("DisableZeroBitAllocation");
27+
let totalBits = getNumberInput("TotalBits");
2728

2829
let varRegex = /\$\((.*?)\)/g;
2930
sourceSemVer = sourceSemVer.replace(varRegex, (match, varName, offset, s) => tl.getVariable(varName));
3031

3132
console.log("Source Semver: " + sourceSemVer);
3233

33-
let encoder = new Encoder(new EncodingConfiguration(disableZeroBitAllocation, minorBits, patchBits, preReleaseTagBits, preReleaseTagMap, preReleaseNumberBits));
34+
let encoder = new Encoder(new EncodingConfiguration(totalBits, disableZeroBitAllocation, minorBits, patchBits, preReleaseTagBits, preReleaseTagMap, preReleaseNumberBits));
3435
let code = checkNaN(encoder.encode(sourceSemVer), "Computed Version Code");
3536

3637
console.log("Computed Version Code: " + code.toString());

Tasks/EncodeSemverToInteger/encoder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ export interface IPrereleaseTagMap {
55
}
66

77
export class EncodingConfiguration {
8-
readonly majorBits: number = 31 - this.minorBits - this.patchBits - this.prereleaseNumberBits - this.prereleaseTagBits;
8+
readonly majorBits: number = this.totalBits - this.minorBits - this.patchBits - this.prereleaseNumberBits - this.prereleaseTagBits;
99

1010
readonly maxPrereleaseTagCode = Math.pow(2, this.prereleaseTagBits) - 1;
1111
readonly maxPrereleaseNumberCode = Math.pow(2, this.prereleaseNumberBits) - 1;
1212

1313
constructor(
14+
readonly totalBits: number,
1415
readonly allowZeroBitAllocation: boolean,
1516
readonly minorBits: number,
1617
readonly patchBits: number,

Tasks/EncodeSemverToInteger/task.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
],
2222
"minimumAgentVersion": "1.91.0",
2323
"instanceNameFormat": "Encode Semver $(SourceSemver)",
24+
"groups": [
25+
{
26+
"name": "advanced",
27+
"displayName": "Advanced options",
28+
"isExpanded": false
29+
}
30+
],
2431
"inputs": [
2532
{
2633
"name": "SourceSemver",
@@ -79,11 +86,21 @@
7986
"helpMarkDown": "Bits used to encode PreReleaseNumber"
8087
},
8188
{
82-
"name": "disableZeroBitAllocation",
89+
"groupName": "advanced",
90+
"name": "DisableZeroBitAllocation",
8391
"type": "boolean",
8492
"label": "Allow zero bit allocation",
8593
"defaultValue": "false",
8694
"helpMarkDown": "If this is enabled you can specify a 0 bit allocation for more or one component without it triggering an overflow error."
95+
},
96+
{
97+
"groupName": "advanced",
98+
"name": "TotalBits",
99+
"type": "string",
100+
"label": "Total bits",
101+
"defaultValue": "31",
102+
"required": true,
103+
"helpMarkDown": "defaults to signed int 32 available bits. You can tweak this value if you want to encode over a larger or smaller integer size."
87104
}
88105
],
89106
"execution": {

Tests/EncodeSemverToInteger/encoding.spec.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ interface IExpectedErrors {
2626
[key: string]: string;
2727
}
2828

29-
let onlyPrereleaseConfiguration = new EncodingConfiguration(true, 0, 0, 2, { "rc": 2, "beta": 1, "alpha": 0, "unstable": 0 }, 29);
30-
let defaultConfiguration = new EncodingConfiguration(false, 5, 4, 2, { "rc": 2, "beta": 1, "alpha": 0, "unstable": 0 }, 9);
31-
let balancedConfiguration = new EncodingConfiguration(false, 6, 5, 2, { "rc": 2, "beta": 1, "alpha": 0 }, 9, "Balanced");
32-
let distributedConfiguration = new EncodingConfiguration(false, 7, 7, 2, { "rc": 2, "beta": 1, "alpha": 0 }, 7, "Distributed");
29+
let onlyPrereleaseConfiguration = new EncodingConfiguration(31, true, 0, 0, 2, { "rc": 2, "beta": 1, "alpha": 0, "unstable": 0 }, 29);
30+
let defaultConfiguration = new EncodingConfiguration(31, false, 5, 4, 2, { "rc": 2, "beta": 1, "alpha": 0, "unstable": 0 }, 9);
31+
let balancedConfiguration = new EncodingConfiguration(31, false, 6, 5, 2, { "rc": 2, "beta": 1, "alpha": 0 }, 9, "Balanced");
32+
let distributedConfiguration = new EncodingConfiguration(31, false, 7, 7, 2, { "rc": 2, "beta": 1, "alpha": 0 }, 7, "Distributed");
33+
let longDistributedConfiguration = new EncodingConfiguration(63, false, 14, 14, 4, { "rc": 2, "beta": 1, "alpha": 0 }, 14, "Long (signed 64 bits) Distributed");
3334

3435
describe("Simple encoding", () => {
3536
let defaultConfigurationEncoder = new Encoder(defaultConfiguration);
@@ -151,7 +152,7 @@ describe("Overflows", () => {
151152
},
152153
},
153154
{
154-
configuration: new EncodingConfiguration(false, 5, 4, 2, { "prod": 4, "preprod": 3, "rc": 2, "beta": 1, "alpha": 0 }, 9),
155+
configuration: new EncodingConfiguration(31, false, 5, 4, 2, { "prod": 4, "preprod": 3, "rc": 2, "beta": 1, "alpha": 0 }, 9),
155156
expectedErrors: {
156157
"0.0.0-prod.1": "Prerelease Tag will overflow allocated bits (4 >= 4).",
157158
"0.0.0-preprod.1": "The max allowed Prerelease Tag value (3) should be kept for Semver without Prerelease Tag.",
@@ -227,6 +228,16 @@ describe("Precedence", () => {
227228
lastCode = code;
228229
}
229230
});
231+
232+
it("should be preserved with configuration '" + longDistributedConfiguration.friendlyName + "'", () => {
233+
let lastCode = -1;
234+
let distributedConfigurationEncoder = new Encoder(distributedConfiguration);
235+
for (let i = 0; i < traits.length; i++) {
236+
let code = distributedConfigurationEncoder.encode(traits[i]);
237+
expect(code).toBeGreaterThan(lastCode);
238+
lastCode = code;
239+
}
240+
});
230241
});
231242

232243
describe("Disabled components encoding", () => {

0 commit comments

Comments
 (0)