From 116fa2b9edd3af80e87ba4594757774f801123b9 Mon Sep 17 00:00:00 2001 From: Ranjit Mahadik <43403528+ranjitmahadik@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:23:20 +0530 Subject: [PATCH] - created path helper module. --- src/cli/generator.rs | 18 +----------------- .../generator/tests/config_generator_spec.rs | 9 ++++++++- src/core/helpers/mod.rs | 1 + src/core/helpers/path.rs | 19 +++++++++++++++++++ 4 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 src/core/helpers/path.rs diff --git a/src/cli/generator.rs b/src/cli/generator.rs index f823beeed3..c77e8706b1 100644 --- a/src/cli/generator.rs +++ b/src/cli/generator.rs @@ -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 { - 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, diff --git a/src/core/generator/tests/config_generator_spec.rs b/src/core/generator/tests/config_generator_spec.rs index dfd9631455..22c7e3d3c8 100644 --- a/src/core/generator/tests/config_generator_spec.rs +++ b/src/core/generator/tests/config_generator_spec.rs @@ -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; @@ -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 { @@ -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 }); } } diff --git a/src/core/helpers/mod.rs b/src/core/helpers/mod.rs index cb961846d2..bab0053b8d 100644 --- a/src/core/helpers/mod.rs +++ b/src/core/helpers/mod.rs @@ -1,5 +1,6 @@ pub mod body; pub mod gql_type; pub mod headers; +pub mod path; pub mod url; pub mod value; diff --git a/src/core/helpers/path.rs b/src/core/helpers/path.rs new file mode 100644 index 0000000000..02f88479d6 --- /dev/null +++ b/src/core/helpers/path.rs @@ -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 { + 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()) +}