Skip to main content

abbaye/
config.rs

1use std::path::PathBuf;
2
3use figment::{
4    Figment,
5    providers::{Format, Toml},
6};
7use miette::{IntoDiagnostic, Result};
8use serde::{Deserialize, Serialize};
9
10use schemars::JsonSchema;
11
12use crate::{
13    builders::BuilderEntry, changelog::ChangelogConfig, version_extractors::AnyVersionExtractor,
14};
15
16/// General website metadata.
17#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]
18pub struct SiteConfig {
19    /// Display name of the project, used in page titles and headings.
20    pub name: String,
21    /// Where to output generated files. Defaults to `public`.
22    #[serde(default = "abbaye_output_dir")]
23    pub output_dir: PathBuf,
24    /// Path to the README file rendered on each version page.
25    /// Defaults to `README.md` in the current working directory.
26    pub readme: Option<PathBuf>,
27    /// Canonical base URL of the published site (e.g. `"https://example.com"`).
28    /// When set, the generated `releases.atom` feed will include absolute links
29    /// and proper entry IDs.  Trailing slashes are stripped automatically.
30    pub base_url: Option<String>,
31    /// URL of the project's repository (e.g. `"https://git.sr.ht/~ololduck/abbaye"`).
32    /// When set, we will show the repository link in the generated website.
33    pub repo_url: Option<String>,
34    /// An optional language code for the website (e.g. `"en"` or `"fr"`).
35    /// When set, the generated HTML will include a `lang` attribute on the `<html>` tag. Defaults to `"en"`.
36    pub lang: Option<String>,
37    /// If you have a Fediverse account, you can set this to your username to enable Fediverse integration. (don't forget the starting '@' !)
38    pub fediverse_creator: Option<String>,
39    /// OpenGraph configuration for the website.
40    pub opengraph: Option<OpenGraphConfig>,
41}
42
43/// OpenGraph configuration for the website.
44#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
45pub struct OpenGraphConfig {
46    /// OpenGraph type of the website. It will be used as the `og:type` meta tag.
47    /// Note: by default, the value "website" will be enforced for the version listing page and "article" for the release notes page.
48    pub r#type: Option<String>,
49    /// URL of the website's image.
50    pub image: String,
51    /// Alt text for the website's image.
52    pub image_alt_text: Option<String>,
53    /// URL of the website. If not set, the `base_url` will be used instead. If `base_url` is not set either, the world explodes. Think of the kittens.
54    pub url: Option<String>,
55    /// Author of the website. Used on release notes pages to indicate the author.
56    pub author: Option<String>,
57}
58
59/// Configuration for the git repository web UI.
60#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
61pub struct GitUiConfig {
62    #[serde(default = "default_branch")]
63    pub default_branch: String,
64    /// Maximum number of commits to show in the log page. Defaults to 200.
65    #[serde(default = "default_max_commits")]
66    pub max_commits: usize,
67    /// Path to the git repository to read. Defaults to `.` (the current directory).
68    pub repo_path: Option<PathBuf>,
69    /// Explicit clone URL displayed in the UI (e.g. `"https://example.com/repository.git"`).
70    /// When absent, derived from `site.base_url` by appending `/repository.git`.
71    pub clone_url: Option<String>,
72    /// Glob patterns matched against a ref's short name (e.g. `"main"`,
73    /// `"feature/*"`, `"v1.0.0"`), using [`globset`](https://docs.rs/globset)
74    /// syntax. Any branch or tag matching one of these patterns is left out
75    /// of the generated UI. Defaults to empty (nothing excluded).
76    #[serde(default)]
77    pub exclude: Vec<String>,
78    /// Glob patterns (same syntax as `exclude`). When non-empty, acts as an
79    /// allowlist: only refs matching at least one `include` pattern are
80    /// considered at all (and are then still subject to `exclude`). When
81    /// empty (the default), every ref is a candidate.
82    #[serde(default)]
83    pub include: Vec<String>,
84}
85
86fn default_branch() -> String {
87    "main".to_string()
88}
89
90fn default_max_commits() -> usize {
91    200
92}
93
94/// A full configuration for the Abbaye site generator.
95///
96/// Here's a sample configuration that works well as a starting point for rust projects:
97///
98/// ```toml
99/// [site]
100/// name = "Abbaye"
101///
102/// [version_extractor]
103/// type = "cargo" # extract version from Cargo.toml
104///
105/// [changelog]
106/// # let's use the default changelog extractor
107///
108/// [[builders]]
109/// type = "cargo" # calls `cargo build --release` for each target
110/// targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]
111/// [[builders]]
112/// type = "cargo_doc" # generates documentation using `cargo doc`
113/// no_deps = true
114///
115/// [[builders]]
116/// type = "archive" # creates a compressed tarball of the source code
117/// ```
118///
119/// You can learn more about each builder type in the [builders module documentation](crate::builders).
120///
121#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
122pub struct AbbayeConfig {
123    /// Metadata about the site.
124    pub site: SiteConfig,
125    /// which version extractor to use to extract the version(s) of the project
126    pub version_extractor: AnyVersionExtractor,
127    /// Configuration for the changelog extractor.
128    pub changelog: ChangelogConfig,
129    /// Builders to run during the build process.
130    pub builders: Vec<BuilderEntry>,
131    /// Optional git repository web UI. When present, abbaye generates browsable HTML
132    /// pages at `<output>/repository/` and a clonable bare repository at
133    /// `<output>/repository.git/`.
134    #[serde(default)]
135    pub git_ui: Option<GitUiConfig>,
136}
137
138fn abbaye_output_dir() -> PathBuf {
139    PathBuf::from("public")
140}
141
142/// Load the Abbaye2 configuration from the current working directory.
143///
144/// Looks for `.abbaye.toml` first, then `abbaye.toml`; when both are present
145/// `abbaye.toml` takes precedence (last merge wins).
146pub fn load_config() -> Result<AbbayeConfig> {
147    let cwd = std::env::current_dir().into_diagnostic()?;
148    Figment::new()
149        .merge(Toml::file(cwd.join(".abbaye.toml")))
150        .merge(Toml::file(cwd.join("abbaye.toml")))
151        .merge(Toml::file(cwd.join(".abbaye").join("abbaye.toml")))
152        .extract()
153        .into_diagnostic()
154}