rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.12k stars 553 forks source link

App name shows as <script> when cli jruby app is packaged into jar file using warbler #742

Open hmistry opened 3 years ago

hmistry commented 3 years ago

Problem

I have a CLI app using Thor with jruby and created a compiled jar file using warbler. When viewing the CLI help, the app name shows as <script> instead of my_app_name when running the jar file using java -jar my_app_name.jar help. If I run my_app_name ruby executable directly under jruby using bin/my_app_name, then the app name shows correctly in the help text. See below.

Root Cause

The root cause is because $PROGRAM_NAME is set to <script> when running the jar file so the following method doesn't return the correct app file name as expected. https://github.com/erikhuda/thor/blob/master/lib/thor/base.rb#L674 $PROGRAM_NAME is correctly set when running directly in jruby.

Tried setting package_name but that doesn't work because it only changes Commands: to my_app_name commands:.

Possible fix

One option is to use __FILE__ instead of $PROGRAM_NAME.

Example Test Case

# bin/my_app
class MyApp < Thor
  desc "hammer", "Thor's hammer"
  def hammer()
    "Thor's hammer"
  end
end
# run in project folder -> works as expected
$ bin/my_app help
    Commands:
       my_app hammer
       my_app help [COMMAND]

# package into jar file
$ warble compiled jar
  -> creates my_app.jar

# run jar file -> doesn't work as expected
$ java -jar my_app.jar help
    Commands:
       <script> hammer
       <script> help [COMMAND]

Ruby: jruby v9.2.13 Thor: 1.0.1 Warbler: 2.0.5