Skip to content

Commit

Permalink
Fixed back #3
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed Apr 22, 2023
1 parent d6f1334 commit 143cdf4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 34 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -51,6 +51,8 @@ This will create the files `game.lua` (containing the sprites and sounds) and `m

### Include your files

In all languages, paths are in the format of `"sub.folder.file"`. Paths are resolved absolutely, starting from the root of your project (see example [here](https://github.com/scambier/TQ-Bundler/blob/master/tests/lua/sub/nested.lua)).

```lua
-- Lua syntax
include "macros" -- will look for ./macros.lua
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Expand Up @@ -84,10 +84,10 @@ fn main() {
}

fn log(str: String) {
println!("{:} - {:}", Local::now().format("%H:%m:%S"), str);
println!("TQ-Bundler - {:}", str);
}
fn log_err(str: String) {
eprintln!("{:} - {:}", Local::now().format("%H:%m:%S"), str);
eprintln!("TQ-Bundler ERROR - {:}", str);
}

fn compile(config: &Config) -> bool {
Expand All @@ -98,7 +98,7 @@ fn compile(config: &Config) -> bool {
let mut path = PathBuf::from(&config.base_folder);
path.push(&config.entry_point);
path.set_extension(&config.filetype.extension);
let main_module = match Module::new(&path, config) {
let main_module = match Module::new(&path) {
Ok(module) => module,
Err(_) => {
log_err(format!("Could not find entry point file: {:?}", &path));
Expand All @@ -124,7 +124,7 @@ fn compile(config: &Config) -> bool {

if !Module::has_module(&modules, &path) {
// Module does not already exist, load it
let module = match Module::new(&path, config) {
let module = match Module::new(&path) {
Ok(module) => module,
Err(_) => {
log_err(format!("Could not find file: {:?}", &path));
Expand Down
32 changes: 3 additions & 29 deletions src/module.rs
@@ -1,7 +1,4 @@
use std::{
fs, io,
path::{Path, PathBuf},
};
use std::{fs, io, path::PathBuf};

use crate::config::Config;

Expand All @@ -13,21 +10,8 @@ pub struct Module {
}

impl Module {
pub fn new(file_path: &PathBuf, config: &Config) -> io::Result<Self> {
let folder = file_path.parent().unwrap();
let mut contents = fs::read_to_string(file_path)?;

// Rewrite includes to be relative to the root folder
let dotted_folder = path_to_dotted(folder);
if !dotted_folder.is_empty() {
let reg_include = &config.filetype.regex;
for capture in reg_include.captures_iter(&contents.clone()) {
let cap = capture.get(1).unwrap();
let range = cap.range();
let to_replace = &format!("{dotted_folder}.{:}", cap.as_str());
contents.replace_range(range, to_replace);
}
}
pub fn new(file_path: &PathBuf) -> io::Result<Self> {
let contents = fs::read_to_string(file_path)?;

Ok(Module {
file_path: file_path.clone(),
Expand All @@ -41,16 +25,6 @@ impl Module {
}
}

fn path_to_dotted(path: &Path) -> String {
let mut parts = path.iter().map(|p| p.to_string_lossy()).collect::<Vec<_>>();
if parts.is_empty() {
return "".to_string();
}
parts.remove(0);

parts.join(".")
}

pub fn dotted_to_path(dots: &str, config: &Config) -> PathBuf {
let mut path = config.base_folder.clone();
for part in dots.split('.') {
Expand Down
2 changes: 1 addition & 1 deletion tests/lua/sub/nested.lua
@@ -1,2 +1,2 @@
include "nested-dep"
include "sub.nested-dep"
-- [[ sub.nested contents ]]

0 comments on commit 143cdf4

Please sign in to comment.