diff --git a/Cargo.lock b/Cargo.lock index a43a78b..9d0754b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -289,7 +289,7 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "swift-precompiler" -version = "0.1.1" +version = "0.1.2" dependencies = [ "base64", "clap", diff --git a/Cargo.toml b/Cargo.toml index 20988c3..1d3e4f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "swift-precompiler" description = "A precompiler for Swift that allows you to use additional macros, include files, and more." -version = "0.1.1" +version = "0.1.2" edition = "2021" license = "MIT" documentation = "https://docs.rs/swift-precompiler" diff --git a/README.md b/README.md index 06f722d..7d9aeca 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Run `swift-precompiler init` to initialise a config file `swift-precompiled.toml Available options: - `dirs` - An array of directories to search for Swift source files that require precompilation -- `path_aliases` - A dictionary of path aliases to use in `precompileIncludeStr` calls +- `path_aliases` - A dictionary of path aliases to use in precompile calls Example: ```toml @@ -37,6 +37,11 @@ Including a file as a string literal at compile time: let javaScript = precompileIncludeStr("path/to/file.js") ``` +Include a file as a Data at compile time: +```swift +let image = precompileIncludeData("path/to/image.png") +``` + Run `swift-precompiler` to precompile all Swift files in the directories specified in the config file ```shell swift-precompiler precompile diff --git a/assets/PrecompiledTemplate.swift b/assets/PrecompiledTemplate.swift index b143f8c..203feed 100644 --- a/assets/PrecompiledTemplate.swift +++ b/assets/PrecompiledTemplate.swift @@ -30,10 +30,21 @@ extension String { func precompileIncludeStr(_ path: String) -> String { var content: String = "" switch (path) { - // + // default: fatalError("Error: include file not found: \(path)") } return String.fromBase64(content) ?? "" +} + +func precompileIncludeData(_ path: String) -> Data { + var content: String = "" + switch (path) { + // + default: + fatalError("Error: include file not found: \(path)") + } + + return Data.fromBase64(content) ?? Data() } \ No newline at end of file diff --git a/src/expression.rs b/src/expression.rs index dadf68a..484c604 100644 --- a/src/expression.rs +++ b/src/expression.rs @@ -4,4 +4,6 @@ impl Expression { pub const INCLUDE_STR_RGX: &'static str = r#"precompileIncludeStr\s*\(\s*["']([^"']+)["']\s*\)"#; + pub const INCLUDE_DATA_RGX: &'static str = + r#"precompileIncludeData\s*\(\s*["']([^"']+)["']\s*\)"#; } diff --git a/src/main.rs b/src/main.rs index 719621c..f8f5167 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,8 @@ use colored::Colorize; use fancy_regex::Regex; use glob::glob; use path_absolutize::*; -use crate::config::Config; +use crate::config::Config; use crate::expression::Expression; mod expression; @@ -141,6 +141,7 @@ fn main() { } let include_str_regex = Regex::new(Expression::INCLUDE_STR_RGX).unwrap(); + let include_data_regex = Regex::new(Expression::INCLUDE_DATA_RGX).unwrap(); let mut included_og_paths: Vec = vec![]; directory.split(":") @@ -158,6 +159,7 @@ fn main() { include_str_regex .captures_iter(entry_content_str.as_str()) .into_iter() + .chain(include_data_regex.captures_iter(entry_content_str.as_str())) .flatten() .for_each(|capture| { let include_str_call = capture.get(0).expect("Unable to get include_str call"); @@ -214,10 +216,14 @@ fn main() { if !included_og_paths.contains(&include_str_og_path.as_str().to_string()) { if !dry_run { let content_of_file = std::fs::read_to_string(include_str_path.as_ref().unwrap()).expect("Unable to read file to embed"); - precompile_file_data = precompile_file_data.replace("// ", &*format!("\ - // + vec!["precompile-content-str", "precompile-content-data"] + .iter() + .for_each(|placeholder| { + precompile_file_data = precompile_file_data.replace(&*format!("// <{}>", placeholder), &*format!("\ + // <{}> case \"{}\": - content = \"{}\"\n", include_str_og_path.as_str(), BASE64_STANDARD.encode(content_of_file))); + content = \"{}\"\n", placeholder, include_str_og_path.as_str(), BASE64_STANDARD.encode(content_of_file.to_owned()))); + }); } included_og_paths.push(include_str_og_path.as_str().to_owned());