Improve editor, plugin updates, and window transfers for 0.1.41
This commit is contained in:
Generated
+1
-1
@@ -3130,7 +3130,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "runestone"
|
||||
version = "0.1.36"
|
||||
version = "0.1.41"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"dunce",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "runestone"
|
||||
version = "0.1.36"
|
||||
version = "0.1.41"
|
||||
description = "Writing workspace plus script editor"
|
||||
authors = ["you"]
|
||||
edition = "2021"
|
||||
@@ -44,4 +44,4 @@ panic = "abort"
|
||||
strip = true
|
||||
|
||||
[package.metadata.runestone]
|
||||
version = "0.1.36"
|
||||
version = "0.1.41"
|
||||
|
||||
+248
-24
@@ -85,6 +85,14 @@ struct Candidate {
|
||||
hash: String,
|
||||
manifest: Manifest,
|
||||
created: Instant,
|
||||
previous: Option<Installed>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct UpdateJournal {
|
||||
previous: Installed,
|
||||
next_hash: String,
|
||||
backup: String,
|
||||
staged: String,
|
||||
}
|
||||
#[derive(Clone, Serialize)]
|
||||
pub struct RemovalPlan {
|
||||
@@ -456,6 +464,7 @@ impl Service {
|
||||
cleanup_errors: BTreeMap::new(),
|
||||
removing: BTreeMap::new(),
|
||||
};
|
||||
service.recover_updates()?;
|
||||
let mut recovered = false;
|
||||
for (id, record) in &mut service.catalog.plugins {
|
||||
if let Some(token) = &record.removal {
|
||||
@@ -474,6 +483,48 @@ impl Service {
|
||||
}
|
||||
Ok(service)
|
||||
}
|
||||
fn recover_updates(&mut self) -> Result<()> {
|
||||
let staging = self.directory("plugin-staging")?;
|
||||
for entry in fs::read_dir(&staging).map_err(fail)? {
|
||||
let entry = entry.map_err(fail)?;
|
||||
let name = entry.file_name().to_string_lossy().into_owned();
|
||||
if !name.starts_with("update-") || !name.ends_with(".json") { continue; }
|
||||
let journal: UpdateJournal = serde_json::from_slice(&bounded(&entry.path(), 128 * 1024)?).map_err(fail)?;
|
||||
let id = journal.previous.manifest.id.clone();
|
||||
if !valid_id(&id) { return Err("Invalid plugin update journal.".into()); }
|
||||
Uuid::parse_str(&journal.backup).map_err(fail)?;
|
||||
Uuid::parse_str(&journal.staged).map_err(fail)?;
|
||||
let target = self.directory("plugins")?.join(&id);
|
||||
let backup = staging.join(&journal.backup);
|
||||
let staged = staging.join(&journal.staged);
|
||||
let committed = self.catalog.plugins.get(&id).is_some_and(|record| record.hash == journal.next_hash)
|
||||
&& package(&target).is_ok_and(|current| current.hash == journal.next_hash);
|
||||
if backup.exists() {
|
||||
safe_child(&staging, &backup)?;
|
||||
if package(&backup)?.hash != journal.previous.hash { return Err("The plugin update backup changed.".into()); }
|
||||
if committed { erase(&staging, &backup)?; }
|
||||
else {
|
||||
if target.exists() {
|
||||
safe_child(&self.directory("plugins")?, &target)?;
|
||||
if package(&target)?.hash != journal.next_hash || staged.exists() {
|
||||
return Err("Plugin update recovery requires inspection of the staging directory.".into());
|
||||
}
|
||||
fs::rename(&target, &staged).map_err(fail)?;
|
||||
}
|
||||
fs::rename(&backup, &target).map_err(fail)?;
|
||||
}
|
||||
}
|
||||
if !committed {
|
||||
if package(&target)?.hash != journal.previous.hash { return Err("The previous plugin package changed.".into()); }
|
||||
let mut previous = journal.previous;
|
||||
previous.enabled = false;
|
||||
self.catalog.plugins.insert(id.clone(), previous);
|
||||
self.persist()?;
|
||||
}
|
||||
erase(&staging, &entry.path())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn directory(&self, name: &str) -> Result<PathBuf> {
|
||||
let path = self.root.join(name);
|
||||
safe_child(&self.root, &path)?;
|
||||
@@ -594,13 +645,14 @@ impl Service {
|
||||
let display = source.to_string_lossy().into_owned();
|
||||
let result = (|| {
|
||||
let current = package(&source)?;
|
||||
if self.catalog.plugins.contains_key(¤t.manifest.id)
|
||||
|| self
|
||||
.directory("plugins")?
|
||||
.join(¤t.manifest.id)
|
||||
.exists()
|
||||
{
|
||||
return Err("This plugin ID is already installed. Uninstall it before installing another version.".into());
|
||||
let previous = self.catalog.plugins.get(¤t.manifest.id).cloned();
|
||||
if let Some(record) = &previous {
|
||||
let row = self.row(¤t.manifest.id)?;
|
||||
if row.error.is_some() || record.removal.is_some() || self.removing.contains_key(¤t.manifest.id) {
|
||||
return Err("Finish plugin cleanup before updating it.".into());
|
||||
}
|
||||
} else if self.directory("plugins")?.join(¤t.manifest.id).exists() {
|
||||
return Err("Refresh the plugin list before installing this package.".into());
|
||||
}
|
||||
let staging = self.directory("plugin-staging")?.join(&token);
|
||||
fs::create_dir(&staging).map_err(fail)?;
|
||||
@@ -630,6 +682,7 @@ impl Service {
|
||||
hash: current.hash,
|
||||
manifest: manifest.clone(),
|
||||
created: Instant::now(),
|
||||
previous,
|
||||
},
|
||||
);
|
||||
Ok(manifest)
|
||||
@@ -682,6 +735,9 @@ impl Service {
|
||||
return Err("The package review expired. Select it again.".into());
|
||||
}
|
||||
let id = candidate.manifest.id.clone();
|
||||
if candidate.previous.is_some() {
|
||||
return self.update(owner, token);
|
||||
}
|
||||
if self.catalog.plugins.contains_key(&id) {
|
||||
return Err("This plugin ID is already installed.".into());
|
||||
}
|
||||
@@ -731,6 +787,82 @@ impl Service {
|
||||
}
|
||||
Ok(candidate.manifest.name)
|
||||
}
|
||||
fn begin_update(&mut self, owner: &str, token: &str) -> Result<Option<String>> {
|
||||
let candidate = self.candidates.get(token).ok_or("Select the package again.")?;
|
||||
if candidate.owner != owner || candidate.created.elapsed() > Duration::from_secs(1800) {
|
||||
return Err("The package review expired. Select it again.".into());
|
||||
}
|
||||
let Some(previous) = &candidate.previous else { return Ok(None); };
|
||||
let id = candidate.manifest.id.clone();
|
||||
let current = self.catalog.plugins.get(&id).ok_or("The installed plugin changed. Select the package again.")?;
|
||||
if current.hash != previous.hash || current.enabled != previous.enabled || current.removal.is_some()
|
||||
|| self.removing.contains_key(&id) || self.cleanup_errors.contains_key(&id)
|
||||
|| package(&self.directory("plugins")?.join(&id))?.hash != previous.hash {
|
||||
return Err("The installed plugin changed. Refresh and select the package again.".into());
|
||||
}
|
||||
self.catalog.plugins.get_mut(&id).unwrap().enabled = false;
|
||||
if let Err(error) = self.persist() {
|
||||
self.catalog.plugins.get_mut(&id).unwrap().enabled = previous.enabled;
|
||||
return Err(error);
|
||||
}
|
||||
self.removing.insert(id.clone(), previous.hash.clone());
|
||||
Ok(Some(id))
|
||||
}
|
||||
fn update(&mut self, owner: &str, token: &str) -> Result<String> {
|
||||
let candidate = self.candidates.get(token).ok_or("Select the package again.")?;
|
||||
if candidate.owner != owner || candidate.created.elapsed() > Duration::from_secs(1800) {
|
||||
return Err("The package review expired. Select it again.".into());
|
||||
}
|
||||
let previous = candidate.previous.clone().ok_or("The plugin update is unavailable.")?;
|
||||
let id = candidate.manifest.id.clone();
|
||||
if self.removing.get(&id) != Some(&previous.hash) {
|
||||
return Err("Start the plugin update again.".into());
|
||||
}
|
||||
if self.leases.get(&id).is_some_and(|windows| !windows.is_empty()) || self.cleanup_errors.contains_key(&id) {
|
||||
return Err("Plugin cleanup did not finish. Close other RuneStone windows and retry the update.".into());
|
||||
}
|
||||
if candidate.source.as_ref().map(|source| package(source)).transpose()?.is_some_and(|source| source.hash != candidate.hash)
|
||||
|| package(&candidate.directory)?.hash != candidate.hash {
|
||||
return Err("The package changed after review. Select it again.".into());
|
||||
}
|
||||
let parent = self.directory("plugins")?;
|
||||
let target = parent.join(&id);
|
||||
safe_child(&parent, &target)?;
|
||||
if package(&target)?.hash != previous.hash {
|
||||
return Err("Plugin files changed during shutdown. Select the package again.".into());
|
||||
}
|
||||
let staging = self.directory("plugin-staging")?;
|
||||
safe_child(&staging, &candidate.directory)?;
|
||||
let backup = staging.join(uid());
|
||||
let journal_path = staging.join(format!("update-{}.json", uid()));
|
||||
let journal = UpdateJournal { previous: previous.clone(), next_hash: candidate.hash.clone(),
|
||||
backup: backup.file_name().unwrap().to_string_lossy().into_owned(),
|
||||
staged: candidate.directory.file_name().unwrap().to_string_lossy().into_owned() };
|
||||
fs::write(&journal_path, serde_json::to_vec(&journal).map_err(fail)?).map_err(fail)?;
|
||||
fs::rename(&target, &backup).map_err(fail)?;
|
||||
if let Err(error) = fs::rename(&candidate.directory, &target) {
|
||||
fs::rename(&backup, &target).map_err(fail)?;
|
||||
let _ = erase(&staging, &journal_path);
|
||||
return Err(fail(error));
|
||||
}
|
||||
self.catalog.plugins.insert(id.clone(), Installed {
|
||||
manifest: candidate.manifest.clone(), hash: candidate.hash.clone(), enabled: previous.enabled,
|
||||
installed_at: Some(SystemTime::now().duration_since(UNIX_EPOCH).map_err(fail)?.as_millis() as u64),
|
||||
removal: None, removal_files: BTreeMap::new(),
|
||||
});
|
||||
if let Err(error) = self.persist() {
|
||||
fs::rename(&target, &candidate.directory).map_err(fail)?;
|
||||
fs::rename(&backup, &target).map_err(fail)?;
|
||||
self.catalog.plugins.insert(id, previous);
|
||||
let _ = erase(&staging, &journal_path);
|
||||
return Err(error);
|
||||
}
|
||||
let name = self.candidates.remove(token).unwrap().manifest.name;
|
||||
// The backup contains only the exact package approved before shutdown.
|
||||
if package(&backup).is_ok_and(|old| old.hash == previous.hash) { let _ = erase(&staging, &backup); }
|
||||
let _ = erase(&staging, &journal_path);
|
||||
Ok(name)
|
||||
}
|
||||
fn prepare(&mut self, owner: &str, id: &str) -> Result<RemovalPlan> {
|
||||
if self.removing.contains_key(id) {
|
||||
return Err("This plugin is already being removed.".into());
|
||||
@@ -1004,24 +1136,42 @@ pub async fn plugins_install(
|
||||
tokens: Vec<String>,
|
||||
enabled: bool,
|
||||
) -> Result<Vec<InstallResult>> {
|
||||
let result = state.with(&app, |service| {
|
||||
Ok(tokens
|
||||
.iter()
|
||||
.map(|token| {
|
||||
let name = service
|
||||
.candidates
|
||||
.get(token)
|
||||
.map(|c| c.manifest.name.clone())
|
||||
.unwrap_or_else(|| "Plugin".into());
|
||||
InstallResult {
|
||||
name,
|
||||
error: service.install(window.label(), token, enabled).err(),
|
||||
let mut result = Vec::new();
|
||||
for token in tokens {
|
||||
let name = state.with(&app, |service| Ok(service.candidates.get(&token)
|
||||
.map(|candidate| candidate.manifest.name.clone()).unwrap_or_else(|| "Plugin".into())))?;
|
||||
let updating = state.with(&app, |service| service.begin_update(window.label(), &token));
|
||||
let error = match updating {
|
||||
Err(error) => Some(error),
|
||||
Ok(id) => {
|
||||
if let Some(id) = &id {
|
||||
changed(&app);
|
||||
let start = Instant::now();
|
||||
while state.with(&app, |service| Ok(service.leases.get(id).is_some_and(|windows| !windows.is_empty())))?
|
||||
&& start.elapsed() < Duration::from_secs(10) {
|
||||
tauri::async_runtime::spawn_blocking(|| std::thread::sleep(Duration::from_millis(50))).await.map_err(fail)?;
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
});
|
||||
let installed = state.with(&app, |service| {
|
||||
let installed = service.install(window.label(), &token, enabled);
|
||||
if let Some(id) = &id {
|
||||
service.removing.remove(id);
|
||||
if installed.is_err() {
|
||||
if let Some(previous) = service.candidates.get(&token).and_then(|candidate| candidate.previous.clone()) {
|
||||
if let Some(record) = service.catalog.plugins.get_mut(id) { record.enabled = previous.enabled; }
|
||||
service.persist()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
installed
|
||||
});
|
||||
installed.err()
|
||||
}
|
||||
};
|
||||
result.push(InstallResult { name, error });
|
||||
}
|
||||
changed(&app);
|
||||
result
|
||||
Ok(result)
|
||||
}
|
||||
#[tauri::command]
|
||||
pub async fn plugins_set_enabled(
|
||||
@@ -1427,6 +1577,80 @@ mod tests {
|
||||
f.install(true);
|
||||
assert!(f.service.row(&id).unwrap().installed_at.unwrap() >= installed_at);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reviewed_update_keeps_settings_and_enabled_state_and_replaces_files() {
|
||||
for enabled in [false, true] {
|
||||
let mut f = Fixture::new();
|
||||
let id = f.install(enabled);
|
||||
let settings = f.service.directory("plugin-data").unwrap().join(format!("{id}.json"));
|
||||
fs::write(&settings, r#"{"keep":true}"#).unwrap();
|
||||
let manifest = fs::read_to_string(f.source.join("plugin.json")).unwrap().replace("1.0.0", "1.0.1");
|
||||
fs::write(f.source.join("plugin.json"), manifest).unwrap();
|
||||
fs::write(f.source.join("main.js"), "export function activate() { /* update */ }").unwrap();
|
||||
let candidate = f.service.inspect("main", f.source.clone());
|
||||
assert!(candidate.error.is_none(), "{:?}", candidate.error);
|
||||
assert!(f.service.begin_update("other", &candidate.token).is_err());
|
||||
assert_eq!(f.service.begin_update("main", &candidate.token).unwrap(), Some(id.clone()));
|
||||
assert!(!f.service.row(&id).unwrap().enabled);
|
||||
f.service.install("main", &candidate.token, !enabled).unwrap();
|
||||
f.service.removing.remove(&id);
|
||||
let row = f.service.row(&id).unwrap();
|
||||
assert_eq!(row.manifest.version, "1.0.1");
|
||||
assert_eq!(row.enabled, enabled);
|
||||
assert!(row.error.is_none());
|
||||
assert_eq!(fs::read_to_string(&settings).unwrap(), r#"{"keep":true}"#);
|
||||
assert_eq!(Service::open(f.root.join("app")).unwrap().row(&id).unwrap().manifest.version, "1.0.1");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_waits_for_cleanup_and_rejects_changed_packages() {
|
||||
let mut f = Fixture::new();
|
||||
let id = f.install(true);
|
||||
let candidate = f.service.inspect("main", f.source.clone());
|
||||
f.service.leases.entry(id.clone()).or_default().insert("main".into());
|
||||
f.service.begin_update("main", &candidate.token).unwrap();
|
||||
assert!(f.service.install("main", &candidate.token, true).unwrap_err().contains("cleanup"));
|
||||
f.service.leases.clear();
|
||||
fs::write(f.source.join("main.js"), "changed after review").unwrap();
|
||||
assert!(f.service.install("main", &candidate.token, true).unwrap_err().contains("changed after review"));
|
||||
assert_eq!(f.service.row(&id).unwrap().manifest.version, "1.0.0");
|
||||
assert_eq!(fs::read_to_string(f.service.directory("plugins").unwrap().join(&id).join("main.js")).unwrap(), "export function activate() {}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interrupted_update_restores_previous_package_until_commit() {
|
||||
for phase in 0..4 {
|
||||
let mut f = Fixture::new();
|
||||
let id = f.install(true);
|
||||
let previous = f.service.catalog.plugins[&id].clone();
|
||||
let manifest = fs::read_to_string(f.source.join("plugin.json")).unwrap().replace("1.0.0", "1.0.1");
|
||||
fs::write(f.source.join("plugin.json"), manifest).unwrap();
|
||||
let row = f.service.inspect("main", f.source.clone());
|
||||
f.service.begin_update("main", &row.token).unwrap();
|
||||
let candidate = &f.service.candidates[&row.token];
|
||||
let journal = UpdateJournal { previous, next_hash: candidate.hash.clone(), backup: uid(), staged: row.token.clone() };
|
||||
let staging = f.service.directory("plugin-staging").unwrap();
|
||||
fs::write(staging.join(format!("update-{}.json", uid())), serde_json::to_vec(&journal).unwrap()).unwrap();
|
||||
let target = f.service.directory("plugins").unwrap().join(&id);
|
||||
if phase >= 1 { fs::rename(&target, staging.join(&journal.backup)).unwrap(); }
|
||||
if phase >= 2 { fs::rename(&candidate.directory, &target).unwrap(); }
|
||||
if phase == 3 {
|
||||
let record = f.service.catalog.plugins.get_mut(&id).unwrap();
|
||||
record.manifest = candidate.manifest.clone();
|
||||
record.hash = candidate.hash.clone();
|
||||
record.enabled = true;
|
||||
f.service.persist().unwrap();
|
||||
}
|
||||
let recovered = Service::open(f.root.join("app")).unwrap();
|
||||
let row = recovered.row(&id).unwrap();
|
||||
assert!(row.error.is_none());
|
||||
assert_eq!(row.manifest.version, if phase == 3 { "1.0.1" } else { "1.0.0" });
|
||||
assert_eq!(row.enabled, phase == 3);
|
||||
assert!(!staging.join(&journal.backup).exists());
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn legacy_and_discovered_plugins_keep_unknown_install_times() {
|
||||
let mut f = Fixture::new();
|
||||
@@ -1522,7 +1746,7 @@ mod tests {
|
||||
f.service.install("main", &row.token, false).unwrap();
|
||||
assert!(!f.service.row("org.test.demo").unwrap().enabled);
|
||||
assert_eq!(package(&f.source).unwrap().hash, original.hash);
|
||||
assert!(f.service.inspect("main", f.source.clone()).error.is_some());
|
||||
assert!(f.service.inspect("main", f.source.clone()).error.is_none());
|
||||
}
|
||||
#[test]
|
||||
fn feature_packages_use_the_standard_install_and_remove_lifecycle() {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "RuneStone",
|
||||
"version": "0.1.36",
|
||||
"version": "0.1.41",
|
||||
"identifier": "com.runestone.app",
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm dev",
|
||||
|
||||
Reference in New Issue
Block a user