1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
extern crate proc_macro; use proc_macro2::Span; use proc_macro_hack::proc_macro_hack; use quote::quote; use syn::{parse_macro_input, Error, LitStr, Result}; use crate::dir::Dir; use std::path::PathBuf; mod command; mod dir; mod file; fn do_it(path: PathBuf) -> Result<Dir> { let parent = path .parent() .ok_or_else(|| Error::new(Span::call_site(), "No Parent Dir"))?; if !parent.exists() { return Err(Error::new( Span::call_site(), format!("Folder {} doesnot exist", parent.display()), )); } #[cfg(any(not(debug_assertions), feature = "embed"))] { let parent = parent .canonicalize() .map_err(|e| Error::new(Span::call_site(), format!("{}", e)))?; if !parent.join("package.json").exists() { return Err(Error::new( Span::call_site(), format!("Folder {} doenot contain package.json", parent.display()), )); } command::run_command( vec!["npm", "install", "--no-package-lock", "--no-audit"], &parent, )?; command::run_command(vec!["npm", "run", "build"], &parent)?; if !path.exists() { return Err(Error::new( Span::call_site(), format!("Folder {} doesnot exist after build", path.display()), )); } } #[cfg(any(not(debug_assertions), feature = "embed"))] let path = path .canonicalize() .map_err(|e| Error::new(Span::call_site(), format!("{}", e)))?; Dir::from_disk(path) } #[proc_macro_hack] pub fn include_package(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let path = PathBuf::from(parse_macro_input!(input as LitStr).value()); do_it(path) .map(|dir| { quote! { #dir } }) .unwrap_or_else(|error| error.to_compile_error()) .into() }