1use crate::config;
2use crate::error::CliError;
3use crate::ops::cargo;
4use crate::ops::git;
5use crate::steps::plan;
6
7#[derive(Debug, Clone, clap::Args)]
8pub struct ReleaseStep {
9 #[command(flatten)]
10 manifest: clap_cargo::Manifest,
11
12 #[command(flatten)]
13 workspace: clap_cargo::Workspace,
14
15 #[arg(long, conflicts_with = "level_or_version")]
17 unpublished: bool,
18
19 #[arg(value_name = "LEVEL|VERSION")]
21 level_or_version: Option<super::TargetVersion>,
22
23 #[arg(short, long, requires = "level_or_version")]
25 metadata: Option<String>,
26
27 #[arg(short = 'x', long)]
29 execute: bool,
30
31 #[arg(short = 'n', long, conflicts_with = "execute", hide = true)]
32 dry_run: bool,
33
34 #[arg(long)]
36 no_confirm: bool,
37
38 #[arg(long, value_name = "NAME")]
40 prev_tag_name: Option<String>,
41
42 #[command(flatten)]
43 config: config::ConfigArgs,
44}
45
46impl ReleaseStep {
47 pub fn run(&self) -> Result<(), CliError> {
48 git::git_version()?;
49 let mut index = crate::ops::index::CratesIoIndex::new();
50
51 if self.dry_run {
52 let _ =
53 crate::ops::shell::warn("`--dry-run` is superfluous, dry-run is done by default");
54 }
55
56 let ws_meta = self
57 .manifest
58 .metadata()
59 .features(cargo_metadata::CargoOpt::AllFeatures)
61 .exec()?;
62 let mut ws_config = config::load_workspace_config(&self.config, &ws_meta)?;
63 let mut pkgs = plan::load(&self.config, &ws_meta)?;
64
65 for pkg in pkgs.values_mut() {
66 if let Some(prev_tag) = self.prev_tag_name.as_ref() {
67 pkg.set_prior_tag(prev_tag.to_owned());
70 }
71 if pkg.config.release() {
72 if let Some(level_or_version) = &self.level_or_version {
73 pkg.bump(level_or_version, self.metadata.as_deref())?;
74 }
75 }
76 if index.has_krate(
77 pkg.config.registry(),
78 &pkg.meta.name,
79 pkg.config.certs_source(),
80 )? {
81 pkg.ensure_owners = false;
83 }
84 }
85
86 let (_selected_pkgs, excluded_pkgs) =
87 if self.unpublished && self.workspace == clap_cargo::Workspace::default() {
88 ws_meta.packages.iter().partition(|_| false)
89 } else {
90 self.workspace.partition_packages(&ws_meta)
91 };
92 for excluded_pkg in &excluded_pkgs {
93 let Some(pkg) = pkgs.get_mut(&excluded_pkg.id) else {
94 continue;
96 };
97 if !pkg.config.release() {
98 continue;
99 }
100
101 let crate_name = pkg.meta.name.as_str();
102 let explicitly_excluded = self.workspace.exclude.contains(&excluded_pkg.name);
103 if pkg.config.release()
106 && pkg.config.publish()
107 && self.unpublished
108 && !explicitly_excluded
109 {
110 let version = &pkg.initial_version;
111 if !cargo::is_published(
112 &mut index,
113 pkg.config.registry(),
114 crate_name,
115 &version.full_version_string,
116 pkg.config.certs_source(),
117 ) {
118 log::debug!(
119 "enabled {}, v{} is unpublished",
120 crate_name,
121 version.full_version_string
122 );
123 continue;
124 }
125 }
126
127 pkg.planned_version = None;
128 pkg.config.release = Some(false);
129
130 if let Some(prior_tag_name) = &pkg.prior_tag {
131 if let Some(changed) =
132 crate::steps::version::changed_since(&ws_meta, pkg, prior_tag_name)
133 {
134 if !changed.is_empty() {
135 let _ = crate::ops::shell::warn(format!(
136 "disabled by user, skipping {crate_name} which has files changed since {prior_tag_name}: {changed:#?}"
137 ));
138 } else {
139 log::trace!(
140 "disabled by user, skipping {} (no changes since {})",
141 crate_name,
142 prior_tag_name
143 );
144 }
145 } else {
146 log::debug!(
147 "disabled by user, skipping {} (no {} tag)",
148 crate_name,
149 prior_tag_name
150 );
151 }
152 } else {
153 log::debug!("disabled by user, skipping {} (no tag found)", crate_name,);
154 }
155 }
156
157 let pkgs = plan::plan(pkgs)?;
158
159 for excluded_pkg in &excluded_pkgs {
160 let Some(pkg) = pkgs.get(&excluded_pkg.id) else {
161 continue;
163 };
164
165 if pkg.config.publish() && pkg.config.registry().is_none() {
167 let version = pkg.planned_version.as_ref().unwrap_or(&pkg.initial_version);
168 let crate_name = pkg.meta.name.as_str();
169 if !cargo::is_published(
170 &mut index,
171 pkg.config.registry(),
172 crate_name,
173 &version.full_version_string,
174 pkg.config.certs_source(),
175 ) {
176 let _ = crate::ops::shell::warn(format!(
177 "disabled by user, skipping {} v{} despite being unpublished",
178 crate_name, version.full_version_string,
179 ));
180 }
181 }
182 }
183
184 let (selected_pkgs, excluded_pkgs): (Vec<_>, Vec<_>) = pkgs
185 .into_iter()
186 .map(|(_, pkg)| pkg)
187 .partition(|p| p.config.release());
188 if selected_pkgs.is_empty() {
189 let _ = crate::ops::shell::error("no packages selected");
190 return Err(2.into());
191 }
192
193 let dry_run = !self.execute;
194 let mut failed = false;
195
196 let consolidate_commits = super::consolidate_commits(&selected_pkgs, &excluded_pkgs)?;
197 ws_config.consolidate_commits = Some(consolidate_commits);
198
199 failed |= !super::verify_git_is_clean(
201 ws_meta.workspace_root.as_std_path(),
202 dry_run,
203 log::Level::Error,
204 )?;
205
206 failed |= !super::verify_tags_missing(&selected_pkgs, dry_run, log::Level::Error)?;
207
208 failed |=
209 !super::verify_monotonically_increasing(&selected_pkgs, dry_run, log::Level::Error)?;
210
211 let mut double_publish = false;
212 for pkg in &selected_pkgs {
213 if !pkg.config.publish() {
214 continue;
215 }
216 let version = pkg.planned_version.as_ref().unwrap_or(&pkg.initial_version);
217 let crate_name = pkg.meta.name.as_str();
218 if cargo::is_published(
219 &mut index,
220 pkg.config.registry(),
221 crate_name,
222 &version.full_version_string,
223 pkg.config.certs_source(),
224 ) {
225 let _ = crate::ops::shell::error(format!(
226 "{} {} is already published",
227 crate_name, version.full_version_string
228 ));
229 double_publish = true;
230 }
231 }
232 if double_publish {
233 failed = true;
234 if !dry_run {
235 return Err(101.into());
236 }
237 }
238
239 super::warn_changed(&ws_meta, &selected_pkgs)?;
240
241 failed |= !super::verify_git_branch(
242 ws_meta.workspace_root.as_std_path(),
243 &ws_config,
244 dry_run,
245 log::Level::Error,
246 )?;
247
248 failed |= !super::verify_if_behind(
249 ws_meta.workspace_root.as_std_path(),
250 &ws_config,
251 dry_run,
252 log::Level::Warn,
253 )?;
254
255 failed |= !super::verify_metadata(&selected_pkgs, dry_run, log::Level::Error)?;
256 failed |= !super::verify_rate_limit(
257 &selected_pkgs,
258 &mut index,
259 &ws_config.rate_limit,
260 dry_run,
261 log::Level::Error,
262 )?;
263
264 super::confirm("Release", &selected_pkgs, self.no_confirm, dry_run)?;
266
267 if consolidate_commits {
269 let update_lock =
270 super::version::update_versions(&ws_meta, &selected_pkgs, &excluded_pkgs, dry_run)?;
271 if update_lock {
272 log::debug!("updating lock file");
273 if !dry_run {
274 let workspace_path = ws_meta.workspace_root.as_std_path().join("Cargo.toml");
275 cargo::update_lock(&workspace_path)?;
276 }
277 }
278
279 for pkg in &selected_pkgs {
280 super::replace::replace(pkg, dry_run)?;
281
282 super::hook::hook(&ws_meta, pkg, dry_run)?;
284 }
285
286 super::commit::workspace_commit(&ws_meta, &ws_config, &selected_pkgs, dry_run)?;
287 } else {
288 for pkg in &selected_pkgs {
289 if let Some(version) = pkg.planned_version.as_ref() {
290 let crate_name = pkg.meta.name.as_str();
291 let _ = crate::ops::shell::status(
292 "Upgrading",
293 format!(
294 "{} from {} to {}",
295 crate_name,
296 pkg.initial_version.full_version_string,
297 version.full_version_string
298 ),
299 );
300 cargo::set_package_version(
301 &pkg.manifest_path,
302 version.full_version_string.as_str(),
303 dry_run,
304 )?;
305 crate::steps::version::update_dependent_versions(
306 &ws_meta, pkg, version, dry_run,
307 )?;
308 if dry_run {
309 log::debug!("updating lock file");
310 } else {
311 cargo::update_lock(&pkg.manifest_path)?;
312 }
313 }
314
315 super::replace::replace(pkg, dry_run)?;
316
317 super::hook::hook(&ws_meta, pkg, dry_run)?;
319
320 super::commit::pkg_commit(pkg, dry_run)?;
321 }
322 }
323
324 super::publish::publish(&selected_pkgs, dry_run, &ws_config.unstable)?;
326 super::owner::ensure_owners(&selected_pkgs, dry_run)?;
327
328 super::tag::tag(&selected_pkgs, dry_run)?;
330
331 super::push::push(&ws_config, &ws_meta, &selected_pkgs, dry_run)?;
333
334 super::finish(failed, dry_run)
335 }
336}