Add impl to replace old remote format

This commit is contained in:
raychu86 2020-08-28 21:45:08 -07:00
parent f5aba15d35
commit d69343f14a

View File

@ -106,7 +106,7 @@ description = "The {name} package"
license = "MIT"
[remote]
author = "[AUTHOR]"
author = "[AUTHOR]" # Add your Aleo Package Manager username, team's name, or organization's name.
"#,
name = self.package.name
)
@ -122,7 +122,7 @@ impl TryFrom<&PathBuf> for Manifest {
path.push(PathBuf::from(MANIFEST_FILE_NAME));
}
let mut file = File::open(path).map_err(|error| ManifestError::Opening(MANIFEST_FILE_NAME, error))?;
let mut file = File::open(path.clone()).map_err(|error| ManifestError::Opening(MANIFEST_FILE_NAME, error))?;
let size = file
.metadata()
.map_err(|error| ManifestError::Metadata(MANIFEST_FILE_NAME, error))?
@ -132,6 +132,65 @@ impl TryFrom<&PathBuf> for Manifest {
file.read_to_string(&mut buffer)
.map_err(|error| ManifestError::Reading(MANIFEST_FILE_NAME, error))?;
Ok(toml::from_str(&buffer).map_err(|error| ManifestError::Parsing(MANIFEST_FILE_NAME, error))?)
// Determine if the old remote format is being used, and update to new convention
let mut old_remote_format: Option<&str> = None;
let mut new_remote_format_exists = false;
let mut new_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
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;
}
// Determine if the new remote format is being used
if line.starts_with("[remote]") {
new_remote_format_exists = true;
}
new_toml += line;
new_toml += "\n";
}
// Update the remote format
if let Some(old_remote) = old_remote_format {
// If both the old remote and new remote are missing,
// then skip appending the new remote, just keep the old remote.
if !new_remote_format_exists {
// Fetch the author from the old remote.
let remote_author = old_remote
.split("/") // Split the old remote as '"{author}' and '{package_name}"'
.collect::<Vec<&str>>()[0] // Fetch just the '"{author}'
.replace(&['\"', ' '][..], ""); // Remove the quotes from the author string
// Construct the new remote section.
let new_remote = format!(
r#"
[remote]
author = "{author}"
"#,
author = remote_author
);
// Append the new remote to the bottom of the manifest file.
new_toml += &new_remote;
}
}
// Rewrite the toml file if it has been updated
if buffer != new_toml {
let mut file = File::create(&path).map_err(|error| ManifestError::Creating(MANIFEST_FILE_NAME, error))?;
file.write_all(new_toml.as_bytes())
.map_err(|error| ManifestError::Writing(MANIFEST_FILE_NAME, error))?;
}
// Read the toml file
Ok(toml::from_str(&new_toml).map_err(|error| ManifestError::Parsing(MANIFEST_FILE_NAME, error))?)
}
}