From 5262710823ef08862fe264115a256abf79eca594 Mon Sep 17 00:00:00 2001 From: sviezypan Date: Tue, 6 Feb 2024 20:39:49 +0100 Subject: [PATCH] only rewrite file if content of module has changed --- auction_app.wit | 43 ------------------------------------------- src/wit_gen.rs | 45 +++++++++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 55 deletions(-) delete mode 100644 auction_app.wit diff --git a/auction_app.wit b/auction_app.wit deleted file mode 100644 index 6117a8d..0000000 --- a/auction_app.wit +++ /dev/null @@ -1,43 +0,0 @@ -package auction:app - -interface api { - - record bidder-id { - bidder-id: string, - } - - record auction-id { - auction-id: string, - } - - record auction { - auction-id: option, - name: string, - description: string, - starting-price: float32, - deadline: deadline, - } - - variant bid-result { - failure(string), - success - } - - type deadline = u64 - - initialize: func(auction: auction) - - bid: func(bidder-id: bidder-id, price: float32) -> bid-result - - close-auction: func() -> option - - create-bidder: func(name: string, address: string) -> bidder-id - - create-auction: func(name: string, description: string, starting-price: float32, deadline: u64) -> auction-id - - get-auctions: func() -> list -} - -world golem-service { - export api -} \ No newline at end of file diff --git a/src/wit_gen.rs b/src/wit_gen.rs index ba6acb5..79c1ed4 100644 --- a/src/wit_gen.rs +++ b/src/wit_gen.rs @@ -377,27 +377,48 @@ fn write_to_file( content: Vec, ast_span: Span, ) -> Result<()> { - let mut file = File::create(file_name.clone()) - .map_err(|e| syn::Error::new(ast_span, format!("Error while creating file {}", e)))?; - file.write_all( + let new_content = format!( - "package {} + "package {} interface api {{ - {} +{} }} world golem-service {{ export api }}", - package_name, - content.join("\n") - ) - .trim() - .as_bytes(), - ) - .map_err(|e| syn::Error::new(ast_span, format!("Error while writing to file {}", e)))?; + package_name, + content.join("\n")); + + match File::open(file_name.clone()) { + Ok(mut file) => { + let mut old_content = String::new(); + + file.read_to_string(&mut old_content) + .map_err(|e| syn::Error::new(ast_span, format!("Error while reading from file {}", e)))?; + + if old_content == new_content { + // do nothing + Ok(()) + } else { + // recreate file + create_and_write_to_file(file_name, new_content, ast_span) + } + }, + Err(_) => { + create_and_write_to_file(file_name, new_content, ast_span) + } + } +} + +fn create_and_write_to_file(file_name: String, new_content: String, ast_span: Span) -> Result<()> { + let mut file = File::create(file_name.clone()) + .map_err(|e| syn::Error::new(ast_span, format!("Error while creating file {}", e)))?; + + file.write_all(new_content.trim().as_bytes()) + .map_err(|e| syn::Error::new(ast_span, format!("Error while writing to file {}", e)))?; Ok(()) }