Skip to content

Commit 59a9dfc

Browse files
committed
Automatically install wsl
1 parent 8ebdc0f commit 59a9dfc

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

src-tauri/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use tauri_plugin_cli::CliExt;
4242
use tauri_plugin_store::StoreExt;
4343
use templates::create_template;
4444
use tokio::sync::Mutex;
45-
use windows::{has_wsl, is_windows};
45+
use windows::{has_wsl, install_wsl, is_windows};
4646

4747
fn main() {
4848
CryptoProvider::install_default(ring::default_provider()).unwrap();
@@ -142,6 +142,7 @@ fn main() {
142142
start_stream_stdout,
143143
stop_stream_stdout,
144144
is_streaming_stdout,
145+
install_wsl
145146
])
146147
.run(tauri::generate_context!())
147148
.expect("error while running tauri application");

src-tauri/src/windows.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ pub fn is_windows() -> bool {
6565
cfg!(target_os = "windows")
6666
}
6767

68+
#[tauri::command]
69+
pub fn install_wsl() -> Result<(), String> {
70+
#[cfg(target_os = "windows")]
71+
{
72+
Command::new("powershell")
73+
.arg("-Command")
74+
.arg("Start-Process powershell -Verb runAs -ArgumentList 'wsl --install'")
75+
.spawn()
76+
.map_err(|e| e.to_string())?;
77+
}
78+
Ok(())
79+
}
80+
6881
// Taken from wslpath2 crate and modified
6982
#[cfg(target_os = "windows")]
7083
#[derive(Debug)]

src/pages/Onboarding.tsx

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import SDKMenu from "../components/SDKMenu";
1111
import ErrorIcon from "@mui/icons-material/Error";
1212
import WarningIcon from "@mui/icons-material/Warning";
1313
import { getVersion } from "@tauri-apps/api/app";
14+
import { invoke } from "@tauri-apps/api/core";
15+
import { useToast } from "react-toast-plus";
16+
import { relaunch } from "@tauri-apps/plugin-process";
1417

1518
export interface OnboardingProps {}
1619

@@ -26,6 +29,7 @@ export default ({}: OnboardingProps) => {
2629
const [ready, setReady] = useState(false);
2730
const [version, setVersion] = useState<string>("");
2831
const navigate = useNavigate();
32+
const { addToast } = useToast();
2933

3034
useEffect(() => {
3135
if (toolchains !== null && isWindows !== null && hasWSL !== null) {
@@ -132,7 +136,7 @@ export default ({}: OnboardingProps) => {
132136
)}
133137
</Typography>
134138
<div className="onboarding-cards">
135-
{isWindows && (
139+
{!isWindows && (
136140
<Card variant="soft">
137141
<Typography level="h3">Windows Subsystem for Linux</Typography>
138142
<Typography level="body-sm">
@@ -147,9 +151,9 @@ export default ({}: OnboardingProps) => {
147151
>
148152
microsoft.com
149153
</Link>
150-
. We recommended installing WSL 2 and Ubuntu 24.04. Other
151-
distributions may work, but are not officially supported.
152-
CrossCode will use your default WSL distribution.
154+
. We recommended WSL 2 and Ubuntu 24.04. Other distributions may
155+
work, but are not officially supported. CrossCode will use your
156+
default WSL distribution.
153157
</Typography>
154158
<Divider />
155159
<CardContent>
@@ -160,7 +164,8 @@ export default ({}: OnboardingProps) => {
160164
"WSL is already installed on your system!"
161165
) : (
162166
<>
163-
WSL is not installed on your system. Please follow the guide
167+
WSL is not installed on your system. CrossCode can attempt
168+
to automatically install WSL. If it fails, follow the guide
164169
on{" "}
165170
<Link
166171
href="#"
@@ -177,6 +182,37 @@ export default ({}: OnboardingProps) => {
177182
</>
178183
)}
179184
</Typography>
185+
{hasWSL && <Divider />}
186+
{hasWSL && (
187+
<Button
188+
onClick={async () => {
189+
try {
190+
await invoke("install_wsl");
191+
} catch (error) {
192+
addToast.error(
193+
"Failed to launch WSL installation. Please try installing it manually."
194+
);
195+
}
196+
}}
197+
>
198+
Install WSL
199+
</Button>
200+
)}
201+
{hasWSL && (
202+
<Button
203+
onClick={async () => {
204+
try {
205+
await relaunch();
206+
} catch (error) {
207+
addToast.error(
208+
"Failed to relaunch CrossCode. Please try manually."
209+
);
210+
}
211+
}}
212+
>
213+
Relaunch CrossCode (post-installation)
214+
</Button>
215+
)}
180216
</CardContent>
181217
</Card>
182218
)}

0 commit comments

Comments
 (0)