From 7450df7af7c915bb9ffab5a2da786a02d101b8a3 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Tue, 6 Feb 2024 14:53:54 -0800 Subject: [PATCH 1/9] Implement Vfs::read_to_string_lf_normalized --- crates/memofs/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/memofs/src/lib.rs b/crates/memofs/src/lib.rs index 744ca60fc..b3931b29b 100644 --- a/crates/memofs/src/lib.rs +++ b/crates/memofs/src/lib.rs @@ -22,9 +22,9 @@ mod noop_backend; mod snapshot; mod std_backend; -use std::io; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex, MutexGuard}; +use std::{io, str}; pub use in_memory_fs::InMemoryFs; pub use noop_backend::NoopBackend; @@ -155,6 +155,26 @@ impl VfsInner { Ok(Arc::new(contents)) } + fn read_to_string_lf_normalized>(&mut self, path: P) -> io::Result> { + let path = path.as_ref(); + let contents = self.backend.read(path)?; + + if self.watch_enabled { + self.backend.watch(path)?; + } + + let contents_str = str::from_utf8(&contents).map_err(|_| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("File was not valid UTF-8: {}", path.display()), + ) + })?; + + Ok(Arc::new( + contents_str.lines().collect::>().join("\n"), + )) + } + fn write, C: AsRef<[u8]>>(&mut self, path: P, contents: C) -> io::Result<()> { let path = path.as_ref(); let contents = contents.as_ref(); @@ -258,6 +278,23 @@ impl Vfs { self.inner.lock().unwrap().read(path) } + /// Read a file from the VFS, or the underlying backend if it isn't + /// resident. + /// + /// Roughly equivalent to [`std::fs::read_to_string`][std::fs::read_to_string], but also performs + /// line ending normalization. This method first confirms that the file + /// contains UTF-8, then converts any line endings to LF. + /// + /// [std::fs::read_to_string]: https://doc.rust-lang.org/stable/std/fs/fn.read_to_string.html + #[inline] + pub fn read_to_string_lf_normalized>(&self, path: P) -> io::Result> { + let path = path.as_ref(); + self.inner + .lock() + .unwrap() + .read_to_string_lf_normalized(path) + } + /// Write a file to the VFS and the underlying backend. /// /// Roughly equivalent to [`std::fs::write`][std::fs::write]. From aec7a40b0b9aa64bfe54ece0129966854578e8c6 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Tue, 6 Feb 2024 14:54:12 -0800 Subject: [PATCH 2/9] Use Vfs::read_to_string_lf_normalized in Lua middleware --- src/snapshot_middleware/lua.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/snapshot_middleware/lua.rs b/src/snapshot_middleware/lua.rs index ab7f15a1f..3f8d26adf 100644 --- a/src/snapshot_middleware/lua.rs +++ b/src/snapshot_middleware/lua.rs @@ -1,6 +1,5 @@ use std::{collections::HashMap, path::Path, str}; -use anyhow::Context; use memofs::{IoResultExt, Vfs}; use rbx_dom_weak::types::Enum; @@ -40,10 +39,8 @@ pub fn snapshot_lua( (_, ScriptType::Module) => ("ModuleScript", None), }; - let contents = vfs.read(path)?; - let contents_str = str::from_utf8(&contents) - .with_context(|| format!("File was not valid UTF-8: {}", path.display()))? - .to_owned(); + let contents = vfs.read_to_string_lf_normalized(path)?; + let contents_str = contents.as_str(); let mut properties = HashMap::with_capacity(2); properties.insert("Source".to_owned(), contents_str.into()); From e846ab7b8bc004165c8197f394d5f1c0e956fbc2 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Tue, 6 Feb 2024 14:54:25 -0800 Subject: [PATCH 3/9] Use Vfs::read_to_string_lf_normalized in text middleware --- src/snapshot_middleware/txt.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index 207243f9f..9403f9609 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -1,6 +1,5 @@ use std::{path::Path, str}; -use anyhow::Context; use maplit::hashmap; use memofs::{IoResultExt, Vfs}; @@ -14,10 +13,8 @@ pub fn snapshot_txt( path: &Path, name: &str, ) -> anyhow::Result> { - let contents = vfs.read(path)?; - let contents_str = str::from_utf8(&contents) - .with_context(|| format!("File was not valid UTF-8: {}", path.display()))? - .to_owned(); + let contents = vfs.read_to_string_lf_normalized(path)?; + let contents_str = contents.as_str(); let properties = hashmap! { "Value".to_owned() => contents_str.into(), From 205b0bf1da10b918851073c21a362f66c7cb0643 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Tue, 6 Feb 2024 15:00:20 -0800 Subject: [PATCH 4/9] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aef0cc15..c4c19617a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Rojo Changelog ## Unreleased Changes +* Rojo now converts any line endings to LF, preventing spurious diffs when syncing text or Lua files on Windows (#[854]) * Fixed Rojo plugin failing to connect when project contains certain unreadable properties ([#848]) * Added popout diff visualizer for table properties like Attributes and Tags ([#834]) * Updated Theme to use Studio colors ([#838]) @@ -59,6 +60,7 @@ [#840]: https://github.com/rojo-rbx/rojo/pull/840 [#847]: https://github.com/rojo-rbx/rojo/pull/847 [#848]: https://github.com/rojo-rbx/rojo/pull/848 +[#854]: https://github.com/rojo-rbx/rojo/pull/854 ## [7.4.0] - January 16, 2024 From e108f456aff731c880a6a5703a54bcb1e4d98f8c Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Tue, 6 Feb 2024 15:01:32 -0800 Subject: [PATCH 5/9] I can type good --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4c19617a..a792d78f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Rojo Changelog ## Unreleased Changes -* Rojo now converts any line endings to LF, preventing spurious diffs when syncing text or Lua files on Windows (#[854]) +* Rojo now converts any line endings to LF, preventing spurious diffs when syncing text or Lua files on Windows ([#854]) * Fixed Rojo plugin failing to connect when project contains certain unreadable properties ([#848]) * Added popout diff visualizer for table properties like Attributes and Tags ([#834]) * Updated Theme to use Studio colors ([#838]) From d0aeb6a86242f854d046061ce4a3c1ad23991267 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Tue, 6 Feb 2024 15:12:05 -0800 Subject: [PATCH 6/9] Add memofs changelog entry --- crates/memofs/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/memofs/CHANGELOG.md b/crates/memofs/CHANGELOG.md index c9961d0ed..3094f3534 100644 --- a/crates/memofs/CHANGELOG.md +++ b/crates/memofs/CHANGELOG.md @@ -2,8 +2,10 @@ ## Unreleased Changes * Changed `StdBackend` file watching component to use minimal recursive watches. [#830] +* Added `Vfs::read_to_string_lf_normalized` [#854] [#830]: https://github.com/rojo-rbx/rojo/pull/830 +[#854]: https://github.com/rojo-rbx/rojo/pull/854 ## 0.2.0 (2021-08-23) * Updated to `crossbeam-channel` 0.5.1. From 845217061a916a8128fe35d1c2bbf1d2c441b17b Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Thu, 8 Feb 2024 12:32:49 -0800 Subject: [PATCH 7/9] Refactor: Vfs::read_to_string --- crates/memofs/CHANGELOG.md | 2 +- crates/memofs/src/lib.rs | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/memofs/CHANGELOG.md b/crates/memofs/CHANGELOG.md index 3094f3534..d678f8c9d 100644 --- a/crates/memofs/CHANGELOG.md +++ b/crates/memofs/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased Changes * Changed `StdBackend` file watching component to use minimal recursive watches. [#830] -* Added `Vfs::read_to_string_lf_normalized` [#854] +* Added `Vfs::read_to_string` and `Vfs::read_to_string_lf_normalized` [#854] [#830]: https://github.com/rojo-rbx/rojo/pull/830 [#854]: https://github.com/rojo-rbx/rojo/pull/854 diff --git a/crates/memofs/src/lib.rs b/crates/memofs/src/lib.rs index b3931b29b..6d41c853b 100644 --- a/crates/memofs/src/lib.rs +++ b/crates/memofs/src/lib.rs @@ -155,7 +155,7 @@ impl VfsInner { Ok(Arc::new(contents)) } - fn read_to_string_lf_normalized>(&mut self, path: P) -> io::Result> { + fn read_to_string>(&mut self, path: P) -> io::Result> { let path = path.as_ref(); let contents = self.backend.read(path)?; @@ -170,9 +170,7 @@ impl VfsInner { ) })?; - Ok(Arc::new( - contents_str.lines().collect::>().join("\n"), - )) + Ok(Arc::new(contents_str.into())) } fn write, C: AsRef<[u8]>>(&mut self, path: P, contents: C) -> io::Result<()> { @@ -278,21 +276,31 @@ impl Vfs { self.inner.lock().unwrap().read(path) } - /// Read a file from the VFS, or the underlying backend if it isn't - /// resident. + /// Read a file from the VFS (or from the underlying backend if it isn't + /// resident) into a string. + /// + /// Roughly equivalent to [`std::fs::read_to_string`][std::fs::read_to_string]. + /// + /// [std::fs::read_to_string]: https://doc.rust-lang.org/stable/std/fs/fn.read_to_string.html + #[inline] + pub fn read_to_string>(&self, path: P) -> io::Result> { + let path = path.as_ref(); + self.inner.lock().unwrap().read_to_string(path) + } + + /// Read a file from the VFS (or the underlying backend if it isn't + /// resident) into a string, and normalize its line endings to LF. /// /// Roughly equivalent to [`std::fs::read_to_string`][std::fs::read_to_string], but also performs - /// line ending normalization. This method first confirms that the file - /// contains UTF-8, then converts any line endings to LF. + /// line ending normalization. /// /// [std::fs::read_to_string]: https://doc.rust-lang.org/stable/std/fs/fn.read_to_string.html #[inline] pub fn read_to_string_lf_normalized>(&self, path: P) -> io::Result> { let path = path.as_ref(); - self.inner - .lock() - .unwrap() - .read_to_string_lf_normalized(path) + let contents = self.inner.lock().unwrap().read_to_string(path)?; + + Ok(contents.lines().collect::>().join("\n").into()) } /// Write a file to the VFS and the underlying backend. From 548eae74698ed1703bb6699ff8ce30c002f1e984 Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Thu, 8 Feb 2024 12:33:56 -0800 Subject: [PATCH 8/9] Do not normalize line endings of text files --- src/snapshot_middleware/txt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/snapshot_middleware/txt.rs b/src/snapshot_middleware/txt.rs index 9403f9609..a69b14d1b 100644 --- a/src/snapshot_middleware/txt.rs +++ b/src/snapshot_middleware/txt.rs @@ -13,7 +13,7 @@ pub fn snapshot_txt( path: &Path, name: &str, ) -> anyhow::Result> { - let contents = vfs.read_to_string_lf_normalized(path)?; + let contents = vfs.read_to_string(path)?; let contents_str = contents.as_str(); let properties = hashmap! { From 21a636b5a53a430fc2777d700b40bd49914244dc Mon Sep 17 00:00:00 2001 From: kennethloeffler Date: Thu, 8 Feb 2024 12:35:18 -0800 Subject: [PATCH 9/9] Update changelog for accuracy --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a792d78f4..d15a6f235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Rojo Changelog ## Unreleased Changes -* Rojo now converts any line endings to LF, preventing spurious diffs when syncing text or Lua files on Windows ([#854]) +* Rojo now converts any line endings to LF, preventing spurious diffs when syncing Lua files on Windows ([#854]) * Fixed Rojo plugin failing to connect when project contains certain unreadable properties ([#848]) * Added popout diff visualizer for table properties like Attributes and Tags ([#834]) * Updated Theme to use Studio colors ([#838])