I have written a shell program in Ruby. Now I want to add it to my bin directory so that I can call the program by running $ my-rb-prog ....
First I tried to symlink my file into /usr/bin but then it said that it couldn't load the modules that I required.
On my second try, I tried to build a gem out of my project which worked fine, but I can still not access my shell program. After that I installed the gem. Here's what my gemspec looks like:
# -*- encoding: utf-8 -*-
$:.unshift(File.join(File.dirname(__FILE__), "/lib"))
require 'webcheck'
Gem::Specification.new do |s|
s.name = "webcheck"
s.version = WebCheck::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Victor Jonsson"]
s.email = ["kontakt#victorjonsson.se"]
s.homepage = "http://victorjonsson.se"
s.summary = %q{Check your website man!}
s.description = %q{Just check it!}
s.required_ruby_version = '>= 1.9.3'
s.add_dependency "httparty", "~> 0.12.0"
s.post_install_message = "Just check it!"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end
I thought that I would get access to my shell program, which is a Ruby file located in the bin directory inside my project, after that I had installed the gem but it clearly isn't that easy.
This is my first day of coding Ruby if you can't tell.
First, add a "she-bang" string as first line of your file. It will allow the shell to run the file. It should be:
#!/usr/bin/env ruby
Then give execution permissions to the file:
$ chmod +x your_file_name.rb
Now you can run your application:
./your_file_name.rb
Also you can add the path to the directory with this script to the PATH variable and run the application from anywhere you want.
# You may do this in ~/.bashrc file
PATH=$PATH:path/to/dir/with/script/
Don't forget to add #!/usr/bin/env ruby to the top of your Ruby script.
"Making a Ruby Script Executable" is a really good tutorial on making your executable available system wide, without the use of a gem.
Related
So, I am trying to run the test but I am getting an error says.
Aruba::LaunchError:Command "seedly-calculator.rb" not found in PATH-variable
-seedly-calculator
-bin
-src
-seedly-calculator.rb
I have tried to change the path in rake file but it doesn't work.
My seedly-calculator.rb file is in the root directory.
require "rspec/core/rake_task"
namespace :spec do
desc "Run the functional suite against the CLI"
RSpec::Core::RakeTask.new(:functional, [] => [:set_path])
task :set_path do
project_bin_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', 'bin')
ENV['PATH'] = project_bin_dir + ':'+ ENV['PATH']
end
end
it shows error like:
Failure/Error: let(:command) { run "seedly-calculator.rb" }
Aruba::LaunchError:
Command "seedly-calculator.rb" not found in PATH-variable "/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/exe:/Users/bilaltariq/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/../bin:/Users/bilaltariq/.rbenv/versions/2.6.2/bin:/usr/local/Cellar/rbenv/1.1.1/libexec:/Users/bilaltariq/.rbenv/shims:/Users/bilaltariq/.asdf/shims:/Users/bilaltariq/.asdf/bin:/usr/local/bin:/Users/bilaltariq/.bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin".
I expect it to hit the file so i can write some test.
am i doing something wrong?
require 'spec_helper'
RSpec.describe 'Command Validation', type: :aruba do
let(:command) { run "seedly-calculator.rb" }
it "wrong/missing arguments" do
command.write("lookup\n")
stop_all_commands
expect(command.output).to end_with("Missing bank_name argument.\n")
end
end
seedly-calculator.rb:
#!/usr/bin/env ruby
# Complete bin/setup so that after it is
# run, ruby seedly-calculator.rb can be used to launch
# it.
# frozen_string_literal: true
require_relative './src/runner'
if !ARGV.length.zero?
input = ARGV
Runner.new.send('process_input', input)
else
puts "Arguments required!."
end
Update
To run a ruby script using run you need to make sure your ruby script is executable and contains a shebang so your system knows to run it with ruby. Here's example from this starter example
#!/usr/bin/env ruby
file = ARGV[0]
if file.nil? || file.empty?
abort "aruba-test-cli [file]: Filename is missing"
elsif !File.exist? file
abort "aruba-test-cli [file]: File does not exist"
end
puts File.read(file).chomp
So in your case you'll need to add this to the first line of your seedly-calculator.rb file
#!/usr/bin/env ruby
Then run this from command line to make it executable.
chmod +x #!/usr/bin/env ruby
I made a simple example forked off the one I reffed above. See this commit
Rspec convention is that it should match the same file structure of your project. It is not a good idea to set PATH manually.
Rake tasks are normally put in a tasks folder so you should have in project root a tasks folder
my_project/tasks/something.rake
Then you should have a spec folder that matches
my_project/spec/tasks/something_spec.rb
Then you should be able to get rid of task :set_path do end block and just run the spec without that.
You should also have a Gemfile to load your gems, run bundle install then invoke your test with
bundle exec rspec spec/tasks/sometask_spec.rb
Running an installed gem is much slower than running its local source counterpart.
Installed gem:
$ time wmctile switch_to Thunderbird
real 0m0.682s
user 0m0.491s
sys 0m0.091s
Local source:
$ time ./work/wmctile/bin/wmctile switch_to Thunderbird
real 0m0.197s
user 0m0.118s
sys 0m0.064s
Why? Could it be because of RVM, or is this a "feature" of Ruby gems in general? Is there a way to speed it up?
This is the generated bin file:
$ which wmctile
/home/some_user_name/.rvm/gems/ruby-2.1.2/bin/wmctile
$ cat $( which wmctile )
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'wmctile' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
gem 'wmctile', version
load Gem.bin_path('wmctile', 'wmctile', version)
RVM puts the proper directories for your Ruby version and gemset in the path whenever the RVM Ruby is set. My PATH begins with this:
/Users/kbennett/.rvm/gems/ruby-2.3.0/bin
/Users/kbennett/.rvm/gems/ruby-2.3.0#global/bin
/Users/kbennett/.rvm/rubies/ruby-2.3.0/bin
/Users/kbennett/.rvm/bin
So, I think it's the OS and not Ruby itself that is responsible for the delay. You could test this by putting a simple shell script file in that gem bin directory, and calling it with and without its absolute location to see if you get the same difference.
I'm following a tutorial to create ruby gems http://guides.rubygems.org/make-your-own-gem/
The tutorial tells me to create a ruby file like this:
% cat lib/hola.rb
class Hola
def self.hi
puts "Hello world!"
end
end
Then a gemspec file like this:
% cat hola.gemspec
Gem::Specification.new do |s|
s.name = 'hola'
s.version = '0.0.0'
s.date = '2010-04-28'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Nick Quaranto"]
s.email = 'nick#quaran.to'
s.files = ["lib/hola.rb"]
s.homepage =
'http://rubygems.org/gems/hola'
end
When I gem build hola.gemspec I get this error:
Invalid gemspec in [hola.gemspec]: hola.gemspec:1: syntax error, unexpected tIDENTIFIER, expecting $end
% cat hola.gemspec
^
ERROR: Error loading gemspec. Aborting.
Now his code on Github will not build without the Rakefile.
So how can I make this work? Do I need to add a Rakefile or is there something wrong with the code?
Your error indicates that your file has the line % cat hola.gemspec in it literally. This line in the example isn't intended to be part of the file itself; it's the Unix command the author used to print the contents of the file. Remove that line and the similar line from the other file and you should be OK to move to the next step.
The first line, % cat lib/hola.rb is not meant to be part of the file, but rather the whole thing is command-line output. cat is a command used to output the contents of a file, and things like % and $ are often used to denote the start of a command. So, remove the first line from the file.
I have a simple extension in ext/Q/flagvalue.c
My ext/Q/extconfig.rb looks like this:
require 'mkmf'
create_makefile('Q/flagvalue')
The task in Rakefile is set-up just so:
Rake::ExtensionTask.new("Q") do |extension|
extension.lib_dir = 'lib/Q'
end
when I rake build, i get the following output:
mkdir -p tmp/x86_64-linux/Q/1.9.3
cd tmp/x86_64-linux/Q/1.9.3
/usr/local/rvm/rubies/ruby-1.9.3-p286/bin/ruby -I. ../../../../ext/Q/extconf.rb
creating Makefile
cd -
cd tmp/x86_64-linux/Q/1.9.3
make
compiling ../../../../ext/Q/flagvalue.c
linking shared-object Q/flagvalue.so
cd -
install -c tmp/x86_64-linux/Q/1.9.3/Q.so lib/Q/Q.so
rake aborted!
No such file or directory - tmp/x86_64-linux/Q/1.9.3/Q.so
So it seems like the compiler compiles and links flagvalue.so and the installer tries to install non-existent Q.so… where does this error come from and what can I do about it?
Try this in your Rakefile:
Rake::ExtensionTask.new 'flagvalue' do |extension|
extension.ext_dir = 'ext/Q'
extension.lib_dir = 'lib/Q'
end
This does entail some duplication, as the Rake tasks doesn’t know what you specify as your target in extconf (i.e. it doesn’t know about the Q directory), so you have to specify again. This also means there won’t be a Q directory in the structure the the task creates under the tmp dir in your project, but that’s probably not a problem.
Ok, after some digging (and some vague guessing :-Δ) I found the solution:
I just got some code for my gemspec (from Writehack.com), that was so:
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
the problem with this method is that you'd have to have the just-to compile-binaries already in your repository. the correct way was to get the *.c-files from ext-directory and rename them to *.so like this:
s.executables = s.files.grep(%r{^ext/.*c$}).map{ |f| File.basename(f, '.c') + '.so'}
s.bindir = 'bin'
and also adding a bindir to Rakefile's ExtensionTask and make it get its files from spec:
spec = Gem::Specification.load('Q.gemspec')
spec.executables.each do |f|
Rake::ExtensionTask.new('Q', spec) do |ext|
ext.name = f.gsub(/\.so$/,'')
ext.tmp_dir = 'tmp'
ext.lib_dir = 'bin'
end
end
:-Δ
What is the best way to manage the require paths in a ruby program?
Let me give a basic example, consider a structure like:
\MyProgram
\MyProgram\src\myclass.rb
\MyProgram\test\mytest.rb
If in my test i use require '../src/myclass' then I can only call the test from \MyProgram\test folder, but I want to be able to call it from any path!
The solution I came up with is to define in all source files the following line:
ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(ROOT) and then always use require "#{ROOT}/src/myclass"
Is there a better way to do it?
As of Ruby 1.9 you can use require_relative to do this:
require_relative '../src/myclass'
If you need this for earlier versions you can get it from the extensions gem as per this SO comment.
Here is a slightly modified way to do it:
$LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "src"))
By prepending the path to your source to $LOAD_PATH (aka $:) you don't have to supply the root etc. explicitly when you require your code i.e. require 'myclass'
The same, less noisy IMHO:
$:.unshift File.expand_path("../../src", __FILE__)
require 'myclass'
or just
require File.expand_path "../../src/myclass", __FILE__
Tested with ruby 1.8.7 and 1.9.0 on (Debian) Linux - please tell me if it works on Windows, too.
Why a simpler method (eg. 'use', 'require_relative', or sg like this) isn't built into the standard lib? UPDATE: require_relative is there since 1.9.x
Pathname(__FILE__).dirname.realpath
provides a the absolute path in a dynamic way.
Use following code to require all "rb" files in specific folder (=> Ruby 1.9):
path='../specific_folder/' # relative path from current file to required folder
Dir[File.dirname(__FILE__) + '/'+path+'*.rb'].each do |file|
require_relative path+File.basename(file) # require all files with .rb extension in this folder
end
sris's answer is the standard approach.
Another way would be to package your code as a gem. Then rubygems will take care of making sure your library files are in your path.
This is what I ended up with - a Ruby version of a setenv shell script:
# Read application config
$hConf, $fConf = {}, File.expand_path("../config.rb", __FILE__)
$hConf = File.open($fConf) {|f| eval(f.read)} if File.exist? $fConf
# Application classpath
$: << ($hConf[:appRoot] || File.expand_path("../bin/app", __FILE__))
# Ruby libs
$lib = ($hConf[:rubyLib] || File.expand_path("../bin/lib", __FILE__))
($: << [$lib]).flatten! # lib is string or array, standardize
Then I just need to make sure that this script is called once before anything else, and don't need to touch the individual source files.
I put some options inside a config file, like the location of external (non-gem) libraries:
# Site- and server specific config - location of DB, tmp files etc.
{
:webRoot => "/srv/www/myapp/data",
:rubyLib => "/somewhere/lib",
:tmpDir => "/tmp/myapp"
}
This has been working well for me, and I can reuse the setenv script in multiple projects just by changing the parameters in the config file. A much better alternative than shell scripts, IMO.