Skip to content

Commit 758a137

Browse files
committed
Add precompileIncludeData
1 parent bda726d commit 758a137

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "swift-precompiler"
33
description = "A precompiler for Swift that allows you to use additional macros, include files, and more."
4-
version = "0.1.1"
4+
version = "0.1.2"
55
edition = "2021"
66
license = "MIT"
77
documentation = "https://docs.rs/swift-precompiler"

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Run `swift-precompiler init` to initialise a config file `swift-precompiled.toml
1919

2020
Available options:
2121
- `dirs` - An array of directories to search for Swift source files that require precompilation
22-
- `path_aliases` - A dictionary of path aliases to use in `precompileIncludeStr` calls
22+
- `path_aliases` - A dictionary of path aliases to use in precompile calls
2323

2424
Example:
2525
```toml
@@ -37,6 +37,11 @@ Including a file as a string literal at compile time:
3737
let javaScript = precompileIncludeStr("path/to/file.js")
3838
```
3939

40+
Include a file as a Data at compile time:
41+
```swift
42+
let image = precompileIncludeData("path/to/image.png")
43+
```
44+
4045
Run `swift-precompiler` to precompile all Swift files in the directories specified in the config file
4146
```shell
4247
swift-precompiler precompile

assets/PrecompiledTemplate.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,21 @@ extension String {
3030
func precompileIncludeStr(_ path: String) -> String {
3131
var content: String = ""
3232
switch (path) {
33-
// <precompile-content>
33+
// <precompile-content-str>
3434
default:
3535
fatalError("Error: include file not found: \(path)")
3636
}
3737

3838
return String.fromBase64(content) ?? ""
39+
}
40+
41+
func precompileIncludeData(_ path: String) -> Data {
42+
var content: String = ""
43+
switch (path) {
44+
// <precompile-content-data>
45+
default:
46+
fatalError("Error: include file not found: \(path)")
47+
}
48+
49+
return Data.fromBase64(content) ?? Data()
3950
}

src/expression.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ impl Expression {
44
pub const INCLUDE_STR_RGX: &'static str =
55
r#"precompileIncludeStr\s*\(\s*["']([^"']+)["']\s*\)"#;
66

7+
pub const INCLUDE_DATA_RGX: &'static str =
8+
r#"precompileIncludeData\s*\(\s*["']([^"']+)["']\s*\)"#;
79
}

src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use colored::Colorize;
1313
use fancy_regex::Regex;
1414
use glob::glob;
1515
use path_absolutize::*;
16-
use crate::config::Config;
1716

17+
use crate::config::Config;
1818
use crate::expression::Expression;
1919

2020
mod expression;
@@ -141,6 +141,7 @@ fn main() {
141141
}
142142

143143
let include_str_regex = Regex::new(Expression::INCLUDE_STR_RGX).unwrap();
144+
let include_data_regex = Regex::new(Expression::INCLUDE_DATA_RGX).unwrap();
144145
let mut included_og_paths: Vec<String> = vec![];
145146

146147
directory.split(":")
@@ -158,6 +159,7 @@ fn main() {
158159
include_str_regex
159160
.captures_iter(entry_content_str.as_str())
160161
.into_iter()
162+
.chain(include_data_regex.captures_iter(entry_content_str.as_str()))
161163
.flatten()
162164
.for_each(|capture| {
163165
let include_str_call = capture.get(0).expect("Unable to get include_str call");
@@ -214,10 +216,14 @@ fn main() {
214216
if !included_og_paths.contains(&include_str_og_path.as_str().to_string()) {
215217
if !dry_run {
216218
let content_of_file = std::fs::read_to_string(include_str_path.as_ref().unwrap()).expect("Unable to read file to embed");
217-
precompile_file_data = precompile_file_data.replace("// <precompile-content>", &*format!("\
218-
// <precompile-content>
219+
vec!["precompile-content-str", "precompile-content-data"]
220+
.iter()
221+
.for_each(|placeholder| {
222+
precompile_file_data = precompile_file_data.replace(&*format!("// <{}>", placeholder), &*format!("\
223+
// <{}>
219224
case \"{}\":
220-
content = \"{}\"\n", include_str_og_path.as_str(), BASE64_STANDARD.encode(content_of_file)));
225+
content = \"{}\"\n", placeholder, include_str_og_path.as_str(), BASE64_STANDARD.encode(content_of_file.to_owned())));
226+
});
221227
}
222228

223229
included_og_paths.push(include_str_og_path.as_str().to_owned());

0 commit comments

Comments
 (0)