mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-24 02:31:44 +03:00
Fix manifest update logic
This commit is contained in:
parent
beb4ed420f
commit
a036dc8ac3
@ -44,6 +44,6 @@ version = "1.3.0"
|
||||
|
||||
[features]
|
||||
default = [ "manifest_refactors" ]
|
||||
manifest_refactors = [ "manifest_refactor_remote", "manifest_refactor_project" ]
|
||||
manifest_refactors = [ "manifest_refactor_project", "manifest_refactor_remote" ]
|
||||
manifest_refactor_project = [ ]
|
||||
manifest_refactor_remote = [ ]
|
||||
manifest_refactor_project = [ ]
|
@ -128,43 +128,56 @@ 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);
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
|
||||
// Determine if the new remote format is being used
|
||||
if line.starts_with("[remote]") {
|
||||
new_remote_format_exists = true;
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Determine if the new remote format is being used
|
||||
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.
|
||||
#[cfg(feature = "manifest_refactor_project")]
|
||||
{
|
||||
if line.starts_with("[package]") {
|
||||
new_toml += "[project]";
|
||||
} else {
|
||||
new_toml += line;
|
||||
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")]
|
||||
{
|
||||
refactored_toml += "[project]";
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "manifest_refactor_project"))]
|
||||
{
|
||||
new_toml += line;
|
||||
#[cfg(not(feature = "manifest_refactor_project"))]
|
||||
{
|
||||
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))?)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user