schovi / baked_file_system

Virtual File System for Crystal language. Embedding your assets into final binary.
MIT License
177 stars 18 forks source link

Pass __DIR__ automatically #9

Closed oprypin closed 6 years ago

oprypin commented 6 years ago

Better and more often usage will be this, where you need to locate files in your repository That repository can be in different locations (imagine more ppl working on same program) Use relative path and pass second argument __DIR__

This weird API is because how crystal macros and variable/constants resolving in them works. You cant pass any expression in them

But you can specify __DIR__ as the default macro argument. Like so:

/tmp$ cat a.cr 
require "b"

test "hi"
/tmp$ cat lib/b.cr
macro test(a, b = __DIR__)
  puts {{b + "/" + a}}
end
/tmp$ crystal a.cr 
/tmp/hi
schovi commented 6 years ago

@oprypin Will try, but somehow I was not able to achieve it even after discussion on Crystal irc.

schovi commented 6 years ago

@oprypin You are right. It works. But it has impact on other part of library. This works fine now: BakedFileSystem.load("./something")

But it change behavior of absolute paths! Now when you call BakedFileSystem.load("/home/name") it loads /home/name because of default source = "". With this upgrade it will because of source = __DIR__ do File.join(__DIR__, "/home/name") which is /dir/path/home/name - invalid

What would make sense is to check if first argument is absolute path and ignore the other one. What do you think?

schovi commented 6 years ago

Whoa. There is no method to check if path is absolute or relative in Crystal!

schovi commented 6 years ago

@oprypin There is branch with update, can you test it?

oprypin commented 6 years ago

Hi. I'm not going to run the code, but I can comment on it.

You don't need to check if the path is absolute, just apply this function, which you seem to already have found.

File.expand_path("foo.html", "/home") #=> "/home/foo.html"
File.expand_path("/foo.html", "/home") #=> "/foo.html"