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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! # Monorepo Descriptor Implementation
//!
//! ## What
//! This file implements the `MonorepoDescriptor` struct, providing methods to
//! analyze and query monorepo structures, packages, and dependencies.
//!
//! ## How
//! The implementation provides methods for accessing monorepo properties,
//! finding packages by name or path, and generating dependency graphs.
//!
//! ## Why
//! Monorepos have complex relationships between packages that need to be
//! navigated and analyzed. This implementation provides the tools to
//! efficiently work with these relationships.
use super::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
use crate::node::{PackageManager, RepoKind};
use crate::project::{ProjectInfo, ProjectKind, ProjectValidationStatus};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
impl MonorepoDescriptor {
/// Creates a new `MonorepoDescriptor` instance.
///
/// # Arguments
///
/// * `kind` - The kind of monorepo (npm, yarn, pnpm, etc.)
/// * `root` - The root directory of the monorepo
/// * `packages` - A vector of packages found in the monorepo
/// * `package_manager` - Optional package manager for this monorepo
/// * `package_json` - Optional root package.json content
/// * `validation_status` - Validation status of the monorepo
///
/// # Returns
///
/// A new `MonorepoDescriptor` instance with the provided properties.
///
/// # Examples
///
/// ```
/// use std::path::PathBuf;
/// use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
/// use sublime_standard_tools::project::ProjectValidationStatus;
///
/// let root = PathBuf::from("/projects/my-monorepo");
/// let packages = vec![
/// // Package definitions would go here
/// ];
///
/// // Use minimal() for simple cases with default values
/// let descriptor = MonorepoDescriptor::minimal(
/// MonorepoKind::YarnWorkspaces,
/// root,
/// packages,
/// );
/// ```
#[must_use]
pub fn new(
kind: MonorepoKind,
root: PathBuf,
packages: Vec<WorkspacePackage>,
package_manager: Option<PackageManager>,
package_json: Option<package_json::PackageJson>,
validation_status: ProjectValidationStatus,
) -> Self {
// Build name-to-package map for quick lookups
let mut name_to_package = HashMap::new();
for (i, package) in packages.iter().enumerate() {
name_to_package.insert(package.name.clone(), i);
}
Self {
kind,
root,
packages,
name_to_package,
package_manager,
package_json,
validation_status,
}
}
/// Creates a new `MonorepoDescriptor` instance with minimal information.
///
/// This is a convenience constructor for cases where only basic information
/// is available during initial detection.
///
/// # Arguments
///
/// * `kind` - The kind of monorepo (npm, yarn, pnpm, etc.)
/// * `root` - The root directory of the monorepo
/// * `packages` - A vector of packages found in the monorepo
///
/// # Returns
///
/// A new `MonorepoDescriptor` instance with validation status set to `NotValidated`.
///
/// # Examples
///
/// ```
/// use std::path::PathBuf;
/// use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
///
/// let root = PathBuf::from("/projects/my-monorepo");
/// let packages = vec![
/// // Package definitions would go here
/// ];
///
/// let descriptor = MonorepoDescriptor::minimal(
/// MonorepoKind::YarnWorkspaces,
/// root,
/// packages
/// );
/// ```
#[must_use]
pub fn minimal(kind: MonorepoKind, root: PathBuf, packages: Vec<WorkspacePackage>) -> Self {
Self::new(kind, root, packages, None, None, ProjectValidationStatus::NotValidated)
}
/// Returns the kind of monorepo.
///
/// # Returns
///
/// A reference to the `MonorepoKind` indicating what type of monorepo this is.
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/fake/path"),
/// # vec![]
/// # );
/// #
/// let kind = descriptor.kind();
/// assert!(matches!(kind, MonorepoKind::YarnWorkspaces));
/// ```
#[must_use]
pub fn kind(&self) -> &MonorepoKind {
&self.kind
}
/// Returns the root directory of the monorepo.
///
/// # Returns
///
/// A reference to the Path of the monorepo's root directory.
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/my-monorepo"),
/// # vec![]
/// # );
/// #
/// let root = descriptor.root();
/// assert_eq!(root, PathBuf::from("/projects/my-monorepo"));
/// ```
#[must_use]
pub fn root(&self) -> &Path {
&self.root
}
/// Returns all packages in the monorepo.
///
/// # Returns
///
/// A slice containing all `WorkspacePackage` instances in the monorepo.
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/fake/path"),
/// # vec![]
/// # );
/// #
/// let packages = descriptor.packages();
/// println!("Found {} packages", packages.len());
/// ```
#[must_use]
pub fn packages(&self) -> &[WorkspacePackage] {
&self.packages
}
/// Gets a package by name.
///
/// # Arguments
///
/// * `name` - The name of the package to find
///
/// # Returns
///
/// * `Some(&WorkspacePackage)` - If a package with the given name exists
/// * `None` - If no package with the given name exists
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
/// #
/// # let ui_pkg = WorkspacePackage {
/// # name: "ui".to_string(),
/// # version: "1.0.0".to_string(),
/// # location: PathBuf::from("packages/ui"),
/// # absolute_path: PathBuf::from("/projects/monorepo/packages/ui"),
/// # workspace_dependencies: vec![],
/// # workspace_dev_dependencies: vec![],
/// # };
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/monorepo"),
/// # vec![ui_pkg]
/// # );
/// #
/// if let Some(package) = descriptor.get_package("ui") {
/// println!("Found UI package at {}", package.location.display());
/// }
/// ```
#[must_use]
pub fn get_package(&self, name: &str) -> Option<&WorkspacePackage> {
self.name_to_package.get(name).map(|&i| &self.packages[i])
}
/// Generates a dependency graph for the monorepo.
///
/// This method creates a mapping from package names to the packages
/// that depend on them (their dependents).
///
/// # Returns
///
/// A `HashMap` mapping package names to vectors of their dependent packages.
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
/// #
/// # let pkg1 = WorkspacePackage {
/// # name: "shared".to_string(),
/// # version: "1.0.0".to_string(),
/// # location: PathBuf::from("packages/shared"),
/// # absolute_path: PathBuf::from("/fake/path/packages/shared"),
/// # workspace_dependencies: vec![],
/// # workspace_dev_dependencies: vec![],
/// # };
/// #
/// # let pkg2 = WorkspacePackage {
/// # name: "app".to_string(),
/// # version: "1.0.0".to_string(),
/// # location: PathBuf::from("packages/app"),
/// # absolute_path: PathBuf::from("/fake/path/packages/app"),
/// # workspace_dependencies: vec!["shared".to_string()],
/// # workspace_dev_dependencies: vec![],
/// # };
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/fake/path"),
/// # vec![pkg1, pkg2]
/// # );
/// #
/// let graph = descriptor.get_dependency_graph();
///
/// // Find all packages that depend on "shared"
/// if let Some(dependents) = graph.get("shared") {
/// println!("{} packages depend on shared", dependents.len());
/// for pkg in dependents {
/// println!(" - {}", pkg.name);
/// }
/// }
/// ```
#[must_use]
pub fn get_dependency_graph(&self) -> HashMap<&str, Vec<&WorkspacePackage>> {
let mut dependency_graph: HashMap<&str, Vec<&WorkspacePackage>> = HashMap::new();
// Initialize the graph with empty vectors for each package
for package in &self.packages {
dependency_graph.insert(&package.name, Vec::new());
}
// Populate the graph by iterating through all packages
for package in &self.packages {
// Add this package as a dependent to each of its dependencies
for dep_name in &package.workspace_dependencies {
if let Some(dependents) = dependency_graph.get_mut(dep_name.as_str()) {
dependents.push(package);
}
}
// Also do the same for dev dependencies
for dep_name in &package.workspace_dev_dependencies {
if let Some(dependents) = dependency_graph.get_mut(dep_name.as_str()) {
dependents.push(package);
}
}
}
dependency_graph
}
/// Finds all workspace dependencies of a given package.
///
/// # Arguments
///
/// * `package_name` - The name of the package to find dependencies for
///
/// # Returns
///
/// A vector of references to the `WorkspacePackage` objects that the
/// specified package depends on.
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
/// #
/// # let pkg1 = WorkspacePackage {
/// # name: "shared".to_string(),
/// # version: "1.0.0".to_string(),
/// # location: PathBuf::from("packages/shared"),
/// # absolute_path: PathBuf::from("/fake/path/packages/shared"),
/// # workspace_dependencies: vec![],
/// # workspace_dev_dependencies: vec![],
/// # };
/// #
/// # let pkg2 = WorkspacePackage {
/// # name: "app".to_string(),
/// # version: "1.0.0".to_string(),
/// # location: PathBuf::from("packages/app"),
/// # absolute_path: PathBuf::from("/fake/path/packages/app"),
/// # workspace_dependencies: vec!["shared".to_string()],
/// # workspace_dev_dependencies: vec![],
/// # };
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/fake/path"),
/// # vec![pkg1, pkg2]
/// # );
/// #
/// // Find all dependencies of the "app" package
/// let deps = descriptor.find_dependencies_by_name("app");
/// for dep in deps {
/// println!("app depends on: {}", dep.name);
/// }
/// ```
#[must_use]
pub fn find_dependencies_by_name(&self, package_name: &str) -> Vec<&WorkspacePackage> {
// First, find the package
if let Some(package) = self.get_package(package_name) {
// Collect all dependencies (both regular and dev)
let all_deps: Vec<&String> = package
.workspace_dependencies
.iter()
.chain(package.workspace_dev_dependencies.iter())
.collect();
// Return the corresponding package references
all_deps.into_iter().filter_map(|dep_name| self.get_package(dep_name)).collect()
} else {
Vec::new()
}
}
/// Finds the package that contains a specific path.
///
/// # Arguments
///
/// * `path` - The path to locate within the monorepo
///
/// # Returns
///
/// * `Some(&WorkspacePackage)` - If the path is within a package
/// * `None` - If the path is not within any package
///
/// # Examples
///
/// ```
/// # use std::path::{Path, PathBuf};
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind, WorkspacePackage};
/// #
/// # let pkg = WorkspacePackage {
/// # name: "ui".to_string(),
/// # version: "1.0.0".to_string(),
/// # location: PathBuf::from("packages/ui"),
/// # absolute_path: PathBuf::from("/projects/monorepo/packages/ui"),
/// # workspace_dependencies: vec![],
/// # workspace_dev_dependencies: vec![],
/// # };
/// #
/// # let descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/monorepo"),
/// # vec![pkg]
/// # );
/// #
/// // Find which package contains a file
/// let file_path = Path::new("/projects/monorepo/packages/ui/src/Button.js");
/// if let Some(package) = descriptor.find_package_for_path(file_path) {
/// println!("File is in package: {}", package.name);
/// }
/// ```
#[must_use]
pub fn find_package_for_path(&self, path: &Path) -> Option<&WorkspacePackage> {
// Normalize and make path absolute for comparison
let abs_path = if path.is_absolute() { path.to_path_buf() } else { self.root.join(path) };
self.packages.iter().find(|pkg| abs_path.starts_with(&pkg.absolute_path))
}
}
impl ProjectInfo for MonorepoDescriptor {
fn root(&self) -> &Path {
&self.root
}
fn package_manager(&self) -> Option<&PackageManager> {
self.package_manager.as_ref()
}
fn package_json(&self) -> Option<&package_json::PackageJson> {
self.package_json.as_ref()
}
fn validation_status(&self) -> &ProjectValidationStatus {
&self.validation_status
}
fn kind(&self) -> ProjectKind {
ProjectKind::Repository(RepoKind::Monorepo(self.kind.clone()))
}
}
impl MonorepoDescriptor {
/// Sets the package manager for this monorepo.
///
/// # Arguments
///
/// * `package_manager` - The package manager to set
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// # use sublime_standard_tools::node::{PackageManager, PackageManagerKind};
/// # use sublime_standard_tools::project::ProjectValidationStatus;
/// #
/// # let mut descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/my-monorepo"),
/// # vec![]
/// # );
/// #
/// let manager = PackageManager::new(PackageManagerKind::Yarn, "/projects/my-monorepo");
/// descriptor.set_package_manager(Some(manager));
/// ```
pub fn set_package_manager(&mut self, package_manager: Option<PackageManager>) {
self.package_manager = package_manager;
}
/// Sets the root package.json content for this monorepo.
///
/// # Arguments
///
/// * `package_json` - The root package.json content to set
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// # use package_json::PackageJson;
/// #
/// # let mut descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/my-monorepo"),
/// # vec![]
/// # );
/// #
/// // Assuming package_json is loaded from file
/// // descriptor.set_package_json(Some(package_json));
/// ```
pub fn set_package_json(&mut self, package_json: Option<package_json::PackageJson>) {
self.package_json = package_json;
}
/// Sets the validation status for this monorepo.
///
/// # Arguments
///
/// * `validation_status` - The validation status to set
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// # use sublime_standard_tools::project::ProjectValidationStatus;
/// #
/// # let mut descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/my-monorepo"),
/// # vec![]
/// # );
/// #
/// descriptor.set_validation_status(ProjectValidationStatus::Valid);
/// ```
pub fn set_validation_status(&mut self, validation_status: ProjectValidationStatus) {
self.validation_status = validation_status;
}
/// Gets a mutable reference to the validation status.
///
/// This is useful for validators that need to update the status in place.
///
/// # Returns
///
/// A mutable reference to the monorepo's validation status.
///
/// # Examples
///
/// ```
/// # use std::path::PathBuf;
/// # use sublime_standard_tools::monorepo::{MonorepoDescriptor, MonorepoKind};
/// # use sublime_standard_tools::project::ProjectValidationStatus;
/// #
/// # let mut descriptor = MonorepoDescriptor::minimal(
/// # MonorepoKind::YarnWorkspaces,
/// # PathBuf::from("/projects/my-monorepo"),
/// # vec![]
/// # );
/// #
/// *descriptor.validation_status_mut() = ProjectValidationStatus::Valid;
/// ```
pub fn validation_status_mut(&mut self) -> &mut ProjectValidationStatus {
&mut self.validation_status
}
}
impl Clone for MonorepoDescriptor {
fn clone(&self) -> Self {
let json: Option<package_json::PackageJson> = {
let root_path = self.root.clone();
let pkg_json_path = root_path.join("package.json");
let mut pkg_json =
package_json::PackageJsonManager::with_file_path(pkg_json_path.as_path());
if pkg_json.read_ref().is_ok() {
let json_ref = pkg_json.as_ref();
match serde_json::to_value(json_ref).and_then(serde_json::from_value) {
Ok(value) => Some(value),
Err(e) => {
log::warn!(
"Failed to serialize/deserialize package.json at {}: {}",
pkg_json_path.display(),
e
);
None
}
}
} else {
None
}
};
Self {
kind: self.kind.clone(),
root: self.root.clone(),
packages: self.packages.clone(),
name_to_package: self.name_to_package.clone(),
package_manager: self.package_manager.clone(),
package_json: json,
validation_status: self.validation_status.clone(),
}
}
}