-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtoolchain.ts
More file actions
203 lines (192 loc) · 7.64 KB
/
Copy pathtoolchain.ts
File metadata and controls
203 lines (192 loc) · 7.64 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
203
import { exec } from "node:child_process";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { arch, homedir, platform } from "node:os";
import { runShell as runPiped } from "./process.js";
/** Async exec — resolves with stdout, rejects on non-zero exit. */
function run(cmd: string, opts?: { shell?: string }): Promise<string> {
return new Promise((resolve, reject) => {
exec(cmd, { shell: opts?.shell ?? "bash" }, (err, stdout) => {
if (err) reject(err);
else resolve(stdout);
});
});
}
/**
* Prepend `dir` to `process.env.PATH` if not already present. Lets a step that
* just installed a binary expose it to the rest of `dot init` without waiting
* for a shell restart.
*/
export function prependPath(dir: string): void {
const segments = (process.env.PATH ?? "").split(":").filter(Boolean);
if (segments.includes(dir)) return;
process.env.PATH = process.env.PATH ? `${dir}:${process.env.PATH}` : dir;
}
export async function commandExists(cmd: string): Promise<boolean> {
if (!/^[a-zA-Z0-9_-]+$/.test(cmd)) {
throw new Error(`Invalid command name: ${cmd}`);
}
try {
await run(`command -v ${cmd}`);
return true;
} catch {
return false;
}
}
async function hasRustNightly(): Promise<boolean> {
try {
const out = await run("rustup toolchain list");
return out.includes("nightly");
} catch {
return false;
}
}
async function hasRustSrc(): Promise<boolean> {
try {
const out = await run("rustup component list --toolchain nightly");
return out.includes("rust-src (installed)");
} catch {
return false;
}
}
async function hasCdm(): Promise<boolean> {
return (await commandExists("cdm")) && (await commandExists("cargo-pvm-contract"));
}
async function hasFoundryPolkadot(): Promise<boolean> {
// Stock `forge` lacks `--resolc`, which we need for PolkaVM codegen;
// `foundryup-polkadot` wires in the polkadot fork.
const home = homedir();
const foundryupPolkadot = resolve(home, ".foundry/bin/foundryup-polkadot");
return (await commandExists("forge")) && existsSync(foundryupPolkadot);
}
function isIpfsInitialized(): boolean {
return existsSync(resolve(homedir(), ".ipfs"));
}
export async function isGhAuthenticated(): Promise<boolean> {
try {
await run("gh auth status");
return true;
} catch {
return false;
}
}
export interface ToolStep {
name: string;
check: () => Promise<boolean>;
install: (onData?: (line: string) => void) => Promise<void>;
manualHint?: string;
}
export const TOOL_STEPS: ToolStep[] = [
{
name: "rustup",
check: () => commandExists("rustup"),
install: async (onData) => {
await runPiped(
'curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y',
onData,
);
// rustup-init writes binaries to $CARGO_HOME/bin (default ~/.cargo/bin)
// and updates shell rc files, but those edits don't reach the running
// dot process. Prepend the bin dir so the very next step in this same
// `dot init` can resolve `rustup`.
prependPath(resolve(process.env.CARGO_HOME ?? `${homedir()}/.cargo`, "bin"));
},
manualHint: "https://rustup.rs",
},
{
name: "Rust nightly",
check: () => hasRustNightly(),
install: (onData) => runPiped("rustup toolchain install nightly", onData),
},
{
name: "rust-src",
check: () => hasRustSrc(),
install: (onData) => runPiped("rustup component add rust-src --toolchain nightly", onData),
},
{
name: "cdm & cargo-pvm-contract",
check: () => hasCdm(),
install: (onData) =>
runPiped(
"curl -fsSL https://raw.githubusercontent.com/paritytech/contract-dependency-manager/main/install.sh | bash",
onData,
),
manualHint:
"curl -fsSL https://raw.githubusercontent.com/paritytech/contract-dependency-manager/main/install.sh | bash",
},
{
name: "IPFS",
check: async () => (await commandExists("ipfs")) && isIpfsInitialized(),
install: async (onData) => {
if (!(await commandExists("ipfs"))) {
if (platform() === "darwin" && (await commandExists("brew"))) {
await runPiped("brew install ipfs", onData);
} else {
const os = platform() === "darwin" ? "darwin" : "linux";
const cpu = arch() === "arm64" ? "arm64" : "amd64";
await runPiped(
`curl -fsSL https://dist.ipfs.tech/kubo/v0.33.2/kubo_v0.33.2_${os}-${cpu}.tar.gz | tar xz && cd kubo && sudo bash install.sh && cd .. && rm -rf kubo`,
onData,
);
}
}
if (!isIpfsInitialized()) {
await runPiped("ipfs init", onData);
}
},
manualHint: "https://docs.ipfs.tech/install/ then run: ipfs init",
},
{
name: "foundry (polkadot)",
check: () => hasFoundryPolkadot(),
install: (onData) =>
runPiped(
"curl -L https://raw.githubusercontent.com/paritytech/foundry-polkadot/refs/heads/master/foundryup/install | bash && $HOME/.foundry/bin/foundryup-polkadot",
onData,
),
manualHint:
"curl -L https://raw.githubusercontent.com/paritytech/foundry-polkadot/refs/heads/master/foundryup/install | bash && ~/.foundry/bin/foundryup-polkadot",
},
{
name: "git",
check: () => commandExists("git"),
install: async (onData) => {
if (platform() === "darwin" && (await commandExists("brew"))) {
await runPiped("brew install git", onData);
} else if (platform() === "linux") {
await runPiped("sudo apt update && sudo apt install -y git", onData);
} else {
throw new Error(
"Cannot install git automatically on this platform — install manually.",
);
}
},
manualHint: "https://git-scm.com/downloads",
},
{
name: "GitHub CLI",
check: () => commandExists("gh"),
install: async (onData) => {
if (await commandExists("brew")) {
await runPiped("brew install gh", onData);
} else {
// GH install instructions: https://github.com/cli/cli/blob/trunk/docs/install_linux.md
await runPiped(
[
"(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y))",
"sudo mkdir -p -m 755 /etc/apt/keyrings",
"out=$(mktemp)",
"wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg",
"cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null",
"sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg",
`echo 'deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main' | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null`,
"sudo apt update",
"sudo apt install gh -y",
].join(" && "),
onData,
);
}
},
manualHint: "https://cli.github.com",
},
];