Release RuneStone 0.1.7 with plugin documents and shared menus
This commit is contained in:
+272
-53
@@ -6,7 +6,7 @@ use std::{
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
sync::Mutex,
|
||||
time::{Duration, Instant},
|
||||
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use tauri::{AppHandle, Emitter, Manager, State};
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
@@ -44,6 +44,8 @@ struct Installed {
|
||||
hash: String,
|
||||
enabled: bool,
|
||||
#[serde(default)]
|
||||
installed_at: Option<u64>,
|
||||
#[serde(default)]
|
||||
removal: Option<String>,
|
||||
#[serde(default)]
|
||||
removal_files: BTreeMap<String, String>,
|
||||
@@ -59,6 +61,7 @@ pub struct PluginRow {
|
||||
pub manifest: Manifest,
|
||||
pub hash: String,
|
||||
pub enabled: bool,
|
||||
pub installed_at: Option<u64>,
|
||||
pub path: String,
|
||||
pub error: Option<String>,
|
||||
pub removal: bool,
|
||||
@@ -77,7 +80,7 @@ pub struct CandidateRow {
|
||||
}
|
||||
struct Candidate {
|
||||
owner: String,
|
||||
source: PathBuf,
|
||||
source: Option<PathBuf>,
|
||||
directory: PathBuf,
|
||||
hash: String,
|
||||
manifest: Manifest,
|
||||
@@ -267,11 +270,6 @@ fn tree_hash(path: &Path) -> Result<String> {
|
||||
Ok(fingerprint(&directory_files(path)?))
|
||||
}
|
||||
fn package(source: &Path) -> Result<Package> {
|
||||
let source = if source.file_name().is_some_and(|n| n == "plugin.json") {
|
||||
source.parent().ok_or("Missing plugin folder.")?
|
||||
} else {
|
||||
source
|
||||
};
|
||||
let mut files = BTreeMap::new();
|
||||
let mut names = BTreeSet::new();
|
||||
let mut total = 0;
|
||||
@@ -279,9 +277,7 @@ fn package(source: &Path) -> Result<Package> {
|
||||
if meta.is_dir() {
|
||||
walk(source, source, &mut files, &mut names, &mut total, &mut 0)?;
|
||||
} else {
|
||||
if source.extension().and_then(|s| s.to_str()) != Some("runestone-plugin") {
|
||||
return Err("Select a .runestone-plugin package or plugin.json.".into());
|
||||
}
|
||||
require_zip(source)?;
|
||||
if meta.len() > MAX_TOTAL as u64 {
|
||||
return Err("The archive exceeds 100 MiB.".into());
|
||||
}
|
||||
@@ -369,6 +365,19 @@ fn package(source: &Path) -> Result<Package> {
|
||||
hash,
|
||||
})
|
||||
}
|
||||
fn require_zip(source: &Path) -> Result<()> {
|
||||
if !source
|
||||
.extension()
|
||||
.and_then(|s| s.to_str())
|
||||
.is_some_and(|s| s.eq_ignore_ascii_case("zip"))
|
||||
|| !plain(source)?.is_file()
|
||||
{
|
||||
return Err(
|
||||
"Select a plugin .zip file. The archive must contain plugin.json at its root.".into(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn safe_child(parent: &Path, child: &Path) -> Result<()> {
|
||||
plain(parent)?;
|
||||
plain(child)?;
|
||||
@@ -517,6 +526,7 @@ impl Service {
|
||||
manifest,
|
||||
hash,
|
||||
enabled: record.enabled && error.is_none() && !self.removing.contains_key(id),
|
||||
installed_at: record.installed_at,
|
||||
path: path.to_string_lossy().into_owned(),
|
||||
error,
|
||||
removal: record.removal.is_some(),
|
||||
@@ -543,6 +553,7 @@ impl Service {
|
||||
manifest: current.manifest,
|
||||
hash: current.hash,
|
||||
enabled: false,
|
||||
installed_at: None,
|
||||
removal: None,
|
||||
removal_files: BTreeMap::new(),
|
||||
},
|
||||
@@ -609,7 +620,7 @@ impl Service {
|
||||
token.clone(),
|
||||
Candidate {
|
||||
owner: owner.into(),
|
||||
source,
|
||||
source: Some(source),
|
||||
directory: staging,
|
||||
hash: current.hash,
|
||||
manifest: manifest.clone(),
|
||||
@@ -641,6 +652,22 @@ impl Service {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn inspect_download(&mut self, owner: &str, download: crate::plugin_sources::Download) -> Result<CandidateRow> {
|
||||
let parent = self.directory("plugin-staging")?;
|
||||
let archive = parent.join(format!("{}.zip", uid()));
|
||||
fs::write(&archive, download.bytes).map_err(fail)?;
|
||||
let mut row = self.inspect(owner, archive.clone());
|
||||
if let Some(candidate) = self.candidates.get_mut(&row.token) {
|
||||
// Review pins the downloaded bytes. Remote releases can change independently.
|
||||
candidate.source = None;
|
||||
}
|
||||
row.source = download.source;
|
||||
if let Err(error) = erase(&parent, &archive) {
|
||||
if let Some(candidate) = self.candidates.remove(&row.token) { let _ = erase(&parent, &candidate.directory); }
|
||||
return Err(error);
|
||||
}
|
||||
Ok(row)
|
||||
}
|
||||
fn install(&mut self, owner: &str, token: &str, enabled: bool) -> Result<String> {
|
||||
let candidate = self
|
||||
.candidates
|
||||
@@ -653,7 +680,7 @@ impl Service {
|
||||
if self.catalog.plugins.contains_key(&id) {
|
||||
return Err("This plugin ID is already installed.".into());
|
||||
}
|
||||
if package(&candidate.source)?.hash != candidate.hash
|
||||
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());
|
||||
@@ -664,6 +691,10 @@ impl Service {
|
||||
return Err("The plugin directory already exists.".into());
|
||||
}
|
||||
safe_child(&self.directory("plugin-staging")?, &candidate.directory)?;
|
||||
let installed_at = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_err(fail)?
|
||||
.as_millis() as u64;
|
||||
// Persist disabled first. A crash cannot enable unreviewed or partial code.
|
||||
self.catalog.plugins.insert(
|
||||
id.clone(),
|
||||
@@ -671,6 +702,7 @@ impl Service {
|
||||
manifest: candidate.manifest.clone(),
|
||||
hash: candidate.hash.clone(),
|
||||
enabled: false,
|
||||
installed_at: Some(installed_at),
|
||||
removal: None,
|
||||
removal_files: BTreeMap::new(),
|
||||
},
|
||||
@@ -874,61 +906,56 @@ pub async fn plugins_snapshot(app: AppHandle, state: State<'_, PluginState>) ->
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn plugins_choose(
|
||||
app: AppHandle,
|
||||
window: tauri::Window,
|
||||
folder: bool,
|
||||
) -> Result<Vec<CandidateRow>> {
|
||||
pub async fn plugins_choose(app: AppHandle, window: tauri::Window) -> Result<Vec<CandidateRow>> {
|
||||
let picker_app = app.clone();
|
||||
let source = tauri::async_runtime::spawn_blocking(move || {
|
||||
let picker = picker_app
|
||||
picker_app
|
||||
.dialog()
|
||||
.file()
|
||||
.set_title("Install RuneStone plugins");
|
||||
if folder {
|
||||
picker.blocking_pick_folder()
|
||||
} else {
|
||||
picker
|
||||
.add_filter("RuneStone plugins", &["runestone-plugin", "json"])
|
||||
.blocking_pick_file()
|
||||
}
|
||||
.set_title("Install plugin ZIP")
|
||||
.add_filter("Plugin ZIP archives", &["zip"])
|
||||
.blocking_pick_files()
|
||||
})
|
||||
.await
|
||||
.map_err(fail)?;
|
||||
let Some(source) = source else {
|
||||
let Some(sources) = source else {
|
||||
return Ok(Vec::new());
|
||||
};
|
||||
let source = source.into_path().map_err(fail)?;
|
||||
if sources.len() > 100 {
|
||||
return Err("Select at most 100 plugin ZIP files.".into());
|
||||
}
|
||||
let paths: Vec<PathBuf> = sources
|
||||
.into_iter()
|
||||
.map(|source| source.into_path().map_err(fail))
|
||||
.collect::<Result<_>>()?;
|
||||
for path in &paths {
|
||||
require_zip(path)?;
|
||||
}
|
||||
app.state::<PluginState>().with(&app, |service| {
|
||||
let mut paths = Vec::new();
|
||||
if source.is_dir() && !source.join("plugin.json").exists() {
|
||||
plain(&source)?;
|
||||
for (index, entry) in fs::read_dir(&source).map_err(fail)?.enumerate() {
|
||||
if index >= 100 {
|
||||
return Err("Choose a collection with at most 100 entries.".into());
|
||||
}
|
||||
let path = entry.map_err(fail)?.path();
|
||||
if path.is_dir()
|
||||
|| path.extension().and_then(|n| n.to_str()) == Some("runestone-plugin")
|
||||
{
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
if paths.len() > 100 {
|
||||
return Err("Choose a collection with at most 100 plugins.".into());
|
||||
}
|
||||
} else {
|
||||
paths.push(source);
|
||||
}
|
||||
if paths.is_empty() {
|
||||
return Err("No plugin packages or folders were found.".into());
|
||||
}
|
||||
Ok(paths
|
||||
.into_iter()
|
||||
.map(|path| service.inspect(window.label(), path))
|
||||
.collect())
|
||||
})
|
||||
}
|
||||
#[tauri::command]
|
||||
pub async fn plugins_from_git(
|
||||
app: AppHandle,
|
||||
window: tauri::Window,
|
||||
url: String,
|
||||
tag: String,
|
||||
token: Option<String>,
|
||||
) -> Result<Vec<CandidateRow>> {
|
||||
let repository = crate::plugin_sources::Repository::parse(&url, &tag)?;
|
||||
// Network access never holds the plugin service lock.
|
||||
let download = tauri::async_runtime::spawn_blocking(move || {
|
||||
crate::plugin_sources::download(&repository, token.as_deref())
|
||||
}).await.map_err(fail)??;
|
||||
app.state::<PluginState>().with(&app, |service| {
|
||||
Ok(vec![service.inspect_download(window.label(), download)?])
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn plugins_cancel(
|
||||
app: AppHandle,
|
||||
@@ -1263,11 +1290,203 @@ mod tests {
|
||||
}
|
||||
zip.finish().unwrap();
|
||||
}
|
||||
fn review_remote(fixture: &mut Fixture, bytes: Vec<u8>) -> CandidateRow {
|
||||
use crate::plugin_sources::{tests::server, Repository};
|
||||
let (asset_url, asset_server) = server(vec![("200 OK".into(), bytes)]);
|
||||
let metadata = serde_json::json!({"tag_name":"trial-1", "assets":[{
|
||||
"name":"plugin.zip", "browser_download_url":format!("{asset_url}/plugin.zip")
|
||||
}]});
|
||||
let (base, metadata_server) = server(vec![("200 OK".into(), serde_json::to_vec(&metadata).unwrap())]);
|
||||
let repository = Repository::parse(&format!("{base}/owner/plugin.git"), "trial-1").unwrap();
|
||||
let download = crate::plugin_sources::download(&repository, Some("test-token")).unwrap();
|
||||
let requests = metadata_server.join().unwrap();
|
||||
assert!(requests[0].starts_with("GET /api/v1/repos/owner/plugin/releases/tags/trial-1 "));
|
||||
assert!(requests[0].contains("authorization: token test-token"));
|
||||
assert!(!asset_server.join().unwrap()[0].contains("test-token"));
|
||||
fixture.service.inspect_download("main", download).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remote_download_uses_review_and_package_checks() {
|
||||
let mut f = Fixture::new();
|
||||
let archive_path = f.root.join("demo.zip");
|
||||
let manifest = fs::read(f.source.join("plugin.json")).unwrap();
|
||||
archive(&archive_path, &[("plugin.json", &manifest), ("main.js", b"export function activate() {}")]);
|
||||
let bytes = fs::read(&archive_path).unwrap();
|
||||
let candidate = review_remote(&mut f, bytes.clone());
|
||||
assert!(candidate.error.is_none(), "{:?}", candidate.error);
|
||||
assert!(candidate.source.contains("trial-1"));
|
||||
assert!(f.service.candidates[&candidate.token].source.is_none());
|
||||
assert!(f.service.install("other-window", &candidate.token, false).is_err());
|
||||
let staging = f.service.candidates[&candidate.token].directory.clone();
|
||||
fs::write(staging.join("main.js"), "changed").unwrap();
|
||||
assert!(f.service.install("main", &candidate.token, false).is_err());
|
||||
let candidate = review_remote(&mut f, bytes);
|
||||
let id = candidate.manifest.as_ref().unwrap().id.clone();
|
||||
f.service.install("main", &candidate.token, false).unwrap();
|
||||
assert!(!f.service.row(&id).unwrap().enabled);
|
||||
assert!(f.service.install("main", &candidate.token, false).is_err());
|
||||
let invalid = review_remote(&mut f, b"not a zip".to_vec());
|
||||
assert!(invalid.error.is_some());
|
||||
archive(&archive_path, &[("../outside", b"bad")]);
|
||||
let invalid = review_remote(&mut f, fs::read(&archive_path).unwrap());
|
||||
assert!(invalid.error.is_some());
|
||||
assert!(!f.root.join("outside").exists());
|
||||
assert!(fs::read_dir(f.service.directory("plugin-staging").unwrap()).unwrap()
|
||||
.all(|entry| entry.unwrap().path().extension().is_none_or(|ext| ext != "zip")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Set RUNESTONE_GIT_TRIAL_PACKAGES to the folder with the four trial ZIPs."]
|
||||
fn four_plugin_release_trial() {
|
||||
let directory = PathBuf::from(std::env::var("RUNESTONE_GIT_TRIAL_PACKAGES").unwrap());
|
||||
let archives: Vec<_> = fs::read_dir(directory).unwrap().map(|entry| entry.unwrap().path())
|
||||
.filter(|path| path.extension().is_some_and(|ext| ext == "zip")).collect();
|
||||
assert_eq!(archives.len(), 4);
|
||||
let mut f = Fixture::new();
|
||||
let mut ids = BTreeSet::new();
|
||||
for path in archives {
|
||||
let expected = package(&path).unwrap();
|
||||
let candidate = review_remote(&mut f, fs::read(&path).unwrap());
|
||||
assert!(candidate.error.is_none(), "{:?}", candidate.error);
|
||||
let id = candidate.manifest.as_ref().unwrap().id.clone();
|
||||
f.service.install("main", &candidate.token, false).unwrap();
|
||||
assert_eq!(f.service.row(&id).unwrap().hash, expected.hash);
|
||||
assert!(!f.service.row(&id).unwrap().enabled);
|
||||
println!("Release download, review, and isolated install passed: {} {}", id, expected.manifest.version);
|
||||
ids.insert(id);
|
||||
}
|
||||
assert_eq!(ids.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Requires an approved live Gitea release and repository read access."]
|
||||
fn live_gitea_release_trial() {
|
||||
let repository = crate::plugin_sources::Repository::parse(
|
||||
&std::env::var("RUNESTONE_GIT_TRIAL_URL").unwrap(),
|
||||
&std::env::var("RUNESTONE_GIT_TRIAL_TAG").unwrap(),
|
||||
).unwrap();
|
||||
let token = std::env::var("RUNESTONE_GIT_TRIAL_TOKEN").ok();
|
||||
let download = crate::plugin_sources::download(&repository, token.as_deref()).unwrap();
|
||||
let expected = package(&PathBuf::from(std::env::var("RUNESTONE_GIT_TRIAL_ZIP").unwrap())).unwrap();
|
||||
let mut f = Fixture::new();
|
||||
let candidate = f.service.inspect_download("main", download).unwrap();
|
||||
assert!(candidate.error.is_none(), "{:?}", candidate.error);
|
||||
let id = candidate.manifest.as_ref().unwrap().id.clone();
|
||||
f.service.install("main", &candidate.token, false).unwrap();
|
||||
assert_eq!(f.service.row(&id).unwrap().hash, expected.hash);
|
||||
assert!(!f.service.row(&id).unwrap().enabled);
|
||||
println!("Live Gitea download, review, and isolated install passed: {} {}", id, expected.manifest.version);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn install_time_survives_reload_and_state_changes() {
|
||||
let mut f = Fixture::new();
|
||||
let before = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as u64;
|
||||
let id = f.install(false);
|
||||
let after = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as u64;
|
||||
let installed_at = f.service.row(&id).unwrap().installed_at.unwrap();
|
||||
assert!((before..=after).contains(&installed_at));
|
||||
let mut reopened = Service::open(f.root.join("app")).unwrap();
|
||||
reopened.catalog.plugins.get_mut(&id).unwrap().enabled = true;
|
||||
reopened.persist().unwrap();
|
||||
let mut reopened = Service::open(f.root.join("app")).unwrap();
|
||||
let snapshot = serde_json::to_value(reopened.snapshot().unwrap()).unwrap();
|
||||
assert_eq!(snapshot["plugins"][0]["installedAt"], installed_at);
|
||||
let plan = reopened.prepare("main", &id).unwrap();
|
||||
assert_eq!(plan.plugin.installed_at, Some(installed_at));
|
||||
reopened.begin_remove("main", &plan.token).unwrap();
|
||||
reopened.finish_remove(&id).unwrap();
|
||||
assert!(!reopened.catalog.plugins.contains_key(&id));
|
||||
f.service = reopened;
|
||||
f.install(true);
|
||||
assert!(f.service.row(&id).unwrap().installed_at.unwrap() >= installed_at);
|
||||
}
|
||||
#[test]
|
||||
fn legacy_and_discovered_plugins_keep_unknown_install_times() {
|
||||
let mut f = Fixture::new();
|
||||
let id = f.install(false);
|
||||
let path = f.root.join("app/plugin-state.json");
|
||||
let mut catalog = serde_json::to_value(&f.service.catalog).unwrap();
|
||||
catalog["plugins"][&id]
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.remove("installedAt");
|
||||
fs::write(&path, serde_json::to_vec(&catalog).unwrap()).unwrap();
|
||||
let mut reopened = Service::open(f.root.join("app")).unwrap();
|
||||
assert_eq!(reopened.snapshot().unwrap().plugins[0].installed_at, None);
|
||||
reopened.catalog.plugins.clear();
|
||||
reopened.persist().unwrap();
|
||||
assert_eq!(reopened.snapshot().unwrap().plugins[0].installed_at, None);
|
||||
let mut reopened = Service::open(f.root.join("app")).unwrap();
|
||||
assert_eq!(reopened.snapshot().unwrap().plugins[0].installed_at, None);
|
||||
}
|
||||
#[test]
|
||||
fn installer_accepts_only_zip_files() {
|
||||
let f = Fixture::new();
|
||||
assert!(require_zip(&f.source).is_err());
|
||||
assert!(require_zip(&f.source.join("plugin.json")).is_err());
|
||||
let legacy = f.root.join("demo.runestone-plugin");
|
||||
archive(&legacy, &[("main.js", b"test")]);
|
||||
assert!(require_zip(&legacy).is_err());
|
||||
assert!(package(&legacy).is_err());
|
||||
let upper = f.root.join("demo.ZIP");
|
||||
fs::copy(&legacy, &upper).unwrap();
|
||||
assert!(require_zip(&upper).is_ok());
|
||||
let wrapped = f.root.join("wrapped.zip");
|
||||
archive(&wrapped, &[("demo/plugin.json", b"{}")]);
|
||||
assert!(package(&wrapped).unwrap_err().contains("at its root"));
|
||||
}
|
||||
#[test]
|
||||
fn author_packager_produces_an_installable_repeatable_zip() {
|
||||
let mut f = Fixture::new();
|
||||
fs::create_dir(f.source.join("assets")).unwrap();
|
||||
fs::write(f.source.join("assets/style.css"), "body { color: red; }").unwrap();
|
||||
let script = Path::new(env!("CARGO_MANIFEST_DIR")).join("../scripts/packagePlugin.mjs");
|
||||
let output = f.root.join("releases");
|
||||
let build = || {
|
||||
let result = std::process::Command::new("node")
|
||||
.arg(&script)
|
||||
.arg(&f.source)
|
||||
.arg(&output)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(
|
||||
result.status.success(),
|
||||
"{}",
|
||||
String::from_utf8_lossy(&result.stderr)
|
||||
);
|
||||
};
|
||||
build();
|
||||
let path = output.join("org.test.demo-1.0.0.zip");
|
||||
let first = fs::read(&path).unwrap();
|
||||
build();
|
||||
assert_eq!(first, fs::read(&path).unwrap());
|
||||
assert_eq!(
|
||||
package(&path).unwrap().hash,
|
||||
package(&f.source).unwrap().hash
|
||||
);
|
||||
let row = f.service.inspect("main", path);
|
||||
assert!(row.error.is_none(), "{:?}", row.error);
|
||||
f.service.install("main", &row.token, false).unwrap();
|
||||
let installed = f.service.row("org.test.demo").unwrap();
|
||||
assert!(!installed.enabled);
|
||||
assert_eq!(
|
||||
fs::read(Path::new(&installed.path).join("assets/style.css")).unwrap(),
|
||||
b"body { color: red; }"
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn installs_directory_and_zip_without_changing_source() {
|
||||
let mut f = Fixture::new();
|
||||
let original = package(&f.source).unwrap();
|
||||
let archive_path = f.root.join("demo.runestone-plugin");
|
||||
let archive_path = f.root.join("demo.zip");
|
||||
let entries: Vec<_> = original
|
||||
.files
|
||||
.iter()
|
||||
@@ -1336,7 +1555,7 @@ mod tests {
|
||||
assert!(!valid_id(id), "{id}");
|
||||
}
|
||||
let f = Fixture::new();
|
||||
let path = f.root.join("bad.runestone-plugin");
|
||||
let path = f.root.join("bad.zip");
|
||||
archive(&path, &[("../outside", b"test")]);
|
||||
assert!(package(&path).is_err());
|
||||
assert!(!f.root.join("outside").exists());
|
||||
|
||||
Reference in New Issue
Block a user