diff --git a/packages/jsii-pacmak/lib/targets/version-utils.ts b/packages/jsii-pacmak/lib/targets/version-utils.ts index 41afa1db02..70d2d632ae 100644 --- a/packages/jsii-pacmak/lib/targets/version-utils.ts +++ b/packages/jsii-pacmak/lib/targets/version-utils.ts @@ -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 }); } /** @@ -34,33 +34,37 @@ 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]); @@ -68,7 +72,7 @@ function toBracketNotation(semverRange: string, suffix?: string): string { 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 { @@ -77,6 +81,11 @@ 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; @@ -84,10 +93,14 @@ function toBracketNotation(semverRange: string, suffix?: string): string { 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; } } diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId.csproj b/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId.csproj index 6df81f8e66..c85c0e3114 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId.csproj +++ b/packages/jsii-pacmak/test/expected.jsii-calc-base/dotnet/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId/Amazon.JSII.Tests.CalculatorPackageId.BasePackageId.csproj @@ -26,7 +26,7 @@ - + 0612,0618 diff --git a/packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId.csproj b/packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId.csproj index ab48cbd8c8..f5350812e2 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId.csproj +++ b/packages/jsii-pacmak/test/expected.jsii-calc-lib/dotnet/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId/Amazon.JSII.Tests.CalculatorPackageId.LibPackageId.csproj @@ -26,8 +26,8 @@ - - + + 0612,0618 diff --git a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon.JSII.Tests.CalculatorPackageId.csproj b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon.JSII.Tests.CalculatorPackageId.csproj index 525025ccb2..6a40934b9d 100644 --- a/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon.JSII.Tests.CalculatorPackageId.csproj +++ b/packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/Amazon.JSII.Tests.CalculatorPackageId.csproj @@ -28,9 +28,9 @@ - - - + + + 0612,0618 diff --git a/packages/jsii-pacmak/test/targets/version-utils.test.ts b/packages/jsii-pacmak/test/targets/version-utils.test.ts index 9b227c7303..99192bcda8 100644 --- a/packages/jsii-pacmak/test/targets/version-utils.test.ts +++ b/packages/jsii-pacmak/test/targets/version-utils.test.ts @@ -9,12 +9,12 @@ const examples: Record }, '~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', }, @@ -26,12 +26,12 @@ const examples: Record }, '~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', },