use std::fs;
use std::path::{Path, PathBuf};
use clap::ValueEnum;
use crate::runner::cli::{InitArgs, NewArgs, Target, WebRenderer};
use crate::runner::config::WEB_PROFILE_TOML;
const TELAR_VERSION: &str = env!("CARGO_PKG_VERSION");
pub(crate) fn run_new_cmd(args: NewArgs) {
let NewArgs {
path,
name,
target,
renderer,
lib,
} = args;
let crate_name = derive_crate_name(&path, name);
if let Err(msg) = validate_name(&crate_name) {
fail(&msg);
}
if path.exists()
&& fs::read_dir(&path)
.map(|mut d| d.next().is_some())
.unwrap_or(false)
{
fail(&format!(
"`{}` already exists and is not empty",
path.display()
));
}
let kind = Kind::new(lib, target, renderer);
let files = kind.files(&crate_name);
write_all(&path, &files);
kind.finish(&path, &crate_name);
}
pub(crate) fn run_init_cmd(args: InitArgs) {
let InitArgs {
path,
name,
target,
renderer,
lib,
} = args;
let path = path.unwrap_or_else(|| PathBuf::from("."));
let crate_name = derive_crate_name(&path, name);
if let Err(msg) = validate_name(&crate_name) {
fail(&msg);
}
let kind = Kind::new(lib, target, renderer);
let files = kind.files(&crate_name);
let conflicts = conflicts_in(&path, &files);
if !conflicts.is_empty() {
fail(&format!(
"`{}` already has files this scaffold would write, so nothing was written:\n{}\n\nRemove or rename them first โ `init` never overwrites an existing file.",
path.display(),
conflicts
.iter()
.map(|rel| format!(" {rel}"))
.collect::<Vec<_>>()
.join("\n")
));
}
write_all(&path, &files);
kind.finish(&path, &crate_name);
}
#[derive(Clone, Copy)]
enum Kind {
App {
target: Target,
renderer: WebRenderer,
},
Library,
}
impl Kind {
fn new(lib: bool, target: Target, renderer: WebRenderer) -> Self {
match lib {
true => Self::Library,
false => Self::App { target, renderer },
}
}
fn files(self, crate_name: &str) -> Vec<(&'static str, String)> {
match self {
Self::App { target, renderer } => scaffold_files(crate_name, target, renderer),
Self::Library => library_files(crate_name),
}
}
fn finish(self, path: &Path, crate_name: &str) {
match self {
Self::App { target, renderer } => finish(path, crate_name, target, renderer),
Self::Library => finish_library(path, crate_name),
}
}
}
fn scaffold_files(
crate_name: &str,
target: Target,
renderer: WebRenderer,
) -> Vec<(&'static str, String)> {
let module = crate_name.replace('-', "_");
vec![
("Cargo.toml", manifest(crate_name, target, renderer)),
("telar.toml", config(crate_name, target)),
(".gitignore", "/target\n.telar/\n".to_string()),
("src/main.rs", main_rs(&module)),
("src/lib.rs", LIB_RS.to_string()),
("src/app.rs", APP_RS.to_string()),
("src/theme.rs", THEME_RS.to_string()),
("src/home.rsx", HOME_RSX.to_string()),
]
}
fn library_files(crate_name: &str) -> Vec<(&'static str, String)> {
vec![
("Cargo.toml", library_manifest(crate_name)),
("telar.toml", LIBRARY_CONFIG.to_string()),
(".gitignore", "/target\n.telar/\n".to_string()),
("src/lib.rs", LIBRARY_LIB_RS.to_string()),
("src/badge.rsx", BADGE_RSX.to_string()),
]
}
fn conflicts_in<'a>(root: &Path, files: &[(&'a str, String)]) -> Vec<&'a str> {
files
.iter()
.map(|(rel, _)| *rel)
.filter(|rel| root.join(rel).exists())
.collect()
}
fn write_all(root: &Path, files: &[(&str, String)]) {
for (rel, contents) in files {
write(root, rel, contents);
}
}
fn finish(path: &Path, crate_name: &str, target: Target, renderer: WebRenderer) {
let producer = format!("cargo-telar {TELAR_VERSION}");
super::transpile::transpile_member(path, &producer, TELAR_VERSION);
let dev = match (target, renderer) {
(Target::Desktop, _) => "cargo telar dev".to_string(),
(Target::Web, renderer) => {
let renderer = renderer
.to_possible_value()
.expect("every renderer is a value of the flag");
format!(
"cargo telar dev --target web --renderer {}",
renderer.get_name()
)
}
(other, _) => format!("cargo telar dev --target {}", target_name(other)),
};
println!("Created `{crate_name}` at {}", path.display());
println!();
println!(" cd {}", path.display());
println!(" {dev}");
println!();
println!(
"Other targets are one word each: `default = [\"โฆ\"]` in Cargo.toml, or `--target` on the command line."
);
}
fn finish_library(path: &Path, crate_name: &str) {
let producer = format!("cargo-telar {TELAR_VERSION}");
super::transpile::transpile_member(path, &producer, TELAR_VERSION);
println!(
"Created the telar library `{crate_name}` at {}",
path.display()
);
println!();
println!(" cd {}", path.display());
println!(" cargo telar package --check");
println!();
println!(
"`cargo telar package` transpiles and bakes it, checks that the package carries an artifact that answers for its sources, and writes the .crate."
);
}
fn derive_crate_name(path: &Path, name: Option<String>) -> String {
name.or_else(|| name_from_path(path, || std::env::current_dir().ok()))
.unwrap_or_else(|| {
fail(&format!(
"cannot derive a package name from `{}` โ pass --name",
path.display()
))
})
}
fn name_from_path(path: &Path, cwd: impl FnOnce() -> Option<PathBuf>) -> Option<String> {
let effective = if path.as_os_str() == std::ffi::OsStr::new(".") {
cwd().unwrap_or_else(|| path.to_path_buf())
} else {
path.to_path_buf()
};
effective
.file_name()
.and_then(|n| n.to_str())
.map(|segment| segment.to_string())
}
fn write(root: &Path, rel: &str, contents: &str) {
let path = root.join(rel);
if let Some(parent) = path.parent()
&& let Err(e) = fs::create_dir_all(parent)
{
fail(&format!("cannot create {}: {e}", parent.display()));
}
if let Err(e) = fs::write(&path, contents) {
fail(&format!("cannot write {}: {e}", path.display()));
}
}
fn fail(msg: &str) -> ! {
eprintln!("error: {msg}");
std::process::exit(1);
}
fn validate_name(name: &str) -> Result<(), String> {
if name.is_empty() {
return Err("package name is empty".to_string());
}
if name.starts_with(|c: char| c.is_ascii_digit()) {
return Err(format!("`{name}` cannot start with a digit"));
}
if !name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
{
return Err(format!(
"`{name}` may only contain letters, digits, `-` and `_`"
));
}
Ok(())
}
fn target_name(target: Target) -> &'static str {
match target {
Target::Desktop => "desktop",
Target::Tui => "tui",
Target::Web => "web",
Target::Android => "android",
}
}
fn manifest(name: &str, target: Target, renderer: WebRenderer) -> String {
let default_feature = target.feature(renderer);
let tooling = super::config::tooling_feature_entry();
let web_profile = match target {
Target::Web => format!("\n{WEB_PROFILE_TOML}"),
_ => String::new(),
};
format!(
r#"[package]
name = "{name}"
version = "0.1.0"
edition = "2024"
# `cdylib` is the library `cargo telar dev` swaps on a hot reload; `lib` is what the binary and the tests link.
[lib]
crate-type = ["cdylib", "lib"]
[[bin]]
name = "{name}"
path = "src/main.rs"
[dependencies]
# `default-features = false` so this file is the only place a target is named: telar's own default is a
# desktop window, and it would otherwise ride along into a build that asked for the terminal or a browser.
telar = {{ version = "{TELAR_VERSION}", default-features = false, features = ["runtime"] }}
# The widget catalogue, every widget in every build. Its `icons` feature draws `icon:"set:name"` ids through `telar-icons`.
telar-components = {{ version = "{TELAR_VERSION}", default-features = false }}
# The devtools overlay: an FPS counter, a node inspector and the build-error banner. Optional, so only
# `cargo telar dev` and `cargo telar preview` compile it, by turning on the `telar-devtools` feature below.
telar-devtools = {{ version = "{TELAR_VERSION}", optional = true }}
# The component workshop `cargo telar preview` shows every `[preview]` in. Optional for the same reason.
telar-workshop = {{ version = "{TELAR_VERSION}", optional = true }}
[features]
# The target this project builds for. Each one is complete on its own โ swapping one word here, or
# passing `--target` (and, for the browser, `--renderer`) on the command line, is the whole of the change.
default = ["{default_feature}"]
desktop = ["telar/desktop"]
tui = ["telar/tui"]
web = ["telar/web"]
web-dom = ["telar/web-dom"]
android = ["telar/android"]
telar-devtools = ["dep:telar-devtools"]
telar-workshop = ["dep:telar-workshop"]
# Never turned on by a build: `cargo telar` names these on the command line for its own commands, and this entry is what lets Cargo.lock pin what they bring in.
{tooling}
[profile.dev]
opt-level = 1
debug = "line-tables-only"
lto = "off"
# Dependencies compile once and are not what you are stepping through: optimising them buys a renderer and
# a layout engine that run at a usable speed in dev, and dropping their debug info takes most of the weight
# out of the cdylib a hot reload rewrites. Your own crates keep theirs, so panics still name a line.
[profile.dev.package."*"]
opt-level = 3
debug = false
# Do not add `panic = "abort"`: Telar unwinds to keep one panicking surface from taking the process with it.
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
strip = "symbols"
{web_profile}"#
)
}
fn library_manifest(name: &str) -> String {
let include = super::library::include_block();
format!(
r#"[package]
name = "{name}"
version = "0.1.0"
edition = "2024"
# A dependency is compiled from the transpiled `.rsx` this package ships, and cargo leaves `.telar/` out of a
# package unless it is named here. `cargo telar package --check` says what to add when this falls behind.
{include}
[dependencies]
# Pinned with `=`: a dependency's transpiled `.rsx` is accepted only by the telar it was transpiled for, so a
# library is released in lockstep with telar, and the pin lets cargo pick the release matching an application's.
telar = {{ version = "={TELAR_VERSION}", default-features = false, features = ["runtime"] }}
"#
)
}
const LIBRARY_CONFIG: &str = r#"[telar]
# Compiled as a dependency, this package is wired read-only from the artifact it ships. A library inherits
# nothing from a workspace's telar.toml: what it declares here is all it is transpiled against.
library = true
# Crates whose components every `.rsx` file sees without a `use`.
prelude = []
"#;
const LIBRARY_LIB_RS: &str = "telar::rsx_modules!();\ntelar::library_previews!();\n";
const BADGE_RSX: &str = r#"[logic]
#[derive(Default)]
pub struct Props {
pub label: &'static str,
}
[view]
col pad:8
text "{props.label}" font_size:14
"#;
fn config(name: &str, target: Target) -> String {
let window = match target {
Target::Desktop | Target::Android => format!(
r#"
[telar.dev.window]
title = "{name}"
width = 1000
height = 700
"#
),
Target::Tui | Target::Web => String::new(),
};
format!(
r#"[telar]
# "auto" draws with the GPU where there is one and the CPU where there is not.
backend = "auto"
# Crates whose components every `.rsx` file sees without a `use`.
prelude = ["telar_components"]
{window}"#
)
}
fn main_rs(module: &str) -> String {
format!(
r#"fn main() {{
{module}::run();
}}
"#
)
}
const LIB_RS: &str = r#"telar::app!(
theme::AppTheme,
{
telar::set_theme(theme::AppTheme::light());
},
telar::AppConfig::default(),
app::Root
);
"#;
const APP_RS: &str = r#"use telar::{App, Color, ScrollPage, reset_layout_runtime};
use crate::theme::theme;
pub struct Root;
impl App for Root {
fn root(&self) -> Box<dyn telar::Component> {
reset_layout_runtime();
let content = crate::home::home(crate::home::HomeProps::props().build())
.expect("home layout failed");
Box::new(ScrollPage::new(content).expect("page layout failed"))
}
fn clear_color(&self) -> Option<Color> {
Some(theme().surface_alt)
}
}
"#;
const THEME_RS: &str = r#"use telar::{Color, ThemeTokens, use_theme};
/// The application's design tokens. Every component reads these, so a restyle happens here and nowhere else.
#[derive(Clone, ThemeTokens)]
pub struct AppTheme {
pub primary: Color,
pub on_primary: Color,
pub surface: Color,
pub surface_alt: Color,
pub border: Color,
pub ink: Color,
pub muted: Color,
pub scrollbar: Color,
pub success: Color,
pub warning: Color,
pub error: Color,
pub on_error: Color,
pub info: Color,
pub highlight_low: Color,
pub highlight_med: Color,
pub highlight_high: Color,
pub radius: f32,
pub spacing: f32,
pub icon_size: f32,
}
impl AppTheme {
pub fn light() -> Self {
Self {
primary: Color::rgba(0.26, 0.38, 0.93, 1.0),
on_primary: Color::WHITE,
surface: Color::WHITE,
surface_alt: Color::rgba(0.96, 0.97, 0.99, 1.0),
border: Color::rgba(0.86, 0.87, 0.93, 1.0),
ink: Color::rgba(0.09, 0.10, 0.18, 1.0),
muted: Color::rgba(0.38, 0.40, 0.50, 1.0),
scrollbar: Color::rgba(0.66, 0.68, 0.76, 1.0),
success: Color::rgba(0.10, 0.48, 0.30, 1.0),
warning: Color::rgba(0.60, 0.36, 0.02, 1.0),
error: Color::rgba(0.80, 0.20, 0.26, 1.0),
on_error: Color::WHITE,
info: Color::rgba(0.12, 0.42, 0.78, 1.0),
highlight_low: Color::rgba(0.0, 0.0, 0.0, 0.04),
highlight_med: Color::rgba(0.0, 0.0, 0.0, 0.08),
highlight_high: Color::rgba(0.0, 0.0, 0.0, 0.14),
radius: 10.0,
spacing: 8.0,
icon_size: 16.0,
}
}
pub fn dark() -> Self {
Self {
primary: Color::rgba(0.45, 0.58, 1.0, 1.0),
on_primary: Color::rgba(0.05, 0.06, 0.12, 1.0),
surface: Color::rgba(0.11, 0.12, 0.16, 1.0),
surface_alt: Color::rgba(0.07, 0.08, 0.11, 1.0),
border: Color::rgba(0.24, 0.26, 0.32, 1.0),
ink: Color::rgba(0.92, 0.93, 0.96, 1.0),
muted: Color::rgba(0.60, 0.63, 0.72, 1.0),
scrollbar: Color::rgba(0.36, 0.38, 0.46, 1.0),
success: Color::rgba(0.30, 0.78, 0.55, 1.0),
warning: Color::rgba(0.96, 0.72, 0.28, 1.0),
error: Color::rgba(0.94, 0.42, 0.44, 1.0),
on_error: Color::rgba(0.05, 0.06, 0.12, 1.0),
info: Color::rgba(0.42, 0.68, 0.98, 1.0),
highlight_low: Color::rgba(1.0, 1.0, 1.0, 0.05),
highlight_med: Color::rgba(1.0, 1.0, 1.0, 0.10),
highlight_high: Color::rgba(1.0, 1.0, 1.0, 0.16),
radius: 10.0,
spacing: 8.0,
icon_size: 16.0,
}
}
}
/// The active theme, read reactively: a component calling this re-runs when the theme changes.
pub fn theme() -> AppTheme {
use_theme::<AppTheme>()
}
"#;
const HOME_RSX: &str = r#"[logic]
let clicks = signal(0i32);
[view]
col grow:1 gap:16 pad:32 align:center fill:$theme.surface_alt
text "Hello from Telar" font_size:32 color:$theme.ink
text "Clicks ยท {$clicks}" font_size:16 color:$theme.muted
row gap:10
button label:"+1" on_press:(|| $clicks += 1)
button label:"Reset" variant:Variant::Ghost on_press:(|| clicks.set(0))
[preview "Home"]
home
"#;
#[cfg(test)]
#[path = "new.test.rs"]
mod tests;