Repository navigation
Update the Welcome notebook on upgrade; close button on the first-use wizard #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1e48007
9abfefb
21ecb83
d262fe3
f2d0b72
50d6de3
5c2049f
294a43b
cddd0b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,8 +71,10 @@ fn run(app: AndroidApp) -> Result<(), String> { | |
| let setup_payload: PathBuf = payload.clone(); | ||
| let mut app = NoteCraftApp::new(session) | ||
| .with_default_notebooks_root(layout.notebooks) | ||
| .with_welcome_notebook_setup(move |notebooks_root, format| { | ||
| notecraft_setup::install_edition(&setup_payload, notebooks_root, format).map_err(|error| error.to_string()) | ||
| .with_welcome_notebook_setup(move |notebooks_root, format, expected_manifest| match expected_manifest { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Android upgrades never check the Welcome notebook update.
Call the same check in the Android startup path, as desktop does: Suggested fix
If Android is out of scope for this update, show a notice in the UI on Android instead. Do not drop the feature silently.
|
||
| Some(expected) => notecraft_setup::install_edition_if_unchanged(&setup_payload, notebooks_root, format, expected) | ||
| .map_err(|error| error.to_string()), | ||
| None => notecraft_setup::install_edition(&setup_payload, notebooks_root, format).map(Some).map_err(|error| error.to_string()), | ||
| }) | ||
| .with_clipboard(clipboard); | ||
| for (key, message) in alerts.drain(..) { | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| //! A hash of a directory tree, for telling whether the tree changed since it | ||
| //! was recorded. The Welcome notebook update uses it: the manifest recorded | ||
| //! when the notebook was installed is compared with the installed tree, so | ||
| //! an untouched notebook updates silently and an edited one asks first. | ||
| //! | ||
| //! Sorted relative paths; each file contributes its contents' hash, each | ||
| //! directory and link its path, so a file the person added, or a link they | ||
| //! made, counts as a change. Contents only: times and permissions do not | ||
| //! matter. A read failure is an error, so a tree that cannot be read is | ||
| //! treated as changed, never as untouched. | ||
| use std::io; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| /// What one entry of a directory tree holds: a regular file's contents hash, | ||
| /// a directory, or a link's target. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| enum Entry { | ||
| File(String), | ||
| Dir, | ||
| Link(PathBuf), | ||
| } | ||
|
|
||
| /// The state of a directory tree: every entry in traversal order (depth-first, | ||
| /// entries sorted in each directory), so the aggregate hash is stable. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct TreeSnapshot { | ||
| entries: Vec<(PathBuf, Entry)>, | ||
| } | ||
|
|
||
| impl TreeSnapshot { | ||
| /// The aggregate SHA-256 of the recorded tree: each file contributes a | ||
| /// line with its contents' hash, each directory and link a line with its | ||
| /// path, in traversal order. | ||
| pub fn manifest(&self) -> String { | ||
| use sha2::Digest; | ||
| let mut hasher = sha2::Sha256::new(); | ||
| for (relative, entry) in &self.entries { | ||
| let relative = relative.to_string_lossy(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encode paths without losing native filename bytes. On Unix, files named with byte Use a lossless path encoding for both entry paths and link targets.
|
||
| match entry { | ||
| Entry::File(hash) => hasher.update(format!("F {relative} {hash}\n")), | ||
| Entry::Dir => hasher.update(format!("D {relative}\n")), | ||
| Entry::Link(target) => hasher.update(format!("L {relative} -> {}\n", target.display())), | ||
| } | ||
| } | ||
| format!("{:x}", hasher.finalize()) | ||
| } | ||
|
|
||
| /// The recorded contents hash of one regular file, if the snapshot holds it. | ||
| pub fn file_hash(&self, relative: &Path) -> Option<&str> { | ||
| self.entries.iter().find_map(|(path, entry)| match entry { | ||
| Entry::File(hash) if path == relative => Some(hash.as_str()), | ||
| _ => None, | ||
| }) | ||
| } | ||
|
|
||
| /// Whether the snapshot holds any entry at this relative path. | ||
| pub fn contains(&self, relative: &Path) -> bool { | ||
| self.entries.iter().any(|(path, _)| path == relative) | ||
| } | ||
| } | ||
|
|
||
| /// Reads a directory tree into a snapshot. See the module docs for what counts. | ||
| pub fn snapshot_tree(dir: &Path) -> io::Result<TreeSnapshot> { | ||
| let mut entries = Vec::new(); | ||
| visit(dir, dir, &mut entries)?; | ||
| Ok(TreeSnapshot { entries }) | ||
| } | ||
|
|
||
| /// The aggregate SHA-256 of a directory tree, for telling whether it changed. | ||
| /// See the module docs; a read failure is an error. | ||
| pub fn tree_manifest(dir: &Path) -> io::Result<String> { | ||
| Ok(snapshot_tree(dir)?.manifest()) | ||
| } | ||
|
|
||
| /// The SHA-256 of one file's contents. | ||
| pub fn file_hash(path: &Path) -> io::Result<String> { | ||
| use sha2::Digest; | ||
| let mut file = std::fs::File::open(path).map_err(|error| io::Error::new(error.kind(), format!("could not read {}: {error}", path.display())))?; | ||
| let mut hasher = sha2::Sha256::new(); | ||
| std::io::copy(&mut file, &mut hasher).map_err(|error| io::Error::new(error.kind(), format!("could not read {}: {error}", path.display())))?; | ||
| Ok(format!("{:x}", hasher.finalize())) | ||
| } | ||
|
|
||
| fn visit(dir: &Path, base: &Path, entries: &mut Vec<(PathBuf, Entry)>) -> io::Result<()> { | ||
| let mut paths: Vec<PathBuf> = std::fs::read_dir(dir) | ||
| .map_err(|error| io::Error::new(error.kind(), format!("could not list {}: {error}", dir.display())))? | ||
| .collect::<io::Result<Vec<_>>>() | ||
| .map_err(|error| io::Error::new(error.kind(), format!("could not list {}: {error}", dir.display())))? | ||
| .into_iter() | ||
| .map(|entry| entry.path()) | ||
| .collect(); | ||
| paths.sort(); | ||
| for path in paths { | ||
| let relative = path | ||
| .strip_prefix(base) | ||
| .map_err(|_| io::Error::new(io::ErrorKind::Other, format!("{} is not under {}", path.display(), base.display())))? | ||
| .to_path_buf(); | ||
| let kind = | ||
| std::fs::symlink_metadata(&path).map_err(|error| io::Error::new(error.kind(), format!("could not read {}: {error}", path.display())))?; | ||
| if kind.file_type().is_symlink() { | ||
| let target = | ||
| std::fs::read_link(&path).map_err(|error| io::Error::new(error.kind(), format!("could not read {}: {error}", path.display())))?; | ||
| entries.push((relative, Entry::Link(target))); | ||
| } else if kind.is_dir() { | ||
| entries.push((relative, Entry::Dir)); | ||
| visit(&path, base, entries)?; | ||
| } else { | ||
| entries.push((relative, Entry::File(file_hash(&path)?))); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::testing::TempDir; | ||
| use std::fs; | ||
|
|
||
| fn tree(dir: &Path) { | ||
| fs::create_dir_all(dir.join("00-start/welcome")).expect("tree"); | ||
| fs::write(dir.join("00-start/welcome/index.html"), "welcome").expect("page"); | ||
| fs::write(dir.join("_node.toml"), "node").expect("node"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_manifest_tells_changed_files_apart() { | ||
| let temp = TempDir::new("tree-hash-manifest"); | ||
| let first = temp.path().join("first"); | ||
| let second = temp.path().join("second"); | ||
| tree(&first); | ||
| tree(&second); | ||
| let manifest = tree_manifest(&first).expect("manifest"); | ||
| assert_eq!(tree_manifest(&second).expect("same tree"), manifest, "identical trees share a manifest"); | ||
| fs::write(second.join("00-start/welcome/index.html"), "welcome, edited").expect("edit"); | ||
| assert_ne!(tree_manifest(&second).expect("changed tree"), manifest, "an edited page changes the manifest"); | ||
| fs::write(second.join("00-start/welcome/index.html"), "welcome").expect("restore"); | ||
| fs::write(second.join("extra.txt"), "added").expect("added file"); | ||
| assert_ne!(tree_manifest(&second).expect("added file"), manifest, "an added file changes the manifest"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_snapshot_records_each_file_and_its_paths() { | ||
| let temp = TempDir::new("tree-hash-snapshot"); | ||
| let dir = temp.path().join("tree"); | ||
| tree(&dir); | ||
| let snapshot = snapshot_tree(&dir).expect("snapshot"); | ||
| let page = Path::new("00-start/welcome/index.html"); | ||
| let recorded = file_hash(&dir.join(page)).expect("file hash"); | ||
| assert_eq!(snapshot.file_hash(page), Some(recorded.as_str()), "a file's recorded hash matches its contents"); | ||
| assert_eq!(snapshot.file_hash(Path::new("missing.txt")), None, "a missing file has no recorded hash"); | ||
| assert!(snapshot.contains(Path::new("00-start/welcome")), "a directory is recorded"); | ||
| assert!(!snapshot.contains(Path::new("missing")), "a missing path is not recorded"); | ||
| assert_eq!(snapshot.manifest(), tree_manifest(&dir).expect("manifest"), "the snapshot and the tree share a manifest"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap the browser install result in
Some.with_welcome_notebook_setupnow requiresResult<Option<PathBuf>, String>(app.rs, Line 2641). This callback still returnsResult<PathBuf, String>frominstall_edition. The website pipeline therefore fails to compile the wasm app.Use
install_edition(format).map(Some)and update the matching assertion intest_bundle_demo.py, Line 50. This preserves successful installation and error propagation.