Skip to content

Commit

Permalink
Implement support for extend-project
Browse files Browse the repository at this point in the history
  • Loading branch information
visigoth authored and jmgao committed Dec 27, 2023
1 parent 604d7da commit 915c24c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct ContactInfo {
pub bug_url: String,
}

#[derive(Default, Debug)]
#[derive(Clone, Default, Debug)]
pub struct Project {
pub name: String,
pub path: Option<String>,
Expand Down Expand Up @@ -140,6 +140,44 @@ impl Project {
}
}

#[derive(Default, Debug)]
pub struct ExtendProject {
pub name: String,
pub path: Option<String>,
pub groups: Option<Vec<String>>,
pub revision: Option<String>,
pub remote: Option<String>,
}

impl ExtendProject {
pub fn extend(&self, project: &Project) -> Project {
// Limit changes to projects at the specified path
if let Some(path) = &self.path {
if *path != project.path() {
return project.clone();
}
}

let mut extended = project.clone();

if let Some(groups) = &self.groups {
let mut old_groups = project.groups.clone().unwrap_or_default();
old_groups.extend(groups.clone());
extended.groups = Some(old_groups);
}

if let Some(revision) = &self.revision {
extended.revision = Some(revision.clone());
}

if let Some(remote) = &self.remote {
extended.remote = Some(remote.clone());
}

extended
}
}

#[derive(Clone, Debug)]
pub enum FileOperation {
LinkFile { src: String, dst: String },
Expand Down
41 changes: 41 additions & 0 deletions src/manifest/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ fn parse_manifest(
manifest.projects.insert(path, project);
}

b"extend-project" => {
let extensions = parse_extend_project(&e, reader)?;
let old_project = manifest.projects.values().find(|p| p.name == extensions.name);
if old_project.is_none() {
bail!("extend-project: no previous project named {}", extensions.name);
} else {
let new_project = extensions.extend(old_project.unwrap());
manifest.projects.insert(PathBuf::from(new_project.path()), new_project);
}
}

b"remote" => {
let remote = parse_remote(&e, &reader)?;
if manifest.remotes.contains_key(&remote.name) {
Expand Down Expand Up @@ -488,6 +499,36 @@ fn parse_project(event: &BytesStart, reader: &mut Reader<impl BufRead>, has_chil
Ok(project)
}

fn parse_extend_project(event: &BytesStart, reader: &Reader<impl BufRead>) -> Result<ExtendProject, Error> {
let mut extensions = ExtendProject::default();
let mut name = None;
for attribute in event.attributes() {
let attribute = attribute?;
let mut value = attribute.unescape_and_decode_value(&reader)?;
match attribute.key {
b"name" => {
while value.ends_with('/') {
value.pop();
}
populate_option!(name, value)
}
b"path" => populate_option!(extensions.path, value),
b"remote" => populate_option!(extensions.remote, value),
b"revision" => populate_option!(extensions.revision, value),
b"groups" => populate_option!(extensions.groups, value.split(',').map(ToString::to_string).collect()),
key => eprintln!(
"warning: unexpected attribute in <extend-project>: {}",
std::str::from_utf8(key).unwrap_or("???")
),
}
}

ensure!(name != None, "name not specified in <project>");
extensions.name = name.unwrap();

Ok(extensions)
}

fn parse_file_operation(event: &BytesStart, reader: &Reader<impl BufRead>, copy: bool) -> Result<FileOperation, Error> {
let op_name = if copy { "copyfile" } else { "linkfile" };

Expand Down

0 comments on commit 915c24c

Please sign in to comment.