-
Notifications
You must be signed in to change notification settings - Fork 1
/
cargobi.ts
194 lines (182 loc) · 5.68 KB
/
cargobi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
$,
ALL_ARCH,
ALL_OS,
defaultLatestStable,
depExecShimPath,
DownloadArgs,
type InstallArgs,
InstallConfigFat,
type InstallConfigSimple,
type ListAllArgs,
logger,
osXarch,
pathsWithDepArts,
PortBase,
std_fs,
std_path,
thinInstallConfig,
zod,
} from "../port.ts";
import * as std_ports from "../modules/ports/std.ts";
import {
ghConfValidator,
GithubReleasesInstConf,
readGhVars,
} from "../modules/ports/ghrel.ts";
import rust, { RustInstallConf } from "./rust.ts";
const manifest = {
ty: "denoWorker@v1" as const,
name: "cargobi_cratesio",
version: "0.1.0",
moduleSpecifier: import.meta.url,
buildDeps: [std_ports.cbin_ghrel, std_ports.rust_rustup],
// FIXME: we can't know crate platform support at this point
platforms: osXarch([...ALL_OS], [...ALL_ARCH]),
};
const confValidator = zod.object({
crateName: zod.string().regex(/[a-z0-9._-]*/),
profile: zod.string().regex(/[a-zA-Z_-]+/).nullish(),
noDefaultFeatures: zod.boolean().nullish(),
features: zod.string().regex(/[a-zA-Z_-]+/).array().nullish(),
locked: zod.boolean().nullish(),
target: zod.string()
.regex(/^[^-\s]+(-[^-\s]+){1,}?$/).nullish(),
// TODO: expose more cargo install flags
}).passthrough();
export type CargobiInstallConf =
& InstallConfigSimple
& GithubReleasesInstConf
& { rustConfOverride?: RustInstallConf }
& zod.input<typeof confValidator>;
export default function conf(config: CargobiInstallConf) {
const { rustConfOverride, ...thisConf } = config;
const out: InstallConfigFat = {
...readGhVars(),
...confValidator.parse(thisConf),
port: manifest,
};
if (rustConfOverride) {
out.buildDepConfigs = {
[std_ports.rust_rustup.name]: thinInstallConfig(rust({
...rustConfOverride,
})),
};
}
return out;
}
export class Port extends PortBase {
async listAll(args: ListAllArgs) {
const conf = confValidator.parse(args.config);
// https://doc.rust-lang.org/cargo/reference/registry-index.html#index-files
const lowerCName = conf.crateName.toLowerCase();
let indexPath;
if (lowerCName.length == 1) {
indexPath = `1/${lowerCName}`;
} else if (lowerCName.length == 2) {
indexPath = `2/${lowerCName}`;
} else if (lowerCName.length == 3) {
indexPath = `3/${conf.crateName[0]}/${lowerCName}`;
} else {
indexPath = `${conf.crateName.slice(0, 2)}/${
conf.crateName.slice(2, 4)
}/${lowerCName}`;
}
const metadataText = await $.request(
`https://index.crates.io/${indexPath}`,
).text();
const versions = metadataText
.split("\n")
.filter((str) => str.length > 0)
.map((str) =>
JSON.parse(str) as {
vers: string;
}
);
return versions.map((ver) => ver.vers);
}
override latestStable(args: ListAllArgs): Promise<string> {
return defaultLatestStable(this, args);
}
override async download(args: DownloadArgs) {
const conf = confValidator.parse(args.config);
const fileName = conf.crateName;
if (await std_fs.exists(std_path.resolve(args.downloadPath, fileName))) {
logger().debug(
`file ${fileName} already downloaded, skipping whole download`,
);
return;
}
const ghConf = ghConfValidator.parse(args.config);
const target = conf.target ? `--target ${conf.target}` : "";
const profile = conf.profile ? `--profile ${conf.profile}` : "";
const noDefaultFeatures = conf.noDefaultFeatures
? "--no-default-features"
: "";
const features = conf.features ? `--features ${conf.features.join()}` : "";
const locked = conf.locked ? `--locked` : "";
const cargoBinstall = () => {
return $.raw`${
depExecShimPath(std_ports.cbin_ghrel, "cargo-binstall", args.depArts)
} ${conf.crateName} --version ${args.installVersion} --disable-strategies compile --root ${args.tmpDirPath} --no-confirm --no-track ${
[
target,
locked,
].filter((str) => str.length > 0)
}`.env(
{
// cargo-binstall might want to access cargo
...pathsWithDepArts(args.depArts, args.platform.os),
...ghConf.ghToken ? { GITHUB_TOKEN: ghConf.ghToken } : {},
},
).noThrow(true);
};
const cargoInstall = () => {
return $.raw`${
depExecShimPath(std_ports.rust_rustup, "cargo", args.depArts)
} install ${conf.crateName} --version ${args.installVersion} --root ${args.tmpDirPath} --no-track ${
[
target,
noDefaultFeatures,
features,
locked,
profile,
].filter((str) => str.length > 0)
}`.env(
{
// cargo will need to access rustc
...pathsWithDepArts(args.depArts, args.platform.os),
...ghConf.ghToken ? { GITHUB_TOKEN: ghConf.ghToken } : {},
},
);
};
// if any paramaters unsupported by cargo binstall are present
if ([profile, noDefaultFeatures, features].some((str) => str.length > 0)) {
// directly go to cargo install
await cargoInstall();
} else {
const res = await cargoBinstall();
// code 94 implies cargo binstall tried to fall back
// to cargo install
if (res.code == 94) {
await cargoInstall();
} else if (res.code != 0) {
throw new Error(`error ${res.code} on cargo-binstall\n${res.combined}`);
}
}
await std_fs.move(
args.tmpDirPath,
args.downloadPath,
);
}
override async install(args: InstallArgs) {
const installPath = $.path(args.installPath);
if (await installPath.exists()) {
await installPath.remove({ recursive: true });
}
await std_fs.copy(
args.downloadPath,
args.installPath,
);
}
}