Fix manifest update logic

This commit is contained in:
raychu86 2020-09-13 18:49:45 -07:00
parent beb4ed420f
commit a036dc8ac3
2 changed files with 49 additions and 30 deletions

View File

@ -44,6 +44,6 @@ version = "1.3.0"
[features]
default = [ "manifest_refactors" ]
manifest_refactors = [ "manifest_refactor_remote", "manifest_refactor_project" ]
manifest_refactor_remote = [ ]
manifest_refactors = [ "manifest_refactor_project", "manifest_refactor_remote" ]
manifest_refactor_project = [ ]
manifest_refactor_remote = [ ]

View File

@ -128,18 +128,27 @@ impl TryFrom<&PathBuf> for Manifest {
let mut old_remote_format: Option<&str> = None;
let mut new_remote_format_exists = false;
let mut new_toml = "".to_owned();
// Toml file adhering to the new format.
let mut final_toml = "".to_owned();
// New Toml file format that should be written based on feature flags.
let mut refactored_toml = "".to_owned();
// Read each individual line of the toml file
for line in buffer.lines() {
// Determine if the old remote format is being used
#[cfg(feature = "manifest_refactor_remote")]
{
if line.starts_with("remote") {
let remote = line
.split("=") // Split the line as 'remote' = '"{author}/{package_name}"'
.collect::<Vec<&str>>()[1]; // Fetch just '"{author}/{package_name}"'
old_remote_format = Some(remote);
// Retain the old remote format if the `manifest_refactor_remote` is not enabled
#[cfg(not(feature = "manifest_refactor_remote"))]
{
refactored_toml += line;
refactored_toml += "\n";
}
continue;
}
@ -147,24 +156,28 @@ impl TryFrom<&PathBuf> for Manifest {
if line.starts_with("[remote]") {
new_remote_format_exists = true;
}
}
// If the old project format is being being used, update the toml file
// to use the new format instead.
if line.starts_with("[package]") {
final_toml += "[project]";
// Refactor the old project format if the `manifest_refactor_project` is enabled
#[cfg(feature = "manifest_refactor_project")]
{
if line.starts_with("[package]") {
new_toml += "[project]";
} else {
new_toml += line;
}
refactored_toml += "[project]";
}
#[cfg(not(feature = "manifest_refactor_project"))]
{
new_toml += line;
refactored_toml += line;
}
} else {
final_toml += line;
refactored_toml += line;
}
new_toml += "\n";
final_toml += "\n";
refactored_toml += "\n";
}
// Update the remote format
@ -188,18 +201,24 @@ author = "{author}"
);
// Append the new remote to the bottom of the manifest file.
new_toml += &new_remote;
final_toml += &new_remote;
// Add the new remote format if the `manifest_refactor_remote` is enabled
#[cfg(feature = "manifest_refactor_remote")]
{
refactored_toml += &new_remote;
}
}
}
// Rewrite the toml file if it has been updated
if buffer != new_toml {
if buffer != refactored_toml {
let mut file = File::create(&path).map_err(|error| ManifestError::Creating(MANIFEST_FILENAME, error))?;
file.write_all(new_toml.as_bytes())
file.write_all(refactored_toml.as_bytes())
.map_err(|error| ManifestError::Writing(MANIFEST_FILENAME, error))?;
}
// Read the toml file
Ok(toml::from_str(&new_toml).map_err(|error| ManifestError::Parsing(MANIFEST_FILENAME, error))?)
Ok(toml::from_str(&final_toml).map_err(|error| ManifestError::Parsing(MANIFEST_FILENAME, error))?)
}
}