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
//! Apply upgrades with automatic changeset creation.
//!
//! **What**: Provides a high-level function for applying dependency upgrades with
//! automatic changeset creation, integrating the upgrade application with the
//! changeset workflow.
//!
//! **How**: This module wraps the core upgrade application logic and adds automatic
//! changeset creation based on configuration. It determines which packages were
//! affected and creates or updates a changeset accordingly.
//!
//! **Why**: To provide a seamless integration between dependency upgrades and the
//! changeset workflow, ensuring all upgrades are properly tracked for versioning
//! and release management.
use create_changeset_for_upgrades;
use crateChangesetManager;
use crateUpgradeConfig;
use crateUpgradeResult;
use crateUpgradeSelection;
use cratePackageUpgrades;
use HashSet;
use Path;
use AsyncFileSystem;
use crateapply_upgrades;
use crateUpgradeResult as UpgradeResultType;
/// Applies upgrades with automatic changeset creation.
///
/// This function applies dependency upgrades and optionally creates or updates a changeset
/// based on the configuration. It's the recommended entry point for upgrade operations
/// that should be tracked through the changeset workflow.
///
/// # Workflow
///
/// 1. Apply upgrades to package.json files
/// 2. If `auto_changeset` is enabled and not dry-run:
/// - Get current git branch
/// - Create or update changeset with affected packages
/// - Set version bump type from config
/// 3. Return result with changeset ID
///
/// # Arguments
///
/// * `available_upgrades` - List of available upgrades from detection
/// * `selection` - Selection criteria for filtering upgrades
/// * `dry_run` - If true, preview changes without writing files or creating changesets
/// * `workspace_root` - Root directory of the workspace
/// * `config` - Upgrade configuration including changeset settings
/// * `changeset_manager` - Manager instance for changeset operations (optional)
/// * `fs` - Filesystem implementation for reading/writing files
///
/// # Returns
///
/// `UpgradeResult` containing details of applied upgrades, summary, and changeset ID
///
/// # Errors
///
/// Returns `UpgradeError` if:
/// - Upgrade application fails
/// - Changeset creation fails (only if auto_changeset is enabled)
/// - Configuration is invalid
///
/// # Examples
///
/// ```rust,ignore
/// use sublime_pkg_tools::upgrade::{
/// apply_with_changeset, detect_upgrades, DetectionOptions, UpgradeSelection
/// };
/// use sublime_pkg_tools::changeset::ChangesetManager;
/// use sublime_pkg_tools::config::{PackageToolsConfig, UpgradeConfig};
/// 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 fs = FileSystemManager::new();
/// let config = PackageToolsConfig::default();
///
/// // Detect upgrades
/// let options = DetectionOptions::all();
/// let available = detect_upgrades(&workspace_root, options, &fs).await?;
///
/// // Apply with automatic changeset creation
/// let manager = ChangesetManager::new(&workspace_root).await?;
/// let selection = UpgradeSelection::patch_only();
/// let result = apply_with_changeset(
/// available.packages,
/// selection,
/// false, // Not dry run
/// &workspace_root,
/// &config.upgrade,
/// Some(&manager),
/// &fs
/// ).await?;
///
/// if let Some(changeset_id) = result.changeset_id {
/// println!("Upgrades tracked in changeset: {}", changeset_id);
/// }
/// # Ok(())
/// # }
/// ```
pub async
/// Extracts unique package names from upgrade results.
///
/// # Arguments
///
/// * `result` - Upgrade result containing applied upgrades
///
/// # Returns
///
/// Set of unique package names that were modified
/// Extracts package name from a package path.
///
/// This function attempts to read the package.json file to get the package name.
/// If that fails, it uses the directory name as a fallback.
///
/// # Arguments
///
/// * `package_path` - Path to the package directory
///
/// # Returns
///
/// Package name if it can be determined, None otherwise