Deploy a shell script with Ruby gem and install in bin directory

后端 未结 1 828
不思量自难忘°
不思量自难忘° 2021-01-04 19:26

I wish to place a shell script in my gem\'s bin dir, along with other Ruby programs that belong to the package. I wish to have this shell script installed in the bin directo

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 20:18

    This issue is described here: https://github.com/rubygems/rubygems/issues/88

    If the gem you're developing is intended only for your own use, you can simply install it with

    gem install --no-wrapper my_gem
    

    I think you'd best write a ruby script which runs your bash script. Here's an example on how to do that:


    bin/test_gem

    #!/usr/bin/env ruby
    
    bin_dir = File.expand_path(File.dirname(__FILE__))
    shell_script_path = File.join(bin_dir, 'test_gem.sh')
    
    `#{shell_script_path}`
    

    bin/test_gem.sh

    #!/bin/sh
    
    echo "Hello World!"
    

    test_gem.gemspec

    spec.files = [
      # ...
      'bin/test_gem', 'bin/test_gem.sh'
    ]
    
    # ...
    
    spec.executables = ['test_gem']
    

    NOTE: Don't forget to set both files in the bin folder to executable!

    Note that while test_gem.sh is registered with the files Rubygems command, it's not registered as executables: it will just be placed in the installed gem's dir but not wrapped/shimmed.

    If you install your gem (and run rbenv rehash if necessary), calling test_gem will result in the ruby script executing your shell script.

    0 讨论(0)
提交回复
热议问题