nsh-core 0.8.1

Platform-independent core: shadow tree, styles, layout and mount instructions.
Documentation
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
//! `promptForUpdate`: NSH's built-in update prompt, as macOS apps' Sparkle
//! asks. The check, the user's choices kept between runs (a skipped version,
//! automatic installs) and the alert it falls back on are here; the window
//! it shows on the desktops is `nsh-base`'s (`updater_prompt`), which a UI
//! set installs with [`set_update_presenter`]: the core cannot draw it.

use std::{
    future::Future,
    path::{Path, PathBuf},
    rc::Rc,
};

use serde::{Deserialize, Serialize};

use super::{AppUpdate, UpdateProgress, native};
use crate::{
    element::{JsError, JsObject, System},
    ops::{AlertDialogRequest, DialogButton, DialogButtonStyle},
};

/// Options of `promptForUpdate` ([`prompt_for_update`]).
#[derive(Debug, Clone, Default, PartialEq, JsObject)]
#[non_exhaustive]
pub struct UpdatePromptOptions {
    /// As `checkForUpdate`'s: the channel in `nsh.json`'s
    /// `"updater": { "channels" }` to check instead of the default source.
    pub channel: Option<String>,
    /// As `checkForUpdate`'s: the language of the notes, when the manifest
    /// has them in it.
    pub locale: Option<String>,
    /// A check the user did not ask for (at startup, in the background):
    /// nothing shows when the app is up to date, the check fails or the
    /// user skipped the version, and an update installs as the app quits
    /// without asking once the user chose automatic updates. Default false:
    /// the user asked ("Check for Updates…") and hears every outcome.
    pub quiet: Option<bool>,
}

impl UpdatePromptOptions {
    /// Checks the channel `name` of `nsh.json`'s `"updater": { "channels" }`.
    pub fn channel(mut self, name: impl Into<String>) -> Self {
        self.channel = Some(name.into());
        self
    }

    /// The notes in `locale` (a BCP 47 tag such as `zh-TW`), when the
    /// manifest has them.
    pub fn locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = Some(locale.into());
        self
    }

    /// Whether the check is one the user did not ask for
    /// ([`UpdatePromptOptions::quiet`]).
    pub fn quiet(mut self, quiet: bool) -> Self {
        self.quiet = Some(quiet);
        self
    }
}

/// Checks for a newer version and asks the user about it in NSH's built-in
/// prompt (TS: `promptForUpdate` of `nsh/updater`), to await in a task or
/// drop. Rust apps check with the engine's network and the configuration
/// `nsh package` left among the app's assets.
///
/// ```ignore
/// // At startup:
/// spawn(prompt_for_update(UpdatePromptOptions::default().quiet(true)));
/// // A "Check for Updates…" menu item:
/// spawn(prompt_for_update(UpdatePromptOptions::default()));
/// ```
pub fn prompt_for_update(options: UpdatePromptOptions) -> impl Future<Output = ()> + 'static {
    let mut sys = System::default().with_updater(crate::element::updater_host());
    sys.data_dir = crate::element::data_dir();
    run(sys, options)
}

/// What shows the prompt's window: `nsh-base`'s.
type Presenter = Rc<dyn Fn(&UpdatePrompt) -> bool>;

/// The engine's [`Presenter`], if a UI set installed one.
#[derive(Default, Clone)]
struct Presenters(Option<Presenter>);

/// Shows the built-in prompt with `present`, which returns whether it
/// showed it (else the system's alert asks). `nsh-base` installs its window
/// with it in each engine's setup.
#[doc(hidden)]
pub fn set_update_presenter(present: impl Fn(&UpdatePrompt) -> bool + 'static) {
    crate::element::global::<Presenters>().update(|p| p.0 = Some(Rc::new(present)));
}

/// An update to ask the user about, and what the prompt can do with it
/// ([`set_update_presenter`]).
#[doc(hidden)]
#[derive(Clone)]
#[non_exhaustive]
pub struct UpdatePrompt {
    /// The app's display name.
    pub app_name: String,
    /// The version that runs now.
    pub current_version: String,
    /// The newer version.
    pub version: String,
    /// What changed, as Markdown.
    pub notes: Option<String>,
    /// Whether the app installs it itself (else Install Update opens the
    /// update's page).
    pub can_install: bool,
    /// Whether the user chose to download and install updates
    /// automatically.
    pub automatic: bool,
    sys: System,
    url: String,
}

impl UpdatePrompt {
    /// Skips this version: quiet checks no longer offer it.
    pub fn skip(&self) {
        let dir = self.sys.data_dir.as_deref();
        let mut preferences = Preferences::load(dir);
        preferences.skipped = Some(self.version.clone());
        preferences.save(dir);
    }

    /// Whether quiet checks download and install updates without asking.
    pub fn set_automatic(&self, automatic: bool) {
        let dir = self.sys.data_dir.as_deref();
        let mut preferences = Preferences::load(dir);
        preferences.automatic = automatic;
        preferences.save(dir);
    }

    /// Downloads the update, calling `on_progress` with the bytes received
    /// and the total when known.
    pub fn download(
        &self,
        on_progress: impl Fn(u64, Option<u64>) + 'static,
    ) -> impl Future<Output = Result<(), String>> + 'static {
        let progress = Box::new(move |p: UpdateProgress| {
            on_progress(p.received as u64, p.total.map(|t| t as u64))
        });
        let done = native::download(self.sys.clone(), Some(progress));
        async move { done.await.map_err(message) }
    }

    /// Installs the downloaded update and restarts the app into it.
    pub fn install_and_restart(&self) -> impl Future<Output = Result<(), String>> + 'static {
        let done = native::install(self.sys.clone());
        async move { done.await.map_err(message) }
    }

    /// Installs the downloaded update as the app quits.
    pub fn install_on_quit(&self) -> impl Future<Output = Result<(), String>> + 'static {
        let done = native::install_on_quit(self.sys.clone());
        async move { done.await.map_err(message) }
    }

    /// Opens the update's page (a store's, or the archive) outside the
    /// app.
    pub fn open_url(&self) -> impl Future<Output = bool> + 'static {
        crate::prelude::open_url(self.url.clone())
    }
}

fn message(e: JsError) -> String {
    match e {
        JsError::Error(m) | JsError::Type(m) => m,
        other => format!("{other:?}"),
    }
}

/// What the user chose, kept between runs in the app's data directory.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
struct Preferences {
    /// The version Skip This Version skipped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    skipped: Option<String>,
    /// "Automatically download and install updates in the future".
    #[serde(default)]
    automatic: bool,
}

impl Preferences {
    fn path(dir: &Path) -> PathBuf {
        dir.join("updates").join("prompt.json")
    }

    /// What `dir` keeps; the defaults without a directory or a readable
    /// file.
    fn load(dir: Option<&Path>) -> Preferences {
        dir.and_then(|dir| std::fs::read(Self::path(dir)).ok())
            .and_then(|bytes| serde_json::from_slice(&bytes).ok())
            .unwrap_or_default()
    }

    fn save(&self, dir: Option<&Path>) {
        let Some(dir) = dir else {
            return;
        };
        let path = Self::path(dir);
        let written = path
            .parent()
            .map_or(Ok(()), std::fs::create_dir_all)
            .and_then(|()| std::fs::write(&path, serde_json::to_vec(self).unwrap_or_default()));
        if let Err(e) = written {
            log::warn!("updater: cannot keep the prompt's preferences: {e}");
        }
    }
}

/// What a check that found `version` does.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Next {
    /// Nothing: a quiet check of a skipped version.
    Nothing,
    /// Downloads it and installs it as the app quits, without asking.
    InstallOnQuit,
    /// Asks the user.
    Ask,
}

fn next(preferences: &Preferences, version: &str, can_install: bool, quiet: bool) -> Next {
    if !quiet {
        return Next::Ask;
    }
    if preferences.skipped.as_deref() == Some(version) {
        Next::Nothing
    } else if preferences.automatic && can_install {
        Next::InstallOnQuit
    } else {
        Next::Ask
    }
}

pub(super) async fn run(sys: System, options: UpdatePromptOptions) {
    let quiet = options.quiet.unwrap_or(false);
    if sys.updater.is_none() {
        log::info!("updater: the app does not update here: no prompt");
        return;
    }
    let (name, current) = native::shipped(&sys).await;
    let app_name = name.unwrap_or_else(executable_name);
    let current_version = current.unwrap_or_default();
    let check = super::UpdateCheckOptions {
        channel: options.channel,
        locale: options.locale,
        url: None,
    };
    let update = match native::check(sys.clone(), check).await {
        Ok(Some(update)) => update,
        Ok(None) => {
            if !quiet {
                alert(
                    "You're up to date!".into(),
                    format!(
                        "{app_name} {current_version} is currently the newest version available."
                    ),
                )
                .await;
            }
            return;
        }
        Err(e) => {
            let e = message(e);
            log::warn!("updater: check: {e}");
            if !quiet {
                alert(
                    "Update Error!".into(),
                    format!("An error occurred in retrieving update information. Please try again later.\n\n{e}"),
                )
                .await;
            }
            return;
        }
    };
    let AppUpdate {
        version,
        notes,
        url,
        can_install,
        ..
    } = update;
    let preferences = Preferences::load(sys.data_dir.as_deref());
    match next(&preferences, &version, can_install, quiet) {
        Next::Nothing => return,
        Next::InstallOnQuit => {
            let installed = async {
                native::download(sys.clone(), None).await?;
                native::install_on_quit(sys.clone()).await
            };
            match installed.await {
                Ok(()) => log::info!("updater: version {version} installs as the app quits"),
                Err(e) => log::warn!("updater: cannot install version {version}: {}", message(e)),
            }
            return;
        }
        Next::Ask => {}
    }
    let prompt = UpdatePrompt {
        app_name,
        current_version,
        version,
        notes,
        can_install,
        automatic: preferences.automatic,
        sys,
        url,
    };
    let presenter = crate::element::global::<Presenters>().with_untracked(|p| p.0.clone());
    let shown = crate::element::is_desktop() && presenter.is_some_and(|present| present(&prompt));
    if !shown {
        ask_in_alert(prompt).await;
    }
}

/// An alert with an OK button.
async fn alert(title: String, message: String) {
    let _ = crate::element::dialog(AlertDialogRequest {
        id: 0,
        title: Some(title),
        message: Some(message),
        buttons: vec![DialogButton {
            text: "OK".into(),
            style: DialogButtonStyle::Default,
        }],
        cancelable: true,
        sheet: false,
        input: None,
    })
    .await;
}

/// The longest notes the alert shows: an alert does not scroll everywhere.
const ALERT_NOTES: usize = 600;

/// The prompt as the system's alert: where no window shows it (iOS,
/// Android, an app without `nsh-base`).
async fn ask_in_alert(prompt: UpdatePrompt) {
    let mut message = format!(
        "{} {} is now available\u{2014}you have {}.",
        prompt.app_name, prompt.version, prompt.current_version
    );
    if let Some(notes) = prompt
        .notes
        .as_deref()
        .map(str::trim)
        .filter(|n| !n.is_empty())
    {
        message.push_str("\n\n");
        let mut chars = notes.chars();
        message.extend(chars.by_ref().take(ALERT_NOTES));
        if chars.next().is_some() {
            message.push('\u{2026}');
        }
    }
    let button = |text: &str, style| DialogButton {
        text: text.into(),
        style,
    };
    let answer = crate::element::dialog(AlertDialogRequest {
        id: 0,
        title: Some(format!(
            "A new version of {} is available!",
            prompt.app_name
        )),
        message: Some(message),
        buttons: vec![
            button(
                if prompt.can_install {
                    "Install Update"
                } else {
                    "Update"
                },
                DialogButtonStyle::Default,
            ),
            button("Skip This Version", DialogButtonStyle::Default),
            button("Remind Me Later", DialogButtonStyle::Cancel),
        ],
        cancelable: true,
        sheet: false,
        input: None,
    })
    .await;
    match answer.index {
        0 if prompt.can_install => {
            let installed = async {
                prompt.download(|_, _| {}).await?;
                prompt.install_and_restart().await
            };
            if let Err(e) = installed.await {
                alert("Update Error!".into(), e).await;
            }
        }
        0 => {
            prompt.open_url().await;
        }
        1 => prompt.skip(),
        _ => {}
    }
}

/// The app's name where its package did not say it: the `.app` bundle's on
/// macOS, else the executable's.
fn executable_name() -> String {
    let exe = std::env::current_exe().ok();
    let bundle = exe.as_deref().and_then(|exe| {
        exe.ancestors()
            .find(|p| p.extension().is_some_and(|e| e == "app"))
            .and_then(Path::file_stem)
    });
    bundle
        .or_else(|| exe.as_deref().and_then(Path::file_stem))
        .map(|name| name.to_string_lossy().into_owned())
        .unwrap_or_else(|| "The app".into())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn preferences_are_kept_in_the_data_directory() {
        let dir = std::env::temp_dir().join(format!("nsh-prompt-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        assert_eq!(Preferences::load(Some(&dir)), Preferences::default());
        let kept = Preferences {
            skipped: Some("1.2.0".into()),
            automatic: true,
        };
        kept.save(Some(&dir));
        assert_eq!(Preferences::load(Some(&dir)), kept);
        // Without a directory nothing is kept, and nothing fails.
        kept.save(None);
        assert_eq!(Preferences::load(None), Preferences::default());
        std::fs::write(Preferences::path(&dir), b"not json").unwrap();
        assert_eq!(Preferences::load(Some(&dir)), Preferences::default());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn quiet_checks_respect_the_users_choices() {
        let skipped = Preferences {
            skipped: Some("1.2.0".into()),
            automatic: false,
        };
        assert_eq!(next(&skipped, "1.2.0", true, true), Next::Nothing);
        // A newer version than the skipped one asks again.
        assert_eq!(next(&skipped, "1.3.0", true, true), Next::Ask);
        // The user asked: a skipped version shows all the same.
        assert_eq!(next(&skipped, "1.2.0", true, false), Next::Ask);

        let automatic = Preferences {
            skipped: None,
            automatic: true,
        };
        assert_eq!(next(&automatic, "1.2.0", true, true), Next::InstallOnQuit);
        // Where the app cannot install itself it asks.
        assert_eq!(next(&automatic, "1.2.0", false, true), Next::Ask);
        assert_eq!(next(&automatic, "1.2.0", true, false), Next::Ask);
    }
}