How should I structure my ruby ​​gem command line service?

I am writing a ruby ​​stone that users can install and use a ruby ​​command line tool to interact with the service. You can start and stop the service (it will be removed from the child process).

I did a lot of research to make the best use of a network service like ØMQ / EventMachine, and I get how to create a Ruby stone that installs a binary file that you can use on the command line, but I'm struggling to install a good one code structure.

My command line utility will take various arguments (I will use Trollop ), and it will use different classes to do something, and use various other ruby ​​gems.

I'm not sure where I should put my class files and how to require them in my binary, so the paths are correct.

+4
source share
3 answers

In many ways, RubyGems will take care of this for you. You will need to include the executable in the files list and put it in executables in your gemspec. Usually for your executable in bin in your directory, for example:

 $ ls bin/ myapp.gemspec lib/ Rakefile $ ls bin bin/myapp 

Your gemspec would now look like this:

 Gem::Specification.new do |s| s.name = 'myapp' # whatever else is in your gemspec s.files = ["bin/myapp","lib/myapp.rb"] # or whatever other files you want s.executables = ["bin/todo"] end 

At this point, when users install your application through RubyGems, myapp will be on its way, and lib will be in your application download path, so your executable can simply start with:

 #!/usr/bin/env ruby require 'myapp' # whatever other requires 

The only problem is that during development, you cannot just make bin/myapp and run the application. Some developers manipulate by loading via $: or $LOAD_PATH , but this is considered a bad form.

If you use bundler, the easiest way to run the application locally is with bundle exec , for example. bundle exec bin/myapp . You can use the RUBYLIB environment RUBYLIB , for example, one at a time. RUBYLIB=lib bin/myapp , which will put lib in the boot path.

+9
source

You can create a gem project structure using the Bundler .

Short:

Install bundler

 $ gem install bundler 

Use the bundler to create a gem project

 $ bundle gem myapp $ cd myapp 

Add executable file

 $ mkdir bin $ cat > bin/mycommand << EOSCRIPT #!/usr/bin/env ruby require 'myapp' puts "Executing myapp" EOSCRIPT $ chmod +x bin/mycommand 

Set your gem

 $ rake install 

Run the script

 $ mycommand Executing mycommand 

Share your utility on rubygems.org

 $ rake release 

More documents on the website

+4
source

Since all gemstones are natural in nature, you can always take a look at some of the best examples. Using a gem builder such as a jeweler or hoe will also help you with some basic organizational speaking structure.

0
source

Source: https://habr.com/ru/post/1400658/


All Articles