Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module: "github.com/product-os/cue-semver2"

91 changes: 91 additions & 0 deletions lib/range.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package semver

import (
"regexp"
"strings"
"strconv"
)

#Range: {
#range: string
range: (#hyphenReplace&{#incPr: false, #matches: regexp.FindAllSubmatch(patterns.HYPHENRANGE, #range, -1)[0]})
}

#isX: {
#id: string
#return: [
if #id == "" { true },
if strings.ToLower(#id) == "x" { true },
if #id == "*" { true },
false
][0]
}

#hyphenReplace: {
#incPr: bool
#matches: [string, string, string, string, string, string, string, string, string, string, string, string, string]
#$0: #matches[0]
#from: #matches[1]
#fM: #matches[2]
#fm: #matches[3]
#fp: #matches[4]
#fpr: #matches[5]
#fb: #matches[6]
#to: #matches[7]
#tM: #matches[8]
#tm: #matches[9]
#tp: #matches[10]
#tpr: #matches[11]
#tb: #matches[12]
#fromNew: [
if (#isX&{#id: #fM}).#return { "" },
if #incPr {
if (#isX&{#id: #fm}).#return != _|_ { ">=\(#fM).0.0-0" },
if (#isX&{#id: #fp}).#return != _|_ { ">=\(#fM).\(#fm).0-0" },
if #fpr != "" { ">=\(#from)" },
">=\(#from)-0",
},
if !#incPr {
if (#isX&{#id: #fm}).#return != _|_ { ">=\(#fM).0.0" },
if (#isX&{#id: #fp}).#return != _|_ { ">=\(#fM).\(#fm).0" },
if #fpr != "" { ">=\(#from)" },
">=\(#from)"
},
]
// if (isX(fM)) {
// from = ''
// } else if (isX(fm)) {
// from = `>=${fM}.0.0${incPr ? '-0' : ''}`
// } else if (isX(fp)) {
// from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
// } else if (fpr) {
// from = `>=${from}`
// } else {
// from = `>=${from}${incPr ? '-0' : ''}`
// }
#toNew: [
if (#isX&{#id: #tM}).#return { "" },
if (#isX&{#id: #tm}).#return { "<\(strconv.Atoi(#tM) + 1).0.0-0" },
if (#isX&{#id: #tp}).#return { "<\(#tM).\(strconv.Atoi(#tm) + 1).0-0" },
if #tpr != "" { "<=\(#tM).\(#tm).\(#tp)-\(#tpr)" },
if #incPr { "<=\(#tM).\(#tm).\(strconv.Atoi(#tp) + 1)-0" },
if !#incPr { "<=\(#to)" }
]
// if (isX(tM)) {
// to = ''
// } else if (isX(tm)) {
// to = `<${+tM + 1}.0.0-0`
// } else if (isX(tp)) {
// to = `<${tM}.${+tm + 1}.0-0`
// } else if (tpr) {
// to = `<=${tM}.${tm}.${tp}-${tpr}`
// } else if (incPr) {
// to = `<${tM}.${tm}.${+tp + 1}-0`
// } else {
// to = `<=${to}`
// }
#return: "\(#fromNew[0]) \(#toNew[0])"
// #return: "\(#from) \(#to)"
}

range: #Range&{#range: "5.1.* - 7.2.3"}
44 changes: 44 additions & 0 deletions lib/regexp.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package semver

patterns: {
"NUMERICIDENTIFIER": #"0|[1-9]\d*"#,
"NUMERICIDENTIFIERLOOSE": #"[0-9]+"#,
"NONNUMERICIDENTIFIER": #"\d*[a-zA-Z-][a-zA-Z0-9-]*"#,
"MAINVERSION": #"(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)"#,
"MAINVERSIONLOOSE": #"([0-9]+)\.([0-9]+)\.([0-9]+)"#,
"PRERELEASEIDENTIFIER": #"(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)"#,
"PRERELEASEIDENTIFIERLOOSE": #"(?:[0-9]+|\d*[a-zA-Z-][a-zA-Z0-9-]*)"#,
"PRERELEASE": #"(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*))"#,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]*))*)))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?)?)?)\s+-\s+([v=\s]*(0|[1-9]\d*|x|X|\*)(?:\.(0|[1-9]\d*|x|X|\*)(?:\.(0|[1-9]\d*|x|X|\*)(?:(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*)))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?)?)?)\s*$"#,
"HYPHENRANGELOOSE": #"^\s*([v=\s]*([0-9]+|x|X|\*)(?:\.([0-9]+|x|X|\*)(?:\.([0-9]+|x|X|\*)(?:(?:-?((?:[0-9]+|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:[0-9]+|\d*[a-zA-Z-][a-zA-Z0-9-]*))*)))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?)?)?)\s+-\s+([v=\s]*([0-9]+|x|X|\*)(?:\.([0-9]+|x|X|\*)(?:\.([0-9]+|x|X|\*)(?:(?:-?((?:[0-9]+|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:[0-9]+|\d*[a-zA-Z-][a-zA-Z0-9-]*))*)))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?)?)?)\s*$"#,
"STAR": #"(<|>)?=?\s*\*"#,
"GTE0": #"^\s*>=\s*0.0.0\s*$"#,
"GTE0PRE": #"^\s*>=\s*0.0.0-0\s*$"#
}
Empty file added lib/satisfies.cue
Empty file.
32 changes: 32 additions & 0 deletions lib/semver.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package semver

import (
"regexp"
"strings"
"strconv"
)

#SemVer: {
version: string

let matches = regexp.FindAllSubmatch(patterns.FULL, version, -1)
//TODO: handle failed match
major: strconv.Atoi(matches[0][1])
minor: strconv.Atoi(matches[0][2])
patch: strconv.Atoi(matches[0][3])
if matches[0][4] == "" {
prerelease: []
}
if matches[0][4] != "" {
prerelease: [for id in strings.Split(matches[0][4], ".") {
if regexp.Find("^[0-9]+$", id) != _|_ { strconv.Atoi(id) }
if regexp.Find("^[0-9]+$", id) == _|_ { id }
}]
}
if matches[0][5] == "" {
build: []
}
if matches[0][5] != "" {
build: [strings.Split(matches[0][5], ".")]
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
],
"scripts": {
"clean": "rimraf build",
"build": "npm run clean",
"lint": "balena-lint lib tests",
"lint-fix": "balena-lint --typescript --fix lib tests",
"test:node": "mocha -r ts-node/register --reporter spec tests/**/*.spec.ts",
Expand Down
7 changes: 7 additions & 0 deletions tests/chai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
chai.use(chaiAsPromised);

export default chai;

export const { expect } = chai;
3 changes: 3 additions & 0 deletions tests/fixtures/version-to-semver.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package semver

out: #SemVer&{version: string @tag(version)}
15 changes: 15 additions & 0 deletions tests/main.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as path from 'path';
import { expect } from './chai';
import cue from 'cuelang-js';

describe('cue', function () {
it('should parse version string to semver', async function () {
const fixturePath = path.join(__dirname, './fixtures/version-to-semver.cue')
const semverPath = path.join(__dirname, '../lib/semver.cue')
const regexpPath = path.join(__dirname, '../lib/regexp.cue')
const result = await cue('export', [fixturePath, semverPath, regexpPath], {"--inject": "version=1.2.3-alpha+build.1", "-e": "out"})
expect(result.code).to.equal(0)
const semver = JSON.parse(result.stdout)
expect(semver.major).to.equal(1)
});
});