Absolute path of the project root directory in Julia - julia

The project root directory of a file located in PROJECT_ROOT/lib/code.jl can be accessed with this code:
root = dirname(dirname(#__FILE__))
Using dirname() twice seems pretty ugly. Is there a better way to do this? With Ruby, I would use this code:
root = File.expand_path('../', File.dirname(__FILE__))

Thanks for making me find out about:
"/"*relpath((#__FILE__)*"/../..","/")
According to ?relpath, it gives a path from the location of the second argument in the file-system, to the first argument. Is this better than the double dirname solution?
A variant of the same niceness is:
normpath(joinpath(#__FILE__,"..",".."))
Closest to Ruby equivalent might be:
realpath(dirname(#__FILE__)*"/..")

I like to use
module Foo
const PROJECT_ROOT = pkgdir(Foo)
end # module
where the definition of PROJECT_ROOT can also be replaced by
const PROJECT_ROOT = dirname(dirname(pathof(Foo)))
Or, you could use
const PROJECT_ROOT = pkdir(#__MODULE__)

I just use
const PROJECT_ROOT = #__DIR__
from inside my _init.jl file, which resides in the project root directory (next to the src directory) and gives you a canonical path.
I get my _init.jl files automatically executed when opening a Julia session from inside that directories by having
isfile("_init.jl") && include(joinpath(pwd(), "_init.jl"))
in my ~/.julia/config/startup.jl file. If you started Julia elsewhere, you have to include("_init.jl") it (or respective relative path) manually.

Related

How to convert from File to git path?

Im using jgit, log() command, and the addPath(String path) requires me to input a git compatible path, that is, a relative path like src/java/com/foo/Test.java. But what I got is a File object, that will have an absolute path of something like: c:\hello\irrelevant\myproject\src\java\com\foo\test.java.
How to convert from this to the git path? Is there some handy function within jgit itself? I cant find it....
You can use Path::relativize to get the relative path between the file to add and the work directory.
For example:
File workDir = git.getWorkTree(); // e.g. "/path/to/workdir"
File file = new File("/path/to/workdir/foo.txt");
Path relativePath = workDir.toPath().relativize(file.toPath());
assertEquals("foo.txt", relativePath.toString());

Find a given file recursively inside a directory

Find a given file recursively inside a dir. The code I tried is not showing any output, though I have a file C:\Users\anaveed\test\hoax\a.txt
Below the code
import glob
import os
os.chdir(r'C:\Users\anaveed\test')
for f in glob.iglob('a.txt', recursive=True):
print(f)
No output
Looks like you need.
import glob
for f in glob.iglob(r'C:\Users\anaveed\test\**\a.txt', recursive=True):
print(f)
This is another way of doing it:
import os
path = r'C:\Users\anaveed\test'
filename = 'a.txt'
for root, dirs, files in os.walk(path):
for name in files:
if name == filename:
print(os.path.join(root, name))
A couple of comments:
you do not need to use glob if you are not specifying wildcards, just use os.walk()
you do not need to move to a specific path to look for files therein, just save the path into a variable.
it would be even better to wrap this into a function (perhaps using a list comprehension).
the glob solution is typically faster.

Change output directory in "sbt-native-packager"

Sorry, I'm new to sbt and the "sbt-native-packager". What I need to do is to map whole directories to the .zip file and change the output path.
This how I've done my mapping of the directory:
mappings in Universal <++= (packageBin in Compile, baseDirectory ) map { (_, baseDirectory) =>
val dir = baseDirectory / "migrations"
(dir.***) pair relativeTo(dir.getParentFile)
}
The mapping works perfectly fine, but I need to have a specific folder structure in the resulting .zip file.
In this example this directory is mapped to ".../target/stage/universal/migrations" but I need it to be mapped into a folder "db" like this: ".../target/stage/db/universal/migrations"
Many thanks in advance!
For mapping complete directories there are some MappingHelpers you can use. Your code can be simplified to
mappings in Universal ++= directory(baseDirectory.value / "migrations")
Regarding your second question, how to change the output folder. The question is not quite correct, as it should be: "how to change the destination path of a mapping". The universal packaging is a bit special as the target ouput looks like the resulting package.
Native packager uses mappings (sequence of File -> String tuples) that define a file and the corresponding output path in the resulting package. So if you want to change
# current
./target/stage/universal/migrations
# expected
./target/stage/db/universal/migrations
I assume you want the migrations in your zip file in a db folder like this
/ # zip root
bin/ # start scripts
db/ # migrations go here
conf/ # configuration files
lib/ # jars
In order to accomplish this you have to change the destination string. This would look something like this ( not tested ):
mappings in Universal ++= contentOf(baseDirectory.value / "migrations").map {
case (file, dest) => file -> s"db/$dest"
}
cheers,
Muki

How to get relative path, not full path in map files with Closure Controller? `

I'm using Google Closure Compiler to minify my JS scripts: https://developers.google.com/closure/compiler/docs/gettingstarted_app?hl=en
The command I'm using is:
java -jar /home/user/compiler/compiler.jar --js $File::Find::name --create_source_map $File::Find::name.map --source_map_format=V3 --compilation_level=WHITESPACE_ONLY --js_output_file $minified --charset=Windows-1251 --output_wrapper '%output%\n//# sourceMappingURL=output.js.map'
Thats fine, apart from one thing - the .js.map file has the FULL path for the file, not the relative one:
"version":3,
"file":"/home/user/public_html/new_design/common37.min.js",
"lineCount":375,
....
I assume I can change this in the invocation of the compiler.jar script? Otherwise, I guess I will have to add some more code into my script (not something I want to do, if its possible "out of the box")
EDIT: I've done a little bit of a dirty hack in my Perl script:
# now open the map file one, and edit it to remove the full path.. needs to be relative
my $contents = File::Slurp::read_file("/home/user/public_html/$tmp.map");
$contents =~ s|/home/user/public_html||g;
File::Slurp::write_file("/home/user/public_html/$tmp.map",$contents);
That gets rid of the path info correctly. I've prefer if there were an option to use relative urls in the .map file (compared to the full path it currently puts in)
Thanks!
Specify sourcemap location transformations by using the --source_map_location_mapping flag. The flag expects a value formatted as:
--source_map_location_mapping=/filesystem/src/root|relative/source/root

about the file path

There are relative path and absolute path of the a file. But some of the writings confuse me sometimes:
/a/b/c.php //relative document root
./a/b/c.php //what does this mean? equals to '/a/b/c.php' or a/b/c.php?
a/b/c.php //relative to current directory
../a/b/c.php //parent folder relative to current directory
/../a/b/c.php //what does this mean? parent folder of document root?
Are there other ways of writing this?
Thank you.
Here's some basic directory symbol for you:
. (dot) is your current directory
.. (double-dot) is the parent of your current directory
~ (tilde) is your home directory.
/ (slash) if it present at first character, it usually is called root directory.
These all came from linux / unix terminology (CMIIW here).
Now, let's take a look at the implementation:
Let's say, you are on /home/username/
if you write something like this, the result is:
./wwwroot/somedir/ => /home/username/wwwroot/somedir/
../wwwroot/somedir/ => /home/wwwroot/somedir/
/../wwwroot/somedir/ => /wwwroot/somedir
You might get confused on example #3. If you put / in front of path info, it mean you are at the root directory. Therefore, if you write /../somedir/ it mean, you are pointing to /somedir/. Why? because root directory doesn't have parent.
. = current directory. So ./a/b/c.php would be equivalent to a/b/c.php.
/../a/b/c.php means go to the root directory, then up one, then directory a, then directory b, then c.php.

Resources