Skip to content

Commit

Permalink
avoid fs read in get_sanitized_file
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicBurkart committed Jul 6, 2021
1 parent e53f3b3 commit 30934e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions turbolift_internals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ tracing = {version="0.1", features=["attributes"]}
tracing-futures = "0.2.4"
uuid = { version="0.8", features=["v4"] }
derivative = "2.2.0"
pathdiff = "0.2.0"
include_dir = "0.6.1"

# kubernetes-specific requirements
kube = "0.51.0"
Expand Down
16 changes: 15 additions & 1 deletion turbolift_internals/src/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::io::Cursor;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use include_dir::Dir;
extern crate pathdiff;
use proc_macro2::TokenStream as TokenStream2;
use quote::ToTokens;
use syn::spanned::Spanned;
Expand All @@ -17,6 +19,8 @@ type ParamTypes = syn::punctuated::Punctuated<Box<syn::Type>, syn::Token![,]>;

const IGNORED_DIRECTORIES: [&str; 3] = ["target", ".git", ".turbolift"];

static PROJECT_DIR: Dir = include_dir!(".");

#[tracing::instrument]
pub fn get_fn_item(function: TokenStream2) -> syn::ItemFn {
match syn::parse2(function).unwrap() {
Expand Down Expand Up @@ -137,7 +141,17 @@ pub fn get_sanitized_file(function: &TokenStream2) -> TokenStream2 {
if !path.exists() {
panic!("File path for the targeted function does not exist: {:?} does the compiler support getting the TokenStream from a path?", path);
}
let file_contents = std::fs::read_to_string(path).unwrap();
let file_contents = PROJECT_DIR
.get_file(
pathdiff::diff_paths(
path,
fs::canonicalize(".").expect("code directory cannot be found"),
)
.expect("get_sanitized_file: could not find the relative path of source code"),
)
.expect("get_sanitized_file: could not locate source code within file store")
.contents_utf8()
.expect("get_sanitized_file: could not decode source code from file store");

// remove target function
let target_function_removed = {
Expand Down
3 changes: 3 additions & 0 deletions turbolift_internals/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate include_dir;

use std::path::Path;

pub mod build_project;
Expand Down

0 comments on commit 30934e5

Please sign in to comment.