ferrosite 0.1.0

A railway-oriented static site generator for personal homepages
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
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
use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;

use tempfile::TempDir;
use walkdir::WalkDir;

use crate::config::load_site_config_for_root;
use crate::error::{SiteError, SiteResult};

use super::{bundled_plugins_dir, site_plugins_dir, Plugin};

#[derive(Debug, Clone)]
pub struct PluginInstallOutcome {
    pub plugin_name: String,
    pub install_dir: PathBuf,
    pub already_installed: bool,
    pub source: String,
}

#[derive(Debug, Clone)]
pub struct PluginUninstallOutcome {
    pub plugin_name: String,
    pub removed_dir: Option<PathBuf>,
    pub disabled_only: bool,
    pub usage_files: Vec<PathBuf>,
}

#[derive(Debug, Clone)]
struct InstalledPlugin {
    plugin: Plugin,
    dir_name: String,
}

#[derive(Debug, Clone)]
struct PluginDirConfig {
    dir: PathBuf,
    configured_value: Option<String>,
}

pub fn install_plugin(site_root: &Path, source: &str) -> SiteResult<PluginInstallOutcome> {
    let plugin_dir_config = resolve_plugin_dir(site_root)?;

    let staged = if is_git_source(source) {
        std::fs::create_dir_all(&plugin_dir_config.dir)?;
        let temp = TempDir::new()?;
        let checkout_dir = temp.path().join("plugin");
        clone_plugin_repo(source, &checkout_dir)?;
        StagedPlugin {
            plugin: Plugin::from_dir(&checkout_dir)?,
            source: format!("git clone {}", source),
            _temp_dir: Some(temp),
            source_dir: checkout_dir,
        }
    } else {
        let source_dir = find_bundled_plugin_dir(source)?;
        StagedPlugin {
            plugin: Plugin::from_dir(&source_dir)?,
            source: format!("bundled plugin {}", source),
            _temp_dir: None,
            source_dir,
        }
    };

    if let Some(existing) =
        find_installed_plugin(&plugin_dir_config.dir, &staged.plugin.manifest.name)?
    {
        persist_plugin_enabled(
            site_root,
            &existing.plugin.manifest.name,
            plugin_dir_config.configured_value.as_deref(),
        )?;

        return Ok(PluginInstallOutcome {
            plugin_name: existing.plugin.manifest.name,
            install_dir: existing.plugin.dir,
            already_installed: true,
            source: staged.source,
        });
    }

    if !is_git_source(source) {
        persist_plugin_enabled(
            site_root,
            &staged.plugin.manifest.name,
            plugin_dir_config.configured_value.as_deref(),
        )?;

        return Ok(PluginInstallOutcome {
            plugin_name: staged.plugin.manifest.name,
            install_dir: staged.source_dir,
            already_installed: false,
            source: staged.source,
        });
    }

    let install_dir = plugin_dir_config.dir.join(&staged.plugin.manifest.name);
    copy_dir_recursive(&staged.source_dir, &install_dir)?;
    persist_plugin_enabled(
        site_root,
        &staged.plugin.manifest.name,
        plugin_dir_config.configured_value.as_deref(),
    )?;

    Ok(PluginInstallOutcome {
        plugin_name: staged.plugin.manifest.name,
        install_dir,
        already_installed: false,
        source: staged.source,
    })
}

pub fn uninstall_plugin(site_root: &Path, plugin_ref: &str) -> SiteResult<PluginUninstallOutcome> {
    let plugin_dir_config = resolve_plugin_dir(site_root)?;
    if let Some(installed) = find_installed_plugin(&plugin_dir_config.dir, plugin_ref)? {
        let usage_files = find_plugin_usage_files(
            site_root,
            &installed.plugin,
            &installed.dir_name,
            &plugin_dir_config.dir,
        )?;

        persist_plugin_disabled(
            site_root,
            &installed.plugin.manifest.name,
            &installed.dir_name,
        )?;
        std::fs::remove_dir_all(&installed.plugin.dir)?;

        return Ok(PluginUninstallOutcome {
            plugin_name: installed.plugin.manifest.name,
            removed_dir: Some(installed.plugin.dir),
            disabled_only: false,
            usage_files,
        });
    }

    let bundled = find_bundled_plugin(plugin_ref)?.ok_or_else(|| SiteError::Plugin {
        plugin: plugin_ref.to_string(),
        message: format!(
            "Plugin '{}' is not available in '{}' or bundled ferrosite plugins.",
            plugin_ref,
            plugin_dir_config.dir.display()
        ),
    })?;

    let usage_files = find_plugin_usage_files(
        site_root,
        &bundled.plugin,
        &bundled.dir_name,
        &plugin_dir_config.dir,
    )?;

    persist_plugin_disabled(
        site_root,
        &bundled.plugin.manifest.name,
        &bundled.dir_name,
    )?;

    Ok(PluginUninstallOutcome {
        plugin_name: bundled.plugin.manifest.name,
        removed_dir: None,
        disabled_only: true,
        usage_files,
    })
}

struct StagedPlugin {
    plugin: Plugin,
    source: String,
    _temp_dir: Option<TempDir>,
    source_dir: PathBuf,
}

fn resolve_plugin_dir(site_root: &Path) -> SiteResult<PluginDirConfig> {
    let config = load_site_config_for_root(site_root)?;
    Ok(PluginDirConfig {
        dir: site_plugins_dir(site_root, config.plugins.plugins_dir.as_deref()),
        configured_value: config.plugins.plugins_dir,
    })
}

fn clone_plugin_repo(source: &str, destination: &Path) -> SiteResult<()> {
    let status = Command::new("git")
        .arg("clone")
        .arg(source)
        .arg(destination)
        .status()
        .map_err(|err| match err.kind() {
            std::io::ErrorKind::NotFound => SiteError::Build(
                "git is required to install plugins from repositories.".to_string(),
            ),
            _ => SiteError::from(err),
        })?;

    if !status.success() {
        return Err(SiteError::Build(format!(
            "'git clone {}' failed with status {}",
            source, status
        )));
    }

    Ok(())
}

fn is_git_source(source: &str) -> bool {
    source.starts_with("https://")
        || source.starts_with("ssh://")
        || source.starts_with("git@")
        || source.starts_with("file://")
        || source.ends_with(".git")
        || Path::new(source).join(".git").exists()
}

fn find_bundled_plugin_dir(name: &str) -> SiteResult<PathBuf> {
    let plugins_dir = bundled_plugins_dir();
    let mut available = Vec::new();

    if !plugins_dir.exists() {
        return Err(SiteError::Plugin {
            plugin: name.to_string(),
            message: "No bundled plugins are available.".to_string(),
        });
    }

    for entry in std::fs::read_dir(&plugins_dir)? {
        let entry = entry?;
        if !entry.path().is_dir() {
            continue;
        }

        let plugin_name = entry.file_name().to_string_lossy().into_owned();
        available.push(plugin_name.clone());

        if plugin_name == name && entry.path().join("manifest.toml").exists() {
            return Ok(entry.path());
        }
    }

    available.sort();
    available.dedup();

    Err(SiteError::Plugin {
        plugin: name.to_string(),
        message: if available.is_empty() {
            "No bundled plugins are available.".to_string()
        } else {
            format!(
                "No bundled plugin named '{}'. Available bundled plugins: {}",
                name,
                available.join(", ")
            )
        },
    })
}

fn find_bundled_plugin(plugin_ref: &str) -> SiteResult<Option<InstalledPlugin>> {
    find_installed_plugin(&bundled_plugins_dir(), plugin_ref)
}

fn find_installed_plugin(
    plugins_dir: &Path,
    plugin_ref: &str,
) -> SiteResult<Option<InstalledPlugin>> {
    if !plugins_dir.exists() {
        return Ok(None);
    }

    for entry in std::fs::read_dir(plugins_dir)? {
        let entry = entry?;
        if !entry.path().is_dir() {
            continue;
        }

        let dir_name = entry.file_name().to_string_lossy().into_owned();
        let plugin = Plugin::from_dir(&entry.path())?;

        if plugin.manifest.name == plugin_ref || dir_name == plugin_ref {
            return Ok(Some(InstalledPlugin { plugin, dir_name }));
        }
    }

    Ok(None)
}

fn find_plugin_usage_files(
    site_root: &Path,
    plugin: &Plugin,
    dir_name: &str,
    plugins_dir: &Path,
) -> SiteResult<Vec<PathBuf>> {
    let patterns = plugin_usage_patterns(plugin, dir_name);
    if patterns.is_empty() {
        return Ok(Vec::new());
    }

    let mut usage_files = BTreeSet::new();

    for entry in WalkDir::new(site_root)
        .into_iter()
        .filter_entry(|entry| !should_skip_path(entry.path(), plugins_dir))
        .filter_map(|entry| entry.ok())
        .filter(|entry| entry.file_type().is_file())
    {
        let path = entry.path();
        if path.file_name() == Some(OsStr::new("ferrosite.toml")) {
            continue;
        }

        let Ok(contents) = std::fs::read_to_string(path) else {
            continue;
        };

        if patterns.iter().any(|pattern| contents.contains(pattern)) {
            usage_files.insert(path.strip_prefix(site_root)?.to_path_buf());
        }
    }

    Ok(usage_files.into_iter().collect())
}

fn plugin_usage_patterns(plugin: &Plugin, dir_name: &str) -> Vec<String> {
    let mut patterns = BTreeSet::new();

    patterns.insert(plugin.manifest.name.clone());
    patterns.insert(dir_name.to_string());
    patterns.insert(plugin.manifest.worker_route.clone());

    for slot in &plugin.manifest.slots {
        patterns.insert(slot.clone());
    }

    for tag in infer_component_tags(&plugin.component_source) {
        patterns.insert(tag);
    }

    patterns
        .into_iter()
        .filter(|pattern| !pattern.trim().is_empty())
        .collect()
}

fn infer_component_tags(source: &str) -> Vec<String> {
    let mut tags = BTreeSet::new();

    extract_js_call_strings(source, r#"pfusch(""#, '"', &mut tags);
    extract_js_call_strings(source, "pfusch('", '\'', &mut tags);
    extract_js_call_strings(source, r#"customElements.define(""#, '"', &mut tags);
    extract_js_call_strings(source, "customElements.define('", '\'', &mut tags);

    tags.into_iter().collect()
}

fn extract_js_call_strings(
    source: &str,
    marker: &str,
    closing_quote: char,
    tags: &mut BTreeSet<String>,
) {
    let mut rest = source;

    while let Some(index) = rest.find(marker) {
        let after_marker = &rest[index + marker.len()..];
        if let Some(end_index) = after_marker.find(closing_quote) {
            let candidate = after_marker[..end_index].trim();
            if !candidate.is_empty() {
                tags.insert(candidate.to_string());
            }
            rest = &after_marker[end_index + 1..];
        } else {
            break;
        }
    }
}

fn should_skip_path(path: &Path, plugins_dir: &Path) -> bool {
    path == plugins_dir
        || path.starts_with(plugins_dir)
        || path.file_name().is_some_and(|name| {
            matches!(
                name.to_str(),
                Some(".git" | "target" | "dist" | "node_modules")
            )
        })
}

fn persist_plugin_enabled(
    site_root: &Path,
    plugin_name: &str,
    configured_plugins_dir: Option<&str>,
) -> SiteResult<()> {
    update_plugin_config(site_root, |plugins| {
        if !plugins.contains_key("plugins_dir") {
            plugins.insert(
                "plugins_dir".into(),
                toml::Value::String(configured_plugins_dir.unwrap_or("plugins").to_string()),
            );
        }

        let enabled = plugins
            .entry("enabled")
            .or_insert_with(|| toml::Value::Array(Vec::new()));

        match enabled {
            toml::Value::Array(items) => {
                if !items.iter().any(|item| item.as_str() == Some(plugin_name)) {
                    items.push(toml::Value::String(plugin_name.to_string()));
                }
                Ok(())
            }
            _ => Err(SiteError::Config(
                "Expected 'plugins.enabled' in ferrosite.toml to be an array.".to_string(),
            )),
        }
    })
}

fn persist_plugin_disabled(site_root: &Path, plugin_name: &str, dir_name: &str) -> SiteResult<()> {
    update_plugin_config(site_root, |plugins| {
        if let Some(enabled) = plugins.get_mut("enabled") {
            match enabled {
                toml::Value::Array(items) => {
                    items.retain(|item| {
                        item.as_str()
                            .map(|value| value != plugin_name && value != dir_name)
                            .unwrap_or(true)
                    });
                    Ok(())
                }
                _ => Err(SiteError::Config(
                    "Expected 'plugins.enabled' in ferrosite.toml to be an array.".to_string(),
                )),
            }
        } else {
            Ok(())
        }
    })
}

fn update_plugin_config(
    site_root: &Path,
    mut mutate: impl FnMut(&mut toml::map::Map<String, toml::Value>) -> SiteResult<()>,
) -> SiteResult<()> {
    let config_path = site_root.join("ferrosite.toml");
    if !config_path.exists() {
        return Err(SiteError::Config(format!(
            "No ferrosite.toml found in '{}'. Run 'ferrosite new <name>' first or pass '--root <site-dir>'.",
            site_root.display()
        )));
    }

    let raw = std::fs::read_to_string(&config_path)?;
    let mut value: toml::Value = toml::from_str(&raw)?;
    let root_table = value.as_table_mut().ok_or_else(|| {
        SiteError::Config(format!(
            "'{}' must contain a top-level TOML table.",
            config_path.display()
        ))
    })?;

    let plugins = ensure_toml_table(root_table, "plugins")?;
    mutate(plugins)?;

    let rendered = toml::to_string_pretty(&value)?;
    std::fs::write(&config_path, rendered)?;
    Ok(())
}

fn ensure_toml_table<'a>(
    table: &'a mut toml::map::Map<String, toml::Value>,
    key: &str,
) -> SiteResult<&'a mut toml::map::Map<String, toml::Value>> {
    let value = table
        .entry(key.to_string())
        .or_insert_with(|| toml::Value::Table(toml::map::Map::new()));

    match value {
        toml::Value::Table(table) => Ok(table),
        _ => Err(SiteError::Config(format!(
            "Expected '{}' in ferrosite.toml to be a table.",
            key
        ))),
    }
}

fn copy_dir_recursive(src: &Path, dst: &Path) -> SiteResult<()> {
    std::fs::create_dir_all(dst)?;

    for entry in WalkDir::new(src).into_iter().filter_map(|entry| entry.ok()) {
        let path = entry.path();
        let relative = path.strip_prefix(src)?;
        let destination = dst.join(relative);

        if path.is_dir() {
            std::fs::create_dir_all(&destination)?;
        } else {
            std::fs::copy(path, &destination)?;
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn minimal_site_config() -> &'static str {
        r#"[site]
title = "Example"
description = "Example site"
base_url = "https://example.com"

[site.author]
name = "Example Author"

[build]
template = "developer"

[deploy]
provider = "cloudflare"

[deploy.cloudflare]
project_name = "example"
account_id = "abc123"
"#
    }

    #[test]
    fn install_bundled_plugin_copies_files_and_enables_it() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        std::fs::write(root.join("ferrosite.toml"), minimal_site_config()).expect("config");

        let result = install_plugin(root, "contact-form").expect("plugin install");

        assert_eq!(result.plugin_name, "contact-form");
        assert!(!result.already_installed);
        assert!(!root.join("plugins/contact-form/manifest.toml").exists());
        assert!(result.install_dir.ends_with("plugins/contact-form"));

        let config = std::fs::read_to_string(root.join("ferrosite.toml")).expect("updated config");
        let value: toml::Value = toml::from_str(&config).expect("valid toml");
        assert_eq!(value["plugins"]["plugins_dir"].as_str(), Some("plugins"));
        assert_eq!(
            value["plugins"]["enabled"][0].as_str(),
            Some("contact-form")
        );
    }

    #[test]
    fn uninstall_plugin_removes_dir_and_reports_usage_files() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        std::fs::write(root.join("ferrosite.toml"), minimal_site_config()).expect("config");
        std::fs::create_dir_all(root.join("content")).expect("content dir");
        std::fs::write(
            root.join("content/about.md"),
            "slot = 'contact-form'\n<ferrosite-contact-form></ferrosite-contact-form>",
        )
        .expect("usage file");

        install_plugin(root, "contact-form").expect("plugin install");
        let result = uninstall_plugin(root, "contact-form").expect("plugin uninstall");

        assert_eq!(result.plugin_name, "contact-form");
        assert!(result.disabled_only);
        assert!(result.removed_dir.is_none());
        assert_eq!(result.usage_files, vec![PathBuf::from("content/about.md")]);

        let config = std::fs::read_to_string(root.join("ferrosite.toml")).expect("updated config");
        let value: toml::Value = toml::from_str(&config).expect("valid toml");
        assert!(value["plugins"]["enabled"]
            .as_array()
            .is_some_and(|items| items.is_empty()));
    }

    #[test]
    fn install_plugin_from_git_uses_manifest_name_for_destination() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        std::fs::write(root.join("ferrosite.toml"), minimal_site_config()).expect("config");

        let repo_root = temp.path().join("git-plugin-repo");
        std::fs::create_dir_all(&repo_root).expect("repo dir");
        std::fs::write(
            repo_root.join("manifest.toml"),
            r#"[plugin]
name = "custom-plugin"
version = "1.0.0"
author = "Example"
slots = ["contact-form"]
component_file = "component.js"
worker_file = "worker.js"
worker_route = "/api/custom"
"#,
        )
        .expect("manifest");
        std::fs::write(
            repo_root.join("component.js"),
            "pfusch('custom-widget', {});",
        )
        .expect("component");
        std::fs::write(repo_root.join("worker.js"), "export default {};").expect("worker");

        let init_status = Command::new("git")
            .arg("init")
            .arg("-b")
            .arg("main")
            .current_dir(&repo_root)
            .status()
            .expect("git init");
        assert!(init_status.success());

        let email_status = Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(&repo_root)
            .status()
            .expect("git config email");
        assert!(email_status.success());

        let name_status = Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(&repo_root)
            .status()
            .expect("git config name");
        assert!(name_status.success());

        let add_status = Command::new("git")
            .args(["add", "."])
            .current_dir(&repo_root)
            .status()
            .expect("git add");
        assert!(add_status.success());

        let commit_status = Command::new("git")
            .args(["commit", "-m", "Initial plugin"])
            .current_dir(&repo_root)
            .status()
            .expect("git commit");
        assert!(commit_status.success());

        let result =
            install_plugin(root, repo_root.to_str().expect("repo path")).expect("git install");

        assert_eq!(result.plugin_name, "custom-plugin");
        assert!(root.join("plugins/custom-plugin/manifest.toml").exists());
        assert!(!root.join("plugins/git-plugin-repo").exists());

        let config = std::fs::read_to_string(root.join("ferrosite.toml")).expect("updated config");
        let value: toml::Value = toml::from_str(&config).expect("valid toml");
        assert_eq!(
            value["plugins"]["enabled"][0].as_str(),
            Some("custom-plugin")
        );
    }
}