forked from denoland/wasmbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanifest.ts
More file actions
202 lines (183 loc) · 5.28 KB
/
manifest.ts
File metadata and controls
202 lines (183 loc) · 5.28 KB
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
195
196
197
198
199
200
201
202
// Copyright 2018-2025 the Deno authors. MIT license.
import { Sha1 } from "./utils/sha1.ts";
import { expandGlob } from "@std/fs/expand-glob";
import { Path } from "@david/path";
export interface CargoMetadata {
packages: CargoPackageMetadata[];
/** Identifiers in the `packages` array of the workspace members. */
"workspace_members": string[];
/** The absolute workspace root directory path. */
"workspace_root": string;
/** Path to the target directory. */
"target_directory": string;
resolve: {
nodes: {
id: string;
dependencies: string[];
}[];
};
}
export interface CargoPackageMetadata {
id: string;
name: string;
version: string;
dependencies: CargoDependencyMetadata[];
targets?: CargoPackageTarget[];
/** Path to Cargo.toml */
"manifest_path": string;
}
export interface CargoDependencyMetadata {
name: string;
/** Version requrement (ex. ^0.1.0) */
req: string;
}
export interface CargoPackageTarget {
kind: string[];
name: string;
crate_types?: string[];
}
export async function getCargoWorkspace(
directory: Path,
cargoFlags: string[],
) {
const p = new Deno.Command("cargo", {
cwd: directory.toString(),
args: ["metadata", "--format-version", "1", ...cargoFlags],
stdout: "piped",
stderr: "piped",
});
const output = await p.output();
if (!output.success) {
const stderr = new TextDecoder().decode(output.stderr).trim();
throw new Error(
`Error retrieving cargo metadata.\n${stderr}`,
);
}
const result = new TextDecoder().decode(output.stdout);
return new CargoWorkspace(JSON.parse(result!) as CargoMetadata);
}
export class CargoWorkspace {
constructor(public readonly metadata: CargoMetadata) {
}
getWasmCrate(filterName?: string | undefined) {
const wasmCrates = this.getWasmCrates();
if (filterName) {
const wasmCrate = wasmCrates.find((c) => c.name === filterName);
if (wasmCrate == null) {
const pkg = this.metadata.packages.find((p) => p.name === filterName);
if (pkg == null) {
throw new Error(`Could not find crate with name '${filterName}'.`);
} else {
throw new Error(`Crate ${filterName} was not a cdylib crate.`);
}
}
return wasmCrate;
}
if (wasmCrates.length === 0) {
throw new Error("Could not find a cdylib crate in the workspace.");
} else if (wasmCrates.length > 1) {
throw new Error(
"There were multiple cdylib crates in the repo. " +
"Please select one by providing the '-p <crate-name>' cli flag.\n\n" +
wasmCrates.map((p) => ` * ${p.name}`).join("\n"),
);
} else {
return wasmCrates[0];
}
}
getWasmCrates() {
const crates: WasmCrate[] = [];
for (const pkg of this.getWorkspacePackages()) {
const wasmLibName = getWasmLibName(pkg);
if (wasmLibName != null) {
crates.push(
new WasmCrate({
metadata: this.metadata,
pkg,
libName: wasmLibName,
}),
);
}
}
return crates;
}
getWorkspacePackages() {
const pkgs: CargoPackageMetadata[] = [];
for (const memberId of this.metadata.workspace_members) {
const pkg = this.metadata.packages.find((pkg) => pkg.id === memberId);
if (!pkg) {
throw new Error(`Could not find package with id ${memberId}`);
}
pkgs.push(pkg);
}
return pkgs;
}
}
export class WasmCrate {
#metadata: CargoMetadata;
#pkg: CargoPackageMetadata;
libName: string;
constructor(opts: {
metadata: CargoMetadata;
pkg: CargoPackageMetadata;
libName: string;
}) {
this.#pkg = opts.pkg;
this.#metadata = opts.metadata;
this.libName = opts.libName;
}
get name() {
return this.#pkg.name;
}
getDependencyVersion(name: string) {
const node = this.#metadata.resolve.nodes
.find((n) => n.id === this.#pkg.id);
for (const depId of node?.dependencies ?? []) {
const pkg = this.#metadata.packages.find((pkg) => pkg.id === depId);
if (pkg?.name === name) {
return pkg.version;
}
}
return undefined;
}
get rootFolder() {
return new Path(this.#pkg.manifest_path).parentOrThrow();
}
async getSourcesHash() {
// simple for now...
const paths = await this.#getSourcePaths();
paths.sort();
const hasher = new Sha1();
for (const path of paths) {
const fileText = path.readTextSync();
// standardize file paths so this is not subject to
// however git is configured to checkout files
hasher.update(fileText.replace(/\r?\n/g, "\n"));
}
return hasher.hex();
}
async #getSourcePaths() {
const paths = [];
for await (
const entry of expandGlob("**/{*.rs,Cargo.toml}", {
root: this.rootFolder.toString(),
exclude: ["./target"],
})
) {
if (entry.isFile) {
paths.push(new Path(entry.path));
}
}
return paths;
}
}
function getWasmLibName(pkg: CargoPackageMetadata) {
// [lib]
// name = "deno_wasm"
// crate-type = ["cdylib"]
const wasmlib = pkg.targets?.find((p) =>
p.kind.includes("cdylib") && p.crate_types?.includes("cdylib")
);
// Hyphens are not allowed in crate names https://doc.rust-lang.org/reference/items/extern-crates.html
return wasmlib?.name?.replaceAll("-", "_");
}