diff --git a/compiler/crates/relay-compiler/src/build_project/artifact_writer.rs b/compiler/crates/relay-compiler/src/build_project/artifact_writer.rs index 83578809f02d7..44b0278be972b 100644 --- a/compiler/crates/relay-compiler/src/build_project/artifact_writer.rs +++ b/compiler/crates/relay-compiler/src/build_project/artifact_writer.rs @@ -12,14 +12,10 @@ use std::io; use std::io::prelude::*; use std::path::Path; use std::path::PathBuf; -use std::sync::atomic::AtomicUsize; use std::sync::Mutex; -use common::sync::Ordering::Acquire; use dashmap::DashSet; use log::info; -use serde::Serialize; -use serde::Serializer; use sha1::Digest; use sha1::Sha1; @@ -123,207 +119,6 @@ impl ArtifactWriter for ArtifactFileWriter { } } -#[derive(Serialize)] -struct CodegenRecords { - pub removed: Vec, - pub changed: Vec, -} - -#[derive(Serialize)] -struct ArtifactDeletionRecord { - pub path: PathBuf, -} - -#[derive(Serialize)] -struct ArtifactUpdateRecord { - pub path: PathBuf, - #[serde(serialize_with = "from_utf8")] - pub data: Vec, -} - -fn from_utf8(slice: &[u8], s: S) -> Result -where - S: Serializer, -{ - s.serialize_str(std::str::from_utf8(slice).unwrap()) -} - -pub struct ArtifactDifferenceWriter { - codegen_records: Mutex, - codegen_filepath: PathBuf, - verify_changes_against_filesystem: bool, -} - -impl ArtifactDifferenceWriter { - pub fn new( - codegen_filepath: PathBuf, - verify_changes_against_filesystem: bool, - ) -> ArtifactDifferenceWriter { - ArtifactDifferenceWriter { - codegen_filepath, - codegen_records: Mutex::new(CodegenRecords { - changed: Vec::new(), - removed: Vec::new(), - }), - verify_changes_against_filesystem, - } - } -} - -impl ArtifactWriter for ArtifactDifferenceWriter { - fn should_write( - &self, - path: &Path, - content: &[u8], - hash: Option, - ) -> Result { - let op = |error| BuildProjectError::WriteFileError { - file: path.to_owned(), - source: error, - }; - if !self.verify_changes_against_filesystem { - Ok(true) - } else if let Some(file_hash) = hash { - hash_is_different(file_hash, content).map_err(op) - } else { - content_is_different(path, content).map_err(op) - } - } - - fn write(&self, path: PathBuf, content: Vec) -> BuildProjectResult { - self.codegen_records - .lock() - .unwrap() - .changed - .push(ArtifactUpdateRecord { - path, - data: content, - }); - Ok(()) - } - - fn remove(&self, path: PathBuf) -> BuildProjectResult { - if path.exists() { - self.codegen_records - .lock() - .unwrap() - .removed - .push(ArtifactDeletionRecord { path }); - } - Ok(()) - } - - fn finalize(&self) -> crate::errors::Result<()> { - (|| { - let mut file = File::create(&self.codegen_filepath)?; - file.write_all(serde_json::to_string(&self.codegen_records)?.as_bytes()) - })() - .map_err(|error| crate::errors::Error::WriteFileError { - file: self.codegen_filepath.clone(), - source: error, - }) - } -} - -#[derive(Serialize)] -struct ArtifactUpdateShardedRecord { - pub path: PathBuf, - pub index: usize, -} - -#[derive(Serialize)] -struct CodegenShardedRecords { - pub removed: Vec, - pub changed: Vec, -} -pub struct ArtifactDifferenceShardedWriter { - codegen_records: Mutex, - codegen_filepath: PathBuf, - codegen_shard_directory: PathBuf, - verify_changes_against_filesystem: bool, - codegen_index: AtomicUsize, -} - -impl ArtifactDifferenceShardedWriter { - pub fn new( - codegen_filepath: PathBuf, - codegen_shard_directory: PathBuf, - verify_changes_against_filesystem: bool, - ) -> Self { - Self { - codegen_filepath, - codegen_records: Mutex::new(CodegenShardedRecords { - changed: Vec::new(), - removed: Vec::new(), - }), - codegen_shard_directory, - verify_changes_against_filesystem, - codegen_index: AtomicUsize::new(0), - } - } -} - -impl ArtifactWriter for ArtifactDifferenceShardedWriter { - fn should_write( - &self, - path: &Path, - content: &[u8], - hash: Option, - ) -> Result { - let op = |error| BuildProjectError::WriteFileError { - file: path.to_owned(), - source: error, - }; - if !self.verify_changes_against_filesystem { - Ok(true) - } else if let Some(file_hash) = hash { - hash_is_different(file_hash, content).map_err(op) - } else { - content_is_different(path, content).map_err(op) - } - } - - fn write(&self, path: PathBuf, content: Vec) -> BuildProjectResult { - let index = self.codegen_index.fetch_add(1, Acquire); - (|| { - let mut file = File::create(self.codegen_shard_directory.join(index.to_string()))?; - file.write_all(&content) - })() - .map_err(|error| BuildProjectError::WriteFileError { - file: path.to_owned(), - source: error, - })?; - self.codegen_records - .lock() - .unwrap() - .changed - .push(ArtifactUpdateShardedRecord { path, index }); - Ok(()) - } - - fn remove(&self, path: PathBuf) -> BuildProjectResult { - if path.exists() { - self.codegen_records - .lock() - .unwrap() - .removed - .push(ArtifactDeletionRecord { path }); - } - Ok(()) - } - - fn finalize(&self) -> crate::errors::Result<()> { - (|| { - let mut file = File::create(&self.codegen_filepath)?; - file.write_all(serde_json::to_string(&self.codegen_records)?.as_bytes()) - })() - .map_err(|error| crate::errors::Error::WriteFileError { - file: self.codegen_filepath.clone(), - source: error, - }) - } -} - fn ensure_file_directory_exists(file_path: &Path) -> io::Result<()> { if let Some(file_directory) = file_path.parent() { if !file_directory.exists() { diff --git a/compiler/crates/relay-compiler/src/lib.rs b/compiler/crates/relay-compiler/src/lib.rs index f9f62799bd8cd..88d23692e49eb 100644 --- a/compiler/crates/relay-compiler/src/lib.rs +++ b/compiler/crates/relay-compiler/src/lib.rs @@ -25,8 +25,6 @@ pub mod status_reporter; mod utils; pub use artifact_map::ArtifactSourceKey; -pub use build_project::artifact_writer::ArtifactDifferenceShardedWriter; -pub use build_project::artifact_writer::ArtifactDifferenceWriter; pub use build_project::artifact_writer::ArtifactFileWriter; pub use build_project::artifact_writer::ArtifactValidationWriter; pub use build_project::artifact_writer::ArtifactWriter;