Skip to content

Commit 330a40f

Browse files
authored
Only overwrite Codable.swift if it has changed. (1Password#205)
* Only overwrite Codable if it has changed. * Avoid cloning expected output.
1 parent 41de151 commit 330a40f

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

core/src/language/swift.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use lazy_format::lazy_format;
1414
use std::{
1515
borrow::Cow,
1616
collections::{BTreeSet, HashMap},
17-
fs::File,
17+
fs,
1818
io::{self, Write},
1919
path::Path,
2020
sync::atomic::{AtomicBool, Ordering},
@@ -231,7 +231,7 @@ impl Language for Swift {
231231

232232
fn end_file(&mut self, w: &mut dyn Write) -> io::Result<()> {
233233
if self.should_emit_codable_void.load(Ordering::SeqCst) && !self.multi_file {
234-
self.write_codable(w)?;
234+
self.write_codable(w, &self.get_codable_contents())?;
235235
}
236236

237237
Ok(())
@@ -756,18 +756,20 @@ impl Swift {
756756
/// When using multiple file generation we write this into a separate module vs at the
757757
/// end of the generated file.
758758
fn write_codable_file(&self, output_folder: &str) -> std::io::Result<()> {
759-
let mut w = File::create(Path::new(output_folder).join("Codable.swift"))?;
760-
self.write_codable(&mut w)
761-
}
759+
let output_string = self.get_codable_contents();
760+
let output_path = Path::new(output_folder).join("Codable.swift");
762761

763-
/// Write the `CodableVoid` type.
764-
fn write_codable(&self, w: &mut dyn Write) -> io::Result<()> {
765-
writeln!(w)?;
766-
writeln!(
767-
w,
768-
r"/// () isn't codable, so we use this instead to represent Rust's unit type"
769-
)?;
762+
if let Ok(buf) = fs::read(&output_path) {
763+
if buf == output_string.as_bytes() {
764+
return Ok(());
765+
}
766+
}
767+
768+
let mut w = fs::File::create(output_path)?;
769+
self.write_codable(&mut w, &output_string)
770+
}
770771

772+
fn get_codable_contents(&self) -> String {
771773
let mut decs = self
772774
.get_default_decorators()
773775
.chain(self.codablevoid_constraints.iter().map(|s| s.as_str()))
@@ -778,7 +780,12 @@ impl Swift {
778780
decs.push(CODABLE);
779781
}
780782

781-
writeln!(w, "public struct CodableVoid: {} {{}}", decs.join(", "))
783+
format!("\n/// () isn't codable, so we use this instead to represent Rust's unit type\npublic struct CodableVoid: {} {{}}", decs.join(", "))
784+
}
785+
786+
/// Write the `CodableVoid` type.
787+
fn write_codable(&self, w: &mut dyn Write, output_string: &str) -> io::Result<()> {
788+
writeln!(w, "{}", output_string)
782789
}
783790

784791
/// Build the generic constraints output. This checks for the `swiftGenericConstraints` typeshare attribute and combines

0 commit comments

Comments
 (0)