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
|
use crate::console;
use crate::package::{self, Stability};
use crate::packagist;
use crate::validation;
use crate::version;
use clap::Args;
#[derive(Args)]
pub struct RequireArgs {
/// Package(s) to require
pub packages: Vec<String>,
/// Add requirement to require-dev
#[arg(long)]
pub dev: bool,
/// Only output what would be changed, do not modify files
#[arg(long)]
pub dry_run: bool,
/// Forces installation from package sources when possible
#[arg(long)]
pub prefer_source: bool,
/// Forces installation from package dist
#[arg(long)]
pub prefer_dist: bool,
/// Forces usage of a specific install method (dist, source, auto)
#[arg(long)]
pub prefer_install: Option<String>,
/// Pin the exact version instead of a range
#[arg(long)]
pub fixed: bool,
/// [Deprecated] Do not show install suggestions
#[arg(long)]
pub no_suggest: bool,
/// Do not output download progress
#[arg(long)]
pub no_progress: bool,
/// Disables the automatic update of the lock file
#[arg(long)]
pub no_update: bool,
/// Skip the install step
#[arg(long)]
pub no_install: bool,
/// Skip the audit step
#[arg(long)]
pub no_audit: bool,
/// Audit output format
#[arg(long)]
pub audit_format: Option<String>,
/// Do not block on security advisories
#[arg(long)]
pub no_security_blocking: bool,
/// Run the dependency update with the --no-dev option
#[arg(long)]
pub update_no_dev: bool,
/// [Deprecated] Use --with-dependencies instead
#[arg(short = 'w', long)]
pub update_with_dependencies: bool,
/// [Deprecated] Use --with-all-dependencies instead
#[arg(short = 'W', long)]
pub update_with_all_dependencies: bool,
/// Update also dependencies of newly required packages
#[arg(long)]
pub with_dependencies: bool,
/// Update all dependencies including root requirements
#[arg(long)]
pub with_all_dependencies: bool,
/// Ignore a specific platform requirement
#[arg(long)]
pub ignore_platform_req: Vec<String>,
/// Ignore all platform requirements
#[arg(long)]
pub ignore_platform_reqs: bool,
/// Prefer stable versions of dependencies
#[arg(long)]
pub prefer_stable: bool,
/// Prefer lowest versions of dependencies
#[arg(long)]
pub prefer_lowest: bool,
/// Prefer minimal restriction updates
#[arg(short = 'm', long)]
pub minimal_changes: bool,
/// Sort packages in composer.json
#[arg(long)]
pub sort_packages: bool,
/// Optimizes PSR-0 and PSR-4 packages to be loaded with classmaps
#[arg(short, long)]
pub optimize_autoloader: bool,
/// Autoload classes from the classmap only
#[arg(short = 'a', long)]
pub classmap_authoritative: bool,
/// Use APCu to cache found/not-found classes
#[arg(long)]
pub apcu_autoloader: bool,
/// Use a custom prefix for the APCu autoloader cache
#[arg(long)]
pub apcu_autoloader_prefix: Option<String>,
}
pub fn execute(args: &RequireArgs, cli: &super::Cli) -> anyhow::Result<()> {
if args.packages.is_empty() {
anyhow::bail!("Not enough arguments (missing: \"packages\").");
}
// Resolve working directory
let working_dir = if let Some(ref dir) = cli.working_dir {
std::path::PathBuf::from(dir)
} else {
std::env::current_dir()?
};
let composer_path = working_dir.join("composer.json");
if !composer_path.exists() {
anyhow::bail!(
"composer.json not found in {}. Run `mozart init` to create one.",
working_dir.display()
);
}
// Read existing composer.json
let mut raw = package::read_from_file(&composer_path)?;
// Determine preferred stability from composer.json's minimum-stability
let preferred_stability = raw
.minimum_stability
.as_deref()
.map(|s| match s.to_lowercase().as_str() {
"dev" => Stability::Dev,
"alpha" => Stability::Alpha,
"beta" => Stability::Beta,
"rc" | "RC" => Stability::RC,
_ => Stability::Stable,
})
.unwrap_or(Stability::Stable);
// Process each package argument
let mut additions: Vec<(String, String, bool)> = Vec::new(); // (name, constraint, is_dev)
for pkg_arg in &args.packages {
// Try to parse as "vendor/package:constraint"
let (name, constraint) = match validation::parse_require_string(pkg_arg) {
Ok((n, v)) => (n.to_lowercase(), v),
Err(_) => {
// No version specified — resolve from Packagist
let name = pkg_arg.trim().to_lowercase();
if !validation::validate_package_name(&name) {
anyhow::bail!("Invalid package name: \"{name}\"");
}
println!(
"{}",
console::info(&format!(
"Using version constraint for {name} from Packagist..."
))
);
let versions = packagist::fetch_package_versions(&name)?;
let best = version::find_best_candidate(&versions, preferred_stability)
.ok_or_else(|| {
anyhow::anyhow!(
"Could not find a version of package \"{name}\" matching your minimum-stability ({preferred_stability:?}). \
Try requiring it with an explicit version constraint."
)
})?;
let stability = version::stability_of(&best.version_normalized);
let constraint = if args.fixed {
best.version.clone()
} else {
version::find_recommended_require_version(
&best.version,
&best.version_normalized,
stability,
)
};
println!(
"{}",
console::info(&format!("Using version {constraint} for {name}"))
);
(name, constraint)
}
};
additions.push((name, constraint, args.dev));
}
// Apply changes
for (name, constraint, is_dev) in &additions {
let section_name = if *is_dev { "require-dev" } else { "require" };
let target = if *is_dev {
&mut raw.require_dev
} else {
&mut raw.require
};
if let Some(existing) = target.get(name) {
println!(
"{}",
console::comment(&format!(
"Updating {name} from {existing} to {constraint} in {section_name}"
))
);
} else {
println!(
"{}",
console::info(&format!("Adding {name} ({constraint}) to {section_name}"))
);
}
target.insert(name.clone(), constraint.clone());
}
// Sort packages if requested
if args.sort_packages {
let sorted_require: std::collections::BTreeMap<_, _> = raw.require.clone();
raw.require = sorted_require;
let sorted_dev: std::collections::BTreeMap<_, _> = raw.require_dev.clone();
raw.require_dev = sorted_dev;
}
// Write back
if args.dry_run {
println!(
"{}",
console::comment("Dry run: composer.json not modified.")
);
} else {
package::write_to_file(&raw, &composer_path)?;
}
// Dependency resolution / install notice
if !args.no_update && !args.no_install {
println!(
"{}",
console::comment(
"Dependency resolution and installation are not yet implemented. \
The composer.json has been updated."
)
);
}
Ok(())
}
|