Skip to content

Commit

Permalink
- added docs on to_relative method.
Browse files Browse the repository at this point in the history
  • Loading branch information
laststylebender14 committed Jun 13, 2024
1 parent 3e1207e commit da08ebb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/cli/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@ 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::proto_reader::ProtoReader;
use crate::core::resource_reader::ResourceReader;
use crate::core::runtime::TargetRuntime;
use pathdiff::diff_paths;

/// 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`.
fn to_relative(from: &str, to: &str) -> Option<String> {
let mut from_path = Path::new(from).to_path_buf();
/// 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();

// Ensure `from` is a directory by getting its parent if it is a file
if from_path.is_file() {
from_path = from_path.parent()?.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())
diff_paths(to_path, from_path).map(|p| p.to_string_lossy().to_string())
}

pub struct ConfigConsoleGenerator {
Expand Down Expand Up @@ -110,6 +106,9 @@ impl ConfigConsoleGenerator {

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

for input in config.input {
match input.source {
Expand All @@ -125,9 +124,7 @@ impl ConfigConsoleGenerator {
}
ImportSource::Proto => {
let mut metadata = proto_reader.read(&src).await?;
if let Some(relative_path_to_proto) =
to_relative(&config.output.file, &src)
{
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
3 changes: 2 additions & 1 deletion src/core/generator/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::marker::PhantomData;
use std::path::Path;
use std::{env, marker::PhantomData};

use path_clean::PathClean;
use schemars::JsonSchema;
Expand Down

0 comments on commit da08ebb

Please sign in to comment.