From ed4e458e63257308b658f0184db2e24eebd6e9d6 Mon Sep 17 00:00:00 2001 From: Max Howell Date: Wed, 17 Jan 2024 13:10:11 -0500 Subject: [PATCH] Handle platform specific provides yml --- .github/scripts/gen-algolia-data.ts | 11 +++++------ .github/scripts/gen-pkgs-index.json.ts | 4 +++- .github/scripts/utils/get-provides.ts | 20 ++++++++++++++++++++ src/pkgx.dev/PackageListing.tsx | 25 ++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 .github/scripts/utils/get-provides.ts diff --git a/.github/scripts/gen-algolia-data.ts b/.github/scripts/gen-algolia-data.ts index b452ed3..2ac2391 100755 --- a/.github/scripts/gen-algolia-data.ts +++ b/.github/scripts/gen-algolia-data.ts @@ -1,8 +1,9 @@ #!/usr/bin/env -S pkgx deno run --allow-read=. --allow-net import * as yaml from "https://deno.land/std@0.204.0/yaml/mod.ts"; -import { isArray, isString } from "https://deno.land/x/is_what@v4.1.15/src/index.ts"; import get_pkg_name from "./utils/get-name.ts" +import get_provides from "./utils/get-provides.ts" +import { basename } from "node:path"; interface Package { project: string @@ -24,7 +25,8 @@ export async function getKettleRemoteMetadata() { } function get_name(yml: any, project: string) { - return get_pkg_name({ project, display_name: yml['display-name'], provides: yml['provides'] }) + const provides = get_provides(yml) + return get_pkg_name({ project, display_name: yml['display-name'], provides }) } @@ -37,11 +39,8 @@ for (const obj of rv as Package[]) { const txt = await Deno.readTextFileSync(yaml_path) const yml = await yaml.parse(txt) as Record - const node = yml['provides'] - const provides: string[] = isArray(node) ? node : isString(node) ? [node] : [] - obj.displayName = get_name(yaml_path, obj.project) - obj.programs = provides.map(x => x.slice(4)) + obj.programs = get_provides(yml).map(x => basename(x)) } catch (err) { console.warn(`::warning::${err.message}`) } diff --git a/.github/scripts/gen-pkgs-index.json.ts b/.github/scripts/gen-pkgs-index.json.ts index 3299884..df284fa 100755 --- a/.github/scripts/gen-pkgs-index.json.ts +++ b/.github/scripts/gen-pkgs-index.json.ts @@ -75,12 +75,14 @@ console.log(JSON.stringify(pkgs, null, 2)); ////////////////////////////////////////////////////// import { parse } from "https://deno.land/std@0.204.0/yaml/mod.ts"; +import get_provides from "./utils/get-provides.ts"; import get_pkg_name from "./utils/get-name.ts"; async function get_name(path: string, project: string): Promise { const txt = await Deno.readTextFileSync(path) const yml = await parse(txt) as Record - return get_pkg_name({ project, display_name: yml['display-name'], provides: yml['provides'] }) + const provides = get_provides(yml) + return get_pkg_name({ project, display_name: yml['display-name'], provides }) } import { parse_pkgs_node } from "https://deno.land/x/libpkgx@v0.15.1/src/hooks/usePantry.ts" diff --git a/.github/scripts/utils/get-provides.ts b/.github/scripts/utils/get-provides.ts new file mode 100644 index 0000000..71821b4 --- /dev/null +++ b/.github/scripts/utils/get-provides.ts @@ -0,0 +1,20 @@ +import { isString, isPlainObject } from "https://deno.land/x/is_what@v4.1.15/src/index.ts"; + +export default function get_provides(yml: any): string[] { + let provides = yml['provides'] + if (isString(provides)) { + return [provides] + } + if (isPlainObject(provides)) { + const { darwin, linux, windows } = provides + provides = [] + const set = new Set() + for (const x of [darwin, linux, windows].flatMap(x => x)) { + if (!set.has(x)) { + provides.push(x) + set.add(x) + } + } + } + return provides ?? [] +} diff --git a/src/pkgx.dev/PackageListing.tsx b/src/pkgx.dev/PackageListing.tsx index fb7db8e..6311599 100644 --- a/src/pkgx.dev/PackageListing.tsx +++ b/src/pkgx.dev/PackageListing.tsx @@ -3,7 +3,7 @@ import { S3Client, ListObjectsV2Command, _Object } from '@aws-sdk/client-s3'; import { useParams, Link as RouterLink } from 'react-router-dom'; import ArrowOutwardIcon from '@mui/icons-material/CallMade'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; -import { isArray, isPlainObject } from 'is-what'; +import { isArray, isPlainObject, isString } from 'is-what'; import Terminal from '../components/Terminal'; import get_pkg_name from '../utils/pkg-name'; import { useAsync } from 'react-use'; @@ -190,13 +190,13 @@ function Package({ project, dirs }: { project: string, dirs: string[] }) { function programs() { - const provides: string[] = value?.provides ?? [] + const provides: string[] = get_provides(value) if (!isArray(provides)) { return Unexpected error } else if (provides.length) { return
    {provides.map((program, i) =>
  • - {program.replace(/^bin\//g, '')} + {program.replace(/^s?bin\//g, '')}
  • )}
} else { @@ -300,3 +300,22 @@ function Versions({ project }: { project: string }) { } } + +function get_provides(yml: any): string[] { + let provides = yml['provides'] + if (isString(provides)) { + return [provides] + } + if (isPlainObject(provides)) { + const { darwin, linux, windows } = provides + provides = [] + const set = new Set() + for (const x of [darwin, linux, windows].flatMap(x => x)) { + if (x && !set.has(x)) { + provides.push(x) + set.add(x) + } + } + } + return provides ?? [] +}