Skip to content

Commit

Permalink
[visualbndbuild] Fix rendering issues since it is possible to provide…
Browse files Browse the repository at this point in the history
… paths instead of files
  • Loading branch information
Krusty/Benediction committed Aug 7, 2024
1 parent cab9e57 commit 01a7fb5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
18 changes: 11 additions & 7 deletions cpclib-bndbuild/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::collections::HashSet;
use std::io::{BufReader, Read};
use std::ops::Deref;

use cpclib_common::camino::Utf8Path;
use cpclib_common::camino::{Utf8Path, Utf8PathBuf};
use cpclib_common::itertools::Itertools;
use minijinja::{context, path_loader, Environment, Error, ErrorKind};
use minijinja::{context, Environment, Error, ErrorKind};

use crate::rules::{self, Graph, Rule};
use crate::BndBuilderError;
Expand All @@ -21,8 +21,10 @@ self_cell::self_cell! {
}
}



pub struct BndBuilder {
inner: BndBuilderInner
inner: BndBuilderInner,
}

impl Deref for BndBuilder {
Expand All @@ -48,12 +50,13 @@ impl BndBuilder {
BndBuilder { inner }
}

pub fn from_fname<P: AsRef<Utf8Path>>(fname: P) -> Result<Self, BndBuilderError> {
let content = Self::decode_from_fname(fname)?;
pub fn from_path<P: AsRef<Utf8Path>>(fname: P) -> Result<(Utf8PathBuf, Self), BndBuilderError> {
let (p, content) = Self::decode_from_fname(fname)?;
Self::from_string(content)
.map(|build| (p, build))
}

pub fn decode_from_fname<P: AsRef<Utf8Path>>(fname: P) -> Result<String, BndBuilderError> {
pub fn decode_from_fname<P: AsRef<Utf8Path>>(fname: P) -> Result<(Utf8PathBuf, String), BndBuilderError> {
Self::decode_from_fname_with_definitions(fname, &Vec::<(String, String)>::new())
}

Expand All @@ -64,7 +67,7 @@ impl BndBuilder {
>(
fname: P,
definitions: &[(S1, S2)]
) -> Result<String, BndBuilderError> {
) -> Result<(Utf8PathBuf, String), BndBuilderError> {
let fname = fname.as_ref();

// when a folder is provided try to look for a build file
Expand Down Expand Up @@ -96,6 +99,7 @@ impl BndBuilder {

let rdr = BufReader::new(file);
Self::decode_from_reader(rdr, working_directory, definitions)
.map(|s| (fname.to_owned(), s))
}

pub fn save<P: AsRef<Utf8Path>>(&self, path: P) -> std::io::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion cpclib-bndbuild/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn process_matches(cmd: Command, matches: &ArgMatches) -> Result<(), BndBuil
Default::default()
};

let content = BndBuilder::decode_from_fname_with_definitions(fname, &definitions)?;
let (_path, content) = BndBuilder::decode_from_fname_with_definitions(fname, &definitions)?;
if matches.get_flag("show") {
println!("{content}");
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion cpclib-bndbuild/tests/build_dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn test_dummy_phony() {
use cpclib_common::itertools::Itertools;

let builder_fname = "tests/dummy/bndbuild.yml";
let builder = BndBuilder::from_fname(builder_fname).unwrap();
let builder = BndBuilder::from_path(builder_fname).unwrap();

println!(
"{:#?}",
Expand Down
7 changes: 4 additions & 3 deletions cpclib-visual-bndbuild/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,11 @@ impl BndBuildApp {

pub fn load<P: AsRef<Utf8Path>>(&mut self, path: P) {
let path = path.as_ref();
match cpclib_bndbuild::BndBuilder::from_fname(path) {
Ok(builder) => {
match cpclib_bndbuild::BndBuilder::from_path(path) {
Ok((ref path, builder)) => {
self.filename = Some(path.into());
self.file_content = std::fs::read_to_string(self.filename.as_ref().unwrap()).ok(); // read a second time, but the file exists
self.file_content = std::fs::read_to_string(path).ok().map(|s| s.replace('\r',"")); // read a second time, but the file exists

self.builder_and_layers = BuilderAndCache::from(builder).into();

if let Some(position) = self.recent_files.iter().position(|elem| elem == path) {
Expand Down

0 comments on commit 01a7fb5

Please sign in to comment.