-
Notifications
You must be signed in to change notification settings - Fork 6
Use pdl-compiler directly #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,13 @@ jobs: | |
with: | ||
submodules: recursive | ||
|
||
- name: Check Cargo build | ||
run: | | ||
cargo test | ||
|
||
- name: Install dependencies | ||
run: | | ||
cargo install pdl-compiler --version 0.2.2 | ||
cargo install pdl-compiler --version 0.2.3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming it's desirable for pdl-compiler to be up to date -- if you'd rather stick with 0.2.2 that's fine too of course |
||
|
||
- name: Build | ||
run: | | ||
|
@@ -65,7 +69,7 @@ jobs: | |
|
||
- name: Install dependencies | ||
run: | | ||
cargo install pdl-compiler --version 0.2.2 | ||
cargo install pdl-compiler --version 0.2.3 | ||
python3 -m pip install hatch | ||
|
||
- name: Set VERSION | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Cargo.lock | ||
target/ | ||
bazel-* | ||
/.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use std::env; | |
use std::fs::File; | ||
use std::path::{Path, PathBuf}; | ||
use std::process::{Command, Stdio}; | ||
use std::io::Write; | ||
|
||
fn main() { | ||
install_generated_module( | ||
|
@@ -41,47 +42,51 @@ fn install_generated_module(module_name: &str, prebuilt_var: &str, pdl_name: &Pa | |
Err(_) => PathBuf::from(module_name), | ||
}; | ||
|
||
if Path::new(module_prebuilt.as_os_str()).exists() { | ||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
std::fs::copy( | ||
module_prebuilt.as_os_str().to_str().unwrap(), | ||
out_dir.join(module_name).as_os_str().to_str().unwrap(), | ||
) | ||
.unwrap(); | ||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join(module_name); | ||
|
||
if module_prebuilt.exists() { | ||
std::fs::copy(module_prebuilt, &out_path).unwrap(); | ||
} else { | ||
generate_module(pdl_name); | ||
generate_module(pdl_name, &out_path); | ||
} | ||
|
||
// set var for use in `include!` | ||
println!("cargo:rustc-env={}={}", prebuilt_var, out_path.to_str().unwrap()); | ||
} | ||
|
||
fn generate_module(in_file: &PathBuf) { | ||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
let out_file = | ||
File::create(out_dir.join(in_file.file_name().unwrap()).with_extension("rs")).unwrap(); | ||
fn generate_module(in_file: &Path, out_path: &Path) { | ||
let mut out_file = File::create(out_path).unwrap(); | ||
|
||
// Find the pdl tool. Expecting it at CARGO_HOME/bin | ||
let pdl = match env::var("CARGO_HOME") { | ||
let pdlc = match env::var("CARGO_HOME") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it even desirable to maintain this code path? If someone is using Cargo vs the Blaze build, when would they want to use the external |
||
Ok(dir) => PathBuf::from(dir).join("bin").join("pdlc"), | ||
Err(_) => PathBuf::from("pdlc"), | ||
}; | ||
|
||
if !Path::new(pdl.as_os_str()).exists() { | ||
panic!("pdl not found in the current environment: {:?}", pdl.as_os_str().to_str().unwrap()); | ||
} | ||
if pdlc.exists() { | ||
let output = Command::new(pdlc.as_os_str().to_str().unwrap()) | ||
.arg("--output-format") | ||
.arg("rust") | ||
.arg(in_file) | ||
.stdout(Stdio::from(out_file)) | ||
.output() | ||
.unwrap(); | ||
|
||
println!("cargo:rerun-if-changed={}", in_file.display()); | ||
let output = Command::new(pdl.as_os_str().to_str().unwrap()) | ||
.arg("--output-format") | ||
.arg("rust") | ||
.arg(in_file) | ||
.stdout(Stdio::from(out_file)) | ||
.output() | ||
.unwrap(); | ||
println!( | ||
"Status: {}, stderr: {}", | ||
output.status, | ||
String::from_utf8_lossy(output.stderr.as_slice()) | ||
); | ||
|
||
println!( | ||
"Status: {}, stderr: {}", | ||
output.status, | ||
String::from_utf8_lossy(output.stderr.as_slice()) | ||
); | ||
assert!(output.status.success()); | ||
} else { | ||
// use pdl_compiler as a library | ||
let mut sources = pdl_compiler::ast::SourceDatabase::new(); | ||
let parsed_file = pdl_compiler::parser::parse_file(&mut sources, &in_file.to_str().expect("Filename is not UTF-8").to_string()).expect("PDL parse failed"); | ||
let analyzed_file = pdl_compiler::analyzer::analyze(&parsed_file).expect("PDL analysis failed"); | ||
let rust_source = pdl_compiler::backends::rust::generate(&sources, &analyzed_file); | ||
out_file.write_all(rust_source.as_bytes()).expect("Could not write to output file"); | ||
} | ||
|
||
assert!(output.status.success()); | ||
println!("cargo:rerun-if-changed={}", in_file.display()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might as well check that the cargo build works 🤷