Skip to content

Commit

Permalink
feat: support rc version
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Sep 13, 2024
1 parent 3a04105 commit a006242
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
deno:
[1.x, "1.33.1", canary, ~1.32, b290fd01f3f5d32f9d010fc719ced0240759c049]
[1.x, "1.33.1", canary, ~1.32, b290fd01f3f5d32f9d010fc719ced0240759c049, rc]

steps:
- uses: actions/checkout@v3
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ Targets the latest major, minor and patch version of Deno.
deno-version: e7b7129b7a92b7500ded88f8f5baa25a7f59e56e
```
### Release candidate
```yaml
- uses: denoland/setup-deno@v1
with:
deno-version: rc
```
### Version from file
The extension can also automatically read the version file from
Expand Down
9 changes: 3 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ async function main() {
exit("Could not resolve a version for the given range.");
}

core.info(
`Going to install ${
version.isCanary ? "canary" : "stable"
} version ${version.version}.`,
);
core.info(`Going to install ${version.kind} version ${version.version}.`);

await install(version);

core.setOutput("deno-version", version.version);
core.setOutput("is-canary", version.isCanary);
// TODO(@crowlKats): change this output to "kind"
core.setOutput("is-canary", version.kind === "canary");

core.info("Installation complete.");
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const tc = require("@actions/tool-cache");
async function install(version) {
const cachedPath = tc.find(
"deno",
version.isCanary ? `0.0.0-${version.version}` : version.version,
version.kind === "canary" ? `0.0.0-${version.version}` : version.version,
);
if (cachedPath) {
core.info(`Using cached Deno installation from ${cachedPath}.`);
Expand All @@ -20,7 +20,7 @@ async function install(version) {
}

const zip = zipName();
const url = version.isCanary
const url = version.kind === "canary"
? `https://dl.deno.land/canary/${version.version}/${zip}`
: `https://dl.deno.land/release/v${version.version}/${zip}`;

Expand Down
134 changes: 87 additions & 47 deletions src/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const GIT_HASH_RE = /^[0-9a-fA-F]{40}$/;
/**
* @typedef VersionRange
* @property {string} range
* @property {boolean} isCanary
* @property {"canary" | "rc" | "release"} kind
*/

/**
* @typedef Version
* @property {string} version
* @property {boolean} isCanary
* @property {"canary" | "rc" | "release"} kind
*/

/**
Expand All @@ -26,21 +26,25 @@ function parseVersionRange(version) {
version = String(version) || "1.x";
version = version.trim();

if (version == "canary") {
return { range: "latest", isCanary: true };
if (version === "canary") {
return { range: "latest", kind: "canary" };
}

if (version == "latest") {
return { range: "latest", isCanary: false };
if (version === "rc") {
return { range: "latest", kind: "rc" };
}

if (version === "latest") {
return { range: "latest", kind: "release" };
}

if (GIT_HASH_RE.test(version)) {
return { range: version, isCanary: true };
return { range: version, kind: "canary" };
}

const range = semver.validRange(version);
if (range !== null) {
return { range, isCanary: false };
return { range, kind: "release" };
}

return null;
Expand Down Expand Up @@ -79,23 +83,59 @@ function getDenoVersionFromFile(versionFilePath) {
* @param {VersionRange} range
* @returns {Promise<Version | null>}
*/
async function resolveVersion({ range, isCanary }) {
if (isCanary) {
if (range === "latest") {
const res = await fetchWithRetries(
"https://dl.deno.land/canary-latest.txt",
async function resolveVersion({ range, kind }) {
if (kind === "canary") {
return resolveCanary(range);
} else if (kind === "rc") {
// range is always "latest"
return resolveReleaseCandidate();
} else {
return resolveRelease(range);
}
}

/**
* @param {string} range
* @returns {Promise<Version | null>}
*/
async function resolveCanary(range) {
if (range === "latest") {
const res = await fetchWithRetries(
"https://dl.deno.land/canary-latest.txt",
);
if (res.status !== 200) {
throw new Error(
"Failed to fetch canary version info from dl.deno.land. Please try again later.",
);
if (res.status !== 200) {
throw new Error(
"Failed to fetch canary version info from dl.deno.land. Please try again later.",
);
}
const version = (await res.text()).trim();
return { version, isCanary: true };
}
return { version: range, isCanary: true };
const version = (await res.text()).trim();
return { version, kind: "canary" };
} else {
return { version: range, kind: "canary" };
}
}

/**
* @returns {Promise<Version | null>}
*/
async function resolveReleaseCandidate() {
const res = await fetchWithRetries(
"https://dl.deno.land/release-rc-latest.txt",
);
if (res.status !== 200) {
throw new Error(
"Failed to fetch release candidate version info from dl.deno.land. Please try again later.",
);
}
const version = semver.clean((await res.text()).trim());
return { version, kind: "rc" };
}

/**
* @param {string} range
* @returns {Promise<Version | null>}
*/
async function resolveRelease(range) {
if (range === "latest") {
const res = await fetchWithRetries(
"https://dl.deno.land/release-latest.txt",
Expand All @@ -110,35 +150,35 @@ async function resolveVersion({ range, isCanary }) {
if (version === null) {
return null;
}
return { version, isCanary: false };
}
return { version, kind: "release" };
} else {
const res = await fetchWithRetries("https://deno.com/versions.json");
if (res.status !== 200) {
throw new Error(
"Failed to fetch stable version info from deno.com/versions.json. Please try again later.",
);
}
const versionJson = await res.json();
if (!("cli" in versionJson)) {
throw new Error("Fetched stable version info is invalid.");
}
/** @type {string[]} */
const versions = versionJson.cli;
if (!Array.isArray(versions)) {
throw new Error("Fetched stable version info is invalid.");
}

const res = await fetchWithRetries("https://deno.com/versions.json");
if (res.status !== 200) {
throw new Error(
"Failed to fetch stable version info from deno.com/versions.json. Please try again later.",
);
}
const versionJson = await res.json();
if (!("cli" in versionJson)) {
throw new Error("Fetched stable version info is invalid.");
}
/** @type {string[]} */
const versions = versionJson.cli;
if (!Array.isArray(versions)) {
throw new Error("Fetched stable version info is invalid.");
}
let version = semver.maxSatisfying(versions, range);
if (version === null) {
return null;
}
version = semver.clean(version);
if (version === null) {
return null;
}

let version = semver.maxSatisfying(versions, range);
if (version === null) {
return null;
}
version = semver.clean(version);
if (version === null) {
return null;
return { version, kind: "release" };
}

return { version, isCanary: false };
}

/** @param {string} url */
Expand Down

0 comments on commit a006242

Please sign in to comment.