Skip to content

Commit

Permalink
Add precompileIncludeData
Browse files Browse the repository at this point in the history
  • Loading branch information
lockieluke committed Jan 13, 2024
1 parent bda726d commit 758a137
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion assets/PrecompiledTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ extension String {
func precompileIncludeStr(_ path: String) -> String {
var content: String = ""
switch (path) {
// <precompile-content>
// <precompile-content-str>
default:
fatalError("Error: include file not found: \(path)")
}

return String.fromBase64(content) ?? ""
}

func precompileIncludeData(_ path: String) -> Data {
var content: String = ""
switch (path) {
// <precompile-content-data>
default:
fatalError("Error: include file not found: \(path)")
}

return Data.fromBase64(content) ?? Data()
}
2 changes: 2 additions & 0 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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*\)"#;
}
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> = vec![];

directory.split(":")
Expand All @@ -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");
Expand Down Expand Up @@ -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("// <precompile-content>", &*format!("\
// <precompile-content>
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());
Expand Down

0 comments on commit 758a137

Please sign in to comment.