releasaurus-core 0.16.0

A comprehensive release automation tool that streamlines the software release process across multiple programming languages and forge platforms
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
//! Resolved package configuration.
//!
//! This module handles the resolution of package configuration from
//! multiple sources (TOML config, CLI overrides, defaults) into a
//! single, validated ResolvedPackage ready for processing.

use derive_builder::Builder;
use std::{
    borrow::Cow,
    path::{Path, PathBuf},
    rc::Rc,
};

use crate::{
    analyzer::config::AnalyzerConfig,
    config::{
        package::PackageConfig, prerelease::PrereleaseConfig,
        release_type::ReleaseType,
    },
    error::{ReleasaurusError, Result},
    orchestrator::config::OrchestratorConfig,
};

pub mod analyzer;
pub mod hash;
pub mod manifest;
pub mod resolvers;

pub use hash::ResolvedPackageHash;
pub use manifest::CompiledAdditionalManifest;

use analyzer::{AnalyzerParams, build_analyzer_config};
use manifest::compile_additional_manifests;
use resolvers::*;

use super::path_utils::normalize_path;

/// Builder parameters for constructing a ResolvedPackage.
#[derive(Debug, Builder)]
#[builder(setter(into), build_fn(private, name = "_build"))]
pub struct ResolvedPackageParams {
    orchestrator_config: Rc<OrchestratorConfig>,
    package_config: PackageConfig,
}

impl ResolvedPackageParamsBuilder {
    pub fn build(&self) -> Result<ResolvedPackage> {
        let params = self._build().map_err(|e| {
            ReleasaurusError::invalid_config(format!(
                "Failed to build resolved package: {}",
                e
            ))
        })?;
        ResolvedPackage::new(params)
    }
}

/// A fully resolved package configuration ready for processing.
///
/// This type represents a package after all configuration sources
/// have been merged and validated. All optional values have been
/// resolved to concrete values, paths have been normalized, and
/// complex configurations (like analyzer config) have been built.
#[derive(Debug)]
pub struct ResolvedPackage {
    pub name: String,
    pub normalized_workspace_root: PathBuf,
    pub normalized_full_path: PathBuf,
    pub release_type: ReleaseType,
    pub tag_prefix: String,
    pub sub_packages: Vec<ResolvedPackage>,
    pub prerelease: Option<PrereleaseConfig>,
    pub auto_start_next: bool,
    pub normalized_additional_paths: Vec<PathBuf>,
    pub compiled_additional_manifests: Vec<CompiledAdditionalManifest>,
    pub analyzer_config: AnalyzerConfig,
}

impl ResolvedPackage {
    /// Creates a builder for constructing a ResolvedPackage.
    pub fn builder() -> ResolvedPackageParamsBuilder {
        ResolvedPackageParamsBuilder::default()
    }

    /// Constructs a new ResolvedPackage from builder parameters.
    ///
    /// This orchestrates all the resolution logic by calling
    /// various resolver functions in the correct order.
    pub fn new(params: ResolvedPackageParams) -> Result<Self> {
        // Resolve basic properties
        let name = resolve_package_name(
            &params.package_config,
            &params.orchestrator_config.repo_name,
        );
        let tag_prefix = resolve_tag_prefix(
            &name,
            &params.package_config,
            &params.orchestrator_config.package_overrides,
            &params.orchestrator_config.global_overrides,
        );
        let auto_start = resolve_auto_start_next(
            &params.package_config,
            params.orchestrator_config.auto_start_next,
        );

        // Resolve complex configurations
        let prerelease = resolve_prerelease(
            &params.package_config,
            &params.orchestrator_config.prerelease,
            &params.orchestrator_config.global_overrides,
            &params.orchestrator_config.package_overrides,
        )?;

        let (breaking_always_increment_major, features_always_increment_minor) =
            resolve_version_increment_flags(
                &params.package_config,
                params.orchestrator_config.breaking_always_increment_major,
                params.orchestrator_config.features_always_increment_minor,
            );

        let (custom_major_increment_regex, custom_minor_increment_regex) =
            resolve_custom_increment_regexes(
                &params.package_config,
                &params.orchestrator_config.custom_major_increment_regex,
                &params.orchestrator_config.custom_minor_increment_regex,
            );

        // Normalize paths
        let (normalized_workspace_root, normalized_full_path) =
            normalize_package_paths(&params.package_config);

        // Compile manifests
        let compiled_additional_manifests = compile_additional_manifests(
            &normalized_full_path,
            &params.package_config,
        )?;

        // Resolve additional paths
        let normalized_additional_paths =
            normalize_additional_paths(&params.package_config);

        // Build analyzer config
        let analyzer_config = build_analyzer_config(AnalyzerParams {
            config: Rc::clone(&params.orchestrator_config),
            package_name: name.clone(),
            prerelease: prerelease.clone(),
            tag_prefix: tag_prefix.clone(),
            breaking_always_increment_major,
            custom_major_increment_regex,
            features_always_increment_minor,
            custom_minor_increment_regex,
        });

        // Resolve sub-packages
        let sub_packages = resolve_sub_packages_full(
            &params,
            &normalized_workspace_root,
            &tag_prefix,
            prerelease.clone(),
            auto_start,
            &analyzer_config,
        );

        Ok(ResolvedPackage {
            name,
            normalized_workspace_root,
            normalized_full_path,
            release_type: params
                .package_config
                .release_type
                .unwrap_or_default(),
            tag_prefix,
            sub_packages,
            prerelease,
            auto_start_next: auto_start,
            normalized_additional_paths,
            compiled_additional_manifests,
            analyzer_config,
        })
    }
}

// Private helper functions

/// Normalizes workspace and full package paths.
fn normalize_package_paths(package: &PackageConfig) -> (PathBuf, PathBuf) {
    let mut normalized_root = normalize_path(&package.workspace_root);
    if normalized_root == "." {
        normalized_root = Cow::from("");
    }

    let normalized_workspace_root =
        Path::new(normalized_root.as_ref()).to_path_buf();

    let full_path = normalized_workspace_root
        .join(&package.path)
        .to_string_lossy()
        .to_string();

    let mut normalized_full = normalize_path(&full_path);
    if normalized_full == "." {
        normalized_full = Cow::from("");
    }

    let normalized_full_path =
        Path::new(normalized_full.as_ref()).to_path_buf();

    (normalized_workspace_root, normalized_full_path)
}

/// Normalizes additional paths for a package.
fn normalize_additional_paths(package: &PackageConfig) -> Vec<PathBuf> {
    package
        .additional_paths
        .clone()
        .unwrap_or_default()
        .iter()
        .map(|p| {
            let normalized = normalize_path(p).to_string();
            if normalized == "." {
                Path::new("").to_path_buf()
            } else {
                Path::new(&normalized).to_path_buf()
            }
        })
        .collect()
}

/// Resolves all sub-packages for a package.
fn resolve_sub_packages_full(
    params: &ResolvedPackageParams,
    normalized_workspace_root: &Path,
    tag_prefix: &str,
    prerelease: Option<PrereleaseConfig>,
    auto_start: bool,
    analyzer_config: &AnalyzerConfig,
) -> Vec<ResolvedPackage> {
    let sub_packages = params
        .package_config
        .sub_packages
        .clone()
        .unwrap_or_default();

    sub_packages
        .iter()
        .map(|s| {
            let name = resolve_sub_package_name(
                s,
                &params.package_config.workspace_root,
                &params.orchestrator_config.repo_name,
            );

            let sub_path = normalized_workspace_root
                .join(&s.path)
                .to_string_lossy()
                .to_string();

            let normalized_sub_full = normalize_path(&sub_path);
            let normalized_sub_full_path =
                Path::new(normalized_sub_full.as_ref()).to_path_buf();

            ResolvedPackage {
                name,
                normalized_workspace_root: normalized_workspace_root
                    .to_path_buf(),
                normalized_full_path: normalized_sub_full_path,
                release_type: s.release_type.unwrap_or_default(),
                tag_prefix: tag_prefix.to_string(),
                sub_packages: vec![],
                prerelease: prerelease.clone(),
                auto_start_next: auto_start,
                normalized_additional_paths: vec![],
                compiled_additional_manifests: vec![],
                analyzer_config: analyzer_config.clone(),
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use url::Url;

    use super::*;
    use crate::{
        config::package::{PackageConfigBuilder, SubPackage},
        orchestrator::config::{CommitModifiers, GlobalOverrides},
    };
    use std::rc::Rc;

    #[test]
    fn resolves_sub_packages_with_explicit_names() {
        let config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(Rc::new(Default::default()))
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let pkg_config = PackageConfigBuilder::default()
            .name("parent-pkg")
            .path(".")
            .sub_packages(vec![
                SubPackage {
                    name: "sub-pkg-a".to_string(),
                    path: "packages/a".to_string(),
                    ..Default::default()
                },
                SubPackage {
                    name: "sub-pkg-b".to_string(),
                    path: "packages/b".to_string(),
                    ..Default::default()
                },
            ])
            .build()
            .unwrap();

        let resolved = ResolvedPackage::builder()
            .orchestrator_config(config)
            .package_config(pkg_config)
            .build()
            .unwrap();

        assert_eq!(resolved.sub_packages.len(), 2);
        assert_eq!(resolved.sub_packages[0].name, "sub-pkg-a");
        assert_eq!(resolved.sub_packages[1].name, "sub-pkg-b");
    }

    #[test]
    fn resolves_sub_packages_with_auto_generated_names() {
        let config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(Rc::new(Default::default()))
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let pkg_config = PackageConfigBuilder::default()
            .name("parent-pkg")
            .path(".")
            .sub_packages(vec![SubPackage {
                name: "".to_string(),
                path: "packages/my-package".to_string(),
                ..Default::default()
            }])
            .build()
            .unwrap();

        let resolved = ResolvedPackage::builder()
            .orchestrator_config(config)
            .package_config(pkg_config)
            .build()
            .unwrap();

        assert_eq!(resolved.sub_packages.len(), 1);
        // Name should be derived from the last path component
        assert_eq!(resolved.sub_packages[0].name, "my-package");
    }

    #[test]
    fn sub_packages_inherit_parent_tag_prefix() {
        let config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(Rc::new(Default::default()))
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let pkg_config = PackageConfigBuilder::default()
            .name("parent-pkg")
            .path(".")
            .tag_prefix("v")
            .sub_packages(vec![SubPackage {
                name: "sub-pkg".to_string(),
                path: "packages/sub".to_string(),
                ..Default::default()
            }])
            .build()
            .unwrap();

        let resolved = ResolvedPackage::builder()
            .orchestrator_config(config)
            .package_config(pkg_config)
            .build()
            .unwrap();

        // Parent has explicit tag prefix
        assert_eq!(resolved.tag_prefix, "v");
        // Sub-packages should inherit the same tag prefix
        assert_eq!(resolved.sub_packages[0].tag_prefix, "v");
    }

    #[test]
    fn sub_packages_normalize_paths_correctly() {
        let config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(Rc::new(Default::default()))
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let pkg_config = PackageConfigBuilder::default()
            .name("parent-pkg")
            .path("workspace")
            .sub_packages(vec![SubPackage {
                name: "sub-pkg".to_string(),
                path: "packages/sub".to_string(),
                ..Default::default()
            }])
            .build()
            .unwrap();

        let resolved = ResolvedPackage::builder()
            .orchestrator_config(config)
            .package_config(pkg_config)
            .build()
            .unwrap();

        assert_eq!(resolved.sub_packages.len(), 1);
        // Path should contain the sub-package directory
        let sub_path_str = resolved.sub_packages[0]
            .normalized_full_path
            .to_string_lossy()
            .to_string();
        assert!(
            sub_path_str.contains("packages") || sub_path_str.contains("sub")
        );
        // Workspace root should match parent's workspace root
        assert_eq!(
            resolved.sub_packages[0].normalized_workspace_root,
            resolved.normalized_workspace_root
        );
    }

    #[test]
    fn handles_empty_sub_packages_list() {
        let config = Rc::new(
            OrchestratorConfig::builder()
                .toml_config(Rc::new(Default::default()))
                .repo_name("test-repo")
                .repo_default_branch("main")
                .release_link_base_url(
                    Url::parse("https://example.com/").unwrap(),
                )
                .compare_link_base_url(
                    Url::parse("https://example.com/compare/").unwrap(),
                )
                .package_overrides(std::collections::HashMap::new())
                .global_overrides(GlobalOverrides::default())
                .commit_modifiers(CommitModifiers::default())
                .build()
                .unwrap(),
        );

        let pkg_config = PackageConfigBuilder::default()
            .name("parent-pkg")
            .path(".")
            .build()
            .unwrap();

        let resolved = ResolvedPackage::builder()
            .orchestrator_config(config)
            .package_config(pkg_config)
            .build()
            .unwrap();

        // Should have no sub-packages
        assert_eq!(resolved.sub_packages.len(), 0);
    }
}