fredrikekre / jlpkg

A command line interface (CLI) for Pkg, Julia's package manager.
MIT License
92 stars 4 forks source link

Figure out how to embed the script in the executable on Windows #2

Open fredrikekre opened 5 years ago

fredrikekre commented 5 years ago

Otherwise stuff break when the source code of jlpkg is removed.

heetbeet commented 3 years ago

You can use this to make a template for jlpkg: jlpkg.zip

The main trick is to add the following to the top of a .cmd file:

@eval 1 #= 2>nul 
@call julia "%~dp0%~n0.cmd" %*
@exit /b %errorlevel%
=#
  1. @eval 1 #= 2>nul
    For cmd the @ sign suppresses this line to not be echod to the console and the 2>nul part suppresses any errors. Otherwise you would see 'eval' is not recognized as an internal or external command
    For julia this line is a macro that basically does nothing nothing, following with #= as the start of a multi line comment block.

  2. @call julia "%~dp0%~n0.cmd" %*
    For cmd this will run the current file under julia (note that the file extension still needs to be manually stated, since cmd cannot infer this information if the script is called like jlpkg and not jlpkg.cmd). Julia will simply skip over the cmd header and start executing the julia part of the file.

  3. @exit /b %errorlevel%
    For cmd this will return the error code of the julia run, and exit without arriving at the julia part of the file.
heetbeet commented 3 years ago

Just replace the @call julia "%~dp0%~n0.cmd" %* part with the following juxtaposed string when generating the cmd file: "@call $(julia) $(join(julia_flags, ' ')) $(abspath(@__DIR__, "cli.jl")) %*"

Similarly to what you have here https://github.com/fredrikekre/jlpkg/blob/aa44f0fa88b2450d73365f4a797c4cd7d6d301c4/src/jlpkg.jl#L43

heetbeet commented 3 years ago

https://github.com/fredrikekre/jlpkg/pull/20