ferrflow 5.36.0

Universal semantic versioning for monorepos and classic repos
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
use anyhow::Result;
use serde::Deserialize;
use std::path::{Path, PathBuf};

use crate::error_code::{self, ErrorCodeExt};

use super::Config;
use super::format::{CONFIG_FORMATS, ConfigFileFormat, format_handler};
use super::loader_js::{JS_CONFIG_FILENAME, TS_CONFIG_FILENAME};
use super::package::{FileFormat, PackageConfig, VersionedFile};
use super::types::{
    BranchChannelConfig, ChannelValue, ForgeKind, HooksConfig, PrereleaseIdentifier,
};
use super::workspace::WorkspaceConfig;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
    SemanticRelease,
}

impl Source {
    fn label(self) -> &'static str {
        match self {
            Source::SemanticRelease => "semantic-release",
        }
    }
}

/// A `.releaserc` shape we can parse: JSON (or JSON5) content, whatever the
/// filename. YAML `.releaserc` and JS `release.config.js` are surfaced as
/// unsupported rather than mis-parsed.
const SEMANTIC_RELEASE_JSON_FILES: &[&str] = &[".releaserc", ".releaserc.json"];
const SEMANTIC_RELEASE_UNSUPPORTED_FILES: &[&str] = &[
    ".releaserc.yaml",
    ".releaserc.yml",
    ".releaserc.js",
    "release.config.js",
    "release.config.mjs",
    ".releaserc.cjs",
];

pub fn migrate(from: Option<Source>) -> Result<()> {
    ensure_no_existing_config()?;

    let source = match from {
        Some(s) => s,
        None => detect_source()?,
    };

    match source {
        Source::SemanticRelease => migrate_semantic_release(),
    }
}

fn ensure_no_existing_config() -> Result<()> {
    for handler in CONFIG_FORMATS {
        if Path::new(handler.filename()).exists() {
            return Err(anyhow::anyhow!("{} already exists", handler.filename()))
                .error_code(error_code::CONFIG_ALREADY_EXISTS);
        }
    }
    for filename in [TS_CONFIG_FILENAME, JS_CONFIG_FILENAME] {
        if Path::new(filename).exists() {
            return Err(anyhow::anyhow!("{filename} already exists"))
                .error_code(error_code::CONFIG_ALREADY_EXISTS);
        }
    }
    Ok(())
}

fn detect_source() -> Result<Source> {
    if find_semantic_release_json().is_some() {
        return Ok(Source::SemanticRelease);
    }
    if let Some(f) = SEMANTIC_RELEASE_UNSUPPORTED_FILES
        .iter()
        .find(|f| Path::new(f).exists())
    {
        return Err(anyhow::anyhow!(
            "found {f}, but ferrflow can only migrate JSON `.releaserc` for now. \
             Convert it to `.releaserc.json` (or inline the config as JSON) and rerun."
        ))
        .error_code(error_code::CONFIG_INVALID_JSON);
    }
    Err(anyhow::anyhow!(
        "no supported release-tool config found. Looked for {}. \
         Pass --from to force a source.",
        SEMANTIC_RELEASE_JSON_FILES.join(", ")
    ))
    .error_code(error_code::CONFIG_NOT_FOUND)
}

fn find_semantic_release_json() -> Option<PathBuf> {
    SEMANTIC_RELEASE_JSON_FILES
        .iter()
        .map(PathBuf::from)
        .find(|p| p.exists())
}

#[derive(Debug, Deserialize, Default)]
struct ReleaseRc {
    #[serde(default, rename = "tagFormat")]
    tag_format: Option<String>,
    #[serde(default)]
    branches: Option<Branches>,
    #[serde(default, rename = "repositoryUrl")]
    repository_url: Option<String>,
    #[serde(default)]
    plugins: Vec<PluginEntry>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum Branches {
    One(BranchSpec),
    Many(Vec<BranchSpec>),
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum BranchSpec {
    Name(String),
    Object {
        name: String,
        #[serde(default)]
        prerelease: Option<PrereleaseFlag>,
        #[serde(default)]
        channel: Option<String>,
    },
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum PrereleaseFlag {
    Bool(bool),
    Name(String),
}

/// A plugin is either `"name"` or `["name", { options }]`.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum PluginEntry {
    Name(String),
    WithOptions(String, serde_json::Value),
}

impl PluginEntry {
    fn name(&self) -> &str {
        match self {
            PluginEntry::Name(n) => n,
            PluginEntry::WithOptions(n, _) => n,
        }
    }

    fn options(&self) -> Option<&serde_json::Value> {
        match self {
            PluginEntry::Name(_) => None,
            PluginEntry::WithOptions(_, o) => Some(o),
        }
    }
}

/// Everything the converter wants to say to the user: what it mapped, what it
/// dropped, and what needs a human. Separated from the write so it can be
/// asserted in tests without touching the filesystem.
#[derive(Debug, Default, PartialEq)]
pub struct MigrationReport {
    pub mapped: Vec<String>,
    pub warnings: Vec<String>,
    pub ignored: Vec<String>,
}

pub fn build_config_from_releaserc(raw: &str) -> Result<(Config, MigrationReport)> {
    let rc: ReleaseRc = json5::from_str(raw)
        .map_err(|e| anyhow::anyhow!("could not parse .releaserc as JSON: {e}"))
        .error_code(error_code::CONFIG_INVALID_JSON)?;

    let mut report = MigrationReport::default();
    let mut workspace = WorkspaceConfig::default();

    if let Some(tag_format) = &rc.tag_format {
        workspace.tag_template = Some(convert_tag_format(tag_format));
        report
            .mapped
            .push(format!("tagFormat → tagTemplate ({})", tag_format));
    }

    if let Some(branches) = &rc.branches {
        let converted = convert_branches(branches, &mut report);
        if !converted.is_empty() {
            workspace.branches = Some(converted);
        }
    }

    if let Some(url) = &rc.repository_url {
        report.ignored.push(format!(
            "repositoryUrl ({url}) — ferrflow derives the remote from git"
        ));
    }

    let mut changelog: Option<String> = None;
    let mut hooks = HooksConfig::default();
    let mut hooks_touched = false;

    for plugin in &rc.plugins {
        apply_plugin(
            plugin,
            &mut workspace,
            &mut changelog,
            &mut hooks,
            &mut hooks_touched,
            &mut report,
        );
    }

    if hooks_touched {
        workspace.hooks = Some(hooks);
    }

    let package = PackageConfig {
        name: default_package_name(),
        path: ".".to_string(),
        versioned_files: vec![VersionedFile {
            path: "package.json".to_string(),
            format: FileFormat::Json,
            selector: None,
        }],
        changelog: Some(changelog.unwrap_or_else(|| "CHANGELOG.md".to_string())),
        shared_paths: Vec::new(),
        depends_on: vec![],
        versioning: None,
        tag_template: None,
        hooks: None,
        floating_tags: None,
        publishers: vec![],
        update_lockfiles: None,
    };

    report.warnings.push(
        "semantic-release is single-package; scaffolded one package writing to package.json. \
         Edit `package` if your layout differs, or add more for a monorepo."
            .to_string(),
    );

    Ok((
        Config {
            workspace,
            packages: vec![package],
        },
        report,
    ))
}

fn default_package_name() -> String {
    std::fs::read_to_string("package.json")
        .ok()
        .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
        .and_then(|v| v.get("name").and_then(|n| n.as_str()).map(str::to_string))
        .unwrap_or_else(|| "app".to_string())
}

/// `v${version}` → `v{{version}}`. semantic-release only defines the
/// `${version}` token; anything else is passed through and flagged.
fn convert_tag_format(tag_format: &str) -> String {
    tag_format.replace("${version}", "{{version}}")
}

fn convert_branches(branches: &Branches, report: &mut MigrationReport) -> Vec<BranchChannelConfig> {
    let specs: Vec<&BranchSpec> = match branches {
        Branches::One(b) => vec![b],
        Branches::Many(bs) => bs.iter().collect(),
    };

    let mut out = Vec::new();
    for spec in specs {
        match spec {
            BranchSpec::Name(name) => {
                // A bare non-release branch name in the list is a maintenance
                // or next branch; semantic-release treats `main`/`master` as
                // the stable line.
                let is_stable = name == "main" || name == "master";
                out.push(BranchChannelConfig {
                    name: name.clone(),
                    channel: ChannelValue::Stable(is_stable),
                    prerelease_identifier: PrereleaseIdentifier::default(),
                });
                report
                    .mapped
                    .push(format!("branch {name}{}", channel_desc(is_stable, None)));
            }
            BranchSpec::Object {
                name,
                prerelease,
                channel,
            } => {
                let (channel_value, desc) = match prerelease {
                    Some(PrereleaseFlag::Bool(true)) => {
                        let id = channel.clone().unwrap_or_else(|| name.clone());
                        (
                            ChannelValue::Named(id.clone()),
                            format!("prerelease `{id}`"),
                        )
                    }
                    Some(PrereleaseFlag::Name(id)) => (
                        ChannelValue::Named(id.clone()),
                        format!("prerelease `{id}`"),
                    ),
                    Some(PrereleaseFlag::Bool(false)) | None => {
                        let is_stable = name == "main" || name == "master";
                        (
                            ChannelValue::Stable(is_stable),
                            channel_desc(is_stable, channel.as_deref()),
                        )
                    }
                };
                out.push(BranchChannelConfig {
                    name: name.clone(),
                    channel: channel_value,
                    prerelease_identifier: PrereleaseIdentifier::default(),
                });
                report.mapped.push(format!("branch {name}{desc}"));
            }
        }
    }
    out
}

fn channel_desc(is_stable: bool, channel: Option<&str>) -> String {
    match (is_stable, channel) {
        (true, _) => "stable".to_string(),
        (false, Some(c)) => format!("channel `{c}`"),
        (false, None) => "stable (non-default branch)".to_string(),
    }
}

fn apply_plugin(
    plugin: &PluginEntry,
    workspace: &mut WorkspaceConfig,
    changelog: &mut Option<String>,
    hooks: &mut HooksConfig,
    hooks_touched: &mut bool,
    report: &mut MigrationReport,
) {
    match plugin.name() {
        "@semantic-release/commit-analyzer" => {
            if let Some(opts) = plugin.options()
                && (opts.get("releaseRules").is_some() || opts.get("preset").is_some())
            {
                report.warnings.push(
                    "commit-analyzer has custom releaseRules/preset — ferrflow's bump rules are \
                     fixed (feat→minor, fix/perf/refactor→patch, !/BREAKING→major) and can't honour \
                     them. Review whether the defaults match your intent."
                        .to_string(),
                );
            }
        }
        "@semantic-release/release-notes-generator" => {}
        "@semantic-release/changelog" => {
            let path = plugin
                .options()
                .and_then(|o| o.get("changelogFile"))
                .and_then(|v| v.as_str())
                .unwrap_or("CHANGELOG.md")
                .to_string();
            *changelog = Some(path.clone());
            report.mapped.push(format!("changelog plugin → {path}"));
        }
        "@semantic-release/github" | "@semantic-release/gitlab" => {
            workspace.forge = if plugin.name().contains("gitlab") {
                ForgeKind::Gitlab
            } else {
                ForgeKind::Github
            };
            report.mapped.push(format!("{} → forge", plugin.name()));
        }
        "@semantic-release/git" => {
            report.mapped.push(
                "git plugin → (implicit; ferrflow commits the release by default)".to_string(),
            );
        }
        "@semantic-release/exec" => {
            apply_exec_plugin(plugin, hooks, hooks_touched, report);
        }
        "@semantic-release/npm" => {
            report.warnings.push(
                "@semantic-release/npm → not mapped. ferrflow has publishers, but the semantics \
                 differ enough that an automatic map would mislead. Configure `publishers` by hand \
                 if you publish to npm."
                    .to_string(),
            );
        }
        other => {
            report.warnings.push(format!(
                "plugin {other} has no ferrflow equivalent — review manually"
            ));
        }
    }
}

fn apply_exec_plugin(
    plugin: &PluginEntry,
    hooks: &mut HooksConfig,
    hooks_touched: &mut bool,
    report: &mut MigrationReport,
) {
    let Some(opts) = plugin.options() else {
        return;
    };
    let get = |key: &str| opts.get(key).and_then(|v| v.as_str()).map(str::to_string);

    for (rc_key, ff_label, slot) in [
        ("verifyConditionsCmd", "preRelease", &mut hooks.pre_release),
        ("prepareCmd", "preBump", &mut hooks.pre_bump),
        ("publishCmd", "postPublish", &mut hooks.post_publish),
        ("successCmd", "onSuccess", &mut hooks.on_success),
        ("failCmd", "onError", &mut hooks.on_error),
    ] {
        if let Some(cmd) = get(rc_key) {
            *slot = Some(cmd);
            *hooks_touched = true;
            report
                .mapped
                .push(format!("exec {rc_key} → hooks.{ff_label}"));
        }
    }
}

fn migrate_semantic_release() -> Result<()> {
    let path = find_semantic_release_json().ok_or_else(|| {
        anyhow::anyhow!(
            "no JSON .releaserc found ({})",
            SEMANTIC_RELEASE_JSON_FILES.join(", ")
        )
    })?;
    let raw = std::fs::read_to_string(&path)
        .map_err(|e| anyhow::anyhow!("could not read {}: {e}", path.display()))?;

    let (config, report) = build_config_from_releaserc(&raw)?;

    let handler = format_handler(ConfigFileFormat::Json);
    let content = handler.serialize(&config)?;
    let filename = handler.filename();
    std::fs::write(filename, &content)?;

    print_report(Source::SemanticRelease, &path, filename, &report);
    Ok(())
}

fn print_report(source: Source, from: &Path, wrote: &str, report: &MigrationReport) {
    println!(
        "Migrated {} config from {}{wrote}\n",
        source.label(),
        from.display()
    );

    if !report.mapped.is_empty() {
        println!("Mapped:");
        for m in &report.mapped {
            println!("{m}");
        }
        println!();
    }
    if !report.ignored.is_empty() {
        println!("Ignored:");
        for i in &report.ignored {
            println!("{i}");
        }
        println!();
    }
    if !report.warnings.is_empty() {
        println!("Review manually:");
        for w in &report.warnings {
            println!("  ! {w}");
        }
        println!();
    }

    println!("Next: review {wrote}, then `ferrflow validate` and `ferrflow check`.");
}

#[cfg(test)]
mod tests;