Skip to content

Commit

Permalink
fix -0 suffix added by semver on '<' comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller committed Apr 15, 2020
1 parent 20fe1aa commit a2221e3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
37 changes: 25 additions & 12 deletions packages/jsii-pacmak/lib/targets/version-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Comparator, Range } from 'semver';
* @see https://cwiki.apache.org/confluence/display/MAVENOLD/Dependency+Mediation+and+Conflict+Resolution
*/
export function toMavenVersionRange(semverRange: string, suffix?: string): string {
return toBracketNotation(semverRange, suffix);
return toBracketNotation(semverRange, suffix, { semver: false });
}

/**
Expand All @@ -34,41 +34,45 @@ export function toPythonVersionRange(semverRange: string): string {
return range.set.map(
set => set.map(
comp => {
const versionId = comp.semver.raw?.replace(/-0$/, '') ?? '0.0.0';
switch (comp.operator) {
case '':
// With ^0.0.0, somehow we get a left entry with an empty operator and value, we'll fix this up
return comp.value === '' ? '>=0.0.0' : `==${versionId}`;
case '=':
return `==${comp.semver.raw}`;
return `==${versionId}`;
default: // >, >=, <, <= are all valid expressions
return `${comp.operator}${comp.semver.raw}`;
return `${comp.operator}${versionId}`;
}
}
).join(', ')
).join(', ');
}

function toBracketNotation(semverRange: string, suffix?: string): string {
function toBracketNotation(semverRange: string, suffix?: string, { semver = true }: { semver?: boolean } = {}): string {
const range = new Range(semverRange);
return range.set.map(set => {
if (set.length === 1) {
const version = set[0].semver.raw;
switch (set[0].operator || '=') {
// "[version]" => means exactly version
case '=': return `[${addSuffix(set[0].semver.raw)}]`;
case '=': return `[${addSuffix(version)}]`;
// "(version,]" => means greater than version
case '>': return `(${addSuffix(set[0].semver.raw)},]`;
case '>': return `(${addSuffix(version)},]`;
// "[version,]" => means greater than or equal to that version
case '>=': return `[${addSuffix(set[0].semver.raw)},]`;
case '>=': return `[${addSuffix(version)},]`;
// "[,version)" => means less than version
case '<': return `[,${addSuffix(set[0].semver.raw)})`;
case '<': return `[,${addSuffix(version, !semver)})`;
// "[,version]" => means less than or equal to version
case '<=': return `[,${addSuffix(set[0].semver.raw)}]`;
case '<=': return `[,${addSuffix(version)}]`;
}
} else if (set.length === 2) {
const nugetRange = toBracketRange(set[0], set[1]);
if (nugetRange) {
return nugetRange;
}
}
throw new Error(`Unsupported SemVer range set: ${set.map(comp => comp.value).join(', ')}`);
throw new Error(`Unsupported SemVer range set in ${semverRange}: ${set.map(comp => comp.value).join(', ')}`);
}).join(', ');

function toBracketRange(left: Comparator, right: Comparator): string | undefined {
Expand All @@ -77,17 +81,26 @@ function toBracketNotation(semverRange: string, suffix?: string): string {
[left, right] = [right, left];
}

// With ^0.0.0, somehow we get a left entry with an empty operator and value, we'll fix this up
if (left.operator === '' && left.value === '') {
left = new Comparator('>=0.0.0', left.options);
}

if (!left.operator.startsWith('>') || !right.operator.startsWith('<')) {
// We only support ranges defined like "> (or >=) left, < (or <=) right"
return undefined;
}

const leftBrace = left.operator.endsWith('=') ? '[' : '(';
const rightBrace = right.operator.endsWith('=') ? ']' : ')';
return `${leftBrace}${addSuffix(left.semver.raw)},${addSuffix(right.semver.raw)}${rightBrace}`;
return `${leftBrace}${addSuffix(left.semver.raw)},${addSuffix(right.semver.raw, right.operator === '<' && !semver)}${rightBrace}`;
}

function addSuffix(str: string) {
function addSuffix(str: string | undefined, trimDashZero = false) {
if (!str) { return ''; }
if (trimDashZero) {
str = str.replace(/-0$/, '');
}
return suffix ? `${str}${suffix}` : str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Amazon.JSII.Runtime" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId" Version="[0.0.0,0.0.1-0)" />
</ItemGroup>
<PropertyGroup>
<NoWarn>0612,0618</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Amazon.JSII.Runtime" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BasePackageId" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BasePackageId" Version="[0.0.0,0.0.1-0)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId" Version="[0.0.0,0.0.1-0)" />
</ItemGroup>
<PropertyGroup>
<NoWarn>0612,0618</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Amazon.JSII.Runtime" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BasePackageId" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId" Version="[0.0.0,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.LibPackageId" Version="[0.0.0-devpreview,0.0.1)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BasePackageId" Version="[0.0.0,0.0.1-0)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId" Version="[0.0.0,0.0.1-0)" />
<PackageReference Include="Amazon.JSII.Tests.CalculatorPackageId.LibPackageId" Version="[0.0.0-devpreview,0.0.1-0)" />
</ItemGroup>
<PropertyGroup>
<NoWarn>0612,0618</NoWarn>
Expand Down
8 changes: 4 additions & 4 deletions packages/jsii-pacmak/test/targets/version-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const examples: Record<string, { maven: string, nuget: string, python: string }>
},
'~1.2.3': {
maven: '[1.2.3,1.3.0)',
nuget: '[1.2.3,1.3.0)',
nuget: '[1.2.3,1.3.0-0)',
python: '>=1.2.3, <1.3.0',
},
'^1.2.3': {
maven: '[1.2.3,2.0.0)',
nuget: '[1.2.3,2.0.0)',
nuget: '[1.2.3,2.0.0-0)',
python: '>=1.2.3, <2.0.0',
},

Expand All @@ -26,12 +26,12 @@ const examples: Record<string, { maven: string, nuget: string, python: string }>
},
'~0.1.2': {
maven: '[0.1.2,0.2.0)',
nuget: '[0.1.2,0.2.0)',
nuget: '[0.1.2,0.2.0-0)',
python: '>=0.1.2, <0.2.0',
},
'^0.1.2': {
maven: '[0.1.2,0.2.0)',
nuget: '[0.1.2,0.2.0)',
nuget: '[0.1.2,0.2.0-0)',
python: '>=0.1.2, <0.2.0',
},

Expand Down

0 comments on commit a2221e3

Please sign in to comment.