From e07739d3f6a88361c1458904bca59c2110e8e276 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Mon, 30 Oct 2023 16:59:57 +0530 Subject: [PATCH 01/16] feat(usePantry): Add support for multiple dists in `distributable`. --- lib/usePantry.ts | 113 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 34 deletions(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index 52dde45f..86697d60 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -60,17 +60,17 @@ const getPlatforms = async (pkg: Package | PackageRequirement) => { return rv } -const getRawDistributableURL = (yml: PlainObject) => { - if (isPlainObject(yml.distributable)) { - return validate.str(yml.distributable.url) - } else if (isString(yml.distributable)) { - return yml.distributable - } else if (yml.distributable === null || yml.distributable === undefined) { - return +const getRawDistributableURL = (dist: PlainObject) => { + if (isPlainObject(dist)) { + return validate.str(dist.url); + } else if (isString(dist)) { + return dist; + } else if (dist === null || dist === undefined) { + return; } else { - throw new Error(`invalid distributable node: ${yml.distributable}`) + throw new Error(`invalid distributable node: ${dist}`); } -} +}; const getGitDistribution = ({ pkg, url: urlstr, ref }: { pkg: Package, url: string, ref: string }) => { if (!ref) { @@ -90,33 +90,78 @@ const getGitDistribution = ({ pkg, url: urlstr, ref }: { pkg: Package, url: stri } const getDistributable = async (pkg: Package) => { - const moustaches = useMoustaches() - - const yml = await hooks.usePantry().project(pkg).yaml() - - if (yml.distributable?.git) { - console.warn("brewkit: using distributable.git instead of distributable.url is deprecated") - return getGitDistribution({ pkg, ...yml.distributable}) - } - if (yml.distributable?.url?.startsWith("git")) { - return getGitDistribution({ pkg, ...yml.distributable}) - } - - let urlstr = getRawDistributableURL(yml) - if (!urlstr) return - let stripComponents: number | undefined - if (isPlainObject(yml.distributable)) { - stripComponents = flatmap(yml.distributable["strip-components"], coerceNumber) + const moustaches = useMoustaches(); + + const yml = await hooks.usePantry().project(pkg).yaml(); + let final_url = ""; + let dists = yml.distributable; + let stripComponents: number | undefined; + if (!isArray(dists)) dists = [dists]; + for (const dist of dists) { + //FIXME: Add check for Git dists as well + if (dist.git) { + console.warn( + "brewkit: using distributable.git instead of distributable.url is deprecated", + ); + return getGitDistribution({ pkg, ...dist }); + } + if (dist.url?.startsWith("git")) { + return getGitDistribution({ pkg, ...dist }); + } + let urlstr = getRawDistributableURL(dist); + let raw_v = ""; + let matched = true; + if (!urlstr) continue; + let tmp_stripComponents: number | undefined; + if (isPlainObject(dist)) { + tmp_stripComponents = flatmap(dist["strip-components"], coerceNumber); + if (Object.keys(dist.rewrite).length) { + raw_v = pkg.version.raw.replace( + new RegExp(dist.rewrite["match"], "gi"), + dist.rewrite["with"], + ); + } + if (dist?.if) { + matched = new RegExp(dist.if).test(pkg.version.raw); + } + } + let v: SemVer; + if (raw_v) { + v = { + raw: raw_v, + major: pkg.version.major, + minor: pkg.version.minor, + patch: pkg.version.patch, + components: pkg.version.components, + prerelease: pkg.version.prerelease, + build: pkg.version.build, + eq: pkg.version.eq, + neq: pkg.version.neq, + gt: pkg.version.gt, + gte: pkg.version.gte, + lt: pkg.version.lt, + lte: pkg.version.lte, + compare: pkg.version.compare, + }; + } else { + v = pkg.version; + } + urlstr = moustaches.apply(urlstr, [ + ...moustaches.tokenize.version(v), + ...moustaches.tokenize.host(), + ]); + if (!matched) continue; + const rsp = await fetch(urlstr, { method: "HEAD" }); + if (rsp.status == 200) { + final_url = urlstr; + stripComponents = tmp_stripComponents; + break; + } } + if (!final_url) return; + const url = new URL(final_url); - urlstr = moustaches.apply(urlstr, [ - ...moustaches.tokenize.version(pkg.version), - ...moustaches.tokenize.host() - ]) - - const url = new URL(urlstr) - - return { url, ref: undefined, stripComponents, type: 'url' } + return { url, ref: undefined, stripComponents, type: "url" } } // deno-lint-ignore no-explicit-any From f623ee01ac7f8dc168004e6e276e2e3cfb53d793 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Mon, 30 Oct 2023 17:10:44 +0530 Subject: [PATCH 02/16] import SemVer --- lib/usePantry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index 86697d60..c815d354 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -1,5 +1,5 @@ import { isNumber, isPlainObject, isString, isArray, PlainObject } from "is-what" -import { Package, PackageRequirement, semver, utils, hooks } from "libpkgx" +import { Package, PackageRequirement, SemVer, semver, utils, hooks } from "libpkgx" import { getScript } from "./usePantry.getScript.ts" import getVersions from "./usePantry.getVersions.ts" From 2cc7b3b1ef0c4cd4d6c94b3ccf9d70c422965e4a Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Mon, 30 Oct 2023 17:12:45 +0530 Subject: [PATCH 03/16] fix rewrite check --- lib/usePantry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index c815d354..2515e5ce 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -115,7 +115,7 @@ const getDistributable = async (pkg: Package) => { let tmp_stripComponents: number | undefined; if (isPlainObject(dist)) { tmp_stripComponents = flatmap(dist["strip-components"], coerceNumber); - if (Object.keys(dist.rewrite).length) { + if (dist.rewrite?.match) { raw_v = pkg.version.raw.replace( new RegExp(dist.rewrite["match"], "gi"), dist.rewrite["with"], From d01f74e980568da3d61fcf7db22832bb1874edc0 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Mon, 30 Oct 2023 17:14:53 +0530 Subject: [PATCH 04/16] add checking for dists --- lib/usePantry.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index 2515e5ce..f99f53ad 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -95,6 +95,7 @@ const getDistributable = async (pkg: Package) => { const yml = await hooks.usePantry().project(pkg).yaml(); let final_url = ""; let dists = yml.distributable; + if (!dists) dists = []; let stripComponents: number | undefined; if (!isArray(dists)) dists = [dists]; for (const dist of dists) { From 195352fab1975074ef3b3ccd10fac549fadb8b60 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Mon, 30 Oct 2023 17:20:01 +0530 Subject: [PATCH 05/16] add warning (for debug) --- lib/usePantry.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index f99f53ad..a8328851 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -157,6 +157,8 @@ const getDistributable = async (pkg: Package) => { final_url = urlstr; stripComponents = tmp_stripComponents; break; + } else { + console.warn(`brewkit: Could not fetch ${urlstr} [${rsp.status}]`) } } if (!final_url) return; From e26a4aee6ed5eff8330f8a9a96046a294258578a Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 01:57:41 +0530 Subject: [PATCH 06/16] fix query.ts --- libexec/query.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/query.ts b/libexec/query.ts index bc4bc424..2e52c7df 100755 --- a/libexec/query.ts +++ b/libexec/query.ts @@ -40,7 +40,7 @@ if (versions) { Deno.exit(0) } -const version = pkg.constraint.single() ?? panic() +const {version} = await usePantry().resolve(pkg) ?? panic() pkg = {project: pkg.project, version } if (src) { From cca3c56a7a5569e0ae787bb3a70d72e320b74177 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 01:59:09 +0530 Subject: [PATCH 07/16] fix extract.ts --- libexec/extract.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libexec/extract.ts b/libexec/extract.ts index c2d79245..06e95d84 100755 --- a/libexec/extract.ts +++ b/libexec/extract.ts @@ -19,8 +19,7 @@ const { flags: { outputDir, pkg: pkgname }, unknown } = parseFlags(Deno.args, { const pantry = usePantry() -let pkg: Package | PackageRequirement = utils.pkg.parse(pkgname) -pkg = { project: pkg.project, version: pkg.constraint.single() ?? panic() } +const pkg: Package | PackageRequirement = await usePantry().resolve(utils.pkg.parse(pkgname)) ?? panic const dstdir = Path.cwd().join(outputDir) From 691db75ceb5e9d7a8941877597fae65459acd568 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:08:28 +0530 Subject: [PATCH 08/16] apply requested changes --- lib/usePantry.ts | 59 +++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 41 deletions(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index a8328851..5e5f28f8 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -93,11 +93,7 @@ const getDistributable = async (pkg: Package) => { const moustaches = useMoustaches(); const yml = await hooks.usePantry().project(pkg).yaml(); - let final_url = ""; - let dists = yml.distributable; - if (!dists) dists = []; - let stripComponents: number | undefined; - if (!isArray(dists)) dists = [dists]; + const dists = isArray(yml.distributable) ? yml.distributable : [yml.distributable] for (const dist of dists) { //FIXME: Add check for Git dists as well if (dist.git) { @@ -106,65 +102,46 @@ const getDistributable = async (pkg: Package) => { ); return getGitDistribution({ pkg, ...dist }); } + if (dist.url?.startsWith("git")) { return getGitDistribution({ pkg, ...dist }); } + let urlstr = getRawDistributableURL(dist); - let raw_v = ""; - let matched = true; if (!urlstr) continue; - let tmp_stripComponents: number | undefined; + + const v = pkg.version + const stripComponents = flatmap(dist["strip-components"], coerceNumber) + if (isPlainObject(dist)) { - tmp_stripComponents = flatmap(dist["strip-components"], coerceNumber); if (dist.rewrite?.match) { - raw_v = pkg.version.raw.replace( + v.raw = v.raw.replace( new RegExp(dist.rewrite["match"], "gi"), dist.rewrite["with"], ); } + if (dist?.if) { - matched = new RegExp(dist.if).test(pkg.version.raw); + const matched = new RegExp(dist.if).test(pkg.version.raw); + if (!matched) continue } } - let v: SemVer; - if (raw_v) { - v = { - raw: raw_v, - major: pkg.version.major, - minor: pkg.version.minor, - patch: pkg.version.patch, - components: pkg.version.components, - prerelease: pkg.version.prerelease, - build: pkg.version.build, - eq: pkg.version.eq, - neq: pkg.version.neq, - gt: pkg.version.gt, - gte: pkg.version.gte, - lt: pkg.version.lt, - lte: pkg.version.lte, - compare: pkg.version.compare, - }; - } else { - v = pkg.version; - } + urlstr = moustaches.apply(urlstr, [ ...moustaches.tokenize.version(v), ...moustaches.tokenize.host(), ]); - if (!matched) continue; + const rsp = await fetch(urlstr, { method: "HEAD" }); + if (rsp.status == 200) { - final_url = urlstr; - stripComponents = tmp_stripComponents; - break; + const url = new URL(urlstr); + return { url, ref: undefined, stripComponents, type: "url" } } else { console.warn(`brewkit: Could not fetch ${urlstr} [${rsp.status}]`) } } - if (!final_url) return; - const url = new URL(final_url); - - return { url, ref: undefined, stripComponents, type: "url" } + return; } // deno-lint-ignore no-explicit-any @@ -178,4 +155,4 @@ async function filepath(project: string) { if (project == pkg.project) return pkg.path } throw new Error(`package.yml not found: ${project}`) -} + } From 714d984f28a06506f786fe8e503ed1e196a5148a Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:11:49 +0530 Subject: [PATCH 09/16] default value for dists --- lib/usePantry.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index 5e5f28f8..ef91bc61 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -94,6 +94,7 @@ const getDistributable = async (pkg: Package) => { const yml = await hooks.usePantry().project(pkg).yaml(); const dists = isArray(yml.distributable) ? yml.distributable : [yml.distributable] + if (!dists) = []; for (const dist of dists) { //FIXME: Add check for Git dists as well if (dist.git) { @@ -121,7 +122,7 @@ const getDistributable = async (pkg: Package) => { ); } - if (dist?.if) { + if (dist.if) { const matched = new RegExp(dist.if).test(pkg.version.raw); if (!matched) continue } From 169a7b2ff9c3b4f29c46a43a9dfebd0a9ca97c7d Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:13:20 +0530 Subject: [PATCH 10/16] Fix syntax error --- lib/usePantry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index ef91bc61..af7118d8 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -94,7 +94,7 @@ const getDistributable = async (pkg: Package) => { const yml = await hooks.usePantry().project(pkg).yaml(); const dists = isArray(yml.distributable) ? yml.distributable : [yml.distributable] - if (!dists) = []; + if (!dists) dists = []; for (const dist of dists) { //FIXME: Add check for Git dists as well if (dist.git) { From 5583ed3659f23735a0842c0544fae1ffcf38949c Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:15:09 +0530 Subject: [PATCH 11/16] Just return nothing if it doesn't exist --- lib/usePantry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index af7118d8..819436d6 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -94,7 +94,7 @@ const getDistributable = async (pkg: Package) => { const yml = await hooks.usePantry().project(pkg).yaml(); const dists = isArray(yml.distributable) ? yml.distributable : [yml.distributable] - if (!dists) dists = []; + if (!dists) return; for (const dist of dists) { //FIXME: Add check for Git dists as well if (dist.git) { From 96f4b840268099acd9334b56383f8f896268c7f5 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:17:07 +0530 Subject: [PATCH 12/16] Make inner v a writable variable --- lib/usePantry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index 819436d6..fbf5ab0b 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -111,7 +111,7 @@ const getDistributable = async (pkg: Package) => { let urlstr = getRawDistributableURL(dist); if (!urlstr) continue; - const v = pkg.version + let v = pkg.version const stripComponents = flatmap(dist["strip-components"], coerceNumber) if (isPlainObject(dist)) { From c72de24d9d46a4cd3c945a6d9dfd1eb92dc74911 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:20:14 +0530 Subject: [PATCH 13/16] Fix it --- lib/usePantry.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index fbf5ab0b..6fa96fa4 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -116,10 +116,26 @@ const getDistributable = async (pkg: Package) => { if (isPlainObject(dist)) { if (dist.rewrite?.match) { - v.raw = v.raw.replace( + const v_raw = v.raw.replace( new RegExp(dist.rewrite["match"], "gi"), dist.rewrite["with"], ); + v = { + raw: v_raw, + major: pkg.version.major, + minor: pkg.version.minor, + patch: pkg.version.patch, + components: pkg.version.components, + prerelease: pkg.version.prerelease, + build: pkg.version.build, + eq: pkg.version.eq, + neq: pkg.version.neq, + gt: pkg.version.gt, + gte: pkg.version.gte, + lt: pkg.version.lt, + lte: pkg.version.lte, + compare: pkg.version.compare, + } } if (dist.if) { From 48da581ff8cf37e3336550a2930aa61ee254fc03 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Tue, 31 Oct 2023 02:30:02 +0530 Subject: [PATCH 14/16] fix dist checking --- lib/usePantry.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/usePantry.ts b/lib/usePantry.ts index 6fa96fa4..be1e752f 100644 --- a/lib/usePantry.ts +++ b/lib/usePantry.ts @@ -94,8 +94,8 @@ const getDistributable = async (pkg: Package) => { const yml = await hooks.usePantry().project(pkg).yaml(); const dists = isArray(yml.distributable) ? yml.distributable : [yml.distributable] - if (!dists) return; for (const dist of dists) { + if (!dist) continue; //FIXME: Add check for Git dists as well if (dist.git) { console.warn( From 703a70a51fa44a0bcb3e7dc66e335102959e9627 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Fri, 17 Nov 2023 19:49:18 +0530 Subject: [PATCH 15/16] Apply requested changes --- libexec/query.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/query.ts b/libexec/query.ts index 2e52c7df..bc4bc424 100755 --- a/libexec/query.ts +++ b/libexec/query.ts @@ -40,7 +40,7 @@ if (versions) { Deno.exit(0) } -const {version} = await usePantry().resolve(pkg) ?? panic() +const version = pkg.constraint.single() ?? panic() pkg = {project: pkg.project, version } if (src) { From 231419dedc54ecd69b34c3c67d0c0cc5aabea334 Mon Sep 17 00:00:00 2001 From: Rajdeep Malakar Date: Sun, 19 Nov 2023 12:48:32 +0530 Subject: [PATCH 16/16] Update query.ts --- libexec/query.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libexec/query.ts b/libexec/query.ts index bc4bc424..2313dc6e 100755 --- a/libexec/query.ts +++ b/libexec/query.ts @@ -40,7 +40,10 @@ if (versions) { Deno.exit(0) } -const version = pkg.constraint.single() ?? panic() +//FIXME: There might be a better way of doing this. It retains the current behaviour for normal cases and fixes the version.raw for those which don't match +const pkg_version = pkg.constraint.single() ?? panic() +const pantry_ver = await usePantry().resolve(pkg) ?? panic() +const version = (pkg_version.raw == pantry_ver.version.raw) ? pkg_version : pantry_ver.version pkg = {project: pkg.project, version } if (src) {