Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/basic/manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
identifier: bsc
display_name: Basic Project
setup_path_template: setup/${asset}/${shot}/${department}

programs:
blender:
Expand Down
48 changes: 29 additions & 19 deletions src/core/commands/command_setup.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::RwLock;
use std::{collections::HashMap, sync::RwLock};

use clap::{command, Args};
use log::warn;
use log::{info, warn};
use ts_rs::TS;

use crate::core::{project::Project, shot::shot_resolver::ShotResolver};
Expand Down Expand Up @@ -43,25 +43,23 @@ impl Command for SetupArgs {
return Err(CommandError::InvalidArguments);
}

// shot is swapped for 'asset' if not in a shot

const DEFAULT_PATH: &str = "setup/${shot}/${department}/${asset}";

let department = self.common.department.clone().unwrap();
let asset = self.common.asset.clone().unwrap();
let mut dir_path = project.read().unwrap().get_root_directory();
dir_path.push("setup");

let mut map = HashMap::<&str, String>::new();
map.insert("department", department.clone());
map.insert("asset", asset.clone());

match &self.common.shot {
Some(shot) => {
if project.read().unwrap().shot_exists(shot) {
dir_path.push("shot");
for part in shot.split("/").into_iter() {
dir_path.push(part);
}
} else {
warn!("Invalid shot: {}", shot);
return Err(CommandError::InvalidArguments);
}
map.insert("shot", "shot/".to_owned() + &shot.clone());
}
None => {
dir_path.push("asset");
map.insert("shot", "asset".into());
}
}

Expand All @@ -80,16 +78,28 @@ impl Command for SetupArgs {
None => format!("{}_{}", asset, department),
};

dir_path.push(&department);
dir_path.push(&asset);
let mut resolved_path: String = match &project.read().unwrap().setup_path_template {
Some(s) => s.clone(),
None => DEFAULT_PATH.to_string(),
};

for (key, value) in map.iter() {
let replace = "${".to_string() + &key.to_string() + "}";
resolved_path = resolved_path.replace(&replace, value);
}

let mut new_dir_path = project.read().unwrap().get_root_directory();
for entry in resolved_path.split("/") {
new_dir_path.push(entry);
}

if self.dry == false {
_ = std::fs::create_dir_all(&dir_path);
_ = std::fs::create_dir_all(&new_dir_path);
}

let file_name_with_ext = file_name.clone() + &self.file_format;

let mut path = dir_path.clone();
let mut path = new_dir_path.clone();
path.push(&file_name_with_ext);

if self.dry {
Expand All @@ -109,7 +119,7 @@ impl Command for SetupArgs {
serde_json::to_value(SetupResult {
asset: self.common.asset.unwrap(),
department: self.common.department.unwrap(),
folder: dir_path.to_str().unwrap().to_string(),
folder: new_dir_path.to_str().unwrap().to_string(),
path: path.to_str().unwrap().to_string(),
file_name: file_name,
shot: shot_code,
Expand Down
8 changes: 7 additions & 1 deletion src/core/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ pub fn pretty_format_yaml(content: String) -> String {
let mut result = "".to_string();

for line in content.lines().into_iter() {
let filter = [" ", "-", "identifier", "display_name"];
let filter = [
" ",
"-",
"identifier",
"display_name",
"setup_path_template",
];

if !filter.iter().any(|f| line.starts_with(f)) {
result.push_str("\n");
Expand Down
22 changes: 22 additions & 0 deletions src/core/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Project {
pub assets: AssetCategory,
pub version_control: VersionControlConfig,
pub shots: ShotEntry,
pub setup_path_template: Option<String>,
}

impl Project {
Expand Down Expand Up @@ -314,6 +315,16 @@ pub fn to_yaml(project: &Project) -> serde_yaml::Value {
serde_yaml::Value::String(project.get_display_name()),
);

match &project.setup_path_template {
Some(template) => {
mapping.insert(
"setup_path_template".into(),
serde_yaml::to_value(template).unwrap(),
);
}
None => (),
}

mapping.insert(
"programs".into(),
serde_yaml::Value::Mapping(program::to_yaml(project.programs.clone())),
Expand Down Expand Up @@ -405,6 +416,16 @@ pub fn from_yaml(content: String, file_path: PathBuf) -> Project {
None => ShotEntry::Subcategory(IndexMap::new()),
};

let mut setup_path_template: Option<String> = None;

match map.get("setup_path_template") {
Some(v) => match v {
serde_yaml::Value::String(v) => setup_path_template = Some(v.clone()),
_ => (),
},
None => (),
}

let result = Project {
manifest_file: file_path,
identifier: identifier.to_string(),
Expand All @@ -414,6 +435,7 @@ pub fn from_yaml(content: String, file_path: PathBuf) -> Project {
version_control: config,
programs: programs,
shots: shots,
setup_path_template: setup_path_template,
};

to_yaml(&result);
Expand Down
Loading