-
Notifications
You must be signed in to change notification settings - Fork 1
feat: toolchain management #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| 'use client' | ||
|
|
||
| import { | ||
| EXPECTED_TOOLCHAIN_ID_RE, | ||
| LEAN_BETA_VERSION_RE, | ||
| LEAN_NIGHTLY_VERSION_RE, | ||
| LEAN_STABLE_VERSION_RE, | ||
| } from '@leanprover/workbench-shared' | ||
| import { useRouter } from 'next/navigation' | ||
| import { use, useState } from 'react' | ||
| import z from 'zod' | ||
|
|
||
| import { doElanInstall, uninstallToolchainVersion } from '@/app/admin/actions' | ||
| import CatchySuspense from '@/app/components/CatchySuspense' | ||
| import TrackedCommandForm from '@/app/components/TrackedCommandForm' | ||
| import { useServerAction, useThrowingSWR } from '@/lib/client/util' | ||
|
|
||
| interface ToolchainManagementProps { | ||
| installedToolchainsPromise: Promise<string[]> | ||
| } | ||
|
|
||
| export function ToolchainManagement(props: ToolchainManagementProps) { | ||
| const router = useRouter() | ||
| return ( | ||
| <section> | ||
| <h2>Lean Toolchains</h2> | ||
| <CatchySuspense loading={<p>Loading installed toolchains…</p>}> | ||
| <ToolchainManagementList {...props} /> | ||
| </CatchySuspense> | ||
| <TrackedCommandForm | ||
| streamCommandKey='elan' | ||
| trackedCommandAction={doElanInstall} | ||
| title='+ New Toolchain' | ||
| successAction={() => router.refresh()} | ||
| > | ||
| <CatchySuspense loading={<p>Loading available toolchains…</p>}> | ||
| <NewToolchainForm /> | ||
| </CatchySuspense> | ||
| </TrackedCommandForm> | ||
| </section> | ||
| ) | ||
| } | ||
|
|
||
| function ToolchainManagementList(props: ToolchainManagementProps) { | ||
| const installedToolchains = use(props.installedToolchainsPromise) | ||
| if (installedToolchains.length === 0) return <p className='empty'>No installed toolchains.</p> | ||
| return ( | ||
| <ul className='project-list'> | ||
| {installedToolchains.map(toolchain => ( | ||
| <ToolchainRow key={toolchain} toolchain={toolchain} /> | ||
| ))} | ||
| </ul> | ||
| ) | ||
| } | ||
|
|
||
| function ToolchainRow(props: { toolchain: string }) { | ||
| const router = useRouter() | ||
| const [confirm, setConfirm] = useState(false) | ||
| const [error, action, pending] = useServerAction(uninstallToolchainVersion, () => { | ||
| router.refresh() | ||
| }) | ||
| return ( | ||
| <li> | ||
| <form className='simple-action-form' action={action}> | ||
| <div style={{ gridArea: 'name' }}>{props.toolchain}</div> | ||
| <input type='hidden' name='toolchain' value={props.toolchain} /> | ||
| {EXPECTED_TOOLCHAIN_ID_RE.test(props.toolchain) /* prevent uninstall of weird-enough-named toolchains */ && ( | ||
| <div className='actions' style={{ gridArea: 'actions' }}> | ||
| {!confirm && ( | ||
| <button type='button' onClick={() => setConfirm(true)}> | ||
| Remove Toolchain | ||
| </button> | ||
| )} | ||
| {confirm && ( | ||
| <> | ||
| <button className='delete' disabled={pending} type='submit'> | ||
| Confirm Removing Toolchain | ||
| </button> | ||
| <button disabled={pending} type='button' onClick={() => setConfirm(false)}> | ||
| Cancel | ||
| </button> | ||
| </> | ||
| )} | ||
| </div> | ||
| )} | ||
| <div style={{ gridArea: 'error', color: '#f00' }}>{error}</div> | ||
| </form> | ||
| </li> | ||
| ) | ||
| } | ||
|
|
||
| const zLeanRelease = z.object({ name: z.string(), created_at: z.iso.datetime() }) | ||
| const zLeanReleases = z.object({ | ||
| version: z.literal('1'), | ||
| stable: z.array(zLeanRelease.transform(tc => ({ type: 'stable' as const, ...tc }))), | ||
| beta: z.array(zLeanRelease.transform(tc => ({ type: 'beta' as const, ...tc }))), | ||
| nightly: z.array(zLeanRelease.transform(tc => ({ type: 'nightly' as const, ...tc }))), | ||
| }) | ||
|
|
||
| function NewToolchainForm() { | ||
| const { data: toolchainsAvailable } = useThrowingSWR( | ||
| 'release.llo', | ||
| async () => { | ||
| const res = await fetch('https://release.lean-lang.org') | ||
| if (!res.ok) throw new Error(`release.lean-lang.org returned error (${res.status})`) | ||
| return zLeanReleases.parse(await res.json()) | ||
| }, | ||
| { suspense: true, revalidateIfStale: false, revalidateOnFocus: false, revalidateOnReconnect: false }, | ||
| ) | ||
|
|
||
| const [stable, setStable] = useState(true) | ||
| const [beta, setBeta] = useState(true) | ||
| const [nightly, setNightly] = useState(false) | ||
| const count = (stable ? 1 : 0) + (beta ? 1 : 0) + (nightly ? 1 : 0) | ||
|
|
||
| const all = [ | ||
| stable ? toolchainsAvailable.stable.filter(tc => LEAN_STABLE_VERSION_RE.test(tc.name)) : [], | ||
| beta ? toolchainsAvailable.beta.filter(tc => LEAN_BETA_VERSION_RE.test(tc.name)) : [], | ||
| nightly ? toolchainsAvailable.nightly.filter(tc => LEAN_NIGHTLY_VERSION_RE.test(tc.name)) : [], | ||
| ] | ||
| .flat() | ||
| .toSorted((a, b) => (a.created_at > b.created_at ? -1 : a.created_at < b.created_at ? 1 : 0)) | ||
|
|
||
| return ( | ||
| <> | ||
| <div className='check-seq'> | ||
| <label> | ||
| <input | ||
| id='tc-stable' | ||
| type='checkbox' | ||
| checked={stable} | ||
| disabled={stable && count === 1} | ||
| onChange={() => setStable(s => !s)} | ||
| /> | ||
| Stable | ||
| </label> | ||
| <label> | ||
| <input | ||
| id='tc-beta' | ||
| type='checkbox' | ||
| checked={beta} | ||
| disabled={beta && count === 1} | ||
| onChange={() => setBeta(s => !s)} | ||
| /> | ||
| Beta | ||
| </label> | ||
| <label> | ||
| <input | ||
| id='tc-nightly' | ||
| type='checkbox' | ||
| checked={nightly} | ||
| disabled={nightly && count === 1} | ||
| onChange={() => setNightly(s => !s)} | ||
| /> | ||
| Nightly | ||
| </label> | ||
| </div> | ||
| <select name='selectedToolchain' defaultValue={`stable ${toolchainsAvailable.stable[0]?.name}`}> | ||
| {all.map(({ type, name }) => ( | ||
| <option key={`${type} ${name}`} value={`${type} ${name}`}> | ||
| {name} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { execFile } from 'node:child_process' | ||
| import path from 'node:path' | ||
| import { promisify } from 'node:util' | ||
|
|
||
| import { getElanDir } from '@leanprover/workbench-shared/node' | ||
|
|
||
| import { startTrackedCommand } from './trackedCommand' | ||
|
|
||
| const exec = promisify(execFile) | ||
|
|
||
| const getElanBin = () => path.join(getElanDir(), 'bin', 'elan') | ||
|
|
||
| /** | ||
| * Queries `elan` for a list of installed toolchains. | ||
| * | ||
| * Results for normally-installed release or nightly toolchains are in long-form, e.g. | ||
| * `leanprover/lean4-nightly:nightly-2026-08-27` or `leanprover/lean4:v4.32.2`. | ||
| * The `leanprover/lean4` part is the "origin" (see | ||
| * https://lean-lang.org/doc/reference/latest/Build-Tools-and-Distribution/Managing-Toolchains-with-Elan/) | ||
| */ | ||
| export async function listInstalledToolchains(): Promise<string[]> { | ||
| const ELAN_HOME = getElanDir() | ||
| const { stderr, stdout } = await exec(getElanBin(), ['toolchain', 'list'], { | ||
| env: { ...process.env, ELAN_HOME }, | ||
| }) | ||
| // a successful `elan toolchain list` prints only to standard output | ||
| if (stderr.trim().length !== 0) throw new Error(stderr) | ||
| if (stdout.trim() === 'no installed toolchains') return [] | ||
| return stdout | ||
| .split('\n') | ||
| .map(tc => tc.trim()) | ||
| .filter(tc => tc.length > 0) | ||
| } | ||
|
|
||
| export async function elanUninstall(leanVersion: string) { | ||
| const ELAN_HOME = getElanDir() | ||
| const { stderr, stdout } = await exec(getElanBin(), ['toolchain', 'uninstall', leanVersion], { | ||
| env: { ...process.env, ELAN_HOME }, | ||
| }) | ||
| // a successful `elan toolchain uninstall ...` prints only to standard error | ||
| if (stdout.trim().length !== 0) throw new Error(stdout) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also confused here... either the "standard error" or the "stdout" needs to be changed, or my understanding is wrong. |
||
|
|
||
| return stderr | ||
| .split('\n') | ||
| .map(tc => tc.trim()) | ||
| .filter(tc => tc.length > 0) | ||
| } | ||
|
|
||
| export function startElanInstall(leanVersion: string) { | ||
| const ELAN_HOME = getElanDir() | ||
| return startTrackedCommand('elan', getElanBin(), ['toolchain', 'install', leanVersion], { | ||
| env: { ...process.env, ELAN_HOME }, | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: toss a link here to https://lean-lang.org/doc/reference/latest/Build-Tools-and-Distribution/Managing-Toolchains-with-Elan/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 69de54c