Système de base pour la création de projets (#1)
All checks were successful
Rust Checks / checks (push) Successful in 15s

Reviewed-on: #1
This commit is contained in:
Tipragot 2024-01-26 12:07:00 +00:00
parent c1d8f33aa0
commit 64397c8b04
5 changed files with 7066 additions and 5 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
web

6951
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -18,3 +18,19 @@ missing_docs_in_private_items = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"
nursery = "warn"
[dependencies]
dioxus = "*"
dioxus-fullstack = { version = "*" }
serde = "1.0.195"
[target.'cfg(target_arch = "wasm32")'.dependencies.dioxus-fullstack]
version = "*"
features = ["web"]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dioxus-fullstack = { version = "*", features = ["axum"] }
dioxus-cli = "*"
railwind = "0.1.5"
walkdir = "2.4.0"
regex = "1.10.3"

98
src/lib.rs Normal file
View file

@ -0,0 +1,98 @@
//! Utilitaires permettant de lancer une application dioxus fullstack avec
//! tailwind facilement.
pub use dioxus::prelude::*;
pub use dioxus_fullstack::prelude::*;
#[cfg(all(not(target_arch = "wasm32"), debug_assertions))]
/// Compile le site en webassembly et génaire le fichier de style avec tailwind.
fn build_app() {
use std::fs;
use std::path::{Path, PathBuf};
use dioxus_cli::CrateConfig;
use railwind::{CollectionOptions, SourceOptions};
use regex::Regex;
use walkdir::WalkDir;
let project_folder = Path::new(env!("CARGO_MANIFEST_DIR"));
let output_folder = project_folder.join("web");
// Build WASM
let mut config = CrateConfig::new(Some(project_folder.to_path_buf()))
.expect("unable to load the project configuration");
config.release = true;
config.asset_dir = project_folder.join("assets");
config.out_dir = output_folder.clone();
config.dioxus_config.web.resource.style = Some(vec![PathBuf::from("style.css")]);
dioxus_cli::build(&config, false).expect("unable to build webassembly");
let index_content = dioxus_cli::gen_page(&config.dioxus_config, false);
fs::write(output_folder.join("index.html"), index_content).expect("unable to write index.html");
// Update style
let paths: Vec<_> = WalkDir::new(project_folder.join("src"))
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| e.into_path())
.collect();
let files = paths
.iter()
.map(|p| SourceOptions {
input: p,
option: CollectionOptions::Regex(
Regex::new(r#"class: "([^"]+)""#).expect("unable to create a regex"),
),
})
.collect();
railwind::parse_to_file(
railwind::Source::Files(files),
output_folder
.join("style.css")
.to_str()
.expect("unsupported path"),
true,
&mut Vec::new(),
)
}
/// Lance l'application dioxus.
///
/// Si l'application est lancée en mode debug, le projet sera compilé en
/// webassembly puis un fichier css sera généré automatiquement avec tailwind.
///
/// Si l'application est lancée en mode release, seul le serveur sera compilé.
/// Si l'on veut exporter la version finale du projet, il ne faut donc pas
/// oublier de le lancer en mode debug avant pour générer le webassembly.
pub fn launch<
Props: Clone + Default + serde::Serialize + serde::de::DeserializeOwned + Send + Sync + 'static,
>(
component: Component<Props>,
) {
#[cfg(target_arch = "wasm32")]
LaunchBuilder::new(component).launch();
#[cfg(all(not(target_arch = "wasm32"), debug_assertions))]
{
build_app();
let config = ServeConfigBuilder::new(component, Props::default())
.index_path(concat!(env!("CARGO_MANIFEST_DIR"), "/web/index.html"))
.assets_path(concat!(env!("CARGO_MANIFEST_DIR"), "/web"));
LaunchBuilder::new(|cx| render!("Hello World"))
.server_cfg(config)
.launch();
}
#[cfg(all(not(target_arch = "wasm32"), not(debug_assertions)))]
{
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
let config = ServeConfigBuilder::new(component, Props::default())
.index_path("web/index.html")
.assets_path("web");
LaunchBuilder::new(|cx| render!("Hello World"))
.server_cfg(config)
.addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 80)))
.launch();
}
}

View file

@ -1,5 +0,0 @@
//! A simple program that prints "Hello, world!" to the terminal.
fn main() {
println!("Hello, world!");
}