NSIS relative directory to absolute - directory

In NSIS I need to convert path, that contains relative part to absolute
without "..\" or "../" .
For example:
StrCpy $0 "C:\Windows\System"
StrCpy $1 "$0\..\"
I need some function to return "C:\Windows" in this case

After some more searching I found build-in NSIS function
GetFullPathName output input
http://nsis.sourceforge.net/Reference/GetFullPathName

There is a function to do this:
http://nsis.sourceforge.net/Function_that_makes_absolute_paths_from_relative_ones

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());

Absolute path of the project root directory in 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.

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

Function to concatenate paths?

Is there an existing function to concatenate paths?
I know it is not that difficult to implement, but still... besides taking care of trailing / (or \) I would need to take care of proper OS path format detection (i.e. whether we write C:\dir\file or /dir/file).
As I said, I believe I know how to implement it; the question is: should I do it? Does the functionality already exist in existing R package?
Yes, file.path()
R> file.path("usr", "local", "lib")
[1] "usr/local/lib"
R>
There is also the equally useful system.path() for files in a package:
R> system.file("extdata", "date_time_zonespec.csv", package="RcppBDT")
[1] "/usr/local/lib/R/site-library/RcppBDT/extdata/date_time_zonespec.csv"
R>
which will get the file extdata/date_time_zonespec.csv irrespective of
where the package is installed, and
the OS
which is very handy. Lastly, there is also
R> .Platform$file.sep
[1] "/"
R>
if you insist on doing it manually.
In case anyone wants, this is my own function path.cat. Its functionality is comparable with Python's os.path.join with the extra sugar, that it interprets the ...
With this function, you can construct paths hierarchically, but unlike the file.path, you leave the user the ability to override the hierarchy by putting an absolute path. And as an added sugar, he can put the ".." wherever he likes in the path, with obvious meaning.
e.g.
path.cat("/home/user1","project/data","../data2") yelds /home/user1/project/data2
path.cat("/home/user1","project/data","/home/user2/data") yelds /home/user2/data
The function works only with slashes as path separator, which is fine, since R transparently translates them to backslashes on Windows machine.
library("iterators") # After writing this function I've learned, that iterators are very inefficient in R.
library("itertools")
#High-level function that inteligentely concatenates paths given in arguments
#The user interface is the same as for file.path, with the exception that it understands the path ".."
#and it can identify relative and absolute paths.
#Absolute paths starts comply with "^\/" or "^\d:\/" regexp.
#The concatenation starts from the last absolute path in arguments, or the first, if no absolute paths are given.
path.cat<-function(...)
{
elems<-list(...)
elems<-as.character(elems)
elems<-elems[elems!='' && !is.null(elems)]
relems<-rev(elems)
starts<-grep('^[/\\]',relems)[1]
if (!is.na(starts) && !is.null(starts))
{
relems<-relems[1:starts]
}
starts<-grep(':',relems,fixed=TRUE)
if (length(starts)==0){
starts=length(elems)-length(relems)+1
}else{
starts=length(elems)-starts[[1]]+1}
elems<-elems[starts:length(elems)]
path<-do.call(file.path,as.list(elems))
elems<-strsplit(path,'[/\\]',FALSE)[[1]]
it<-ihasNext(iter(elems))
out<-rep(NA,length(elems))
i<-1
while(hasNext(it))
{
item<-nextElem(it)
if(item=='..')
{
i<-i-1
} else if (item=='' & i!=1) {
#nothing
} else {
out[i]<-item
i<-i+1
}
}
do.call(file.path,as.list(out[1:i-1]))
}

QDir::exists returns true for invalid directory

I am having problem with checking the existence of the directory.
i take path from user input (e.g QLineEdit) and check for directory exists or not and if user specify the path "K:\" (k drive does not exists at all) my code becomes like this
QDir tmp("K:\\");
if(tmp.exists())
return true;
else
return false;
Ideally it should return false, as the Driver Letter "K" is not mapped, but unfortunately it returns true all the time, does anyone have any idea why is it like that? or
what is the correct method to check the existence of the directory?
Two suggestions:
Try tmp.makeAbsolute()
If that doesn't work, try substituting QDir tmp ("K:/"); (Unix forward slash instead of DOs/Windows backslash).
More beautiful way to check for drives is QDir::drives() method. Also you need to use platform independent directory seperator for QDir::exists().

Resources