COMMUNITY

[lix] How to get the path to an installed library?

With haxelib package manager, we can get the path to an installed library by using the libpath command:

With lix, it doesn’t seem that there is an equivalent command. So how can we get the path to an installed library with it?

My use case: I’ve a package that contains web resources (JS files, images, whatever). I install it with:
lix install github:user/my_package (eventually: lix +lib my_package)

These resources need to be copied to the Web root of my local project. For this, I use a dedicated shell script (Bash, PowerShell, whatever).

Well, you can still call haxelib with lix, since it has a Haxelib shim. However, libpath doesn’t seem to work there yet, as it’s a somewhat recent addition - I previously ran into this as well.

Alternatively, you could call haxelib path (which is also what you had to do before libpath existed) and “parse” the output, like in this example where the logic is “the path is the line before the define for the library”.

Something to be aware of there is that haxelib path contains both contents of extraParams.hxml files and and dependencies:

>haxelib path hxnodejs
--macro allowPackage('sys')
--macro define('nodejs')
--macro _internal.SuppressDeprecated.run()
C:\projects\other\hxnodejs\src\
-D hxnodejs=12.1.0

>haxelib path json2object
C:\HaxeToolkit\haxe\lib\json2object/3,8,0/src/
-D json2object=3.8.0
C:\HaxeToolkit\haxe\lib\hxjsonast/1,0,1/src/
-D hxjsonast=1.0.1

Thanks for the suggestion @Gama11… but libpath returns the root path of the package while path (after parsing) returns the class path used by the package. As the class path is freely customizable, how can I easily determine the root path?
(OK, I could scan the directory and its parents to find the haxelib.json file and then use its classPath field to resolve the root path… that’s finally a lot for a simple shell script :sweat_smile:)

Oh, good point… I’m not sure that difference ever even occurred to me, as I usually only needed it for git commands, which scans upwards for the .git directory anyway.

Searching for haxelib.json should work, though I don’t think you also need to read it? The directory that contains it should be the library’s root directory by definition, right?

If things are getting too complex for a Shell script, perhaps consider a Haxe script instead? :wink: haxe --run is perfect for things like that, especially with module-level functions in 4.2.

Of course! And you’re right: it’s probably time to replace this shell script with a Haxe one.

How about putting a run script into my_package? When those are executed, the library root is Sys.getCwd() and the calling path (i.e. the project from which the library is used) is Sys.args()[0].

Yes, it’s also a solution… for the current case. However, I would have preferred something more generic (i.e. that works with any library).

While waiting to rewrite the code, I came up with this quick/dirty/naive workaround (in PowerShell, it does not properly escape the library name for the regex and does not take into account environment variables like HAXE_ROOT, HAXESHIM_ROOT or HAXE_LIBCACHE):

function Get-LibraryPath {
	param ([Parameter(Mandatory = $true, Position = 0)] [String] $library)
	$matchInfo = Select-String " into ($library/.*)" "haxe_libraries/$library.hxml"
	$prefix = $IsWindows ? $Env:APPDATA : $Env:HOME
	"$prefix/haxe/haxe_libraries/$($matchInfo.Matches[0].Groups[1])"
}

Write-Host (Get-LibraryPath tink_sql)
# [...]/AppData/Roaming/haxe/haxe_libraries/tink_sql/0.0.0-alpha.0/github/679e49bfeda117f509b991ab897e973797d4d708