Skip to content

Commit

Permalink
Handle platform specific provides yml
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Jan 17, 2024
1 parent 9a20fd7 commit ed4e458
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
11 changes: 5 additions & 6 deletions .github/scripts/gen-algolia-data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env -S pkgx deno run --allow-read=. --allow-net

import * as yaml from "https://deno.land/[email protected]/yaml/mod.ts";
import { isArray, isString } from "https://deno.land/x/[email protected]/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
Expand All @@ -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 })
}


Expand All @@ -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<string, any>

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}`)
}
Expand Down
4 changes: 3 additions & 1 deletion .github/scripts/gen-pkgs-index.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ console.log(JSON.stringify(pkgs, null, 2));

//////////////////////////////////////////////////////
import { parse } from "https://deno.land/[email protected]/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<string | undefined> {
const txt = await Deno.readTextFileSync(path)
const yml = await parse(txt) as Record<string, any>
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/[email protected]/src/hooks/usePantry.ts"
Expand Down
20 changes: 20 additions & 0 deletions .github/scripts/utils/get-provides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isString, isPlainObject } from "https://deno.land/x/[email protected]/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 ?? []
}
25 changes: 22 additions & 3 deletions src/pkgx.dev/PackageListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -190,13 +190,13 @@ function Package({ project, dirs }: { project: string, dirs: string[] }) {
</Stack>

function programs() {
const provides: string[] = value?.provides ?? []
const provides: string[] = get_provides(value)
if (!isArray(provides)) {
return <Alert severity="error">Unexpected error</Alert>
} else if (provides.length) {
return <ul>
{provides.map((program, i) => <li key={i}>
<code>{program.replace(/^bin\//g, '')}</code>
<code>{program.replace(/^s?bin\//g, '')}</code>
</li>)}
</ul>
} else {
Expand Down Expand Up @@ -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 ?? []
}

0 comments on commit ed4e458

Please sign in to comment.