Skip to content

Commit 19ff372

Browse files
authored
Merge pull request #1109 from doubleailes/support_windows_spk_launcher
add win support for spk-launcher
2 parents 96df633 + dc5a7c6 commit 19ff372

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

crates/spk-launcher/src/main.rs

+54-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
use std::env::{args_os, var_os};
66
use std::ffi::{CString, OsStr, OsString};
7+
#[cfg(unix)]
78
use std::os::unix::ffi::{OsStrExt, OsStringExt};
9+
#[cfg(unix)]
810
use std::os::unix::fs::symlink;
11+
#[cfg(windows)]
12+
use std::os::windows::fs::{symlink_dir, symlink_file};
913
use std::path::{Path, PathBuf};
1014
use std::sync::Arc;
1115

1216
use miette::{bail, miette, Context, IntoDiagnostic, Result};
17+
#[cfg(unix)]
1318
use nix::unistd::execv;
1419
use spfs::encoding::Digest;
1520
use spfs::prelude::*;
@@ -188,9 +193,19 @@ impl<'a> Dynamic<'a> {
188193
Err(miette!("symlink target exists"))
189194
}
190195
.and_then(|_| {
191-
symlink(&digest_string, &symlink_name)
192-
.into_diagnostic()
193-
.wrap_err("create symlink")
196+
#[cfg(unix)]
197+
{
198+
symlink(&digest_string, &symlink_name)
199+
.into_diagnostic()
200+
.wrap_err("create symlink")
201+
}
202+
#[cfg(windows)]
203+
{
204+
symlink_file(&digest_string, &symlink_name)
205+
.or_else(|_| symlink_dir(&digest_string, &symlink_name))
206+
.into_diagnostic()
207+
.wrap_err("create symlink")
208+
}
194209
});
195210
}
196211
}
@@ -200,18 +215,38 @@ impl<'a> Dynamic<'a> {
200215

201216
async fn execute(&self) -> Result<()> {
202217
let bin_tag = var_os(self.tag_env_var()).unwrap_or_else(|| RPM_TAG.into());
218+
#[cfg(unix)]
203219
let args = args_os()
204220
.map(|os_string| CString::new(os_string.as_bytes()))
205221
.collect::<Result<Vec<_>, _>>()
206222
.into_diagnostic()
207223
.wrap_err("valid CStrings")?;
224+
#[cfg(windows)]
225+
let args = args_os().collect::<Vec<_>>();
208226
if bin_tag == RPM_TAG {
227+
#[cfg(unix)]
209228
let bin = CString::new(AsRef::<OsStr>::as_ref(&self.rpm_bin_path()).as_bytes())
210229
.expect("valid CString");
230+
#[cfg(windows)]
231+
let bin = self.rpm_bin_path();
232+
#[cfg(unix)]
211233
execv(&bin, args.as_slice())
212234
.into_diagnostic()
213235
.wrap_err_with(|| format!("execv({}, ...)", bin.to_string_lossy()))?;
214-
unreachable!();
236+
#[cfg(windows)]
237+
{
238+
let bin = bin.to_string_lossy();
239+
let args = args
240+
.iter()
241+
.map(|arg| arg.to_string_lossy())
242+
.collect::<Vec<_>>();
243+
let args = args.join(" ");
244+
std::process::Command::new(bin.to_string())
245+
.args(args.split_whitespace())
246+
.spawn()
247+
.into_diagnostic()
248+
.wrap_err_with(|| format!("spawn({}, {})", bin, args))?;
249+
}
215250
}
216251

217252
let config = spfs::get_config().expect("loaded spfs config");
@@ -264,7 +299,7 @@ impl<'a> Dynamic<'a> {
264299
})?;
265300

266301
std::env::set_var(self.bin_var(), &bin_path);
267-
302+
#[cfg(unix)]
268303
execv(
269304
&CString::new(bin_path.into_vec())
270305
.into_diagnostic()
@@ -278,6 +313,20 @@ impl<'a> Dynamic<'a> {
278313
)
279314
.into_diagnostic()
280315
.wrap_err("process replaced")?;
316+
#[cfg(windows)]
317+
{
318+
let bin = bin_path.to_string_lossy();
319+
let args = args
320+
.iter()
321+
.map(|arg| arg.to_string_lossy())
322+
.collect::<Vec<_>>();
323+
let args = args.join(" ");
324+
std::process::Command::new(bin.to_string())
325+
.args(args.split_whitespace())
326+
.spawn()
327+
.into_diagnostic()
328+
.wrap_err_with(|| format!("spawn({}, {})", bin, args))?;
329+
}
281330
unreachable!();
282331
}
283332
Ok(None) => bail!("Expected platform object from spfs"),

0 commit comments

Comments
 (0)