Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only rewrite WIT file if content of module has changed #7

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions auction_app.wit

This file was deleted.

45 changes: 33 additions & 12 deletions src/wit_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,27 +377,48 @@ fn write_to_file(
content: Vec<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(
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(())
}
Loading