feat(cli,package): add/remove dependencies and cleaner manifest serialization
- `marqdo add <name> --path <dir>` / `--git URL --rev SHA` and `marqdo remove <name>` edit marqdo.yaml only after validating the dependency source (path-relative, git pinned by exact rev, no duplicate entry). Removing a dependency also drops its stale lock entry; locking stays an explicit step so no version changes happen silently. - Manifest serialization omits empty dependency/plugin maps and empty capability requests, keeping generated manifests readable and stable.
This commit is contained in:
1 parent
3d1c37d217
commit
6f04c17f7a
3 files changed
+150
-5
No files matched your search
@@ -918,6 +918,105 @@ fn hash_tree(root: &Path) -> Result<String, String> {
|
|||||||
Ok(hex::encode(hasher.finalize()))
|
Ok(hex::encode(hasher.finalize()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `marqdo add <name> --path <dir> [--git URL --rev SHA]`.
|
||||||
|
pub fn add(
|
||||||
|
name: &str,
|
||||||
|
path: Option<&Path>,
|
||||||
|
git: Option<&str>,
|
||||||
|
rev: Option<&str>,
|
||||||
|
json: bool,
|
||||||
|
) -> i32 {
|
||||||
|
let root = match discover_project_root() {
|
||||||
|
Ok(root) => root,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("error: {error}");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let manifest_path = root.join(marqdo_package::MANIFEST_FILE);
|
||||||
|
let mut manifest = match marqdo_package::Manifest::load(&manifest_path) {
|
||||||
|
Ok(manifest) => manifest,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("error: {error}");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let dependency = marqdo_package::Dependency {
|
||||||
|
path: path.map(|path| path.to_path_buf()),
|
||||||
|
git: git.map(str::to_string),
|
||||||
|
rev: rev.map(str::to_string),
|
||||||
|
};
|
||||||
|
if let Err(error) = dependency.validate(name) {
|
||||||
|
eprintln!("error: {error}");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if manifest.dependencies.contains_key(name) {
|
||||||
|
eprintln!("error: dependency `{name}` already exists; remove it first");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
manifest.dependencies.insert(name.to_string(), dependency);
|
||||||
|
if let Err(error) = std::fs::write(&manifest_path, manifest.to_yaml()) {
|
||||||
|
eprintln!("error: cannot write {}: {error}", manifest_path.display());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if json {
|
||||||
|
output::print_json(&json!({
|
||||||
|
"command": "add",
|
||||||
|
"success": true,
|
||||||
|
"dependency": name,
|
||||||
|
"manifest": manifest_path.display().to_string(),
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
println!("added dependency `{name}` to {}", manifest_path.display());
|
||||||
|
println!("run `marqdo lock` to pin it in {}", marqdo_package::LOCK_FILE);
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `marqdo remove <name>`.
|
||||||
|
pub fn remove(name: &str, json: bool) -> i32 {
|
||||||
|
let root = match discover_project_root() {
|
||||||
|
Ok(root) => root,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("error: {error}");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let manifest_path = root.join(marqdo_package::MANIFEST_FILE);
|
||||||
|
let mut manifest = match marqdo_package::Manifest::load(&manifest_path) {
|
||||||
|
Ok(manifest) => manifest,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("error: {error}");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if manifest.dependencies.remove(name).is_none() {
|
||||||
|
eprintln!("error: dependency `{name}` is not declared");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if let Err(error) = std::fs::write(&manifest_path, manifest.to_yaml()) {
|
||||||
|
eprintln!("error: cannot write {}: {error}", manifest_path.display());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// Keep the lockfile consistent by removing the stale entry if present.
|
||||||
|
let lock_path = root.join(marqdo_package::LOCK_FILE);
|
||||||
|
if let Ok(lock) = marqdo_package::LockFile::load(&lock_path) {
|
||||||
|
let mut updated = lock.clone();
|
||||||
|
updated.packages.retain(|package| package.name != name);
|
||||||
|
let _ = std::fs::write(&lock_path, updated.to_yaml());
|
||||||
|
}
|
||||||
|
if json {
|
||||||
|
output::print_json(&json!({
|
||||||
|
"command": "remove",
|
||||||
|
"success": true,
|
||||||
|
"dependency": name,
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
println!("removed dependency `{name}`");
|
||||||
|
}
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
/// Resolve `--arg` strings into typed arguments.
|
/// Resolve `--arg` strings into typed arguments.
|
||||||
pub fn parse_named_args(raw: &[String]) -> Result<Vec<(String, Value)>, String> {
|
pub fn parse_named_args(raw: &[String]) -> Result<Vec<(String, Value)>, String> {
|
||||||
raw.iter()
|
raw.iter()
|
||||||
|
|||||||
@@ -41,6 +41,10 @@ enum Command {
|
|||||||
Fmt(FmtArgs),
|
Fmt(FmtArgs),
|
||||||
/// Plugin artifact inspection (never loads plugin code).
|
/// Plugin artifact inspection (never loads plugin code).
|
||||||
Plugin(PluginArgs),
|
Plugin(PluginArgs),
|
||||||
|
/// Add a dependency to marqdo.yaml.
|
||||||
|
Add(AddArgs),
|
||||||
|
/// Remove a dependency from marqdo.yaml.
|
||||||
|
Remove(RemoveArgs),
|
||||||
/// Generate a deterministic marqdo.lock for the current project.
|
/// Generate a deterministic marqdo.lock for the current project.
|
||||||
Lock(LockArgs),
|
Lock(LockArgs),
|
||||||
/// Verify that path dependencies match the lockfile.
|
/// Verify that path dependencies match the lockfile.
|
||||||
@@ -199,6 +203,33 @@ struct PluginInspectArgs {
|
|||||||
json: bool,
|
json: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Args)]
|
||||||
|
struct AddArgs {
|
||||||
|
/// Dependency name used by front-matter imports (`@name`).
|
||||||
|
name: String,
|
||||||
|
/// Project-relative path dependency.
|
||||||
|
#[arg(long)]
|
||||||
|
path: Option<PathBuf>,
|
||||||
|
/// Git URL dependency (requires --rev).
|
||||||
|
#[arg(long)]
|
||||||
|
git: Option<String>,
|
||||||
|
/// Exact git revision for a git dependency.
|
||||||
|
#[arg(long)]
|
||||||
|
rev: Option<String>,
|
||||||
|
/// Machine readable JSON output.
|
||||||
|
#[arg(long)]
|
||||||
|
json: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Args)]
|
||||||
|
struct RemoveArgs {
|
||||||
|
/// Dependency name.
|
||||||
|
name: String,
|
||||||
|
/// Machine readable JSON output.
|
||||||
|
#[arg(long)]
|
||||||
|
json: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
struct LockArgs {
|
struct LockArgs {
|
||||||
/// Machine readable JSON output.
|
/// Machine readable JSON output.
|
||||||
@@ -258,6 +289,14 @@ fn main() {
|
|||||||
Command::Plugin(args) => match args.command {
|
Command::Plugin(args) => match args.command {
|
||||||
PluginCommand::Inspect(args) => commands::plugin_inspect(&args.target, args.json),
|
PluginCommand::Inspect(args) => commands::plugin_inspect(&args.target, args.json),
|
||||||
},
|
},
|
||||||
|
Command::Add(args) => commands::add(
|
||||||
|
&args.name,
|
||||||
|
args.path.as_deref(),
|
||||||
|
args.git.as_deref(),
|
||||||
|
args.rev.as_deref(),
|
||||||
|
args.json,
|
||||||
|
),
|
||||||
|
Command::Remove(args) => commands::remove(&args.name, args.json),
|
||||||
Command::Lock(args) => commands::lock(args.json),
|
Command::Lock(args) => commands::lock(args.json),
|
||||||
Command::Sync(args) => commands::sync(args.locked, args.json),
|
Command::Sync(args) => commands::sync(args.locked, args.json),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -69,13 +69,20 @@ pub struct CapabilityRequests {
|
|||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub fs: Option<FsRequests>,
|
pub fs: Option<FsRequests>,
|
||||||
/// Allowed network hosts (empty means none).
|
/// Allowed network hosts (empty means none).
|
||||||
#[serde(default)]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub net: Option<NetRequests>,
|
pub net: Option<NetRequests>,
|
||||||
/// May the project request process execution.
|
/// May the project request process execution.
|
||||||
#[serde(default)]
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
pub process: Option<bool>,
|
pub process: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CapabilityRequests {
|
||||||
|
/// Whether the project requests nothing.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.fs.is_none() && self.net.is_none() && self.process.is_none()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// File system capability requests.
|
/// File system capability requests.
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct FsRequests {
|
pub struct FsRequests {
|
||||||
@@ -101,13 +108,13 @@ pub struct Manifest {
|
|||||||
/// Project identity.
|
/// Project identity.
|
||||||
pub project: ProjectInfo,
|
pub project: ProjectInfo,
|
||||||
/// Dependencies keyed by local name.
|
/// Dependencies keyed by local name.
|
||||||
#[serde(default)]
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||||
pub dependencies: BTreeMap<String, Dependency>,
|
pub dependencies: BTreeMap<String, Dependency>,
|
||||||
/// Plugin requirements keyed by plugin name.
|
/// Plugin requirements keyed by plugin name.
|
||||||
#[serde(default)]
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||||
pub plugins: BTreeMap<String, PluginRequirement>,
|
pub plugins: BTreeMap<String, PluginRequirement>,
|
||||||
/// Capability requests.
|
/// Capability requests.
|
||||||
#[serde(default)]
|
#[serde(default, skip_serializing_if = "CapabilityRequests::is_empty")]
|
||||||
pub capabilities: CapabilityRequests,
|
pub capabilities: CapabilityRequests,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user