use crate::config::{
AssetSource,
WasmModule,
CompressionType,
PackerConfig,
};
use crate::cli::{YamlRoot, Cli};
use crate::encoder::{Base};
use crate::encoder;
use crate::wasmbuilder;
use crate::html;
use crate::fetcher;
use std::error::Error;
use std::fs;
use std::path::PathBuf;
use clap::{Parser};
use base64::prelude::*;
use sha2::{Sha256, Digest};
const RUNTIME_ICON: &str = include_str!("../core/icon.svg");
const RUNTIME_CORE_JS: &str = include_str!("../core/core.js");
const RUNTIME_DECODER_JS: &str = include_str!("../core/wasm_decoder.js");
const RUNTIME_DECODER_WASM: &[u8] = include_bytes!("../core/wasm_decoder_bg.wasm");
pub async fn run() -> Result<(), Box<dyn Error>> {
let cli = Cli::parse();
println!("Config: {}", cli.config.display());
println!("Output: {}", cli.output.display());
let config = load_config(cli.config).await?;
pack(config, cli.output).await?;
Ok(())
}
pub async fn load_config(
config_path: PathBuf,
) -> Result<PackerConfig, Box<dyn Error>> {
let yaml_text = fs::read_to_string(config_path)?;
let yaml_root: YamlRoot = serde_yaml::from_str(&yaml_text)?;
println!("Extracted yaml");
let config = crate::cli::set_config_from_yaml(yaml_root.pack).await?;
println!("Loaded config from yaml");
Ok(config)
}
fn default_runtime(
icons: &mut Vec<String>,
scripts: &mut Vec<String>,
bin: &mut Vec<Base>,
) {
println!("Default runtime is enabled. \
Adding icon, core.js and wasm_decoder");
let encoded_icon_string =
BASE64_STANDARD.encode(RUNTIME_ICON.as_bytes());
icons.insert(0, encoded_icon_string);
scripts.push(RUNTIME_DECODER_JS.to_string());
scripts.push(RUNTIME_CORE_JS.to_string());
let wasm_hash = Sha256::digest(RUNTIME_DECODER_WASM);
let wasm_hash_string = format!("{:x}", wasm_hash);
let wasm_encoded_text = BASE64_STANDARD.encode(RUNTIME_DECODER_WASM);
let decoder_module = Base {
id: "bin-wasm-decoder".to_string(),
hash: wasm_hash_string,
text: wasm_encoded_text,
};
bin.push(decoder_module);
}
pub async fn pack(
config: PackerConfig,
output: PathBuf,
) -> Result<(), Box<dyn Error>> {
if let Some(ref modules) = config.wasm {
wasmbuilder::compile_wasm_modules(modules).await?;
}
let icon_sources = match config.favicon {
Some(source) => get_icons(source).await?,
None => vec![],
};
let mut icons = vec![];
if !icon_sources.is_empty() {
icons.push(icon_sources[0].clone());
}
let styles_text = match config.styles {
Some(source) => get_styles_text(source).await?,
None => "".to_string(),
};
let mut scripts = match config.scripts {
Some(source) => get_sources(source).await?,
None => vec![],
};
let mut bin = match config.wasm {
Some(source) => get_wasm(source)?,
None => vec![],
};
if config.runtime.enabled {
default_runtime(
&mut icons,
&mut scripts,
&mut bin,
);
}
let markup = html::page(
styles_text,
icons,
scripts,
bin,
);
let html = markup.into_string();
html::save_html(html, output)?;
Ok(())
}
async fn get_icons(
icon_sources: Vec<AssetSource>
) -> Result<Vec<String>, Box<dyn Error>> {
let mut icons: Vec<String> = vec![];
for icon in icon_sources {
let path_str = match icon {
AssetSource::Local(path) => path.to_str().ok_or("Invalid favicon path")?.to_string(),
AssetSource::Remote(_) => return Err("Remote favicons not yet supported".into()),
};
let encoded_icon = crate::encoder::encode_base64(&path_str, "favicon")?;
icons.push(encoded_icon.text);
}
Ok(icons)
}
async fn get_styles_text(
style_sources: Vec<AssetSource>
) -> Result<String, Box<dyn Error>> {
let mut styles_text = String::from("");
for source in style_sources {
let text = match source {
AssetSource::Local(path) => fetcher::get_local_file(&path)?,
AssetSource::Remote(url) => fetcher::get_remote_file(url).await?,
};
styles_text.push_str(&text);
}
Ok(styles_text)
}
async fn get_sources(
sources: Vec<AssetSource>
) -> Result<Vec<String>, Box<dyn Error>> {
let mut source_text_list: Vec<String> = vec![];
for source in sources {
let text = match source {
AssetSource::Local(path) => fetcher::get_local_file(&path)?,
AssetSource::Remote(url) => fetcher::get_remote_file(url).await?,
};
source_text_list.push(text);
}
Ok(source_text_list)
}
fn get_wasm(
wasm_modules: Vec<WasmModule>
) -> Result<Vec<Base>, Box<dyn Error>> {
let mut bin: Vec<Base> = vec![];
for module in wasm_modules {
let path_str = match module.source{
AssetSource::Local(path) => path.to_str().ok_or("Invalid WASM path")?.to_string(),
AssetSource::Remote(_) => return Err("Remote WASM modules not yet supported".into()),
};
let encoded_module = match module.compression {
CompressionType::Brotli => encoder::encode_brotli_base64(&path_str, &module.id)?,
CompressionType::None => encoder::encode_base64(&path_str, &module.id)?,
};
bin.push(encoded_module);
}
Ok(bin)
}