1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
use clap::Args;
use mozart_core::console::Verbosity;
use std::path::PathBuf;
#[derive(Args)]
pub struct ArchiveArgs {
/// The package name
pub package: Option<String>,
/// A version constraint
pub version: Option<String>,
/// Format of the resulting archive (tar, tar.gz, tar.bz2, zip)
#[arg(short, long, value_parser = ["tar", "tar.gz", "tar.bz2", "zip"])]
pub format: Option<String>,
/// Write the archive to this directory
#[arg(long)]
pub dir: Option<String>,
/// Write the archive with the given file name
#[arg(long)]
pub file: Option<String>,
/// Ignore filters when saving archive
#[arg(long)]
pub ignore_filters: bool,
}
// ─── Archive config helpers ───────────────────────────────────────────────────
/// Read `archive.name` and `archive.exclude` from a composer.json file.
fn read_archive_config(
composer_json_path: &std::path::Path,
) -> anyhow::Result<(Option<String>, Vec<String>)> {
let content = std::fs::read_to_string(composer_json_path)?;
let value: serde_json::Value = serde_json::from_str(&content)?;
let name = value
.get("archive")
.and_then(|a| a.get("name"))
.and_then(|n| n.as_str())
.map(|s| s.to_string());
let excludes = value
.get("archive")
.and_then(|a| a.get("exclude"))
.and_then(|e| e.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect()
})
.unwrap_or_default();
Ok((name, excludes))
}
// ─── Metadata for a resolved package ─────────────────────────────────────────
struct PackageMeta {
source_dir: PathBuf,
package_name: String,
archive_name: Option<String>,
archive_excludes: Vec<String>,
version: Option<String>,
dist_reference: Option<String>,
dist_type: Option<String>,
source_reference: Option<String>,
/// Holds an optional temp directory that must outlive `source_dir`.
_temp_dir: Option<PathBuf>,
}
impl Drop for PackageMeta {
fn drop(&mut self) {
// Clean up temporary directory used for remote packages
if let Some(ref dir) = self._temp_dir {
let _ = std::fs::remove_dir_all(dir);
}
}
}
// ─── Main entry point ─────────────────────────────────────────────────────────
pub async fn execute(
args: &ArchiveArgs,
cli: &super::Cli,
console: &mozart_core::console::Console,
) -> anyhow::Result<()> {
use mozart_archiver::{
ArchiveFormat, collect_archivable_files, create_archive, generate_archive_filename,
parse_composer_excludes, parse_gitattributes, parse_gitignore_pattern,
self_exclusion_patterns,
};
let cache_config = mozart_registry::cache::build_cache_config(cli.no_cache);
let repo_cache = mozart_registry::cache::Cache::repo(&cache_config);
let files_cache = mozart_registry::cache::Cache::files(&cache_config);
// 1. Determine working directory
let working_dir = match &cli.working_dir {
Some(dir) => PathBuf::from(dir),
None => std::env::current_dir()?,
};
// 2. Load config for format/dir defaults from composer.json's "config" section
let composer_json_path = working_dir.join("composer.json");
let (config_archive_format, config_archive_dir) = if composer_json_path.exists() {
let content = std::fs::read_to_string(&composer_json_path)?;
let value: serde_json::Value = serde_json::from_str(&content)?;
let fmt = value
.get("config")
.and_then(|c| c.get("archive-format"))
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let dir = value
.get("config")
.and_then(|c| c.get("archive-dir"))
.and_then(|v| v.as_str())
.map(|s| s.to_string());
(fmt, dir)
} else {
(None, None)
};
// 3. Determine format: args -> config -> default "tar"
let format_str = args
.format
.as_deref()
.or(config_archive_format.as_deref())
.unwrap_or("tar");
let format = ArchiveFormat::parse(format_str).ok_or_else(|| {
anyhow::anyhow!(
"Unsupported archive format \"{}\". Supported formats: tar, tar.gz, tar.bz2, zip",
format_str
)
})?;
// 4. Determine output directory: args -> config -> default "."
let output_dir_str = args
.dir
.as_deref()
.or(config_archive_dir.as_deref())
.unwrap_or(".");
let output_dir = if std::path::Path::new(output_dir_str).is_absolute() {
PathBuf::from(output_dir_str)
} else {
working_dir.join(output_dir_str)
};
std::fs::create_dir_all(&output_dir)?;
// 5. Determine source directory and package metadata
let meta: PackageMeta = if let Some(ref pkg_name) = args.package {
// Remote package mode
console.info("Searching for the specified package.");
resolve_remote_package(
pkg_name,
args.version.as_deref(),
&repo_cache,
&files_cache,
console,
)
.await?
} else {
// Root package mode
if !composer_json_path.exists() {
anyhow::bail!("No composer.json found in {}", working_dir.display());
}
let root = mozart_core::package::read_from_file(&composer_json_path)?;
let (archive_name, archive_excludes) = read_archive_config(&composer_json_path)?;
let version = root
.extra_fields
.get("version")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
PackageMeta {
source_dir: working_dir.clone(),
package_name: root.name.clone(),
archive_name,
archive_excludes,
version,
dist_reference: None,
dist_type: None,
source_reference: None,
_temp_dir: None,
}
};
// 6. Generate output filename
let filename_base = if let Some(ref f) = args.file {
f.clone()
} else {
generate_archive_filename(
&meta.package_name,
meta.archive_name.as_deref(),
meta.version.as_deref(),
meta.dist_reference.as_deref(),
meta.dist_type.as_deref(),
meta.source_reference.as_deref(),
)
};
// 7. Build exclude patterns
// Self-exclusion: prevent the archive from including itself
let has_extra_parts = args.file.is_none()
&& (meta.version.is_some()
|| meta.dist_reference.is_some()
|| meta.source_reference.is_some());
let self_exclusion_strs = self_exclusion_patterns(&filename_base, has_extra_parts);
let mut all_patterns = Vec::new();
// Self-exclusion always applies
for rule in &self_exclusion_strs {
if let Some(p) = parse_gitignore_pattern(rule) {
all_patterns.push(p);
}
}
if !args.ignore_filters {
// Parse .gitattributes export-ignore rules
let git_patterns = parse_gitattributes(&meta.source_dir);
all_patterns.extend(git_patterns);
// Parse composer.json archive.exclude rules
let composer_patterns = parse_composer_excludes(&meta.archive_excludes);
all_patterns.extend(composer_patterns);
}
// 8. Collect files
let files = collect_archivable_files(&meta.source_dir, &all_patterns)?;
// 9. Create archive
let target_path = output_dir.join(format!("{}.{}", filename_base, format.extension()));
console.info(&format!(
"Creating the archive into \"{}\".",
output_dir.display()
));
create_archive(&meta.source_dir, &files, &target_path, &format)?;
// Print relative path if possible
let display_path = if let Ok(rel) = target_path.strip_prefix(&working_dir) {
rel.display().to_string()
} else {
target_path.display().to_string()
};
console.write_stdout(&format!("Created: {}", display_path), Verbosity::Normal);
Ok(())
}
// ─── Remote package resolution ────────────────────────────────────────────────
async fn resolve_remote_package(
package_name: &str,
version_constraint: Option<&str>,
repo_cache: &mozart_registry::cache::Cache,
files_cache: &mozart_registry::cache::Cache,
console: &mozart_core::console::Console,
) -> anyhow::Result<PackageMeta> {
use mozart_core::package::Stability;
use mozart_registry::version::find_best_candidate;
// Parse @stability suffix from version constraint (e.g. "^1.0@beta" → "^1.0", Stability::Beta)
let (constraint_stripped, stability) = if let Some(raw) = version_constraint {
if let Some(at_pos) = raw.find('@') {
let ver_part = raw[..at_pos].trim();
let stab_part = raw[at_pos + 1..].trim();
let stab = Stability::parse(stab_part);
(Some(ver_part.to_string()), stab)
} else {
(Some(raw.to_string()), Stability::Stable)
}
} else {
(None, Stability::Stable)
};
let version_constraint = constraint_stripped.as_deref();
// Fetch versions from Packagist
let versions =
mozart_registry::packagist::fetch_package_versions(package_name, repo_cache).await?;
if versions.is_empty() {
anyhow::bail!("No versions found for package \"{}\"", package_name);
}
// Apply version constraint filtering if given
let candidate = if let Some(constraint) = version_constraint {
let matches: Vec<_> = versions
.iter()
.filter(|v| v.version == constraint || v.version_normalized.starts_with(constraint))
.collect();
if matches.is_empty() {
anyhow::bail!(
"Could not find version \"{}\" for package \"{}\"",
constraint,
package_name
);
}
let best = matches[0];
if matches.len() > 1 {
console.info(&format!(
"Found multiple matches, selected {} {}.",
package_name, best.version
));
} else {
console.info(&format!(
"Found an exact match {} {}.",
package_name, best.version
));
}
best
} else {
let best = find_best_candidate(&versions, stability)
.or_else(|| find_best_candidate(&versions, Stability::Dev))
.ok_or_else(|| {
anyhow::anyhow!("No suitable version found for package \"{}\"", package_name)
})?;
console.info(&format!(
"Found an exact match {} {}.",
package_name, best.version
));
best
};
let dist = candidate.dist.as_ref().ok_or_else(|| {
anyhow::anyhow!(
"Package \"{}\" version \"{}\" has no dist available",
package_name,
candidate.version
)
})?;
// Create a temp directory using std (not tempfile crate, which is dev-only)
let temp_base = std::env::temp_dir();
let unique = format!(
"mozart-archive-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0)
);
let temp_dir = temp_base.join(&unique);
std::fs::create_dir_all(&temp_dir)?;
let bytes = mozart_registry::downloader::download_dist(
&dist.url,
dist.shasum.as_deref(),
None,
files_cache,
)
.await?;
match dist.dist_type.as_str() {
"zip" => mozart_registry::downloader::extract_zip(&bytes, &temp_dir)?,
"tar" | "tar.gz" | "tgz" => mozart_registry::downloader::extract_tar_gz(&bytes, &temp_dir)?,
other => {
let _ = std::fs::remove_dir_all(&temp_dir);
anyhow::bail!("Unsupported dist type: {}", other);
}
}
// Try to read composer.json from the extracted source for archive.name / archive.exclude
let extracted_composer = temp_dir.join("composer.json");
let (archive_name, archive_excludes) = if extracted_composer.exists() {
read_archive_config(&extracted_composer).unwrap_or((None, vec![]))
} else {
(None, vec![])
};
let version: Option<String> = Some(candidate.version.clone());
let dist_reference: Option<String> = dist.reference.clone();
let dist_type: Option<String> = Some(dist.dist_type.clone());
let source_reference: Option<String> =
candidate.source.as_ref().and_then(|s| s.reference.clone());
Ok(PackageMeta {
source_dir: temp_dir.clone(),
package_name: package_name.to_string(),
archive_name,
archive_excludes,
version,
dist_reference,
dist_type,
source_reference,
_temp_dir: Some(temp_dir),
})
}
|