rlls 0.0.29

Cut a version, tag it, and publish a GitHub Release with raw git notes
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#![allow(dead_code, clippy::empty_line_after_outer_attr)]

use anyhow::anyhow;
use clap::{Parser, Subcommand, ValueEnum};
use indicatif::MultiProgress;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process;
use std::time::Instant;

mod changelog;
mod config;
mod git;
mod notes;
mod project;
mod rollback;
mod selfupdate;
mod semver;
mod shell;
mod ui;
mod util;
mod version;

#[derive(Parser)]
#[command(
    name = "rlls",
    about = "release utility for git + github",
    disable_help_subcommand = true,
    arg_required_else_help = true
)]
struct Cli {
    #[arg(value_enum)]
    kind: Option<ReleaseKind>,
    #[command(subcommand)]
    command: Option<Commands>,
    #[arg(long)]
    no_changelog: bool,
    #[arg(long)]
    repo: Option<String>,
    #[arg(long)]
    dry: bool,
    #[arg(long)]
    ignore_package: bool,
    #[arg(long, default_value = "rc")]
    id: String,
    #[arg(long)]
    bump: Option<ReleaseKind>,
    #[arg(long)]
    monorepo: bool,
    #[arg(long)]
    since: Option<String>,
}

#[derive(Subcommand)]
enum Commands {
    Prerelease,
    Finalize,
    Rollback {
        #[arg(long)]
        local: bool,
    },
    SelfUpdate,
}

#[derive(ValueEnum, Clone, Debug)]
enum ReleaseKind {
    Patch,
    Minor,
    Major,
}

impl std::str::FromStr for ReleaseKind {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_ascii_lowercase().as_str() {
            "patch" | "p" => Ok(ReleaseKind::Patch),
            "minor" | "m" => Ok(ReleaseKind::Minor),
            "major" | "M" => Ok(ReleaseKind::Major),
            _ => Err("invalid bump kind".into()),
        }
    }
}

enum Action<'a> {
    Release(ReleaseKind),
    Prerelease { id: &'a str, base: ReleaseKind },
    Finalize,
    Rollback { remote: bool },
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
    ui::clear_and_header();

    let cli = Cli::parse();
    let cfg = config::load(".rllsrc")?;

    if git::working_tree_dirty()
        && !matches!(cli.command, Some(Commands::Rollback { .. }))
        && !matches!(cli.command, Some(Commands::SelfUpdate))
    {
        ui::err("working tree is not clean");
        process::exit(2);
    }

    ui::section("rlls");
    let repo = cli.repo.clone().unwrap_or(git::current_repo_name()?);
    ui::info(&format!("loading config from {}", cfg.path.display()));
    ui::ok(&format!("detected repo: {}", repo));
    ui::divider();

    let action = match &cli.command {
        Some(Commands::Prerelease) => {
            let base = cli.bump.clone().unwrap_or_else(|| {
                cfg.default_bump.parse().unwrap_or(ReleaseKind::Patch)
            });
            Action::Prerelease { id: &cli.id, base }
        }
        Some(Commands::Finalize) => Action::Finalize,
        Some(Commands::Rollback { local }) => {
            Action::Rollback { remote: !*local }
        }
        Some(Commands::SelfUpdate) => {
            selfupdate::run(&repo).await?;
            return Ok(());
        }
        _ => {
            let k = cli.kind.clone().unwrap_or_else(|| {
                cfg.default_bump.parse().unwrap_or(ReleaseKind::Patch)
            });
            Action::Release(k)
        }
    };

    if let Action::Rollback { remote } = &action {
        rollback::run(&repo, *remote).await?;
        return Ok(());
    }

    let action_label = match action {
        Action::Release(_) => "release",
        Action::Prerelease { .. } => "prerelease",
        Action::Finalize => "finalize",
        Action::Rollback { .. } => unreachable!(),
    };

    ui::info(&format!("selected action: {}", action_label));
    if cli.no_changelog || !cfg.changelog_enabled {
        ui::warn("changelog disabled");
    }
    if cli.dry {
        ui::warn("dry run mode active");
    }
    ui::divider();

    let root = std::env::current_dir()?;
    let auto_monorepo = project::looks_like_monorepo(&root)?;
    let monorepo_mode = cli.monorepo || auto_monorepo;

    if monorepo_mode {
        run_monorepo(&root, &repo, &cfg, &cli, action).await?;
        return Ok(());
    }

    run_single(&root, &repo, &cfg, &cli, action).await
}

async fn run_single(
    root: &Path,
    repo: &str,
    cfg: &config::Config,
    cli: &Cli,
    action: Action<'_>,
) -> anyhow::Result<()> {
    let current_ver = crate::project::read_current_version(root)?;
    let base_ver = crate::semver::base_of(&current_ver);

    let (next_version, next_tag) = match &action {
        Action::Release(kind) => {
            let bk = match kind {
                ReleaseKind::Major => semver::BumpKind::Major,
                ReleaseKind::Minor => semver::BumpKind::Minor,
                ReleaseKind::Patch => semver::BumpKind::Patch,
            };
            let v = crate::semver::bump(&base_ver, bk)?;
            (v.clone(), format!("v{}", v))
        }
        Action::Prerelease { id, base } => {
            let bk = match base {
                ReleaseKind::Major => semver::BumpKind::Major,
                ReleaseKind::Minor => semver::BumpKind::Minor,
                ReleaseKind::Patch => semver::BumpKind::Patch,
            };
            let bumped = crate::semver::bump(&base_ver, bk)?;
            let v = crate::semver::prerelease_new(&bumped, id)?;
            (v.clone(), format!("v{}", v))
        }
        Action::Finalize => {
            let latest = git::last_reachable_tag()
                .ok_or_else(|| anyhow!("no tags to finalize"))?;
            let core = latest.trim_start_matches('v');
            let core_only =
                core.split('-').next().unwrap_or(core).to_string();
            let stable_tag = format!("v{}", core_only);
            if git::tag_exists(&stable_tag)? {
                return Err(anyhow!(
                    "stable tag {} already exists",
                    stable_tag
                ));
            }
            (core_only.clone(), stable_tag)
        }
        _ => unreachable!(),
    };

    let changelog_path = cfg
        .changelog_path
        .clone()
        .unwrap_or_else(|| "CHANGELOG.md".into());

    ui::summary_block(&[
        ("Mode", "single"),
        (
            "Action",
            match action {
                Action::Release(_) => "release",
                Action::Prerelease { .. } => "prerelease",
                Action::Finalize => "finalize",
                _ => "",
            },
        ),
        ("Repo", repo),
        ("Next tag", &next_tag),
        (
            "Changelog",
            if cli.no_changelog || !cfg.changelog_enabled {
                "disabled"
            } else {
                &changelog_path
            },
        ),
        ("Dry run", if cli.dry { "yes" } else { "no" }),
    ]);

    if !ui::confirm("proceed with release?") {
        ui::err("aborted by user");
        process::exit(1);
    }

    let mp: MultiProgress = ui::mp();
    let pb_bump =
        ui::make_spinner(&mp, "[bump]", "updating project version");
    let start_bump = Instant::now();

    tokio::task::spawn_blocking({
        let next = next_version.clone();
        let ignore = cli.ignore_package;
        move || {
            if !ignore {
                crate::project::write_versions(
                    &std::env::current_dir()?,
                    &next,
                )?;
            }
            Ok::<(), anyhow::Error>(())
        }
    })
    .await??;

    pb_bump.finish_with_message(ui::done_style(&format!(
        "done in {:.2?}",
        start_bump.elapsed()
    )));

    let prev_tag = git::last_reachable_tag();
    let notes_anchor = match &prev_tag {
        Some(prev) => format!("{}..{}", prev, next_tag),
        None => format!("{}^..{}", next_tag, next_tag),
    };

    if !cli.no_changelog && cfg.changelog_enabled {
        let pb =
            ui::make_spinner(&mp, "[changelog]", "writing changelog");
        let start = Instant::now();
        let notes = notes::build_release_notes(&notes_anchor, repo)
            .unwrap_or_else(|_| "(no changes)".to_string());
        tokio::task::spawn_blocking({
            let cfg = cfg.clone();
            let tag = next_tag.clone();
            move || changelog::maybe_update(&cfg, &tag, &notes, false)
        })
        .await??;
        pb.finish_with_message(ui::done_style(&format!(
            "done in {:.2?}",
            start.elapsed()
        )));
    }

    let pb_stage =
        ui::make_spinner(&mp, "[stage]", "staging changes");
    let start_stage = Instant::now();
    tokio::task::spawn_blocking(git::add_all).await??;
    pb_stage.finish_with_message(ui::done_style(&format!(
        "done in {:.2?}",
        start_stage.elapsed()
    )));

    let pb_commit =
        ui::make_spinner(&mp, "[commit]", "creating commit");
    let start_commit = Instant::now();
    if tokio::task::spawn_blocking(git::has_staged_changes).await?? {
        let commit_msg = cfg
            .bump_commit_message
            .replace("{{version}}", &next_version);
        let msg = commit_msg.clone();
        tokio::task::spawn_blocking(move || {
            git::commit_with_message(&msg)
        })
        .await??;
        pb_commit.finish_with_message(ui::done_style(&format!(
            "done in {:.2?}",
            start_commit.elapsed()
        )));
    } else {
        pb_commit.finish_with_message(ui::done_style("no changes"));
    }

    let pb_tag = ui::make_spinner(
        &mp,
        "[tag]",
        &format!("creating tag {}", next_tag),
    );
    let start_tag = Instant::now();
    let tag_msg_opt = cfg
        .bump_tag_message
        .as_deref()
        .map(|t| t.replace("{{version}}", &next_version));
    let tag_clone = next_tag.clone();
    tokio::task::spawn_blocking(move || {
        git::create_annotated_tag(&tag_clone, tag_msg_opt.as_deref())
    })
    .await??;
    pb_tag.finish_with_message(ui::done_style(&format!(
        "done in {:.2?}",
        start_tag.elapsed()
    )));

    let range = match prev_tag {
        Some(prev) => format!("{}..{}", prev, next_tag),
        None => format!("{}^..{}", next_tag, next_tag),
    };
    let notes = notes::build_release_notes(&range, repo)
        .unwrap_or_else(|_| "(no changes)".to_string());

    let pb_push_c =
        ui::make_spinner(&mp, "[push:commits]", "pushing commits");
    let dry = cli.dry;
    tokio::task::spawn_blocking(move || git::push_commits(dry))
        .await??;
    pb_push_c.finish_with_message(ui::done_style("done"));

    let pb_push_t =
        ui::make_spinner(&mp, "[push:tag]", "pushing tag");
    let dry2 = cli.dry;
    let tag2 = next_tag.clone();
    tokio::task::spawn_blocking(move || git::push_tag(&tag2, dry2))
        .await??;
    pb_push_t.finish_with_message(ui::done_style("done"));

    let pb_rel = ui::make_spinner(
        &mp,
        "[release]",
        "publishing github release",
    );
    let repo_s = repo.to_string();
    let notes_s = notes.clone();
    let tag3 = next_tag.clone();
    tokio::task::spawn_blocking(move || {
        git::publish_github_release(
            &tag3.replace('/', "_"),
            &repo_s,
            &notes_s,
        )
    })
    .await??;
    pb_rel.finish_with_message(ui::done_style("done"));

    ui::ok("release completed successfully");
    Ok(())
}

async fn run_monorepo(
    root: &Path,
    owner_repo: &str,
    cfg: &config::Config,
    cli: &Cli,
    _action: Action<'_>,
) -> anyhow::Result<()> {
    let since_tag = if let Some(s) = &cli.since {
        Some(s.clone())
    } else {
        git::last_reachable_tag()
    };

    let packages = project::discover_packages(root)?;
    if packages.is_empty() {
        return Err(anyhow!("no packages detected"));
    }

    let range = match &since_tag {
        Some(t) => format!("{}..HEAD", t),
        None => "HEAD^..HEAD".to_string(),
    };
    let changed_files = git::diff_names(&range)?;
    let mut changed_by_pkg: BTreeMap<PathBuf, usize> =
        BTreeMap::new();

    for f in changed_files {
        let pf = PathBuf::from(&f);
        if let Some((pkg, _lang)) =
            project::match_file_to_package(&packages, &pf)
        {
            *changed_by_pkg.entry(pkg.path.clone()).or_insert(0) += 1;
        }
    }

    let affected: Vec<project::Package> = packages
        .iter()
        .filter(|p| changed_by_pkg.contains_key(&p.path))
        .cloned()
        .collect();

    if affected.is_empty() {
        ui::warn("no package changes since tag or range");
    }

    let mut decisions: Vec<(
        project::Package,
        Option<semver::BumpKind>,
    )> = vec![];
    for p in &affected {
        let headless = cli.bump.clone();
        let choice = if let Some(k) = headless {
            Some(map_release_kind(k))
        } else {
            ui::prompt_bump(&format!(
                "{} [{}]",
                p.name,
                p.lang.as_str()
            ))
        };
        decisions.push((p.clone(), choice));
    }

    decisions.retain(|(_, d)| d.is_some());
    if decisions.is_empty() {
        ui::warn("no packages selected for bump");
        return Ok(());
    }

    let mp: MultiProgress = ui::mp();
    let pb_bump =
        ui::make_spinner(&mp, "[bump]", "updating versions");
    let start_bump = Instant::now();

    let mut new_versions: Vec<(project::Package, String, usize)> =
        vec![];

    for (pkg, kind_opt) in &decisions {
        let kind = kind_opt.unwrap();
        let cur = project::read_current_version(&pkg.path)?;
        let base = semver::base_of(&cur);
        let next = semver::bump(&base, kind)?;
        project::write_one_package_version(pkg, &next)?;
        let count =
            changed_by_pkg.get(&pkg.path).copied().unwrap_or(0);
        new_versions.push((pkg.clone(), next, count));
    }

    pb_bump.finish_with_message(ui::done_style(&format!(
        "done in {:.2?}",
        start_bump.elapsed()
    )));
    tokio::task::spawn_blocking(git::add_all).await??;

    let pb_commit =
        ui::make_spinner(&mp, "[commit]", "creating commit");
    let start_commit = Instant::now();

    // build list + template replacements for the single monorepo commit
    let list_lines: Vec<String> = new_versions
        .iter()
        .map(|(pkg, v, cnt)| {
            format!("- {}@{} ({} files)", pkg.name, v, cnt)
        })
        .collect();
    let count = new_versions.len();
    let primary_name = if count == 1 {
        new_versions[0].0.name.clone()
    } else {
        "monorepo".to_string()
    };
    let primary_version = if count == 1 {
        new_versions[0].1.clone()
    } else {
        "multi".to_string()
    };
    let tpl = cfg
        .monorepo_bump_commit_message
        .as_deref()
        .unwrap_or(&cfg.bump_commit_message);
    let templated = tpl
        .replace("{{count}}", &count.to_string())
        .replace("{{list}}", &list_lines.join("\n"))
        .replace("{{name}}", &primary_name)
        .replace("{{version}}", &primary_version);
    let default_msg = format!(
        "chore(release): bump {} packages\n\n{}",
        count,
        list_lines.join("\n")
    );
    let final_msg = if templated.trim().is_empty() {
        default_msg
    } else {
        templated
    };

    if tokio::task::spawn_blocking(git::has_staged_changes).await?? {
        let s = final_msg.clone();
        tokio::task::spawn_blocking(move || {
            git::commit_with_message(&s)
        })
        .await??;
        pb_commit.finish_with_message(ui::done_style(&format!(
            "done in {:.2?}",
            start_commit.elapsed()
        )));
    } else {
        pb_commit.finish_with_message(ui::done_style("no changes"));
    }

    let pb_tags = ui::make_spinner(&mp, "[tag]", "creating tags");
    let start_tags = Instant::now();

    let mut created_tags: Vec<String> = vec![];
    for (pkg, v, _) in &new_versions {
        // use configured template for tag construction
        let tag = project::make_pkg_tag(pkg, v, cfg);
        let msg = cfg
            .bump_tag_message
            .as_deref()
            .unwrap_or("{{name}}@{{version}}")
            .replace("{{name}}", &pkg.name)
            .replace("{{version}}", v);
        git::create_annotated_tag(&tag, Some(&msg))?;
        created_tags.push(tag);
    }
    pb_tags.finish_with_message(ui::done_style(&format!(
        "done in {:.2?}",
        start_tags.elapsed()
    )));

    let pb_push_c =
        ui::make_spinner(&mp, "[push:commits]", "pushing commits");
    let dry = cli.dry;
    tokio::task::spawn_blocking(move || git::push_commits(dry))
        .await??;
    pb_push_c.finish_with_message(ui::done_style("done"));

    let pb_push_t =
        ui::make_spinner(&mp, "[push:tags]", "pushing tags");
    for t in &created_tags {
        let dry = cli.dry;
        let tag = t.replace('/', "_");
        tokio::task::spawn_blocking(move || git::push_tag(&tag, dry))
            .await??;
    }
    pb_push_t.finish_with_message(ui::done_style("done"));

    if !cli.no_changelog && cfg.changelog_enabled {
        let pb =
            ui::make_spinner(&mp, "[changelog]", "writing changelog");
        let start = Instant::now();
        let anchor = match &since_tag {
            Some(prev) => format!("{}..HEAD", prev),
            None => "HEAD^..HEAD".to_string(),
        };
        let mut s = String::new();
        s.push_str("### Packages\n");
        for (pkg, v, cnt) in &new_versions {
            s.push_str(&format!("#### {} @ {}\n", pkg.name, v));
            s.push_str(&format!("files: {}\n", cnt));
            s.push_str(&format!("diff: {}\n\n", anchor));
        }
        let tag_for_entry = {
            use chrono::Utc;
            format!("packages {}", Utc::now().format("%Y-%m-%d"))
        };
        changelog::maybe_update(cfg, &tag_for_entry, &s, false)?;
        pb.finish_with_message(ui::done_style(&format!(
            "done in {:.2?}",
            start.elapsed()
        )));
    }

    // per package github releases with scoped notes
    if cfg.releases_per_package {
        let pb_rel = ui::make_spinner(
            &mp,
            "[release]",
            "publishing github releases",
        );
        for (idx, (pkg, v, _)) in new_versions.iter().enumerate() {
            let tag = project::make_pkg_tag(pkg, v, cfg);
            let prev = project::last_tag_for_package(pkg, cfg)?;
            let range = match prev {
                Some(p) => format!("{}..{}", p, tag),
                None => format!("{}^..{}", tag, tag),
            };
            let notes = notes::build_release_notes_scoped(
                &range,
                owner_repo,
                std::slice::from_ref(&pkg.path),
            )
            .unwrap_or_else(|_| "(no changes)".to_string());
            let repo_s = owner_repo.to_string();
            let tag_s = tag.replace('/', "_");
            // slight stagger for spinner readability
            let _ = &idx;
            tokio::task::spawn_blocking(move || {
                git::publish_github_release(&tag_s, &repo_s, &notes)
            })
            .await??;
        }
        pb_rel.finish_with_message(ui::done_style("done"));

        // optional umbrella release if enabled
        if cfg.umbrella_release {
            let mut body = String::from("## Packages\n\n");
            for (pkg, v, cnt) in &new_versions {
                body.push_str(&format!(
                    "- {}@{} ({} files)\n",
                    pkg.name, v, cnt
                ));
            }
            body.push('\n');
            let umbrella_tag = {
                use chrono::Utc;
                format!(
                    "packages@{}",
                    Utc::now().format("%Y%m%d%H%M%S")
                )
            };
            let repo_s = owner_repo.to_string();
            let tag_s = umbrella_tag.replace('/', "_");
            tokio::task::spawn_blocking(move || {
                git::publish_github_release(&tag_s, &repo_s, &body)
            })
            .await??;
        }
    } else {
        // legacy single combined release
        let pb_rel = ui::make_spinner(
            &mp,
            "[release]",
            "publishing github release",
        );
        let mut rel_body = String::from("## Packages\n\n");
        for (pkg, v, cnt) in &new_versions {
            rel_body.push_str(&format!(
                "- {}@{} ({} files)\n",
                pkg.name, v, cnt
            ));
        }
        rel_body.push('\n');
        let gh_tag =
            created_tags.last().cloned().unwrap_or_else(|| {
                format!(
                    "packages@{}",
                    chrono::Utc::now().format("%Y%m%d%H%M%S")
                )
            });
        let repo_s = owner_repo.to_string();
        let tag_s = gh_tag.replace('/', "_");
        tokio::task::spawn_blocking(move || {
            git::publish_github_release(&tag_s, &repo_s, &rel_body)
        })
        .await??;
        pb_rel.finish_with_message(ui::done_style("done"));
    }

    ui::ok("monorepo release completed successfully");
    Ok(())
}

fn map_release_kind(k: ReleaseKind) -> semver::BumpKind {
    match k {
        ReleaseKind::Major => semver::BumpKind::Major,
        ReleaseKind::Minor => semver::BumpKind::Minor,
        ReleaseKind::Patch => semver::BumpKind::Patch,
    }
}