Skip to content
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

Use $CARGO_BUILD_TARGET_DIR to determine where output should go #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,11 @@ extern crate syn;
extern crate quote;

use std::collections::{HashMap, HashSet};
use std::fs::{File, OpenOptions, create_dir};
use std::io::{Seek, Write};
use std::fs::{create_dir_all, File, OpenOptions};
use std::{
io::{Seek, Write},
path::{Path, PathBuf},
};

use case::CaseExt;
use syn::export::Span;
Expand All @@ -436,6 +439,14 @@ impl Parse for Machine {
}
}

fn path_in_machine_target<P: AsRef<Path>>(relative_name: P) -> PathBuf {
let mut file_name = PathBuf::new();
file_name.push(std::env::var_os("CARGO_BUILD_TARGET_DIR").unwrap_or("target".into()));
file_name.push("machine");
file_name.push(relative_name);
file_name
}

#[proc_macro]
pub fn machine(input: proc_macro::TokenStream) -> syn::export::TokenStream {
let ast = parse_macro_input!(input as Machine);
Expand All @@ -444,9 +455,8 @@ pub fn machine(input: proc_macro::TokenStream) -> syn::export::TokenStream {
let (name, gen) = impl_machine(&ast);

trace!("generated: {}", gen);

let file_name = format!("target/machine/{}.rs", name.to_string().to_lowercase());
let _ = create_dir("target/machine");
let file_name = path_in_machine_target(name.to_string().to_lowercase());
let _ = create_dir_all(file_name.parent().unwrap());
File::create(&file_name)
.and_then(|mut file| {
file.seek(std::io::SeekFrom::End(0))?;
Expand Down Expand Up @@ -647,11 +657,11 @@ impl Parse for Transition {

impl Transitions {
pub fn render(&self) {
let file_name = format!(
"target/machine/{}.dot",
let file_name = path_in_machine_target(format!(
"{}.dot",
self.machine_name.to_string().to_lowercase()
);
let _ = create_dir("target/machine");
));
let _ = create_dir_all(file_name.parent().unwrap());
let mut file = File::create(&file_name).expect("error opening dot file");

file.write_all(format!("digraph {} {{\n", self.machine_name.to_string()).as_bytes())
Expand Down Expand Up @@ -819,8 +829,10 @@ pub fn transitions(input: proc_macro::TokenStream) -> syn::export::TokenStream {

//println!("generated: {:?}", gen);
trace!("generated transitions: {}", stream);
let _ = create_dir("target/machine");
let file_name = format!("target/machine/{}.rs", machine_name.to_string().to_lowercase());
let file_name =
path_in_machine_target(format!("{}.rs", machine_name.to_string().to_lowercase()));
let _ = create_dir_all(file_name.parent().unwrap());

OpenOptions::new()
.create(true)
.write(true)
Expand Down Expand Up @@ -1031,8 +1043,9 @@ pub fn methods(input: proc_macro::TokenStream) -> syn::export::TokenStream {

stream.extend(proc_macro::TokenStream::from(toks));

let file_name = format!("target/machine/{}.rs", machine_name.to_string().to_lowercase());
let _ = create_dir("target/machine");
let file_name =
path_in_machine_target(format!("{}.rs", machine_name.to_string().to_lowercase()));
let _ = create_dir_all(file_name.parent().unwrap());
OpenOptions::new()
.create(true)
.write(true)
Expand Down