From 97b74258949e3b92f2aabfdc75ead48737f0295c Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Wed, 20 Sep 2023 21:48:19 +0100 Subject: [PATCH] Updated the logic in "wasmer init" to not assume a [dependencies] section is always present in the rendered wasmer.toml --- lib/cli/src/commands/init.rs | 39 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/cli/src/commands/init.rs b/lib/cli/src/commands/init.rs index 8e1058586c7..ec6fc7f365b 100644 --- a/lib/cli/src/commands/init.rs +++ b/lib/cli/src/commands/init.rs @@ -153,21 +153,40 @@ impl Init { Self::write_wasmer_toml(&target_file, &constructed_manifest) } - /// Writes the metadata to a wasmer.toml file + /// Writes the metadata to a wasmer.toml file, making sure we include the + /// [`NOTE`] so people get a link to the registry docs. fn write_wasmer_toml( path: &PathBuf, toml: &wasmer_toml::Manifest, ) -> Result<(), anyhow::Error> { - let toml_string = toml::to_string_pretty(&toml)? - .replace( - "[dependencies]", - &format!("{NOTE}{NEWLINE}{NEWLINE}[dependencies]"), - ) - .lines() - .collect::>() - .join(NEWLINE); + let toml_string = toml::to_string_pretty(&toml)?; + + let mut resulting_string = String::new(); + let mut note_inserted = false; + + for line in toml_string.lines() { + resulting_string.push_str(line); + + if !note_inserted && line.is_empty() { + // We've found an empty line after the initial [package] + // section. Let's add our note here. + resulting_string.push_str(NEWLINE); + resulting_string.push_str(NOTE); + resulting_string.push_str(NEWLINE); + note_inserted = true; + } + resulting_string.push_str(NEWLINE); + } + + if !note_inserted { + // Make sure the note still ends up at the end of the file. + resulting_string.push_str(NEWLINE); + resulting_string.push_str(NOTE); + resulting_string.push_str(NEWLINE); + resulting_string.push_str(NEWLINE); + } - std::fs::write(path, toml_string) + std::fs::write(path, resulting_string) .with_context(|| format!("Unable to write to \"{}\"", path.display()))?; Ok(())