FIX: Multiple packaging errors.

* The include setting doesn't do what I thought it did.
* Also when packaging the rules on what build.rs can do
  are a little more strict than was immediately apparent.
This commit is contained in:
Jeremy Wall 2019-01-14 18:44:01 -06:00
parent 5777797580
commit 98323eabbd
4 changed files with 36 additions and 12 deletions

1
.gitignore vendored
View File

@ -5,4 +5,3 @@ target
docsite/site/generated
docsite/site/public
rerast_rules/
src/build/stdlib.rs

View File

@ -9,8 +9,14 @@ documentation = "https://docs.rs/crate/ucg"
readme = "README.md"
keywords = ["compiler", "config"]
license = "Apache-2.0"
include = ["std/**/*.ucg", "std/*.ucg"]
build = "bin/build_main.rs"
include = [
"std/**/*",
"src/**/*",
"bin/build_main.rs",
"Cargo.toml",
"Cargo.lock"
]
[dependencies]
abortable_parser = "~0.2.2"

View File

@ -1,33 +1,45 @@
extern crate walkdir;
use std::path::PathBuf;
use walkdir::WalkDir;
fn generate_rust_module() -> String {
let mut rust_lib = String::new();
rust_lib.push_str("use std::collections::HashMap;\n");
rust_lib.push_str("pub fn get_libs() -> HashMap<String, &'static str> {\n");
rust_lib.push_str("\tlet mut stdlib = HashMap::new();\n");
let out_dir = std::env::var("OUT_DIR").unwrap();
// NOTE(jwall): Since the generated file will be included using the include! macro
// This has to be an expression or item. This means we need to enclose it with
// braces to force it to be a single expression instead of multiple.
rust_lib.push_str("{");
for entry in WalkDir::new("std").into_iter().filter_map(|e| e.ok()) {
// Okay we want to add these as include bytes in a simulated
// filesystem for our binary to use.
let path = entry.into_path();
// We only include files that are not test files.
if path.is_file() && !path.ends_with("_test.ucg") {
let path_str = path.to_string_lossy();
let path_str = path.to_string_lossy().to_string();
if path.is_file() && !path_str.ends_with("_test.ucg") {
println!("Adding lib file: {}", path_str);
let out_path = PathBuf::from(format!("{}/{}", out_dir, path_str));
// We have to copy the file into out out directory to ensure that we
// have a reliable way for the stdlib.rs module file to include them
// from.
std::fs::create_dir_all(out_path.parent().unwrap()).unwrap();
std::fs::copy(&path_str, &out_path).unwrap();
let include = format!(
"\tstdlib.insert(\n\t\t\"{}\".to_string(),\n\t\tinclude_str!(\"../../{}\"));\n",
path_str, path_str
"\tstdlib.insert(\n\t\t\"{}\".to_string(),\n\t\tinclude_str!(\"{}/{}\"));\n",
path_str, out_dir, path_str
);
rust_lib.push_str(&include);
rust_lib.push_str("\n");
}
}
rust_lib.push_str("\tstdlib\n");
rust_lib.push_str("}");
println!("{}", rust_lib);
println!("Finished Adding lib files");
rust_lib
}
fn main() {
let contents = generate_rust_module();
std::fs::write("src/build/stdlib.rs", contents.as_bytes()).unwrap();
let out_dir = std::env::var("OUT_DIR").unwrap();
std::fs::write(format!("{}/stdlib_generated.rs", out_dir), contents.as_bytes()).unwrap();
}

7
src/build/stdlib.rs Normal file
View File

@ -0,0 +1,7 @@
use std::collections::HashMap;
pub fn get_libs() -> HashMap<String, &'static str> {
let mut stdlib = HashMap::new();
include!(concat!(env!("OUT_DIR"), "/stdlib_generated.rs"));
stdlib
}