Abbaye

at bd5ed90

use std::path::PathBuf;

use figment::{
    Figment,
    providers::{Format, Toml},
};
use miette::{IntoDiagnostic, Result};
use serde::{Deserialize, Serialize};

use crate::{
    builders::AnyBuilder, changelog::ChangelogConfig, version_extractors::AnyVersionExtractor,
};

/// General website metadata.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SiteConfig {
    /// Display name of the project, used in page titles and headings.
    pub name: String,
    /// Path to the README file rendered on each version page.
    /// Defaults to `README.md` in the current working directory.
    pub readme: Option<PathBuf>,
    /// Canonical base URL of the published site (e.g. `"https://example.com"`).
    /// When set, the generated `releases.atom` feed will include absolute links
    /// and proper entry IDs.  Trailing slashes are stripped automatically.
    pub base_url: Option<String>,
    /// URL of the project's repository (e.g. `"https://git.sr.ht/~ololduck/abbaye"`).
    /// When set, we will show the repository link in the generated website.
    pub repo_url: Option<String>,
    /// An optional language code for the website (e.g. `"en"` or `"fr"`).
    /// When set, the generated HTML will include a `lang` attribute on the `<html>` tag. Defaults to `"en"`.
    pub lang: Option<String>,
}

/// A full configuration for the Abbaye site generator.
///
/// Here's a sample configuration that works well as a starting point for rust projects:
///
/// ```toml
/// [site]
/// name = "Abbaye"
///
/// [version_extractor]
/// type = "cargo" # extract version from Cargo.toml
///
/// [changelog]
/// # let's use the default changelog extractor
///
/// [[builders]]
/// type = "cargo" # calls `cargo build --release` for each target
/// targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]
/// [[builders]]
/// type = "cargo_doc" # generates documentation using `cargo doc`
/// no_deps = true
///
/// [[builders]]
/// type = "archive" # creates a compressed tarball of the source code
/// ```
///
/// You can learn more about each builder type in the [builders module documentation](crate::builders).
///
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AbbayeConfig {
    pub site: SiteConfig,
    pub version_extractor: AnyVersionExtractor,
    pub changelog: ChangelogConfig,
    pub builders: Vec<AnyBuilder>,
    #[serde(default = "abbaye_output_dir")]
    pub output_dir: Option<PathBuf>,
}

fn abbaye_output_dir() -> Option<PathBuf> {
    Some(PathBuf::from("public"))
}

/// Load the Abbaye2 configuration from the current working directory.
///
/// Looks for `.abbaye.toml` first, then `abbaye.toml`; when both are present
/// `abbaye.toml` takes precedence (last merge wins).
pub fn load_config() -> Result<AbbayeConfig> {
    let cwd = std::env::current_dir().into_diagnostic()?;
    Figment::new()
        .merge(Toml::file(cwd.join(".abbaye.toml")))
        .merge(Toml::file(cwd.join("abbaye.toml")))
        .merge(Toml::file(cwd.join(".abbaye").join("abbaye.toml")))
        .extract()
        .into_diagnostic()
}