Skip to content

Commit 93e9c51

Browse files
Fix rojo plugin install by adding Vfs::exists (#1169)
1 parent 015b5bd commit 93e9c51

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

crates/memofs/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# memofs Changelog
22

33
## Unreleased Changes
4+
* Added `Vfs::exists`. [#1169]
5+
* Added `create_dir` and `create_dir_all` to allow creating directories. [#937]
6+
7+
[#1169]: https://github.com/rojo-rbx/rojo/pull/1169
8+
[#937]: https://github.com/rojo-rbx/rojo/pull/937
49

510
## 0.3.0 (2024-03-15)
611
* Changed `StdBackend` file watching component to use minimal recursive watches. [#830]
712
* Added `Vfs::read_to_string` and `Vfs::read_to_string_lf_normalized` [#854]
8-
* Added `create_dir` and `create_dir_all` to allow creating directories.
913

1014
[#830]: https://github.com/rojo-rbx/rojo/pull/830
1115
[#854]: https://github.com/rojo-rbx/rojo/pull/854

crates/memofs/src/in_memory_fs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ impl VfsBackend for InMemoryFs {
157157
)
158158
}
159159

160+
fn exists(&mut self, path: &Path) -> io::Result<bool> {
161+
let inner = self.inner.lock().unwrap();
162+
Ok(inner.entries.contains_key(path))
163+
}
164+
160165
fn read_dir(&mut self, path: &Path) -> io::Result<ReadDir> {
161166
let inner = self.inner.lock().unwrap();
162167

crates/memofs/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ impl<T> IoResultExt<T> for io::Result<T> {
7070
pub trait VfsBackend: sealed::Sealed + Send + 'static {
7171
fn read(&mut self, path: &Path) -> io::Result<Vec<u8>>;
7272
fn write(&mut self, path: &Path, data: &[u8]) -> io::Result<()>;
73+
fn exists(&mut self, path: &Path) -> io::Result<bool>;
7374
fn read_dir(&mut self, path: &Path) -> io::Result<ReadDir>;
7475
fn create_dir(&mut self, path: &Path) -> io::Result<()>;
7576
fn create_dir_all(&mut self, path: &Path) -> io::Result<()>;
@@ -175,6 +176,11 @@ impl VfsInner {
175176
Ok(Arc::new(contents_str.into()))
176177
}
177178

179+
fn exists<P: AsRef<Path>>(&mut self, path: P) -> io::Result<bool> {
180+
let path = path.as_ref();
181+
self.backend.exists(path)
182+
}
183+
178184
fn write<P: AsRef<Path>, C: AsRef<[u8]>>(&mut self, path: P, contents: C) -> io::Result<()> {
179185
let path = path.as_ref();
180186
let contents = contents.as_ref();
@@ -338,6 +344,17 @@ impl Vfs {
338344
self.inner.lock().unwrap().read_dir(path)
339345
}
340346

347+
/// Return whether the given path exists.
348+
///
349+
/// Roughly equivalent to [`std::fs::exists`][std::fs::exists].
350+
///
351+
/// [std::fs::exists]: https://doc.rust-lang.org/stable/std/fs/fn.exists.html
352+
#[inline]
353+
pub fn exists<P: AsRef<Path>>(&self, path: P) -> io::Result<bool> {
354+
let path = path.as_ref();
355+
self.inner.lock().unwrap().exists(path)
356+
}
357+
341358
/// Creates a directory at the provided location.
342359
///
343360
/// Roughly equivalent to [`std::fs::create_dir`][std::fs::create_dir].

crates/memofs/src/noop_backend.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ impl VfsBackend for NoopBackend {
2222
Err(io::Error::other("NoopBackend doesn't do anything"))
2323
}
2424

25+
fn exists(&mut self, _path: &Path) -> io::Result<bool> {
26+
Err(io::Error::other("NoopBackend doesn't do anything"))
27+
}
28+
2529
fn read_dir(&mut self, _path: &Path) -> io::Result<ReadDir> {
2630
Err(io::Error::other("NoopBackend doesn't do anything"))
2731
}

crates/memofs/src/std_backend.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ impl VfsBackend for StdBackend {
6363
fs_err::write(path, data)
6464
}
6565

66+
fn exists(&mut self, path: &Path) -> io::Result<bool> {
67+
std::fs::exists(path)
68+
}
69+
6670
fn read_dir(&mut self, path: &Path) -> io::Result<ReadDir> {
6771
let entries: Result<Vec<_>, _> = fs_err::read_dir(path)?.collect();
6872
let mut entries = entries?;

src/cli/plugin.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ impl PluginSubcommand {
4646
}
4747
}
4848

49-
fn install_plugin() -> anyhow::Result<()> {
49+
fn initialize_plugin() -> anyhow::Result<ServeSession> {
5050
let plugin_snapshot: VfsSnapshot = bincode::deserialize(PLUGIN_BINCODE)
5151
.expect("Rojo's plugin was not properly packed into Rojo's binary");
5252

53+
let mut in_memory_fs = InMemoryFs::new();
54+
in_memory_fs.load_snapshot("/plugin", plugin_snapshot)?;
55+
56+
let vfs = Vfs::new(in_memory_fs);
57+
Ok(ServeSession::new(vfs, "/plugin")?)
58+
}
59+
60+
fn install_plugin() -> anyhow::Result<()> {
5361
let studio = RobloxStudio::locate()?;
5462

5563
let plugins_folder_path = studio.plugins_path();
@@ -59,17 +67,12 @@ fn install_plugin() -> anyhow::Result<()> {
5967
fs::create_dir(plugins_folder_path)?;
6068
}
6169

62-
let mut in_memory_fs = InMemoryFs::new();
63-
in_memory_fs.load_snapshot("/plugin", plugin_snapshot)?;
64-
65-
let vfs = Vfs::new(in_memory_fs);
66-
let session = ServeSession::new(vfs, "/plugin")?;
67-
6870
let plugin_path = plugins_folder_path.join(PLUGIN_FILE_NAME);
6971
log::debug!("Writing plugin to {}", plugin_path.display());
7072

7173
let mut file = BufWriter::new(File::create(plugin_path)?);
7274

75+
let session = initialize_plugin()?;
7376
let tree = session.tree();
7477
let root_id = tree.get_root_id();
7578

@@ -92,3 +95,8 @@ fn uninstall_plugin() -> anyhow::Result<()> {
9295

9396
Ok(())
9497
}
98+
99+
#[test]
100+
fn plugin_initialize() {
101+
assert!(initialize_plugin().is_ok())
102+
}

src/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl Project {
295295
// Check for default projects.
296296
for default_project_name in DEFAULT_PROJECT_NAMES {
297297
let project_path = path.join(default_project_name);
298-
if project_path.exists() {
298+
if let Ok(true) = vfs.exists(&project_path) {
299299
return Self::load_exact(vfs, &project_path, None);
300300
}
301301
}

0 commit comments

Comments
 (0)