Swadhin Biswas Ci/improve release notes (#41) · 8/28/2026
04d550f
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
use std::path::{Path, PathBuf};
#[cfg(target_os = "linux")]
use std::fs;
#[cfg(target_os = "linux")]
use std::io::Read;
#[cfg(target_os = "linux")]
use std::os::unix::fs::PermissionsExt;
#[cfg(target_os = "linux")]
use std::process::Command;
#[cfg(target_os = "windows")]
use std::process::Command;
use tauri::Emitter;
use uuid::Uuid;
#[cfg(target_os = "linux")]
fn is_appimage_path(path: &Path) -> bool {
matches!(
path.extension().and_then(|ext| ext.to_str()),
Some("AppImage") | Some("appimage")
)
}
#[cfg(target_os = "linux")]
fn is_deb_path(path: &Path) -> bool {
matches!(
path.extension().and_then(|ext| ext.to_str()),
Some("deb") | Some("DEB")
)
}
#[cfg(target_os = "linux")]
fn is_rpm_path(path: &Path) -> bool {
matches!(
path.extension().and_then(|ext| ext.to_str()),
Some("rpm") | Some("RPM")
)
}
#[allow(dead_code)]
fn is_exe_path(path: &Path) -> bool {
matches!(
path.extension().and_then(|ext| ext.to_str()),
Some("exe") | Some("EXE") | Some("msi") | Some("MSI")
)
}
#[cfg(target_os = "linux")]
fn ensure_elf_header(path: &Path) -> Result<(), String> {
let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
let mut header = [0_u8; 4];
file.read_exact(&mut header)
.map_err(|_| "AppImage file is too small to be valid.".to_string())?;
if header == [0x7f, b'E', b'L', b'F'] {
Ok(())
} else {
Err("The selected file is not a valid ELF/AppImage binary.".into())
}
}
#[cfg(target_os = "linux")]
fn set_executable(path: &Path) -> Result<(), String> {
let mut perms = fs::metadata(path).map_err(|e| e.to_string())?.permissions();
perms.set_mode(0o755);
fs::set_permissions(path, perms).map_err(|e| e.to_string())
}
fn emit_progress(app: Option<&tauri::AppHandle>, message: &str) {
if let Some(handle) = app {
let _ = handle.emit("install-progress", message);
}
}
#[cfg(target_os = "linux")]
fn replace_file(from: &Path, to: &Path) -> Result<(), String> {
if let Err(error) = std::fs::rename(from, to) {
if to.exists() {
std::fs::remove_file(to).map_err(|e| e.to_string())?;
std::fs::rename(from, to).map_err(|e| e.to_string())?;
} else {
return Err(error.to_string());
}
}
Ok(())
}
#[cfg(target_os = "linux")]
fn copy_icon_from_extract(
squashfs_root: &Path,
icons_dir: &Path,
base_name: &str,
parsed_icon_name: &str,
) -> Vec<PathBuf> {
let mut copied_icons = Vec::new();
if !parsed_icon_name.is_empty() {
let possible_icons = vec![
squashfs_root.join(format!("{}.png", parsed_icon_name)),
squashfs_root.join(format!("{}.svg", parsed_icon_name)),
squashfs_root.join(format!(
"usr/share/icons/hicolor/512x512/apps/{}.png",
parsed_icon_name
)),
squashfs_root.join(format!(
"usr/share/icons/hicolor/scalable/apps/{}.svg",
parsed_icon_name
)),
squashfs_root.join(format!(
"usr/share/icons/hicolor/256x256/apps/{}.png",
parsed_icon_name
)),
squashfs_root.join(format!(
"usr/share/icons/hicolor/128x128/apps/{}.png",
parsed_icon_name
)),
squashfs_root.join(format!("usr/share/pixmaps/{}.png", parsed_icon_name)),
squashfs_root.join(".DirIcon"),
];
for source in possible_icons {
if source.exists() {
let extension = source
.extension()
.and_then(|ext| ext.to_str())
.filter(|ext| !ext.is_empty())
.unwrap_or("png");
let dest = icons_dir.join(format!("{}_icon.{}", base_name, extension));
if std::fs::copy(&source, &dest).is_ok() {
copied_icons.push(dest);
return copied_icons;
}
}
}
}
if let Ok(entries) = std::fs::read_dir(squashfs_root) {
for entry in entries.flatten() {
let ext = entry
.path()
.extension()
.and_then(|ext| ext.to_str())
.map(|s| s.to_lowercase());
if ext == Some("png".to_string()) || ext == Some("svg".to_string()) {
let dest = icons_dir.join(format!("{}_icon.{}", base_name, ext.unwrap()));
if std::fs::copy(entry.path(), &dest).is_ok() {
copied_icons.push(dest);
break;
}
}
}
}
copied_icons
}
#[cfg(target_os = "linux")]
fn cleanup_failed_install(app_path: &Path, desktop_path: &Path, icon_paths: &[PathBuf]) {
let _ = std::fs::remove_file(app_path);
let _ = std::fs::remove_file(desktop_path);
for icon_path in icon_paths {
let _ = std::fs::remove_file(icon_path);
}
}
#[cfg(target_os = "linux")]
fn detect_deb_package_manager() -> Option<&'static str> {
let managers = ["apt", "dpkg", "pacman"];
managers.into_iter().find(|&cmd| {
Command::new("which")
.arg(cmd)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
})
}
// ── DEB Install (Linux-only) ──
#[cfg(target_os = "linux")]
fn install_deb_direct(path: &Path, app: Option<&tauri::AppHandle>) -> Result<String, String> {
let manager = detect_deb_package_manager().ok_or_else(|| {
// Give a helpful error if they are on an RPM system
let is_rpm = Command::new("which").arg("rpm").output().map(|o| o.status.success()).unwrap_or(false);
if is_rpm {
"This is a DEB package, but your system uses RPM (Fedora, openSUSE, RHEL). Please download an RPM or AppImage instead.".to_string()
} else {
"No supported DEB package manager (apt/dpkg) found on this system.".to_string()
}
})?;
emit_progress(app, &format!("Installing DEB package with {}...", manager));
let install_status = match manager {
"pacman" => return install_deb_with_debtap(path, app),
"apt" => Command::new("pkexec")
.arg("apt")
.arg("install")
.arg("-y")
.arg(path)
.status(),
"dpkg" => {
let s = Command::new("pkexec")
.arg("dpkg")
.arg("-i")
.arg(path)
.status();
if s.as_ref().map(|s| s.success()).unwrap_or(false) {
let _ = Command::new("pkexec")
.arg("apt")
.arg("install")
.arg("-f")
.arg("-y")
.status();
}
s
},
_ => return Err(format!("Unsupported package manager: {}", manager)),
}
.map_err(|e| format!("Failed to install package: {}", e))?;
if !install_status.success() {
return Err("Package installation failed. Check terminal output for details.".to_string());
}
emit_progress(app, "Done");
Ok(format!(
"Successfully installed DEB package via {}",
manager
))
}
#[cfg(target_os = "linux")]
fn install_deb_with_debtap(path: &Path, app: Option<&tauri::AppHandle>) -> Result<String, String> {
emit_progress(app, "Checking debtap availability...");
let debtap_check = Command::new("which")
.arg("debtap")
.output()
.map_err(|_| "debtap is not installed".to_string())?;
if !debtap_check.status.success() {
return Err("debtap is not installed. Please install it first: yay -S debtap".to_string());
}
emit_progress(app, "Updating debtap database...");
let update = Command::new("debtap")
.arg("-u")
.output()
.map_err(|e| format!("debtap -u failed: {}", e))?;
if !update.status.success() {
return Err(format!(
"debtap database update failed: {}",
String::from_utf8_lossy(&update.stderr).trim()
));
}
emit_progress(app, "Converting DEB package...");
let convert = Command::new("debtap")
.arg(path)
.output()
.map_err(|e| format!("debtap failed: {}", e))?;
if !convert.status.success() {
return Err(format!(
"debtap conversion failed: {}",
String::from_utf8_lossy(&convert.stderr).trim()
));
}
let stdout = String::from_utf8_lossy(&convert.stdout);
let pkg_path = stdout
.lines()
.find(|line| line.contains(".pkg.tar"))
.map(|line| line.trim().to_string())
.ok_or("Could not find converted package path".to_string())?;
let pkg_path = Path::new(&pkg_path);
if !pkg_path.exists() {
return Err("Converted package file not found".to_string());
}
emit_progress(app, "Installing converted package...");
let install = Command::new("pkexec")
.arg("pacman")
.arg("-U")
.arg("--noconfirm")
.arg(pkg_path)
.status()
.map_err(|e| format!("Failed to install: {}", e))?;
if !install.success() {
return Err("Package installation failed".to_string());
}
emit_progress(app, "Done");
Ok("Successfully installed DEB package via debtap".to_string())
}
#[cfg(target_os = "linux")]
#[tauri::command]
pub async fn install_deb(path: String) -> Result<String, String> {
install_deb_internal(path).await
}
#[cfg(not(target_os = "linux"))]
#[tauri::command]
pub async fn install_deb(_path: String) -> Result<String, String> {
Err("DEB packages are only supported on Linux".to_string())
}
#[cfg(target_os = "linux")]
pub async fn install_deb_internal(path: String) -> Result<String, String> {
let source_path = Path::new(&path);
if !source_path.exists() {
return Err("DEB file does not exist.".into());
}
if !is_deb_path(source_path) {
return Err("Only .deb files are supported.".into());
}
install_deb_direct(source_path, None)
}
// ── RPM Install (Linux-only) ──
#[cfg(target_os = "linux")]
fn install_rpm_direct(path: &Path, app: Option<&tauri::AppHandle>) -> Result<String, String> {
let managers: &[(&str, &[&str])] = &[
("dnf", &["install", "-y"]),
("zypper", &["install", "-y"]),
("yum", &["localinstall", "-y"]),
("rpm", &["-i", "--nodeps"]),
];
let (manager, args) = managers
.iter()
.find(|(cmd, _)| {
Command::new("which")
.arg(cmd)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
})
.copied()
.ok_or_else(|| {
let is_deb = Command::new("which").arg("dpkg").output().map(|o| o.status.success()).unwrap_or(false);
if is_deb {
"This is an RPM package, but your system uses DEB (Ubuntu, Debian, Mint). Please download a DEB or AppImage instead.".to_string()
} else {
"No supported RPM package manager (dnf/zypper/yum/rpm) found on this system.".to_string()
}
})?;
emit_progress(app, &format!("Installing RPM package with {}...", manager));
let mut cmd = Command::new("pkexec");
cmd.arg(manager);
for arg in args {
cmd.arg(arg);
}
cmd.arg(path);
let status = cmd
.status()
.map_err(|e| format!("Failed to install: {}", e))?;
if !status.success() {
return Err("Package installation failed. Check terminal output for details.".to_string());
}
emit_progress(app, "Done");
Ok(format!(
"Successfully installed RPM package via {}",
manager
))
}
#[cfg(target_os = "linux")]
#[tauri::command]
pub async fn install_rpm(path: String) -> Result<String, String> {
install_rpm_internal(path).await
}
#[cfg(not(target_os = "linux"))]
#[tauri::command]
pub async fn install_rpm(_path: String) -> Result<String, String> {
Err("RPM packages are only supported on Linux".to_string())
}
#[cfg(target_os = "linux")]
pub async fn install_rpm_internal(path: String) -> Result<String, String> {
let source_path = Path::new(&path);
if !source_path.exists() {
return Err("RPM file does not exist.".into());
}
if !is_rpm_path(source_path) {
return Err("Only .rpm files are supported.".into());
}
install_rpm_direct(source_path, None)
}
// ── AppImage Install (Linux-only) ──
#[cfg(target_os = "linux")]
#[tauri::command]
pub async fn install_appimage(path: String) -> Result<String, String> {
install_appimage_internal(path).await
}
#[cfg(not(target_os = "linux"))]
#[tauri::command]
pub async fn install_appimage(_path: String) -> Result<String, String> {
Err("AppImages are only supported on Linux".to_string())
}
#[cfg(target_os = "linux")]
pub async fn install_appimage_internal(path: String) -> Result<String, String> {
let source_path = Path::new(&path);
if !source_path.exists() {
return Err("AppImage file does not exist.".into());
}
if !is_appimage_path(source_path) {
return Err("Only .AppImage files are supported.".into());
}
ensure_elf_header(source_path)?;
let file_name = source_path
.file_name()
.ok_or("Invalid AppImage file name")?
.to_string_lossy()
.to_string();
let home_dir = dirs::home_dir().ok_or("Could not find home directory")?;
let appimages_dir = home_dir.join(".local/appimages");
let apps_dir = home_dir.join(".local/share/applications");
let icons_dir = home_dir.join(".local/share/icons");
std::fs::create_dir_all(&appimages_dir).map_err(|e| e.to_string())?;
std::fs::create_dir_all(&apps_dir).map_err(|e| e.to_string())?;
std::fs::create_dir_all(&icons_dir).map_err(|e| e.to_string())?;
// Duplicate detection: prevent silent overwrite of existing app
let target_path = appimages_dir.join(&file_name);
if target_path.exists() {
return Err(format!(
"An application named '{}' already exists at {}. Remove it first or rename the file.",
file_name,
target_path.display()
));
}
let tmp_dir = std::env::temp_dir().join(format!("linuxy_{}", Uuid::new_v4()));
std::fs::create_dir_all(&tmp_dir).map_err(|e| e.to_string())?;
let install_result = (|| -> Result<String, String> {
let extraction_binary = tmp_dir.join(&file_name);
std::fs::copy(source_path, &extraction_binary).map_err(|e| e.to_string())?;
set_executable(&extraction_binary)?;
// Selective extraction: only pull .desktop files and icons instead of entire squashfs
// This avoids extracting hundreds of MB of data just for metadata
for pattern in &["*.desktop", "*.png", "*.svg"] {
let _ = Command::new(&extraction_binary)
.arg("--appimage-extract")
.arg(pattern)
.current_dir(&tmp_dir)
.output();
}
// Verify extraction produced a squashfs-root directory
let squashfs_root_check = tmp_dir.join("squashfs-root");
if !squashfs_root_check.exists() {
// Fallback: full extraction if selective extraction didn't work (Type 1 AppImages)
let output = Command::new(&extraction_binary)
.arg("--appimage-extract")
.current_dir(&tmp_dir)
.output()
.map_err(|e| format!("Failed to extract AppImage: {}", e))?;
if !output.status.success() {
let err_msg = String::from_utf8_lossy(&output.stderr);
return Err(format!("AppImage extraction failed: {}", err_msg.trim()));
}
}
let squashfs_root = tmp_dir.join("squashfs-root");
let desktop_files = std::fs::read_dir(&squashfs_root)
.map_err(|e| e.to_string())?
.filter_map(Result::ok)
.filter(|entry| {
entry.path().extension().and_then(|ext| ext.to_str()) == Some("desktop")
})
.collect::<Vec<_>>();
if desktop_files.is_empty() {
return Err("No .desktop file found in AppImage".into());
}
let desktop_content =
std::fs::read_to_string(desktop_files[0].path()).map_err(|e| e.to_string())?;
let base_name = file_name.replace(".AppImage", "").replace(".appimage", "");
let final_app_path = appimages_dir.join(&file_name);
let staging_app_path =
appimages_dir.join(format!(".{}.{}.part.AppImage", base_name, Uuid::new_v4()));
let desktop_path = apps_dir.join(format!("{}.desktop", base_name));
// First, extract icon name from desktop content
let mut parsed_icon_name = String::new();
for line in desktop_content.lines() {
let trimmed = line.trim();
if trimmed.starts_with("Icon=") && parsed_icon_name.is_empty() {
parsed_icon_name = trimmed.trim_start_matches("Icon=").trim().to_string();
}
}
// Copy icon to icons_dir
let copied_icons =
copy_icon_from_extract(&squashfs_root, &icons_dir, &base_name, &parsed_icon_name);
let final_icon_str = if let Some(icon_file) = copied_icons.first() {
icon_file.to_string_lossy().to_string()
} else {
format!("{}_icon", base_name)
};
let mut new_desktop = String::new();
let mut in_desktop_entry = false;
let mut has_type = false;
let mut has_terminal = false;
let mut has_categories = false;
let has_libfuse = std::process::Command::new("ldconfig")
.arg("-p")
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).contains("libfuse.so.2"))
.unwrap_or(true);
let exec_str = if !has_libfuse && file_name.to_lowercase().contains(".appimage") {
format!(
"env APPIMAGE_EXTRACT_AND_RUN=1 \"{}\" %U",
final_app_path.to_string_lossy()
)
} else {
format!("\"{}\" %U", final_app_path.to_string_lossy())
};
for line in desktop_content.lines() {
let trimmed = line.trim();
if trimmed == "[Desktop Entry]" {
in_desktop_entry = true;
new_desktop.push_str("[Desktop Entry]\n");
continue;
} else if trimmed.starts_with('[') && trimmed.ends_with(']') {
in_desktop_entry = false;
}
if in_desktop_entry {
if trimmed.contains('[') && trimmed.contains("]=") {
continue;
}
if trimmed.starts_with("Exec=") {
new_desktop.push_str(&format!("Exec={}\n", exec_str));
} else if trimmed.starts_with("Icon=") {
new_desktop.push_str(&format!("Icon={}\n", final_icon_str));
} else if trimmed.starts_with("Type=") {
has_type = true;
new_desktop.push_str(&format!("{}\n", trimmed));
} else if trimmed.starts_with("Terminal=") {
has_terminal = true;
new_desktop.push_str(&format!("{}\n", trimmed));
} else if trimmed.starts_with("Categories=") {
has_categories = true;
new_desktop.push_str(&format!("{}\n", trimmed));
} else if trimmed.starts_with("TryExec=") {
continue;
} else {
new_desktop.push_str(&format!("{}\n", line));
}
} else {
new_desktop.push_str(&format!("{}\n", line));
}
}
if !has_type {
new_desktop =
new_desktop.replace("[Desktop Entry]\n", "[Desktop Entry]\nType=Application\n");
}
if !has_terminal {
new_desktop =
new_desktop.replace("[Desktop Entry]\n", "[Desktop Entry]\nTerminal=false\n");
}
if !has_categories {
new_desktop = new_desktop.replace(
"[Desktop Entry]\n",
"[Desktop Entry]\nCategories=Utility;\n",
);
}
if !new_desktop.contains("X-Linuxy=") {
new_desktop =
new_desktop.replace("[Desktop Entry]\n", "[Desktop Entry]\nX-Linuxy=true\n");
}
std::fs::copy(source_path, &staging_app_path)
.map_err(|e| format!("Failed to copy file: {}", e))?;
if let Err(error) = set_executable(&staging_app_path) {
let _ = std::fs::remove_file(&staging_app_path);
return Err(error);
}
if let Err(error) = replace_file(&staging_app_path, &final_app_path) {
let _ = std::fs::remove_file(&staging_app_path);
return Err(format!("Failed to finalize installation: {}", error));
}
if let Err(error) = std::fs::write(&desktop_path, new_desktop) {
cleanup_failed_install(&final_app_path, &desktop_path, &copied_icons);
return Err(error.to_string());
}
let _ = Command::new("update-desktop-database")
.arg(&apps_dir)
.output();
let _ = Command::new("gtk-update-icon-cache")
.arg("-f")
.arg("-t")
.arg(&icons_dir)
.output();
let _ = filetime::set_file_mtime(&apps_dir, filetime::FileTime::now());
Ok("Successfully installed AppImage".into())
})();
let _ = std::fs::remove_dir_all(&tmp_dir);
install_result
}
// ── Executable Install (Cross-platform) ──
#[tauri::command]
pub async fn install_executable(path: String) -> Result<String, String> {
install_executable_internal(path).await
}
pub async fn install_executable_internal(path: String) -> Result<String, String> {
let source_path = Path::new(&path);
if !source_path.exists() {
return Err("File does not exist.".into());
}
let file_name = source_path
.file_name()
.ok_or("Invalid file name")?
.to_string_lossy()
.to_string();
#[cfg(target_os = "linux")]
{
let home_dir = dirs::home_dir().ok_or("Could not find home directory")?;
let bin_dir = home_dir.join(".local/bin");
let apps_dir = home_dir.join(".local/share/applications");
std::fs::create_dir_all(&bin_dir).map_err(|e| e.to_string())?;
std::fs::create_dir_all(&apps_dir).map_err(|e| e.to_string())?;
let final_app_path = bin_dir.join(&file_name);
let desktop_path = apps_dir.join(format!("{}.desktop", file_name));
std::fs::copy(source_path, &final_app_path)
.map_err(|e| format!("Failed to copy file: {}", e))?;
set_executable(&final_app_path)?;
let new_desktop = format!(
"[Desktop Entry]\nType=Application\nName={}\nExec=\"{}\"\nTerminal=false\nCategories=Utility;\nX-Linuxy=true\n",
file_name,
final_app_path.to_string_lossy()
);
if let Err(error) = std::fs::write(&desktop_path, new_desktop) {
let _ = std::fs::remove_file(&final_app_path);
return Err(error.to_string());
}
let _ = Command::new("update-desktop-database")
.arg(&apps_dir)
.output();
Ok("Successfully installed executable".into())
}
#[cfg(target_os = "windows")]
{
let app_data = std::env::var("LOCALAPPDATA")
.or_else(|_| std::env::var("APPDATA"))
.map_err(|_| "Could not find AppData directory".to_string())?;
let apps_dir = Path::new(&app_data).join("Linuxy").join("apps");
std::fs::create_dir_all(&apps_dir).map_err(|e| e.to_string())?;
let final_app_path = apps_dir.join(&file_name);
std::fs::copy(source_path, &final_app_path)
.map_err(|e| format!("Failed to copy file: {}", e))?;
// Create Start Menu shortcut via PowerShell - pass args via $args to
// avoid injection, ensure the Linuxy folder exists, and surface errors
let ps_script = r#"
$FileName = $args[0]
$TargetPath = $args[1]
$StartMenuDir = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Linuxy"
if (!(Test-Path $StartMenuDir)) {
New-Item -ItemType Directory -Path $StartMenuDir -Force | Out-Null
if (!(Test-Path $StartMenuDir)) { Write-Error "Failed to create Start Menu directory: $StartMenuDir"; exit 1 }
}
$WScriptShell = New-Object -ComObject WScript.Shell
$ShortcutPath = Join-Path $StartMenuDir "$FileName.lnk"
$Shortcut = $WScriptShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = $TargetPath
$Shortcut.Save()
if (!(Test-Path $ShortcutPath)) { Write-Error "Failed to create shortcut: $ShortcutPath"; exit 1 }
"#;
let output = Command::new("powershell")
.args([
"-Command",
ps_script,
&file_name,
&final_app_path.to_string_lossy().to_string(),
])
.output()
.map_err(|e| format!("Failed to launch PowerShell for shortcut creation: {}", e))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let details = if stderr.trim().is_empty() && stdout.trim().is_empty() {
"unknown error".to_string()
} else {
format!(
"{}{}",
stderr.trim(),
if stdout.trim().is_empty() {
String::new()
} else {
format!(" {}", stdout.trim())
}
)
};
return Err(format!(
"Failed to create Start Menu shortcut for '{}': {}",
file_name, details
));
}
Ok("Successfully installed executable on Windows".into())
}
#[cfg(target_os = "macos")]
{
let home_dir = dirs::home_dir().ok_or("Could not find home directory")?;
let apps_dir = home_dir.join("Applications").join("Linuxy");
std::fs::create_dir_all(&apps_dir).map_err(|e| e.to_string())?;
let final_app_path = apps_dir.join(&file_name);
std::fs::copy(source_path, &final_app_path)
.map_err(|e| format!("Failed to copy file: {}", e))?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = std::fs::metadata(&final_app_path)
.map_err(|e| e.to_string())?
.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&final_app_path, perms).map_err(|e| e.to_string())?;
}
#[cfg(not(unix))]
{
let perms = std::fs::metadata(&final_app_path)
.map_err(|e| e.to_string())?
.permissions();
std::fs::set_permissions(&final_app_path, perms).map_err(|e| e.to_string())?;
}
Ok("Successfully installed executable on macOS".into())
}
}
#[cfg(test)]
#[cfg(target_os = "linux")]
mod tests {
use std::fs;
use std::io::Write;
use std::path::Path;
use super::{ensure_elf_header, is_appimage_path};
#[test]
fn recognizes_appimage_extensions() {
assert!(is_appimage_path(Path::new("/tmp/example.AppImage")));
assert!(is_appimage_path(Path::new("/tmp/example.appimage")));
assert!(!is_appimage_path(Path::new("/tmp/example.tar.gz")));
}
#[test]
fn rejects_non_elf_files() {
let test_path = std::env::temp_dir().join(format!("linuxy_test_{}", uuid::Uuid::new_v4()));
let mut file = fs::File::create(&test_path).expect("create test file");
file.write_all(b"not-elf").expect("write test file");
let result = ensure_elf_header(&test_path);
let _ = fs::remove_file(&test_path);
assert!(result.is_err());
}
}