Repository navigation
Expand file tree
/
Copy pathsetup.ts
More file actions
117 lines (105 loc) · 4.82 KB
/
Copy pathsetup.ts
File metadata and controls
117 lines (105 loc) · 4.82 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
// What catpad needs on the machine, and getting it there. It needs two
// things that are not catpad: Lean with Mathlib, which checks the Lean, and
// tectonic with poppler's pdftocairo, which draw the diagrams. They come as
// one kit (kit.ts), at versions catpad pins (kit.json), inside the download.
// On its first start the app unpacks the kit into its own folder. Nothing is
// asked of the machine: no elan, no Homebrew, no terminal, no network.
//
// Where each thing is taken from, in order:
//
// Lean CATPAD_LEAN_DIR, when it is set: a Lake project with Mathlib
// built, run by the `lake` on PATH. For those who keep their own.
// Then the kit. Then, run from source with `lake` on PATH and no
// kit, the Lake project in ./lean, as run.sh sets it up.
// draw the kit, or `tectonic` and `pdftocairo` on PATH.
//
// bwrap Linux only, and only recommended: bubblewrap keeps Lean's #eval
// away from your files. It needs sudo, so the page shows the command,
// and catpad works without it.
//
// The app installs the kit by itself on its first start (server.ts,
// autoSetup); run from source, the page offers an Install button.
import { existsSync } from "fs";
import { join } from "path";
import { findKit, installKit, pins, type Kit } from "./kit";
import { IN_APP, ROOT } from "./paths";
export interface Requirement {
id: "kit" | "bwrap";
name: string;
why: string;
ok: boolean;
// Not having it is fine: catpad works, with less.
optional?: boolean;
// catpad can install it by itself; otherwise `manual` is a command for the user's terminal.
auto?: boolean;
manual?: string;
}
const has = (cmd: string) => Bun.which(cmd, { PATH: process.env.PATH });
// Where Lean and Mathlib come from: the kit, or a Lake project.
export function leanSource(): { kit: Kit } | { project: string } | null {
if (process.env.CATPAD_LEAN_DIR) return { project: process.env.CATPAD_LEAN_DIR };
const kit = findKit();
if (kit) return { kit };
if (!IN_APP && has("lake")) return { project: join(ROOT, "lean") };
return null;
}
// The programs that draw a diagram, and what they need around them.
export interface DrawTools {
tectonic: string;
pdftocairo: string;
env: Record<string, string>;
// The TeX files are in the kit: tectonic is told not to look for newer ones.
cached: boolean;
}
export function drawTools(): DrawTools | null {
const kit = findKit();
if (kit) return { tectonic: kit.tectonic, pdftocairo: kit.pdftocairo, env: kit.drawEnv, cached: true };
const tectonic = has("tectonic"), pdftocairo = has("pdftocairo");
return tectonic && pdftocairo ? { tectonic, pdftocairo, env: {}, cached: false } : null;
}
function linuxInstall(pkg: string): string {
if (has("apt-get")) return `sudo apt-get install -y ${pkg}`;
if (has("dnf")) return `sudo dnf install -y ${pkg}`;
if (has("pacman")) return `sudo pacman -S --needed ${pkg}`;
if (has("zypper")) return `sudo zypper install -y ${pkg}`;
return `install ${pkg} with your distribution's package manager`;
}
export function requirements(): Requirement[] {
const linux = process.platform === "linux";
return [
{
id: "kit", name: "Lean, Mathlib and the diagram tools",
why: `Lean ${pins.lean.version} and Mathlib ${pins.mathlib.rev} check the Lean in the pad; tectonic ${pins.tectonic.version} and poppler ${pins.poppler.version} draw the diagrams`,
ok: !!leanSource() && !!drawTools(), auto: true,
},
...(linux ? [{
id: "bwrap" as const, name: "bubblewrap", why: "recommended: keeps Lean's #eval away from your files and the network",
ok: ["/usr/bin/bwrap", "/bin/bwrap"].some((p) => existsSync(p)),
optional: true, manual: linuxInstall("bubblewrap"),
}] : []),
];
}
// What can be installed and is missing, in the order to install it.
export const missing = () => requirements().filter((r) => !r.ok && r.auto && !r.optional).map((r) => r.id);
// ---------- installing ----------
type Log = (line: string) => void;
const running = new Set<string>();
// Installs one requirement, line by line to `log`. Returns why it failed, or null.
export async function install(id: string, log: Log): Promise<string | null> {
const r = requirements().find((x) => x.id === id);
if (!r) return "catpad does not know that tool.";
if (r.ok) return null;
if (!r.auto) return `catpad cannot install ${r.name} by itself. In a terminal: ${r.manual}`;
if (running.has(id)) return `${r.name} is already being installed.`;
running.add(id);
try {
if (id === "kit") await installKit(log);
if (!requirements().find((x) => x.id === id)!.ok) return `The kit was unpacked, and ${r.name} are still not found.`;
return null;
} catch (e: any) {
return `Setting up ${r.name} failed: ${e?.message ?? e}`;
} finally {
running.delete(id);
}
}
export const installing = () => [...running][0] ?? null;