Skip to content

Commit

Permalink
sdk tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bonez committed Nov 21, 2024
1 parent ed8a7ee commit d145bf6
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 70 deletions.
13 changes: 11 additions & 2 deletions core/startos/src/s9pk/v2/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ pub enum ImageSource {
Packed,
#[serde(rename_all = "camelCase")]
DockerBuild {
#[ts(optional)]
workdir: Option<PathBuf>,
#[ts(optional)]
dockerfile: Option<PathBuf>,
#[serde(skip_serializing_if = "Option::is_none")]
#[ts(optional)]
Expand All @@ -366,8 +368,15 @@ impl ImageSource {
pub fn ingredients(&self) -> Vec<PathBuf> {
match self {
Self::Packed => Vec::new(),
Self::DockerBuild { dockerfile, .. } => {
vec![dockerfile.clone().unwrap_or_else(|| "Dockerfile".into())]
Self::DockerBuild {
dockerfile,
workdir,
..
} => {
vec![workdir
.as_deref()
.unwrap_or(Path::new("."))
.join(dockerfile.as_deref().unwrap_or(Path::new("Dockerfile")))]
}
Self::DockerTag(_) => Vec::new(),
}
Expand Down
10 changes: 6 additions & 4 deletions core/startos/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ impl<'a> std::ops::DerefMut for ExtendedCommand<'a> {
}

impl<'a> Invoke<'a> for tokio::process::Command {
type Extended<'ext> = ExtendedCommand<'ext>
type Extended<'ext>
= ExtendedCommand<'ext>
where
Self: 'ext,
'ext: 'a;
Expand Down Expand Up @@ -162,7 +163,8 @@ impl<'a> Invoke<'a> for tokio::process::Command {
}

impl<'a> Invoke<'a> for ExtendedCommand<'a> {
type Extended<'ext> = &'ext mut ExtendedCommand<'ext>
type Extended<'ext>
= &'ext mut ExtendedCommand<'ext>
where
Self: 'ext,
'ext: 'a;
Expand Down Expand Up @@ -663,8 +665,8 @@ impl FromStr for PathOrUrl {
type Err = <PathBuf as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(url) = s.parse::<Url>() {
if url.scheme() == "file" {
Ok(Self::Path(url.path().parse()?))
if let Some(path) = s.strip_prefix("file://") {
Ok(Self::Path(path.parse()?))
} else {
Ok(Self::Url(url))
}
Expand Down
36 changes: 19 additions & 17 deletions sdk/base/lib/actions/input/builder/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,18 @@ import { DeepPartial } from "../../../types"

type AsRequired<T, Required extends boolean> = Required extends true
? T
: T | null | undefined
: T | null

const testForAsRequiredParser = once(
() => object({ required: literal(true) }).test,
)
function asRequiredParser<
Type,
Input,
Return extends
| Parser<unknown, Type>
| Parser<unknown, Type | null | undefined>,
Return extends Parser<unknown, Type> | Parser<unknown, Type | null>,
>(parser: Parser<unknown, Type>, input: Input): Return {
if (testForAsRequiredParser()(input)) return parser as any
return parser.optional() as any
return parser.nullable() as any
}

export type PartialValue<T> =
Expand Down Expand Up @@ -196,7 +194,7 @@ export class Value<Type, Store> {
}
>,
) {
return new Value<string | null | undefined, Store>(async (options) => {
return new Value<string | null, Store>(async (options) => {
const a = await getA(options)
return {
type: "text" as const,
Expand All @@ -213,7 +211,7 @@ export class Value<Type, Store> {
generate: a.generate ?? null,
...a,
}
}, string.optional())
}, string.nullable())
}
static textarea<Required extends boolean>(a: {
name: string
Expand Down Expand Up @@ -265,7 +263,7 @@ export class Value<Type, Store> {
}
>,
) {
return new Value<string | null | undefined, Store>(async (options) => {
return new Value<string | null, Store>(async (options) => {
const a = await getA(options)
return {
description: null,
Expand All @@ -278,7 +276,7 @@ export class Value<Type, Store> {
immutable: false,
...a,
}
}, string.optional())
}, string.nullable())
}
static number<Required extends boolean>(a: {
name: string
Expand Down Expand Up @@ -351,7 +349,7 @@ export class Value<Type, Store> {
}
>,
) {
return new Value<number | null | undefined, Store>(async (options) => {
return new Value<number | null, Store>(async (options) => {
const a = await getA(options)
return {
type: "number" as const,
Expand All @@ -366,7 +364,7 @@ export class Value<Type, Store> {
immutable: false,
...a,
}
}, number.optional())
}, number.nullable())
}
static color<Required extends boolean>(a: {
name: string
Expand Down Expand Up @@ -413,7 +411,7 @@ export class Value<Type, Store> {
}
>,
) {
return new Value<string | null | undefined, Store>(async (options) => {
return new Value<string | null, Store>(async (options) => {
const a = await getA(options)
return {
type: "color" as const,
Expand All @@ -423,7 +421,7 @@ export class Value<Type, Store> {
immutable: false,
...a,
}
}, string.optional())
}, string.nullable())
}
static datetime<Required extends boolean>(a: {
name: string
Expand Down Expand Up @@ -483,7 +481,7 @@ export class Value<Type, Store> {
}
>,
) {
return new Value<string | null | undefined, Store>(async (options) => {
return new Value<string | null, Store>(async (options) => {
const a = await getA(options)
return {
type: "datetime" as const,
Expand All @@ -496,7 +494,7 @@ export class Value<Type, Store> {
immutable: false,
...a,
}
}, string.optional())
}, string.nullable())
}
static select<Values extends Record<string, string>>(a: {
name: string
Expand Down Expand Up @@ -690,14 +688,14 @@ export class Value<Type, Store> {
// }
// >,
// ) {
// return new Value<FilePath | null | undefined, Store>(
// return new Value<FilePath | null, Store>(
// async (options) => ({
// type: "file" as const,
// description: null,
// warning: null,
// ...(await a(options)),
// }),
// object({ filePath: string }).optional(),
// object({ filePath: string }).nullable(),
// )
// }
static union<
Expand Down Expand Up @@ -822,6 +820,10 @@ export class Value<Type, Store> {
}, parser)
}

map<U>(fn: (value: Type) => U): Value<U, Store> {
return new Value(this.build, this.validator.map(fn))
}

/**
* Use this during the times that the input needs a more specific type.
* Used in types that the value/ variant/ list/ inputSpec is constructed somewhere else.
Expand Down
40 changes: 35 additions & 5 deletions sdk/base/lib/dependencies/setupDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
import * as T from "../types"
import { once } from "../util"

type DependencyType<Manifest extends T.SDKManifest> = {
[K in keyof Manifest["dependencies"]]: Omit<T.DependencyRequirement, "id">
}
export type RequiredDependenciesOf<Manifest extends T.SDKManifest> = {
[K in keyof Manifest["dependencies"]]: Manifest["dependencies"][K]["optional"] extends false
? K
: never
}[keyof Manifest["dependencies"]]
export type OptionalDependenciesOf<Manifest extends T.SDKManifest> = Exclude<
keyof Manifest["dependencies"],
RequiredDependenciesOf<Manifest>
>

type DependencyRequirement =
| {
kind: "running"
healthChecks: Array<T.HealthCheckId>
versionRange: string
}
| {
kind: "exists"
versionRange: string
}
type Matches<T, U> = T extends U ? (U extends T ? null : never) : never
const _checkType: Matches<
DependencyRequirement & { id: T.PackageId },
T.DependencyRequirement
> = null

export type CurrentDependenciesResult<Manifest extends T.SDKManifest> = {
[K in RequiredDependenciesOf<Manifest>]: DependencyRequirement
} & {
[K in OptionalDependenciesOf<Manifest>]?: DependencyRequirement
} & Record<string, DependencyRequirement>

export function setupDependencies<Manifest extends T.SDKManifest>(
fn: (options: { effects: T.Effects }) => Promise<DependencyType<Manifest>>,
fn: (options: {
effects: T.Effects
}) => Promise<CurrentDependenciesResult<Manifest>>,
): (options: { effects: T.Effects }) => Promise<null> {
const cell = { updater: async (_: { effects: T.Effects }) => null }
cell.updater = async (options: { effects: T.Effects }) => {
Expand All @@ -21,7 +51,7 @@ export function setupDependencies<Manifest extends T.SDKManifest>(
dependencies: Object.entries(dependencyType).map(
([id, { versionRange, ...x }, ,]) =>
({
id,
// id,
...x,
versionRange: versionRange.toString(),
}) as T.DependencyRequirement,
Expand Down
4 changes: 2 additions & 2 deletions sdk/base/lib/osBindings/ImageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export type ImageSource =
| "packed"
| {
dockerBuild: {
workdir: string | null
dockerfile: string | null
workdir?: string
dockerfile?: string
buildArgs?: { [key: string]: BuildArg }
}
}
Expand Down
5 changes: 5 additions & 0 deletions sdk/base/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { Effects } from "./Effects"
export { Effects }
export * from "./osBindings"
export { SDKManifest } from "./types/ManifestTypes"
export {
RequiredDependenciesOf as RequiredDependencies,
OptionalDependenciesOf as OptionalDependencies,
CurrentDependenciesResult,
} from "./dependencies/setupDependencies"

export type ExposedStorePaths = string[] & Affine<"ExposedStorePaths">
declare const HealthProof: unique symbol
Expand Down
17 changes: 13 additions & 4 deletions sdk/base/lib/types/ManifestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,19 @@ export type SDKManifest = {
}
}

export type SDKImageInputSpec = {
source: Exclude<ImageSource, "packed">
arch?: string[]
emulateMissingAs?: string | null
// this is hacky but idk a more elegant way
type ArchOptions = {
0: ["x86_64", "aarch64"]
1: ["aarch64", "x86_64"]
2: ["x86_64"]
3: ["aarch64"]
}
export type SDKImageInputSpec = {
[A in keyof ArchOptions]: {
source: Exclude<ImageSource, "packed">
arch?: ArchOptions[A]
emulateMissingAs?: ArchOptions[A][number] | null
}
}[keyof ArchOptions]

export type ManifestDependency = T.Manifest["dependencies"][string]
8 changes: 4 additions & 4 deletions sdk/base/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"isomorphic-fetch": "^3.0.0",
"lodash.merge": "^4.6.2",
"mime-types": "^2.1.35",
"ts-matches": "^6.0.0",
"ts-matches": "^6.1.0",
"yaml": "^2.2.2"
},
"prettier": {
Expand Down
1 change: 1 addition & 0 deletions sdk/package/lib/StartSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class StartSdk<Manifest extends T.SDKManifest, Store> {
}

return {
manifest: this.manifest,
...startSdkEffectWrapper,
action: {
run: actions.runAction,
Expand Down
4 changes: 3 additions & 1 deletion sdk/package/lib/manifest/setupManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export function buildManifest<
(images, [k, v]) => {
v.arch = v.arch || ["aarch64", "x86_64"]
if (v.emulateMissingAs === undefined)
v.emulateMissingAs = v.arch[0] || null
v.emulateMissingAs = (v.arch as string[]).includes("aarch64")
? "aarch64"
: v.arch[0] || null
images[k] = v as ImageConfig
return images
},
Expand Down
Loading

0 comments on commit d145bf6

Please sign in to comment.