-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alvaro Sanchez-Leon <[email protected]>
- Loading branch information
Showing
6 changed files
with
197 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2022 Ericsson and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
export interface VersionRange { | ||
from: string, | ||
to?: string | ||
} | ||
|
||
const semverLength = 3; | ||
export function isInRange(version: string, versionRange: VersionRange): boolean { | ||
const { returning, semanticVersions } = resolveByBranchNames(version, versionRange); | ||
if (returning !== undefined) { | ||
return returning; | ||
} | ||
|
||
return isVersionInRange(semanticVersions.input, semanticVersions.from, semanticVersions.to); | ||
} | ||
|
||
function removeVersionDecorator(version: string) { | ||
return version.startsWith('v') ? version.substring(1) : version; | ||
} | ||
|
||
/** | ||
* Handle provided versions by branch names; either 'master' or 'local' | ||
*/ | ||
function resolveByBranchNames( | ||
versionInput: string, | ||
versionRange: VersionRange | ||
): { returning?: boolean; semanticVersions?: { input: string; from: string; to: string } } { | ||
const input = removeVersionDecorator(versionInput); | ||
const from = removeVersionDecorator(versionRange.from); | ||
let to = versionRange.to ? removeVersionDecorator(versionRange.to) : undefined; | ||
|
||
// a 'to' version pointing to 'master' or 'local' means there is no version ending a condition (stubbing) | ||
// So we treat those as if the 'to' version was 'undefined' | ||
to = to && (to === 'master' || to === 'local') ? undefined : to; | ||
|
||
if (input === 'master' || input == 'local') { | ||
// Handling 'master' and 'local' which are considered the 'latest' | ||
// Assuming, if 'to' version is available then 'latest' is no longer under the condition i.e. stubbed | ||
return { returning: to ? false : true }; | ||
} | ||
|
||
if (from === 'master' || from === 'local') { | ||
// At this point input is not 'master' or 'local' i.e. input is referring to earlier versions than 'from' | ||
return { returning: false }; | ||
} | ||
|
||
// Done handling the supported branch names, | ||
// returning strings representing semantic versions | ||
return { semanticVersions: { input, from, to } } | ||
} | ||
|
||
/** | ||
* Each argument represents a semantic version | ||
*/ | ||
function isVersionInRange(input: string, from: string, to?: string): boolean { | ||
const inputArr = input.split('.'); | ||
const fromArr = from.split('.'); | ||
const toArr = to ? to.split('.') : undefined; | ||
|
||
if ( | ||
inputArr.length != semverLength || | ||
fromArr.length != semverLength || | ||
!!(toArr && toArr.length != semverLength) | ||
) { | ||
throw new Error('The expected length of semantic versions is 3'); | ||
} | ||
|
||
const greaterOrEqual = isGreaterOrEqual(inputArr, fromArr); | ||
if (!greaterOrEqual) { | ||
return false; | ||
} | ||
|
||
if (!toArr) { | ||
// Not having a 'to' version means the condition is still present i.e. stubbing applies. | ||
return true; | ||
} | ||
|
||
return isLessOrEqual(inputArr, toArr); | ||
} | ||
|
||
function isGreaterOrEqual(inputArr: string[], referenceArr: string[]): boolean { | ||
for (let i = 0; i < semverLength; i++) { | ||
if (+inputArr[i] > +referenceArr[i]) { | ||
return true; | ||
} | ||
|
||
if (+inputArr[i] < +referenceArr[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
// Must be equal | ||
return true; | ||
} | ||
|
||
function isLessOrEqual(inputArr: string[], referenceArr: string[]): boolean { | ||
for (let i = 0; i < semverLength; i++) { | ||
if (+inputArr[i] < +referenceArr[i]) { | ||
return true; | ||
} | ||
|
||
if (+inputArr[i] > +referenceArr[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
// Must be equal | ||
return true; | ||
} |