Skip to main content

abbaye/
main.rs

1//! # Abbaye
2//!
3//! ![logo](logo-wordmark.svg)
4//!
5//! Abbaye is a Static Site Generator (SSG) for your software. As GitHub,
6//! Gitea, Forgejo and consorts offer, Abbaye can be used to generate a
7//! website with your software's presentation, documentation, and distribution, per version.
8//!
9//! Here's an example file structure:
10//!
11//! ```text
12//! .
13//! ├── 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?)
14//! ├── releases.feed # the RSS feed of the releases
15//! ├── latest -> v2.0.0 # symlink to the latest version (biggest version number)
16//! ├── v1.0.0/ # the directory containing the version 1.0.0 of the software
17//! │   ├── index.html # the main page of the version 1.0.0, from the README.md file.
18//! │   │   # Contains a sidebar with links to the documentation and distribution.
19//! │   │   # After the readme content, A changelog is displayed.
20//! │   ├── docs/ # the directory containing the documentation of the version 1.0.0
21//! │   │   ├── index.html # the main page of the documentation of the version 1.0.0
22//! │   │   └── …
23//! │   ├── docs.tar.gz # the tarball containing the documentation of the version 1.0.0
24//! │   └── dist/ # the directory containing the distribution of the version 1.0.0
25//! │       ├── source.tgz # the source code of the version 1.0.0
26//! │       ├── mybin-v1.0.0-x86_64-unknown-linux-gnu
27//! │       └── mybin-v1.0.0-x86_64-unknown-linux-musl
28//! └── v2.0.0/ # the directory containing the version 2.0.0 of the software
29//!     ├── index.html # the main page of the version 2.0.0
30//!     ├── …
31//!     └── …
32//! ```
33//!
34//! ## Why ?
35//!
36//! This piece of software is for people that can't or won't use a full-featured forge such as GitHub, GitLab, ForgeJo & others.
37//! These forges provide "release pages" that allow you to upload and distribute your software, as well as get a changelog.
38//!
39//! Abbaye is made to be a simple, lightweight alternative to these forges, for the release/documentation parts.
40//!
41//! ### Why "Abbaye" ?
42//!
43//! [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.
44//!
45//! Anyway, that's where you store and display your relics (your software releases).
46//!
47//! ## Installation
48//!
49//! ### Pre-built binaries
50//!
51//! You can grab a pre-built binary from the [releases page](http://vit.am/~ololduck/abbaye/latest).
52//!
53//! 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).
54//!
55//! ### From source
56//!
57//! To build from source, you need to have Rust installed. You can install Rust using [rustup](https://rustup.rs/).
58//! 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:
59//!
60//! You can clone the [repository](https://git.sr.ht/~ololduck/abbaye) and build the project using `cargo build --release`.
61//!
62//! ## Usage
63//!
64//! Run `abbaye init` in your project's directory to create a `abbaye.toml` configuration file. You can then customize the configuration to your liking.
65//! Here's an example configuration file to get you started:
66//!
67//! ```toml
68//! [site]
69//! name = "Abbaye"
70//! base_url = "http://vit.am/~ololduck/abbaye/" # required for Atom feed generation (canonical URLs are used for feed items)
71//!
72//! [version_extractor]
73//! type = "git" # extract version from git tags
74//! tag_prefix = "v"
75//!
76//! [changelog] # use the default changelog parser (Keepachangelog format in CHANGELOG.md)
77//!
78//! [[builders]]  # builds the project using cargo build --release
79//! type = "cargo"
80//! targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]
81//!
82//! [[builders]]  # generates documentation using cargo doc
83//! type = "cargo_doc"
84//! no_deps = true  # Don't include dependencies in the documentation
85//!
86//! [[builders]]
87//! type = "archive"  # creates a compressed tarball of the source code (can be of anything, really)
88//! ```
89//!
90//! Then run `abbaye build` to build the site. The site will be generated in the `public/` directory by default.
91//! Now you can copy the contents of `public/` to your web server to deploy the site. For instance, with rsync:
92//! `rsync --progress -avz --links --perms --update public/ ololduck@vit.am:public_html/abbaye/`
93//!
94//! ### ✨ Customization ✨
95//!
96//! You can dump the default theme/templates to your local filesystem with `abbaye dump-theme`.
97//!
98//! This will create a `.abbaye/theme/` directory in your current directory with the default templates, which you can then ✨customize✨.
99//!
100//! ## Future plans
101//!
102//! - [x] Add support for theming
103//! - [ ] Add support for more site variables, such as the site title, description, and author, or even a custom footer and stuff.
104//! - [ ] 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`.
105//!
106//! ## Contributing
107//!
108//! 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.
109//!
110//! 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}.
111
112use std::path::PathBuf;
113
114use clap::Parser;
115use human_panic::setup_panic;
116use miette::{IntoDiagnostic, Result};
117use tokio::fs::create_dir_all;
118
119use crate::{
120    builders::{AnyBuilder, archive::ArchiveBuilderConfig},
121    changelog::ChangelogConfig,
122    config::{AbbayeConfig, SiteConfig},
123    version_extractors::{AnyVersionExtractor, git::GitVersionConfig},
124};
125
126/// All builders for the site (ex: cargo build, cargo doc, etc.).
127pub mod builders;
128/// Parses the changelog file and generates a changelog page for the site.
129pub mod changelog;
130mod cli;
131/// Handles the `abbaye.toml` configuration file.
132pub mod config;
133/// Generates the site from the configuration and builds it.
134pub mod site;
135/// Extracts current version information from different sources (ex: git tags, cargo metadata, etc.).
136pub mod version_extractors;
137
138#[tokio::main]
139async fn main() -> Result<()> {
140    setup_panic!();
141    let cli_args = cli::CliArgs::parse();
142
143    tracing_subscriber::fmt()
144        .with_timer(tracing_subscriber::fmt::time::SystemTime)
145        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
146        .init();
147
148    match cli_args.command {
149        cli::CliCommand::Init { path } => {
150            let base_path = if let Some(path) = path {
151                path
152            } else {
153                std::env::current_dir().into_diagnostic()?
154            };
155            if base_path.join("abbaye.toml").exists() {
156                return Err(miette::miette!(
157                    "abbaye.toml already exists in this directory"
158                ));
159            }
160            let abbaye_config = AbbayeConfig {
161                site: SiteConfig {
162                    name: "MyProject Release Page".to_string(),
163                    readme: None,
164                    base_url: None,
165                    repo_url: None,
166                    lang: None,
167                },
168                version_extractor: AnyVersionExtractor::Git(GitVersionConfig {
169                    tag_prefix: Some("v".to_string()),
170                    dirty_suffix: "-dirty".to_string(),
171                }),
172                builders: vec![AnyBuilder::Archive(ArchiveBuilderConfig {
173                    source_dir: None,
174                    output: None,
175                    prefix: None,
176                    ignore_patterns: vec![".git/".to_string(), "*.tar.gz".to_string()],
177                })],
178                changelog: ChangelogConfig {
179                    ..Default::default()
180                },
181                output_dir: None,
182            };
183            let config_path = base_path.join("abbaye.toml");
184            let toml = toml::to_string_pretty(&abbaye_config).into_diagnostic()?;
185            tokio::fs::write(&config_path, toml)
186                .await
187                .into_diagnostic()?;
188        }
189        cli::CliCommand::Build => {
190            let config = config::load_config()?;
191            site::build_site(config).await?;
192        }
193        cli::CliCommand::DumpTheme => {
194            let theme_path = PathBuf::from(".abbaye").join("theme");
195            create_dir_all(&theme_path).await.into_diagnostic()?;
196            tokio::fs::write(
197                theme_path.join("root_index.html.j2"),
198                site::TEMPLATE_ROOT_INDEX,
199            )
200            .await
201            .into_diagnostic()?;
202            tokio::fs::write(
203                theme_path.join("version_index.html.j2"),
204                site::TEMPLATE_VERSION_INDEX,
205            )
206            .await
207            .into_diagnostic()?;
208        }
209    }
210    Ok(())
211}