Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use clap::{ArgGroup, Parser, Subcommand};
use clap_verbosity_flag::{InfoLevel, Verbosity};
use cli::args::{AddOrRemoveOperationArg, PcbSideArg, PlacementOperationArg, SetOrClearOperationArg, TaskActionArg};
use planner_app::Event;
use planning::design::DesignName;
use planning::file::FileReference;
use planning::placement::PlacementSortingItem;
use planning::process::ProcessReference;
use planning::variant::VariantName;
use pnp::object_path::ObjectPath;
use pnp::pcb::PcbUnitNumber;
use pnp::reference::Reference;
use regex::Regex;
use stores::load_out::LoadOutSource;
use thiserror::Error;
#[derive(Parser, Debug)]
#[command(name = "planner_cli")]
#[command(bin_name = "planner_cli")]
#[command(version, about, long_about = None)]
#[command(group(
ArgGroup::new("requires_project")
.args(&["project"])
.required(true)
))]
pub(crate) struct Opts {
#[command(subcommand)]
pub(crate) command: Command,
/// Trace log file
#[arg(long, num_args = 0..=1, default_missing_value = "trace.log")]
pub(crate) trace: Option<PathBuf>,
/// Path
#[arg(long, default_value = ".")]
pub(crate) path: Box<Path>,
/// Project name
#[arg(long, value_name = "PROJECT_NAME")]
pub(crate) project: Option<String>,
#[command(flatten)]
pub(crate) verbose: Verbosity<InfoLevel>,
}
fn parse_design_kv(s: &str) -> Result<(u16, DesignName), String> {
let mut split = s.splitn(2, '=');
let key = split
.next()
.ok_or_else(|| format!("Missing key in '{}'", s))?;
let value = split
.next()
.ok_or_else(|| format!("Missing value in '{}'", s))?;
let key_parsed = key
.parse::<u16>()
.map_err(|e| format!("Invalid number '{}': {}", key, e))?;
Ok((key_parsed, value.into()))
}
#[derive(Subcommand, Debug)]
#[command(arg_required_else_help(true))]
pub(crate) enum Command {
/// Create a new job
Create {},
/// Create a PCB file
CreatePcb {
/// Name of the PCB, e.g. 'panel_1'
#[arg(long)]
name: String,
/// The number of individual PCB units. 1 = single, >1 = panel
#[arg(long)]
units: u16,
/// The mapping of designs to units e.g. '1=design_a,2=design_b,3=design_a,4=design_b'. unit is 1-based.
#[arg(long, required = true, value_parser = parse_design_kv, num_args = 0.., value_delimiter = ',')]
design: Vec<(PcbUnitNumber, DesignName)>,
},
/// Add a PCB file to the project
AddPcb {
/// The path of the PCB, e.g. 'relative:<some_relative_path>' or '<some_absolute_path>'
/// paths can be prefixed with `relative:` to make them relative to the project path.
#[arg(long, value_parser = cli::parsers::FileReferenceParser::default(), value_name = "FILE_REFERENCE")]
file: FileReference,
},
/// Assign a design variant to a PCB unit
AssignVariantToUnit {
/// PCB unit path
#[arg(long, value_parser = clap::value_parser!(ObjectPath), value_name = "OBJECT_PATH")]
unit: ObjectPath,
/// Variant of the design
#[arg(long, value_parser = clap::value_parser!(VariantName), value_name = "VARIANT_NAME")]
variant: VariantName,
},
/// Assign a process to parts
AssignProcessToParts {
/// Process name
#[arg(long)]
process: ProcessReference,
/// Operation
#[arg(long)]
operation: AddOrRemoveOperationArg,
/// Manufacturer pattern (regexp)
#[arg(long)]
manufacturer: Regex,
/// Manufacturer part number (regexp)
#[arg(long)]
mpn: Regex,
},
/// Create a phase
CreatePhase {
/// Process name
#[arg(long)]
process: ProcessReference,
/// Phase reference (e.g. 'top_1')
#[arg(long)]
reference: Reference,
/// Load-out source (e.g. 'load_out_1')
#[arg(long)]
load_out: LoadOutSource,
/// PCB side
#[arg(long)]
pcb_side: PcbSideArg,
},
/// Assign placements to a phase
AssignPlacementsToPhase {
/// Phase reference (e.g. 'top_1')
#[arg(long)]
phase: Reference,
/// Operation
#[arg(long)]
operation: SetOrClearOperationArg,
/// Placements object path pattern (regexp)
#[arg(long)]
placements: Regex,
},
/// Assign feeder to load-out item
AssignFeederToLoadOutItem {
/// Phase reference (e.g. 'top_1')
#[arg(long)]
phase: Reference,
/// Feeder reference (e.g. 'FEEDER_1')
#[arg(long)]
feeder_reference: Reference,
/// Manufacturer pattern (regexp)
#[arg(long)]
manufacturer: Regex,
/// Manufacturer part number (regexp)
#[arg(long)]
mpn: Regex,
},
/// Set placement ordering for a phase
SetPlacementOrdering {
/// Phase reference (e.g. 'top_1')
#[arg(long)]
phase: Reference,
/// Orderings (e.g. 'PCB_UNIT:ASC,FEEDER_REFERENCE:ASC,REF_DES:ASC')
#[arg(long, required = true, num_args = 0.., value_delimiter = ',', value_parser = cli::parsers::PlacementSortingItemParser::default())]
placement_orderings: Vec<PlacementSortingItem>,
},
// FUTURE consider adding a command to allow the phase ordering to be changed, currently phase ordering is determined by the order of phase creation.
/// Generate artifacts
GenerateArtifacts {},
/// Record phase operation
RecordPhaseOperation {
/// Phase reference (e.g. 'top_1')
#[arg(long)]
phase: Reference,
/// Operation reference
#[arg(long)]
operation: Reference,
/// The task to update
#[arg(long)]
task: Reference,
/// The task action to apply
#[arg(long)]
action: TaskActionArg,
},
/// Record placements operation
RecordPlacementsOperation {
/// List of reference designators to apply the operation to
#[arg(long, required = true, num_args = 1.., value_delimiter = ',')]
object_path_patterns: Vec<Regex>,
/// The completed operation to apply
#[arg(long)]
operation: PlacementOperationArg,
},
/// Reset operations
ResetOperations {},
}
// FUTURE consider merging the AssignProcessToParts and AssignLoadOutToParts commands
// consider making a group for the criteria args (manufacturer/mpn/etc).
#[derive(Error, Debug)]
pub enum EventError {
#[error("Missing project name")]
MissingProjectName,
}
impl TryFrom<Opts> for Event {
type Error = EventError;
fn try_from(opts: Opts) -> Result<Self, Self::Error> {
match opts.command {
Command::Create {} => {
let name = opts
.project
.ok_or(EventError::MissingProjectName)?;
let directory = opts.path.clone();
let path = build_project_file_path(&name, &directory);
Ok(Event::CreateProject {
name,
path,
})
}
Command::CreatePcb {
name,
units,
design,
} => {
let unit_map = design
.into_iter()
.collect::<BTreeMap<_, _>>();
Ok(Event::CreateProjectPcb {
name: name.to_string(),
units,
unit_map,
})
}
Command::AddPcb {
file,
} => Ok(Event::AddPcb {
pcb_file: file,
}),
Command::AssignVariantToUnit {
unit,
variant,
} => Ok(Event::AssignVariantToUnit {
unit,
variant,
}),
Command::AssignProcessToParts {
process,
operation,
manufacturer,
mpn,
} => Ok(Event::AssignProcessToParts {
process,
operation: operation.into(),
manufacturer,
mpn,
}),
Command::CreatePhase {
process,
reference,
load_out,
pcb_side,
} => Ok(Event::CreatePhase {
process,
reference,
load_out,
pcb_side: pcb_side.into(),
}),
Command::AssignPlacementsToPhase {
phase,
operation,
placements,
} => Ok(Event::AssignPlacementsToPhase {
phase,
operation: operation.into(),
placements,
}),
Command::SetPlacementOrdering {
phase,
placement_orderings,
} => Ok(Event::SetPlacementOrdering {
phase,
placement_orderings,
}),
Command::GenerateArtifacts {} => Ok(Event::GenerateArtifacts),
Command::AssignFeederToLoadOutItem {
phase,
feeder_reference,
manufacturer,
mpn,
} => Ok(Event::AssignFeederToLoadOutItem {
phase,
feeder_reference,
manufacturer,
mpn,
}),
Command::RecordPhaseOperation {
phase,
operation,
task,
action,
} => Ok(Event::RecordPhaseOperation {
phase,
operation: operation.into(),
task: task.into(),
action: action.into(),
}),
Command::RecordPlacementsOperation {
object_path_patterns,
operation,
} => Ok(Event::RecordPlacementsOperation {
object_path_patterns,
operation: operation.into(),
}),
Command::ResetOperations {} => Ok(Event::ResetOperations {}),
}
}
}
pub fn build_project_file_path(name: &str, directory: &Path) -> PathBuf {
let mut project_file_path: PathBuf = PathBuf::from(directory);
project_file_path.push(format!("project-{}.mpnp.json", name));
project_file_path
}
5397e0182This file is pinned to the selected release. Download the original to open it in your design tool.