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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
use clap::Args;
use mozart_core::console_format;
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Args)]
pub struct BrowseArgs {
/// Package(s) to browse
pub packages: Vec<String>,
/// Open the homepage instead of the repository URL
#[arg(short = 'H', long)]
pub homepage: bool,
/// Only show the homepage or repository URL
#[arg(short, long)]
pub show: bool,
}
// ─── Main entry point ────────────────────────────────────────────────────────
pub async fn execute(
args: &BrowseArgs,
cli: &super::Cli,
console: &mozart_core::console::Console,
) -> anyhow::Result<()> {
let working_dir = match &cli.working_dir {
Some(dir) => PathBuf::from(dir),
None => std::env::current_dir()?,
};
// If no packages specified, use root package name from composer.json
let packages: Vec<String> = if args.packages.is_empty() {
let composer_json = working_dir.join("composer.json");
if !composer_json.exists() {
anyhow::bail!(
"No composer.json found in the current directory and no package specified."
);
}
eprintln!("No package specified, opening homepage for the root package");
let root = mozart_core::package::read_from_file(&composer_json)?;
vec![root.name.clone()]
} else {
args.packages.clone()
};
let mut exit_code = 0i32;
for package_name in &packages {
match resolve_url(package_name, &working_dir, args.homepage).await? {
ResolveResult::Found(url) => {
if args.show {
console.write_stdout(
&console_format!("<info>{}</info>", url),
mozart_core::console::Verbosity::Normal,
);
} else {
open_browser(&url)?;
}
}
ResolveResult::NotFound => {
eprintln!(
"{}",
console_format!("<warning>Package {} not found</warning>", package_name)
);
exit_code = 1;
}
ResolveResult::NoUrl => {
let msg = if args.homepage {
format!("Invalid or missing homepage for {}", package_name)
} else {
format!("Invalid or missing repository URL for {}", package_name)
};
eprintln!("{}", console_format!("<warning>{}</warning>", msg));
exit_code = 1;
}
}
}
if exit_code != 0 {
std::process::exit(exit_code);
}
Ok(())
}
// ─── URL resolution ───────────────────────────────────────────────────────────
enum ResolveResult {
/// Package found and URL resolved
Found(String),
/// Package found but no valid URL available
NoUrl,
/// Package not found in any source
NotFound,
}
async fn resolve_url(
package_name: &str,
working_dir: &Path,
prefer_homepage: bool,
) -> anyhow::Result<ResolveResult> {
// 1. Check root package (composer.json)
let composer_json = working_dir.join("composer.json");
if composer_json.exists()
&& let Ok(root) = mozart_core::package::read_from_file(&composer_json)
&& root.name.eq_ignore_ascii_case(package_name)
{
return Ok(match extract_url_from_root(&root, prefer_homepage) {
Some(url) => ResolveResult::Found(url),
None => ResolveResult::NoUrl,
});
}
// 2. Check lock file (composer.lock)
let lock_path = working_dir.join("composer.lock");
if lock_path.exists()
&& let Ok(lock) = mozart_registry::lockfile::LockFile::read_from_file(&lock_path)
{
let all_packages = lock
.packages
.iter()
.chain(lock.packages_dev.as_deref().unwrap_or(&[]));
for pkg in all_packages {
if pkg.name.eq_ignore_ascii_case(package_name) {
return Ok(match extract_url_from_locked(pkg, prefer_homepage) {
Some(url) => ResolveResult::Found(url),
None => ResolveResult::NoUrl,
});
}
}
}
// 3. Fall back to Packagist API
match mozart_registry::packagist::fetch_package_versions(package_name, None).await {
Ok(versions) if !versions.is_empty() => {
// Find the latest stable version (first non-dev, or fallback to first)
let best = versions
.iter()
.find(|v| !v.version.starts_with("dev-") && !v.version.ends_with("-dev"))
.or_else(|| versions.first());
if let Some(version) = best {
return Ok(match extract_url_from_packagist(version, prefer_homepage) {
Some(url) => ResolveResult::Found(url),
None => ResolveResult::NoUrl,
});
}
Ok(ResolveResult::NotFound)
}
_ => Ok(ResolveResult::NotFound),
}
}
// ─── URL extraction ───────────────────────────────────────────────────────────
fn extract_url_from_locked(
pkg: &mozart_registry::lockfile::LockedPackage,
prefer_homepage: bool,
) -> Option<String> {
if prefer_homepage {
return pkg
.homepage
.as_deref()
.filter(|u| is_valid_url(u))
.map(|u| u.to_string());
}
// Priority: support.source → source.url → homepage
if let Some(ref support) = pkg.support
&& let Some(source_url) = support.get("source").and_then(|v| v.as_str())
&& is_valid_url(source_url)
{
return Some(source_url.to_string());
}
if let Some(ref source) = pkg.source
&& is_valid_url(&source.url)
{
return Some(source.url.clone());
}
pkg.homepage
.as_deref()
.filter(|u| is_valid_url(u))
.map(|u| u.to_string())
}
fn extract_url_from_root(
root: &mozart_core::package::RawPackageData,
prefer_homepage: bool,
) -> Option<String> {
if prefer_homepage {
return root
.homepage
.as_deref()
.filter(|u| is_valid_url(u))
.map(|u| u.to_string());
}
// Priority: support.source → homepage (no source.url in RawPackageData)
if let Some(support_val) = root.extra_fields.get("support")
&& let Some(source_url) = support_val.get("source").and_then(|v| v.as_str())
&& is_valid_url(source_url)
{
return Some(source_url.to_string());
}
root.homepage
.as_deref()
.filter(|u| is_valid_url(u))
.map(|u| u.to_string())
}
fn extract_url_from_packagist(
pkg: &mozart_registry::packagist::PackagistVersion,
prefer_homepage: bool,
) -> Option<String> {
if prefer_homepage {
return pkg
.homepage
.as_deref()
.filter(|u| is_valid_url(u))
.map(|u| u.to_string());
}
// Priority: support.source → source.url → homepage
if let Some(ref support) = pkg.support
&& let Some(source_url) = support.get("source").and_then(|v| v.as_str())
&& is_valid_url(source_url)
{
return Some(source_url.to_string());
}
if let Some(ref source) = pkg.source
&& is_valid_url(&source.url)
{
return Some(source.url.clone());
}
pkg.homepage
.as_deref()
.filter(|u| is_valid_url(u))
.map(|u| u.to_string())
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
fn is_valid_url(url: &str) -> bool {
match url::Url::parse(url) {
Ok(parsed) => matches!(parsed.scheme(), "http" | "https"),
Err(_) => false,
}
}
fn open_browser(url: &str) -> anyhow::Result<()> {
#[cfg(target_os = "macos")]
{
Command::new("open").arg(url).status()?;
return Ok(());
}
#[cfg(target_os = "windows")]
{
Command::new("cmd")
.args(["/C", "start", "web", "explorer", url])
.status()?;
return Ok(());
}
#[cfg(target_os = "linux")]
{
if Command::new("which")
.arg("xdg-open")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
{
Command::new("xdg-open").arg(url).status()?;
return Ok(());
}
if Command::new("which")
.arg("open")
.output()
.map(|o| o.status.success())
.unwrap_or(false)
{
Command::new("open").arg(url).status()?;
return Ok(());
}
eprintln!(
"No suitable browser opener found. Please open manually: {}",
url
);
Ok(())
}
}
// ─── Tests ───────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
fn make_locked_package(
source_url: Option<&str>,
homepage: Option<&str>,
support_source: Option<&str>,
) -> mozart_registry::lockfile::LockedPackage {
let support = support_source.map(|s| serde_json::json!({"source": s}));
let source = source_url.map(|url| mozart_registry::lockfile::LockedSource {
source_type: "git".to_string(),
url: url.to_string(),
reference: None,
});
mozart_registry::lockfile::LockedPackage {
name: "vendor/package".to_string(),
version: "1.0.0".to_string(),
version_normalized: None,
source,
dist: None,
require: BTreeMap::new(),
require_dev: BTreeMap::new(),
conflict: BTreeMap::new(),
suggest: None,
package_type: None,
autoload: None,
autoload_dev: None,
license: None,
description: None,
homepage: homepage.map(|s| s.to_string()),
keywords: None,
authors: None,
support,
funding: None,
time: None,
extra_fields: BTreeMap::new(),
}
}
// ── is_valid_url ──────────────────────────────────────────────────────────
#[test]
fn test_is_valid_url() {
assert!(!is_valid_url("https://"));
assert!(is_valid_url("https://example.com"));
assert!(is_valid_url("http://example.com/path?query=1"));
assert!(!is_valid_url("ftp://example.com"));
assert!(!is_valid_url("not-a-url"));
assert!(!is_valid_url(""));
}
// ── extract_url_from_locked ───────────────────────────────────────────────
#[test]
fn test_extract_url_from_locked_prefers_support_source() {
// Has all three: support.source should win
let pkg = make_locked_package(
Some("https://github.com/vendor/package.git"),
Some("https://vendor.example.com"),
Some("https://github.com/vendor/package"),
);
let url = extract_url_from_locked(&pkg, false);
assert_eq!(url, Some("https://github.com/vendor/package".to_string()));
}
#[test]
fn test_extract_url_from_locked_prefers_homepage() {
// With prefer_homepage=true, only homepage is returned
let pkg = make_locked_package(
Some("https://github.com/vendor/package.git"),
Some("https://vendor.example.com"),
Some("https://github.com/vendor/package"),
);
let url = extract_url_from_locked(&pkg, true);
assert_eq!(url, Some("https://vendor.example.com".to_string()));
}
#[test]
fn test_extract_url_from_locked_fallback_to_source() {
// No support.source, has source.url
let pkg = make_locked_package(
Some("https://github.com/vendor/package.git"),
Some("https://vendor.example.com"),
None,
);
let url = extract_url_from_locked(&pkg, false);
assert_eq!(
url,
Some("https://github.com/vendor/package.git".to_string())
);
}
#[test]
fn test_extract_url_from_locked_fallback_to_homepage() {
// No source URLs, falls back to homepage
let pkg = make_locked_package(None, Some("https://vendor.example.com"), None);
let url = extract_url_from_locked(&pkg, false);
assert_eq!(url, Some("https://vendor.example.com".to_string()));
}
#[test]
fn test_extract_url_from_locked_no_urls() {
// No URLs at all
let pkg = make_locked_package(None, None, None);
let url = extract_url_from_locked(&pkg, false);
assert_eq!(url, None);
}
}
|