From d69343f14a5ae6f9014b23a1b59fdd641ccf0d9d Mon Sep 17 00:00:00 2001 From: raychu86 Date: Fri, 28 Aug 2020 21:45:08 -0700 Subject: [PATCH] Add impl to replace old remote format --- package/src/root/manifest.rs | 65 ++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/package/src/root/manifest.rs b/package/src/root/manifest.rs index 7ba4b76016..293e993683 100644 --- a/package/src/root/manifest.rs +++ b/package/src/root/manifest.rs @@ -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::>()[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::>()[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))?) } }