Skip to content

Commit

Permalink
- created path helper module.
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Jun 13, 2024
1 parent dac8f42 commit 116fa2b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
18 changes: 1 addition & 17 deletions src/cli/generator.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
use std::fs;
use std::path::Path;

use inquire::Confirm;
use pathdiff::diff_paths;

use crate::core::config::{self, ConfigModule};
use crate::core::generator::source::{ConfigSource, ImportSource};
use crate::core::generator::{Generator, GeneratorConfig, GeneratorInput, InputSource, Resolved};
use crate::core::helpers::path::{is_exists, to_relative_path};
use crate::core::proto_reader::ProtoReader;
use crate::core::resource_reader::ResourceReader;
use crate::core::runtime::TargetRuntime;

/// Checks if file or folder already exists or not.
fn is_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}

/// Expects both paths to be absolute and returns a relative path from `from` to
/// `to`. expects `from`` to be directory.
fn to_relative_path(from: &Path, to: &str) -> Option<String> {
let from_path = Path::new(from).to_path_buf();
let to_path = Path::new(to).to_path_buf();

// Calculate the relative path from `from_path` to `to_path`
diff_paths(to_path, from_path).map(|p| p.to_string_lossy().to_string())
}

pub struct ConfigConsoleGenerator {
config_path: String,
runtime: TargetRuntime,
Expand Down
9 changes: 8 additions & 1 deletion src/core/generator/tests/config_generator_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tailcall::core::generator::source::ImportSource;
use tailcall::core::generator::{
Generator, GeneratorConfig, GeneratorInput, InputSource, Resolved, UnResolved,
};
use tailcall::core::helpers::path::to_relative_path;
use tailcall::core::proto_reader::ProtoReader;
use tailcall::core::resource_reader::ResourceReader;
use tokio::runtime::Runtime;
Expand Down Expand Up @@ -43,6 +44,9 @@ async fn resolve_io(

let reader = ResourceReader::cached(runtime.clone());
let proto_reader = ProtoReader::init(reader.clone(), runtime.clone());
let output_dir = Path::new(&config.output.file)
.parent()
.unwrap_or(Path::new(""));

for input in config.input.iter() {
match &input.source {
Expand All @@ -58,7 +62,10 @@ async fn resolve_io(
.push(GeneratorInput::Json { url: src.parse()?, data: content })
}
ImportSource::Proto => {
let metadata = proto_reader.read(&src).await?;
let mut metadata = proto_reader.read(&src).await?;
if let Some(relative_path_to_proto) = to_relative_path(output_dir, src) {
metadata.path = relative_path_to_proto;
}
generator_type_inputs.push(GeneratorInput::Proto { metadata });
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod body;
pub mod gql_type;
pub mod headers;
pub mod path;
pub mod url;
pub mod value;
19 changes: 19 additions & 0 deletions src/core/helpers/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::fs;
use std::path::Path;

use pathdiff::diff_paths;

/// Checks if file or folder already exists or not.
pub fn is_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}

/// Expects both paths to be absolute and returns a relative path from `from` to
/// `to`. expects `from`` to be directory.
pub fn to_relative_path(from: &Path, to: &str) -> Option<String> {
let from_path = Path::new(from).to_path_buf();
let to_path = Path::new(to).to_path_buf();

// Calculate the relative path from `from_path` to `to_path`
diff_paths(to_path, from_path).map(|p| p.to_string_lossy().to_string())
}

0 comments on commit 116fa2b

Please sign in to comment.