sublime_pkg_tools 0.0.27

Package and version management toolkit for Node.js projects with changeset support
Documentation
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
//! Git integration for detecting packages affected by commits.
//!
//! **What**: Provides functionality to analyze Git commits and determine which packages
//! in a workspace are affected by file changes, supporting both monorepo and single-package
//! repositories.
//!
//! **How**: The `PackageDetector` uses `sublime_git_tools` to retrieve commit information
//! and file changes, then uses `sublime_standard_tools` to detect the workspace structure
//! (monorepo vs single package) and map changed files to their corresponding packages.
//!
//! **Why**: Automating the detection of affected packages from Git commits enables
//! developers to quickly identify which packages need version bumps and should be included
//! in a changeset, reducing manual work and potential errors in the release process.

use crate::config::PackageToolsConfig;
use crate::error::{ChangesetError, ChangesetResult};
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use sublime_git_tools::{Repo, RepoCommit};
use sublime_standard_tools::config::MonorepoConfig;
use sublime_standard_tools::filesystem::{AsyncFileSystem, FileSystemManager};
use sublime_standard_tools::monorepo::{MonorepoDetector, MonorepoDetectorTrait, WorkspacePackage};

/// Detects packages affected by Git commits.
///
/// The `PackageDetector` analyzes commits in a Git repository to determine which packages
/// are affected by the changes. It handles both monorepo and single-package repositories,
/// automatically detecting the workspace structure and mapping file changes to packages.
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::changeset::PackageDetector;
/// use sublime_git_tools::Repo;
/// use sublime_standard_tools::filesystem::FileSystemManager;
/// use std::path::PathBuf;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let workspace_root = PathBuf::from(".");
/// let repo = Repo::open(".")?;
/// let fs = FileSystemManager::new();
///
/// let detector = PackageDetector::new(workspace_root, repo, fs);
///
/// // Detect affected packages from specific commits
/// let commit_ids = vec!["abc123".to_string(), "def456".to_string()];
/// let packages = detector.detect_affected_packages(&commit_ids).await?;
///
/// println!("Affected packages: {:?}", packages);
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct PackageDetector<'a> {
    /// Root directory of the workspace.
    workspace_root: PathBuf,
    /// Git repository instance.
    repo: &'a Repo,
    /// Filesystem manager for file operations.
    fs: FileSystemManager,
    /// Monorepo detector for workspace analysis.
    monorepo_detector: MonorepoDetector<FileSystemManager>,
}

impl<'a> PackageDetector<'a> {
    /// Creates a new `PackageDetector`.
    ///
    /// # Parameters
    ///
    /// * `workspace_root` - The root directory of the workspace
    /// * `repo` - Git repository instance
    /// * `fs` - Filesystem manager
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use sublime_pkg_tools::changeset::PackageDetector;
    /// use sublime_git_tools::Repo;
    /// use sublime_standard_tools::filesystem::FileSystemManager;
    /// use std::path::PathBuf;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let detector = PackageDetector::new(
    ///     PathBuf::from("."),
    ///     Repo::open(".")?,
    ///     FileSystemManager::new(),
    /// );
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn new(workspace_root: impl Into<PathBuf>, repo: &'a Repo, fs: FileSystemManager) -> Self {
        let workspace_root = workspace_root.into();
        let monorepo_detector = MonorepoDetector::with_filesystem(fs.clone());
        Self { workspace_root, repo, fs, monorepo_detector }
    }

    /// Creates a new `PackageDetector` with workspace patterns from configuration.
    ///
    /// This constructor uses workspace patterns from the provided configuration,
    /// ensuring that packages in directories like `playground/*` are discovered
    /// correctly when specified in `repo.config.toml`.
    ///
    /// # Parameters
    ///
    /// * `workspace_root` - The root directory of the workspace
    /// * `repo` - Git repository instance
    /// * `fs` - Filesystem manager
    /// * `config` - Package tools configuration containing workspace patterns
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use sublime_pkg_tools::changeset::PackageDetector;
    /// use sublime_pkg_tools::config::PackageToolsConfig;
    /// use sublime_git_tools::Repo;
    /// use sublime_standard_tools::filesystem::FileSystemManager;
    /// use std::path::PathBuf;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = PackageToolsConfig::default();
    /// let detector = PackageDetector::new_with_config(
    ///     PathBuf::from("."),
    ///     Repo::open(".")?,
    ///     FileSystemManager::new(),
    ///     &config,
    /// );
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn new_with_config(
        workspace_root: impl Into<PathBuf>,
        repo: &'a Repo,
        fs: FileSystemManager,
        config: &PackageToolsConfig,
    ) -> Self {
        let workspace_root = workspace_root.into();
        let monorepo_config = Self::build_monorepo_config(config);
        let monorepo_detector =
            MonorepoDetector::with_filesystem_and_config(fs.clone(), monorepo_config);
        Self { workspace_root, repo, fs, monorepo_detector }
    }

    /// Builds a `MonorepoConfig` from the `PackageToolsConfig`.
    ///
    /// This method creates a `MonorepoConfig` that includes workspace patterns from
    /// the `repo.config.toml` file, ensuring all workspace directories are discovered.
    ///
    /// # Arguments
    ///
    /// * `config` - The package tools configuration containing workspace patterns
    ///
    /// # Returns
    ///
    /// Returns a `MonorepoConfig` with merged workspace patterns.
    #[must_use]
    fn build_monorepo_config(config: &PackageToolsConfig) -> MonorepoConfig {
        let mut monorepo_config = config.standard_config.monorepo.clone();

        // Merge workspace patterns from repo.config.toml if available
        if let Some(ref workspace) = config.workspace
            && !workspace.patterns.is_empty()
        {
            for pattern in &workspace.patterns {
                if !monorepo_config.workspace_patterns.contains(pattern) {
                    monorepo_config.workspace_patterns.push(pattern.clone());
                }
            }
        }

        monorepo_config
    }

    /// Detects packages affected by the given commits.
    ///
    /// This method analyzes each commit to determine which files were changed, then maps
    /// those files to packages. It handles both monorepo and single-package repositories.
    ///
    /// # Parameters
    ///
    /// * `commit_ids` - List of commit hashes to analyze
    ///
    /// # Returns
    ///
    /// A vector of unique package names that were affected by the commits.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Git operations fail
    /// - Package detection fails
    /// - File system operations fail
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # async fn example(detector: PackageDetector) -> Result<(), Box<dyn std::error::Error>> {
    /// let commits = vec![
    ///     "abc123def456".to_string(),
    ///     "789012ghi345".to_string(),
    /// ];
    ///
    /// let affected = detector.detect_affected_packages(&commits).await?;
    /// println!("Found {} affected packages", affected.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn detect_affected_packages(
        &self,
        commit_ids: &[String],
    ) -> ChangesetResult<Vec<String>> {
        if commit_ids.is_empty() {
            return Ok(Vec::new());
        }

        // Check if this is a monorepo
        let is_monorepo = self.is_monorepo().await?;

        // Get all changed files from commits
        let changed_files = self.get_changed_files_from_commits(commit_ids)?;

        if changed_files.is_empty() {
            return Ok(Vec::new());
        }

        // Map files to packages
        let packages = if is_monorepo {
            self.map_files_to_packages_monorepo(&changed_files).await?
        } else {
            self.map_files_to_packages_single(&changed_files).await?
        };

        Ok(packages)
    }

    /// Checks if the workspace is a monorepo.
    ///
    /// # Returns
    ///
    /// `true` if the workspace is detected as a monorepo, `false` otherwise.
    ///
    /// # Errors
    ///
    /// Returns an error if monorepo detection fails due to file system issues.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # async fn example(detector: PackageDetector) -> Result<(), Box<dyn std::error::Error>> {
    /// if detector.is_monorepo().await? {
    ///     println!("This is a monorepo workspace");
    /// } else {
    ///     println!("This is a single-package workspace");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn is_monorepo(&self) -> ChangesetResult<bool> {
        let result = self.monorepo_detector.is_monorepo_root(&self.workspace_root).await;

        match result {
            Ok(Some(_)) => Ok(true),
            Ok(None) => Ok(false),
            Err(e) => Err(ChangesetError::GitIntegration {
                operation: "monorepo detection".to_string(),
                reason: format!("Failed to detect monorepo: {}", e),
            }),
        }
    }

    /// Lists all packages in the workspace.
    ///
    /// For monorepos, returns all workspace packages. For single-package repositories,
    /// returns the single package if a package.json exists.
    ///
    /// # Returns
    ///
    /// A vector of package names found in the workspace.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Package detection fails
    /// - File system operations fail
    /// - package.json parsing fails
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # async fn example(detector: PackageDetector) -> Result<(), Box<dyn std::error::Error>> {
    /// let packages = detector.list_packages().await?;
    /// for package in packages {
    ///     println!("Found package: {}", package);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn list_packages(&self) -> ChangesetResult<Vec<String>> {
        let is_monorepo = self.is_monorepo().await?;

        if is_monorepo {
            let packages = self.get_workspace_packages().await?;
            Ok(packages.iter().map(|p| p.name.clone()).collect())
        } else {
            // Single package - get name from package.json
            let package_json_path = self.workspace_root.join("package.json");
            if self.fs.exists(&package_json_path).await {
                let content = self.fs.read_file_string(&package_json_path).await.map_err(|e| {
                    ChangesetError::GitIntegration {
                        operation: "read package.json".to_string(),
                        reason: format!("Failed to read package.json: {}", e),
                    }
                })?;

                let package_json: serde_json::Value =
                    serde_json::from_str(&content).map_err(|e| ChangesetError::GitIntegration {
                        operation: "parse package.json".to_string(),
                        reason: format!("Failed to parse package.json: {}", e),
                    })?;

                if let Some(name) = package_json.get("name").and_then(|n| n.as_str()) {
                    return Ok(vec![name.to_string()]);
                }
            }

            Ok(Vec::new())
        }
    }

    /// Gets the list of commits between two references.
    ///
    /// # Parameters
    ///
    /// * `from_ref` - Starting reference (commits after this)
    /// * `to_ref` - Ending reference (commits up to this)
    ///
    /// # Returns
    ///
    /// A vector of `RepoCommit` instances between the references.
    ///
    /// # Errors
    ///
    /// Returns an error if Git operations fail or references are invalid.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # fn example(detector: PackageDetector) -> Result<(), Box<dyn std::error::Error>> {
    /// let commits = detector.get_commits_between("v1.0.0", "HEAD")?;
    /// println!("Found {} commits", commits.len());
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_commits_between(
        &self,
        from_ref: &str,
        to_ref: &str,
    ) -> ChangesetResult<Vec<RepoCommit>> {
        self.repo.get_commits_between(from_ref, to_ref, &None).map_err(|e| {
            ChangesetError::GitIntegration {
                operation: format!("get commits between {} and {}", from_ref, to_ref),
                reason: format!("Failed to get commits: {}", e),
            }
        })
    }

    /// Gets commits since a specific reference.
    ///
    /// # Parameters
    ///
    /// * `since` - Optional reference to start from. If `None`, gets all commits.
    ///
    /// # Returns
    ///
    /// A vector of `RepoCommit` instances since the reference.
    ///
    /// # Errors
    ///
    /// Returns an error if Git operations fail or reference is invalid.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # fn example(detector: PackageDetector) -> Result<(), Box<dyn std::error::Error>> {
    /// // Get commits since last tag
    /// let commits = detector.get_commits_since(Some("v1.0.0".to_string()))?;
    ///
    /// // Get all commits
    /// let all_commits = detector.get_commits_since(None)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn get_commits_since(&self, since: Option<String>) -> ChangesetResult<Vec<RepoCommit>> {
        self.repo.get_commits_since(since, &None).map_err(|e| ChangesetError::GitIntegration {
            operation: "get commits since reference".to_string(),
            reason: format!("Failed to get commits: {}", e),
        })
    }

    /// Gets all files changed in the given commits.
    ///
    /// This method retrieves the list of files that were modified, added, or deleted
    /// in each commit. This method uses `get_files_changed_in_commit` which
    /// properly handles initial commits (commits without a parent) by comparing
    /// against an empty tree.
    ///
    /// For multiple commits, it collects all changed files from each commit.
    fn get_changed_files_from_commits(
        &self,
        commit_ids: &[String],
    ) -> ChangesetResult<Vec<PathBuf>> {
        if commit_ids.is_empty() {
            return Ok(Vec::new());
        }

        let mut all_files = HashSet::new();

        // Use get_files_changed_in_commit for each commit, which properly handles
        // initial commits (those without a parent) by comparing against an empty tree
        for commit_id in commit_ids {
            let files = self.repo.get_files_changed_in_commit(commit_id).map_err(|e| {
                ChangesetError::GitIntegration {
                    operation: format!("get files changed in commit {}", commit_id),
                    reason: format!("Failed to get changed files: {}", e),
                }
            })?;

            all_files.extend(files.into_iter().map(|f| PathBuf::from(f.path)));
        }

        Ok(all_files.into_iter().collect())
    }

    /// Maps changed files to packages in a monorepo.
    ///
    /// This method determines which packages contain the changed files by checking
    /// if each file path is within a package directory.
    async fn map_files_to_packages_monorepo(
        &self,
        changed_files: &[PathBuf],
    ) -> ChangesetResult<Vec<String>> {
        let packages = self.get_workspace_packages().await?;
        let mut affected_packages = HashSet::new();

        for file in changed_files {
            // Convert file to absolute path
            let file_absolute =
                if file.is_absolute() { file.clone() } else { self.workspace_root.join(file) };

            // Canonicalize the file path if possible, otherwise use as-is
            let file_canonical =
                file_absolute.canonicalize().unwrap_or_else(|_| file_absolute.clone());

            // Check which package contains this file
            for package in &packages {
                // Use the absolute_path field from WorkspacePackage
                let package_canonical = package
                    .absolute_path
                    .canonicalize()
                    .unwrap_or_else(|_| package.absolute_path.clone());

                // Check if file is within this package's directory
                if file_canonical.starts_with(&package_canonical) {
                    affected_packages.insert(package.name.clone());
                    break;
                }
            }
        }

        Ok(affected_packages.into_iter().collect())
    }

    /// Maps changed files to the single package.
    ///
    /// For single-package repositories, any file change (except certain ignored files)
    /// affects the single package.
    async fn map_files_to_packages_single(
        &self,
        changed_files: &[PathBuf],
    ) -> ChangesetResult<Vec<String>> {
        if changed_files.is_empty() {
            return Ok(Vec::new());
        }

        // Get the package name
        let packages = self.list_packages().await?;

        if packages.is_empty() {
            return Ok(Vec::new());
        }

        // In a single package repo, any change affects the package
        Ok(packages)
    }

    /// Gets all workspace packages in a monorepo.
    async fn get_workspace_packages(&self) -> ChangesetResult<Vec<WorkspacePackage>> {
        self.monorepo_detector.detect_packages(&self.workspace_root).await.map_err(|e| {
            ChangesetError::GitIntegration {
                operation: "get workspace packages".to_string(),
                reason: format!("Failed to get workspace packages: {}", e),
            }
        })
    }

    /// Returns a reference to the workspace root.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # fn example(detector: PackageDetector) {
    /// let root = detector.workspace_root();
    /// println!("Workspace root: {:?}", root);
    /// # }
    /// ```
    #[must_use]
    pub fn workspace_root(&self) -> &Path {
        &self.workspace_root
    }

    /// Returns a reference to the Git repository.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use sublime_pkg_tools::changeset::PackageDetector;
    /// # fn example(detector: PackageDetector) {
    /// let repo = detector.repo();
    /// println!("Repository path: {:?}", repo.get_repo_path());
    /// # }
    /// ```
    #[must_use]
    pub fn repo(&self) -> &Repo {
        self.repo
    }
}