on master help sign in
Provide a feature flag to be able to embed the runtime folder.

These changes provide a new feature flag "embed_runtime" that when enabled and built in release mode will embed the runtime folder into the resulting binary.

commit62d181de78077200d7b229ce6f91b2b14129d998 authorBrian Dawn <brian.t.dawn@gmail.com> dateThu 3 Jun 2021, 15:46 (5 years ago) parent8c2fa12
7 files changed+86−11
Cargo.lock+34 README.md+7 book/src/install.md+7 helix-core/Cargo.toml+5 helix-core/src/lib.rs+1 helix-core/src/syntax.rs+29−11 helix-term/Cargo.toml+3
Cargo.lock+34
@@ -266,6 +266,7 @@ dependencies = [
266266 "once_cell",
267267 "regex",
268268 "ropey",
269+ "rust-embed",
269270 "serde",
270271 "smallvec",
271272 "tendril",
@@ -692,6 +693,39 @@ dependencies = [
692693 "smallvec",
693694]
694695
696+[[package]]
697+name = "rust-embed"
698+version = "5.9.0"
699+source = "registry+https://github.com/rust-lang/crates.io-index"
700+checksum = "2fe1fe6aac5d6bb9e1ffd81002340363272a7648234ec7bdfac5ee202cb65523"
701+dependencies = [
702+ "rust-embed-impl",
703+ "rust-embed-utils",
704+ "walkdir",
705+]
706+
707+[[package]]
708+name = "rust-embed-impl"
709+version = "5.9.0"
710+source = "registry+https://github.com/rust-lang/crates.io-index"
711+checksum = "3ed91c41c42ef7bf687384439c312e75e0da9c149b0390889b94de3c7d9d9e66"
712+dependencies = [
713+ "proc-macro2",
714+ "quote",
715+ "rust-embed-utils",
716+ "syn",
717+ "walkdir",
718+]
719+
720+[[package]]
721+name = "rust-embed-utils"
722+version = "5.1.0"
723+source = "registry+https://github.com/rust-lang/crates.io-index"
724+checksum = "2a512219132473ab0a77b52077059f1c47ce4af7fbdc94503e9862a34422876d"
725+dependencies = [
726+ "walkdir",
727+]
728+
695729[[package]]
696730name = "ryu"
697731version = "1.0.5"
README.md+7
@@ -45,6 +45,13 @@ the `HELIX_RUNTIME` environment variable.
4545> NOTE: You should set this to <path to repository>/runtime in development (if
4646> running via cargo).
4747
48+If you want to bake the `runtime/` directory into the Helix binary you can build
49+it with:
50+
51+```
52+cargo install --path helix-term --features "embed_runtime"
53+```
54+
4855## Arch Linux
4956There are two packages available from AUR:
5057 - `helix-bin`: contains prebuilt binary from GitHub releases
book/src/install.md+7
@@ -35,3 +35,10 @@ This will install the `hx` binary to `$HOME/.cargo/bin`.
3535Now copy the `runtime/` directory somewhere. Helix will by default look for the
3636runtime inside the same folder as the executable, but that can be overriden via
3737the `HELIX_RUNTIME` environment variable.
38+
39+If you want to bake the `runtime/` directory into the Helix binary you can build
40+it with:
41+
42+```
43+cargo install --path helix-term --features "embed_runtime"
44+```
helix-core/Cargo.toml+5
@@ -7,6 +7,10 @@ license = "MPL-2.0"
77
88# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99
10+
11+[features]
12+embed_runtime = []
13+
1014[dependencies]
1115helix-syntax = { path = "../helix-syntax" }
1216
@@ -24,3 +28,4 @@ serde = { version = "1.0", features = ["derive"] }
2428toml = "0.5"
2529
2630etcetera = "0.3"
31+rust-embed="5.9.0"
helix-core/src/lib.rs+1
@@ -44,6 +44,7 @@ pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) -
4444 None
4545}
4646
47+#[cfg(not(embed_runtime))]
4748pub fn runtime_dir() -> std::path::PathBuf {
4849 // runtime env var || dir where binary is located
4950 std::env::var("HELIX_RUNTIME")
helix-core/src/syntax.rs+29−11
@@ -73,16 +73,37 @@ pub struct IndentQuery {
7373 pub outdent: HashSet<String>,
7474}
7575
76−fn read_query(language: &str, filename: &str) -> String {
77− static INHERITS_REGEX: Lazy<Regex> =
78− Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap());
79−
76+#[cfg(not(feature = "embed_runtime"))]
77+fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> {
8078 let root = crate::runtime_dir();
81− // let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
79+ let path = root.join("queries").join(language).join(filename);
80+ std::fs::read_to_string(&path)
81+}
82+
83+#[cfg(feature = "embed_runtime")]
84+use rust_embed::RustEmbed;
85+
86+#[cfg(feature = "embed_runtime")]
87+#[derive(RustEmbed)]
88+#[folder = "../runtime/"]
89+struct Runtime;
8290
91+#[cfg(feature = "embed_runtime")]
92+fn load_runtime_file(language: &str, filename: &str) -> Result<String, Box<dyn std::error::Error>> {
93+ let root = PathBuf::new();
8394 let path = root.join("queries").join(language).join(filename);
8495
85− let query = std::fs::read_to_string(&path).unwrap_or_default();
96+ let query_bytes = Runtime::get(&path.as_path().display().to_string()).unwrap_or_default();
97+ std::str::from_utf8(query_bytes.as_ref())
98+ .map(|s| s.to_string())
99+ .map_err(|err| err.into())
100+}
101+
102+fn read_query(language: &str, filename: &str) -> String {
103+ static INHERITS_REGEX: Lazy<Regex> =
104+ Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap());
105+
106+ let query = load_runtime_file(language, filename).unwrap_or_default();
86107
87108 // TODO: the collect() is not ideal
88109 let inherits = INHERITS_REGEX
@@ -146,11 +167,8 @@ impl LanguageConfiguration {
146167 .get_or_init(|| {
147168 let language = get_language_name(self.language_id).to_ascii_lowercase();
148169
149− let root = crate::runtime_dir();
150− let path = root.join("queries").join(language).join("indents.toml");
151−
152− let toml = std::fs::read(&path).ok()?;
153− toml::from_slice(&toml).ok()
170+ let toml = load_runtime_file(&language, "indents.toml").ok()?;
171+ toml::from_slice(&toml.as_bytes()).ok()
154172 })
155173 .as_ref()
156174 }
helix-term/Cargo.toml+3
@@ -8,6 +8,9 @@ license = "MPL-2.0"
88
99# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010
11+[features]
12+embed_runtime = ["helix-core/embed_runtime"]
13+
1114[[bin]]
1215name = "hx"
1316path = "src/main.rs"