Abbaye

at 44bfea6

//! # Abbaye
//!
//! ![logo](logo-wordmark.svg)
//!
//! Abbaye is a Static Site Generator (SSG) for your software. As GitHub,
//! Gitea, Forgejo and consorts offer, Abbaye can be used to generate a
//! website with your software's presentation, documentation, and distribution, per version.
//!
//! Here's an example file structure:
//!
//! ```text
//! .
//! ├── index.html # the main page of the website, enabling choosing a version, defaults to "latest" (contains a list of available versions and a iframe to the selected version?)
//! ├── releases.feed # the RSS feed of the releases
//! ├── latest -> v2.0.0 # symlink to the latest version (biggest version number)
//! ├── v1.0.0/ # the directory containing the version 1.0.0 of the software
//! │   ├── index.html # the main page of the version 1.0.0, from the README.md file.
//! │   │   # Contains a sidebar with links to the documentation and distribution.
//! │   │   # After the readme content, A changelog is displayed.
//! │   ├── docs/ # the directory containing the documentation of the version 1.0.0
//! │   │   ├── index.html # the main page of the documentation of the version 1.0.0
//! │   │   └── …
//! │   ├── docs.tar.gz # the tarball containing the documentation of the version 1.0.0
//! │   └── dist/ # the directory containing the distribution of the version 1.0.0
//! │       ├── source.tgz # the source code of the version 1.0.0
//! │       ├── mybin-v1.0.0-x86_64-unknown-linux-gnu
//! │       └── mybin-v1.0.0-x86_64-unknown-linux-musl
//! └── v2.0.0/ # the directory containing the version 2.0.0 of the software
//!     ├── index.html # the main page of the version 2.0.0
//!     ├── …
//!     └── …
//! ```
//!
//! ## Why ?
//!
//! This piece of software is for people that can't or won't use a full-featured forge such as GitHub, GitLab, ForgeJo & others.
//! These forges provide "release pages" that allow you to upload and distribute your software, as well as get a changelog.
//!
//! Abbaye is made to be a simple, lightweight alternative to these forges, for the release/documentation parts.
//!
//! ### Why "Abbaye" ?
//!
//! [Abbaye](https://en.wikipedia.org/wiki/Abbaye) is a French word for Abbey. An Abbey is a type of monastery, on the big-ish side, but still a small, quiet place.
//!
//! Anyway, that's where you store and display your relics (your software releases).
//!
//! ## Installation
//!
//! ### Pre-built binaries
//!
//! You can grab a pre-built binary from the [releases page](http://vit.am/~ololduck/abbaye/latest).
//!
//! The `-musl` binaries are statically linked and should run everywhere, while the `-gnu` binaries are dynamically linked and require a compatible system library(which is probably available if you're not using an exotic distribution).
//!
//! ### From source
//!
//! To build from source, you need to have Rust installed. You can install Rust using [rustup](https://rustup.rs/).
//! If you don't have rust installed, this project won't be of much use to you, as it currently only implements rust builders :stuck_out_tongue:
//!
//! You can clone the [repository](https://git.sr.ht/~ololduck/abbaye) and build the project using `cargo build --release`.
//!
//! ## Usage
//!
//! Run `abbaye init` in your project's directory to create a `abbaye.toml` configuration file. You can then customize the configuration to your liking.
//! Here's an example configuration file to get you started:
//!
//! ```toml
//! [site]
//! name = "Abbaye"
//! base_url = "http://vit.am/~ololduck/abbaye/" # required for Atom feed generation (canonical URLs are used for feed items)
//!
//! [version_extractor]
//! type = "git" # extract version from git tags
//! tag_prefix = "v"
//!
//! [changelog] # use the default changelog parser (Keepachangelog format in CHANGELOG.md)
//!
//! [[builders]]  # builds the project using cargo build --release
//! type = "cargo"
//! targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]
//!
//! [[builders]]  # generates documentation using cargo doc
//! type = "cargo_doc"
//! no_deps = true  # Don't include dependencies in the documentation
//!
//! [[builders]]
//! type = "archive"  # creates a compressed tarball of the source code (can be of anything, really)
//! ```
//!
//! Then run `abbaye build` to build the site. The site will be generated in the `public/` directory by default.
//! Now you can copy the contents of `public/` to your web server to deploy the site. For instance, with rsync:
//! `rsync --progress -avz --links --perms --update public/ ololduck@vit.am:public_html/abbaye/`
//!
//! ### ✨ Customization ✨
//!
//! You can dump the default theme/templates to your local filesystem with `abbaye dump-theme`.
//!
//! This will create a `.abbaye/theme/` directory in your current directory with the default templates, which you can then ✨customize✨.
//!
//! ## Future plans
//!
//! - [x] Add support for theming
//! - [ ] Add support for more site variables, such as the site title, description, and author, or even a custom footer and stuff.
//! - [ ] Add support for a `self-update`-like command to update the abbaye binary to the latest version. The mechanisms put in place for this goal should be usable to any user of `abbaye`.
//!
//! ## Contributing
//!
//! Contributions are welcome! As i am mainly a rust developer, i am open to any contributions that improve the project, especially to support more artifacts builders/types.
//!
//! Just clone the repository and {send me an email,contact me on {IRC (ololduck@irc.libera.chat),the Fediverse (@ololduck@fosstodon.org)}} with {a link to your fork,a git patch,compliments and adoration}.

use std::path::PathBuf;

use clap::Parser;
use human_panic::setup_panic;
use miette::{IntoDiagnostic, Result};
use tokio::fs::create_dir_all;

use crate::{
    builders::{AnyBuilder, archive::ArchiveBuilderConfig},
    changelog::ChangelogConfig,
    config::{AbbayeConfig, SiteConfig},
    version_extractors::{AnyVersionExtractor, git::GitVersionConfig},
};

/// All builders for the site (ex: cargo build, cargo doc, etc.).
pub mod builders;
/// Parses the changelog file and generates a changelog page for the site.
pub mod changelog;
mod cli;
/// Handles the `abbaye.toml` configuration file.
pub mod config;
/// Generates the site from the configuration and builds it.
pub mod site;
/// Extracts current version information from different sources (ex: git tags, cargo metadata, etc.).
pub mod version_extractors;

#[tokio::main]
async fn main() -> Result<()> {
    setup_panic!();
    let cli_args = cli::CliArgs::parse();

    tracing_subscriber::fmt()
        .with_timer(tracing_subscriber::fmt::time::SystemTime)
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

    match cli_args.command {
        cli::CliCommand::Init { path } => {
            let base_path = if let Some(path) = path {
                path
            } else {
                std::env::current_dir().into_diagnostic()?
            };
            if base_path.join("abbaye.toml").exists() {
                return Err(miette::miette!(
                    "abbaye.toml already exists in this directory"
                ));
            }
            let abbaye_config = AbbayeConfig {
                site: SiteConfig {
                    name: "MyProject Release Page".to_string(),
                    readme: None,
                    base_url: None,
                    repo_url: None,
                    lang: None,
                },
                version_extractor: AnyVersionExtractor::Git(GitVersionConfig {
                    tag_prefix: Some("v".to_string()),
                    dirty_suffix: "-dirty".to_string(),
                }),
                builders: vec![AnyBuilder::Archive(ArchiveBuilderConfig {
                    source_dir: None,
                    output: None,
                    prefix: None,
                    ignore_patterns: vec![".git/".to_string(), "*.tar.gz".to_string()],
                })],
                changelog: ChangelogConfig {
                    ..Default::default()
                },
                output_dir: None,
            };
            let config_path = base_path.join("abbaye.toml");
            let toml = toml::to_string_pretty(&abbaye_config).into_diagnostic()?;
            tokio::fs::write(&config_path, toml)
                .await
                .into_diagnostic()?;
        }
        cli::CliCommand::Build => {
            let config = config::load_config()?;
            site::build_site(config).await?;
        }
        cli::CliCommand::DumpTheme => {
            let theme_path = PathBuf::from(".abbaye").join("theme");
            create_dir_all(&theme_path).await.into_diagnostic()?;
            tokio::fs::write(
                theme_path.join("root_index.html.j2"),
                site::TEMPLATE_ROOT_INDEX,
            )
            .await
            .into_diagnostic()?;
            tokio::fs::write(
                theme_path.join("version_index.html.j2"),
                site::TEMPLATE_VERSION_INDEX,
            )
            .await
            .into_diagnostic()?;
        }
    }
    Ok(())
}