Skip to content

Commit 5f190c2

Browse files
authored
feat: semver range (#8)
* replace `latest` with `*` * feat: semver range * build(deps): upgrade * fix * fix
1 parent 1341dd8 commit 5f190c2

File tree

8 files changed

+189
-93
lines changed

8 files changed

+189
-93
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- uses: ./
3939
- run: flatc --version
4040

41-
test-semver:
41+
test-locked:
4242
strategy:
4343
matrix:
4444
os:
@@ -62,3 +62,29 @@ jobs:
6262
echo "Expected '$EXPECTED', got '$ACTUAL'"
6363
exit 1
6464
fi
65+
66+
test-range:
67+
strategy:
68+
matrix:
69+
os:
70+
- ubuntu-latest
71+
- macos-latest
72+
- windows-latest
73+
version:
74+
- ["24.*", "24.12.23"]
75+
- ["24.3.*", "24.3.25"]
76+
- ["23.*", "23.5.26"]
77+
runs-on: ${{ matrix.os }}
78+
steps:
79+
- uses: actions/checkout@v4
80+
- uses: ./
81+
with:
82+
version: ${{ matrix.version[0] }}
83+
- shell: bash
84+
run: |
85+
ACTUAL=$(flatc --version)
86+
EXPECTED="flatc version ${{ matrix.version[1] }}"
87+
if [ "$ACTUAL" != "$EXPECTED" ]; then
88+
echo "Expected '$EXPECTED', got '$ACTUAL'"
89+
exit 1
90+
fi

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ Install a specific version:
2020
with:
2121
version: "24.12.23"
2222
```
23+
24+
Install by a semver range:
25+
26+
```yaml
27+
- name: Install flatc
28+
uses: Nugine/setup-flatc@v1
29+
with:
30+
version: "24.*"
31+
```
32+
33+
Semver syntax: <https://jsr.io/@std/semver>

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ inputs:
99
version:
1010
description: "The version of flatc to install"
1111
required: false
12-
default: "latest"
12+
default: "*"
1313
github-token:
1414
description: "GITHUB_TOKEN"
1515
required: false

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"@actions/tool-cache": "npm:@actions/tool-cache@^2.0.1",
99
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
1010
"@std/semver": "jsr:@std/semver@^1.0.3",
11-
"esbuild": "npm:esbuild@^0.24.0",
11+
"esbuild": "npm:esbuild@^0.24.2",
1212
"octokit": "npm:octokit@^4.0.3"
1313
}
1414
}

deno.lock

Lines changed: 56 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31424,34 +31424,62 @@ var tc = __importStar(require_tool_cache());
3142431424
var semver = __importStar(require_mod());
3142531425
var octokit_1 = (init_dist_bundle14(), __toCommonJS(dist_bundle_exports3));
3142631426
async function resolveVersion(gh, version) {
31427-
if (version === "latest") {
31427+
if (version === "*") {
3142831428
const resp = await gh.rest.repos.getLatestRelease({
3142931429
owner: "google",
3143031430
repo: "flatbuffers"
3143131431
});
3143231432
version = resp.data.tag_name;
31433+
if (version.startsWith("v")) {
31434+
version = version.slice(1);
31435+
}
3143331436
}
31434-
if (version.startsWith("v")) {
31435-
version = version.slice(1);
31437+
if (semver.canParse(version)) {
31438+
return version;
3143631439
}
31437-
if (!semver.canParse(version)) {
31438-
throw new Error(`Invalid version: ${version}`);
31440+
const range = semver.tryParseRange(version);
31441+
if (range) {
31442+
const resp = await gh.rest.repos.listReleases({
31443+
owner: "google",
31444+
repo: "flatbuffers",
31445+
page: 1,
31446+
per_page: 100
31447+
});
31448+
for (const release of resp.data) {
31449+
version = release.tag_name;
31450+
if (version.startsWith("v")) {
31451+
version = version.slice(1);
31452+
}
31453+
const ver = semver.parse(version);
31454+
if (semver.satisfies(ver, range)) {
31455+
return version;
31456+
}
31457+
}
31458+
throw new Error("No matching version found for range");
3143931459
}
31440-
return version;
31460+
throw new Error(`Invalid version: ${version}`);
3144131461
}
31442-
function getDownloadUrl(version) {
31443-
const repo = "google/flatbuffers";
31444-
const baseUrl = `https://github.com/${repo}/releases/download`;
31462+
async function getDownloadUrl(gh, version) {
3144531463
const platformMap = {
31446-
linux: "Linux.flatc.binary.g++-13.zip",
31447-
darwin: "Mac.flatc.binary.zip",
31448-
win32: "Windows.flatc.binary.zip"
31464+
linux: /Linux\.flatc\.binary\.g\+\+-\d+\.zip/,
31465+
darwin: /Mac\.flatc\.binary\.zip/,
31466+
win32: /Windows\.flatc\.binary\.zip/
3144931467
};
31450-
const filename = platformMap[core.platform.platform];
31451-
if (!filename) {
31468+
const fileRegex = platformMap[core.platform.platform];
31469+
if (!fileRegex) {
3145231470
throw new Error(`Unsupported platform: ${core.platform.platform}`);
3145331471
}
31454-
return `${baseUrl}/v${version}/${filename}`;
31472+
const resp = await gh.rest.repos.getReleaseByTag({
31473+
owner: "google",
31474+
repo: "flatbuffers",
31475+
tag: `v${version}`
31476+
});
31477+
for (const asset of resp.data.assets) {
31478+
if (fileRegex.test(asset.name)) {
31479+
return asset.browser_download_url;
31480+
}
31481+
}
31482+
throw new Error("No matching asset found for platform");
3145531483
}
3145631484
async function downloadFlatc(version, url) {
3145731485
let cachedPath = tc.find("flatc", version);
@@ -31469,11 +31497,11 @@ async function downloadFlatc(version, url) {
3146931497
async function main() {
3147031498
const githubToken = core.getInput("github-token") ?? void 0;
3147131499
const gh = new octokit_1.Octokit({ auth: githubToken });
31472-
const inputVersion = core.getInput("version") ?? "latest";
31500+
const inputVersion = core.getInput("version") ?? "*";
3147331501
core.info(`Input version: ${inputVersion}`);
3147431502
const version = await resolveVersion(gh, inputVersion);
3147531503
core.info(`Resolved version: ${version}`);
31476-
const url = getDownloadUrl(version);
31504+
const url = await getDownloadUrl(gh, version);
3147731505
const cachedPath = await downloadFlatc(version, url);
3147831506
core.info(`Cached at: ${cachedPath}`);
3147931507
core.addPath(cachedPath);

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dev:
33
deno lint
44
deno check .
55
just test
6-
just bundle
76

87
test:
98
# deno test -A
@@ -17,4 +16,5 @@ assert_unchanged:
1716

1817
ci:
1918
just dev
19+
just bundle
2020
just assert_unchanged

0 commit comments

Comments
 (0)