BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / linux_install.rs

linux_install.rs

feat(packaging): Linux AppImage, deb, rpm and Flatpak
metis Samuel David Merlin Kemper · pushed by metis · 2 weeks ago
228f36c5 History
linux_install.rs 7.7 KiB Download Raw
1//! Linux desktop integration on start: offer to install the binary into
2//! `~/.local/share/rusty-teams` with a menu entry.
3
4use std::collections::HashMap;
5use std::path::{Path, PathBuf};
6
7use app_base::linux_desktop::{self, Layout};
8use gpui_kit::component::button::{Button, ButtonVariants as _};
9use gpui_kit::component::dialog::{Dialog, DialogFooter};
10use gpui_kit::component::{WindowExt as _, v_flex};
11use gpui_kit::*;
12
13use crate::app_state::AppHandle;
14use crate::i18n::{tr, tr_args};
15use crate::theme;
16
17const DIALOG_WIDTH: f32 = 460.;
18
19pub fn start(cx: &mut App) {
20 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
21 return;
22 };
23 if linux_desktop::is_packaged(&executable, std::env::var_os("APPIMAGE").as_deref()) {
24 return;
25 }
26 if linux_desktop::is_installed(&executable, &layout) {
27 // Keeps the menu entry pointing at this file, e.g. after it was deleted by hand.
28 if !layout.desktop_file().is_file()
29 && linux_desktop::write_desktop_entry(&layout, &executable).is_ok()
30 {
31 linux_desktop::refresh_desktop_caches(&layout);
32 }
33 return;
34 }
35 let dont_ask = cx
36 .try_global::<AppHandle>()
37 .is_some_and(|handle| linux_desktop::dont_ask_again(&handle.0.read(cx).store));
38 if linux_desktop::should_offer_install(&executable, &layout, dont_ask) {
39 // After the shell's first frame, so the dialog sits on top of the splash.
40 cx.defer(move |cx| offer_install(executable, layout, cx));
41 }
42}
43
44fn offer_install(executable: PathBuf, layout: Layout, cx: &mut App) {
45 let Some(window) = crate::main_window::current(cx) else {
46 return;
47 };
48 let folder = layout.install_dir();
49 let mut args = HashMap::new();
50 args.insert("folder".to_owned(), display_home(&folder));
51 let body = tr_args("install.body", &args);
52 window
53 .update(cx, move |_, window, cx| {
54 window.open_dialog(cx, move |dialog: Dialog, _, _| {
55 let (install_from, install_layout) = (executable.clone(), layout.clone());
56 let footer = DialogFooter::new()
57 .child(
58 Button::new("install-dont-ask")
59 .label(tr("install.dont_ask"))
60 .on_click(|_, window, cx| {
61 if let Some(handle) = cx.try_global::<AppHandle>() {
62 linux_desktop::set_dont_ask_again(&handle.0.read(cx).store);
63 }
64 window.close_dialog(cx);
65 }),
66 )
67 .child(
68 Button::new("install-not-now")
69 .label(tr("install.not_now"))
70 .on_click(|_, window, cx| window.close_dialog(cx)),
71 )
72 .child(
73 Button::new("install-move")
74 .primary()
75 .label(tr("install.move"))
76 .on_click(move |_, window, cx| {
77 window.close_dialog(cx);
78 install(&install_from, &install_layout, cx);
79 }),
80 );
81 dialog
82 .title(tr("install.title"))
83 .w(px(DIALOG_WIDTH))
84 .child(
85 v_flex()
86 .gap(px(8.))
87 .text_color(theme::text())
88 .child(body.clone()),
89 )
90 .footer(footer)
91 });
92 })
93 .ok();
94}
95
96fn install(executable: &Path, layout: &Layout, cx: &mut App) {
97 match linux_desktop::install_binary(executable, layout) {
98 Ok(installed) => relaunch(&installed, cx),
99 Err(error) => {
100 log::warn!("install failed: {error}");
101 notice(failure("install.failed", &error.to_string()), cx);
102 }
103 }
104}
105
106fn failure(key: &str, detail: &str) -> String {
107 let mut args = HashMap::new();
108 args.insert("detail".to_owned(), detail.to_owned());
109 tr_args(key, &args)
110}
111
112fn notice(text: String, cx: &mut App) {
113 if let Some(handle) = cx.try_global::<AppHandle>() {
114 handle
115 .0
116 .clone()
117 .update(cx, |state, cx| state.raise_notice(text, None, cx));
118 }
119}
120
121/// Where this process stands relative to the install folder.
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum Installation {
124 /// This process is the installed copy.
125 Running,
126 /// Another binary is installed; this one runs from elsewhere.
127 OtherCopy,
128 NotInstalled,
129}
130
131pub fn installation() -> Installation {
132 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
133 return Installation::NotInstalled;
134 };
135 if linux_desktop::is_installed(&executable, &layout) {
136 Installation::Running
137 } else if layout.installed_binary().is_file() {
138 Installation::OtherCopy
139 } else {
140 Installation::NotInstalled
141 }
142}
143
144pub fn install_folder() -> String {
145 Layout::current()
146 .map(|layout| display_home(&layout.install_dir()))
147 .unwrap_or_default()
148}
149
150/// Settings > General: installs (or replaces the installed copy with) this binary.
151pub fn install_now(cx: &mut App) {
152 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
153 return;
154 };
155 if let Some(handle) = cx.try_global::<AppHandle>() {
156 linux_desktop::ask_again(&handle.0.read(cx).store);
157 }
158 install(&executable, &layout, cx);
159}
160
161/// Settings > General: removes the menu entry and the installed binary.
162pub fn remove(cx: &mut App) {
163 let Some(layout) = Layout::current() else {
164 return;
165 };
166 match linux_desktop::uninstall(&layout) {
167 Ok(()) => notice(tr("install.removed"), cx),
168 Err(error) => notice(failure("install.remove_failed", &error.to_string()), cx),
169 }
170 cx.refresh_windows();
171}
172
173pub fn asks_on_start(cx: &App) -> bool {
174 cx.try_global::<AppHandle>()
175 .is_some_and(|handle| !linux_desktop::dont_ask_again(&handle.0.read(cx).store))
176}
177
178pub fn set_asks_on_start(ask: bool, cx: &mut App) {
179 if let Some(handle) = cx.try_global::<AppHandle>() {
180 let store = &handle.0.read(cx).store;
181 if ask {
182 linux_desktop::ask_again(store);
183 } else {
184 linux_desktop::set_dont_ask_again(store);
185 }
186 }
187 cx.refresh_windows();
188}
189
190/// The release this build counts as: its tag in CI release builds, the workspace
191/// version (kept equal to the latest release) in a local build.
192pub fn running_release() -> &'static str {
193 app_base::version::release_tag().unwrap_or(concat!("v", env!("CARGO_PKG_VERSION")))
194}
195
196/// The release to update from, when this process is the installed copy.
197pub fn updatable_release() -> Option<&'static str> {
198 crate::updater::self_update_allowed().then(running_release)
199}
200
201/// Starts the installed copy and quits; the new process waits for this one to let go
202/// of the single-instance socket.
203fn relaunch(installed: &Path, cx: &mut App) {
204 let started = std::process::Command::new(installed)
205 .env(crate::updater::RELAUNCH_ENV, std::process::id().to_string())
206 .spawn();
207 match started {
208 Ok(_) => cx.quit(),
209 Err(error) => log::warn!("could not start {}: {error}", installed.display()),
210 }
211}
212
213fn display_home(path: &Path) -> String {
214 match directories::BaseDirs::new().and_then(|base| {
215 path.strip_prefix(base.home_dir())
216 .ok()
217 .map(Path::to_path_buf)
218 }) {
219 Some(relative) => format!("~/{}", relative.display()),
220 None => path.display().to_string(),
221 }
222}