Is there any bundler *after* hook? - ruby

I would like to have ctags generate a TAGS file of all my bundled gems or all the gems under the rvm gemset directory bundler installs its gems. Ideally, a bundle install or bundle update should generate a TAGS file at the last step using a ruby script I'll provide. Afterthat emacs joy.
Is there any kind of a bundler after hook I can use?

You could look at what Tim Pope does in his Hookup project:
https://github.com/tpope/hookup
I'd imagine it wouldn't be too hard to an an extra step after the bundler run.
Personally I just have a good old Makefile in my Ruby project:
.PHONY: tags
tags:
ETAGS=ctags
rm -rf TAGS
ctags -a -e -f TAGS --tag-relative -R app lib vendor
I have a shell script I run in the morning which sets up my dev environment which also runs make tags.

According to https://github.com/bundler/bundler/blob/dd1e11d8f8e869ffab4fc68d4854b27e1f486de4/lib/bundler/source/path.rb, there is the ability to run 'post_install' hooks. It uses meta-programming to deduce the method name, and the gem is supposed to implement that method. Will try and check if this works

My approach has been two pronged:
1) Put a rake task in place that generates tags for all code in the project as well as all required gems:
desc 'Create ctags'
task :tags do
system "ctags -R --language-force=ruby app config lib `rvm gemdir`/gems"
end
2) Using the excellent "foreman" gem (which I was using anyway) to run inotifywait and fire off the rake task if a file changes:
tags: while inotifywait -q -r -e MODIFY --exclude swp$ app/ config/ lib/ ; do bundle exec rake tags; done
If you are not using foreman you can of course just run that line without the first "tags:" part manually in a shell.

Related

Make shell does not inherit console shell environment variables

I am working on a Makefile to build an application where frontend and backend are completely separated in two different directories, like:
- myapp
|_be
|_fe
This is my Makefile:
.ONESHELL:
all: frontend backend
frontend:
cd ./fe && \
npm install && \
./node_modules/webpack/bin/webpack.js
backend: clean
cd ./be && \
gem install bundler && \
bundle install
clean:
rm -f ./fe/public/bundle.js && rm -rf ./fe/public/resources
webpack:
cd ./fe && \
.node_modules/webpack/bin/webpack.js
test:
cd ./fe && \
npm test
run:
cd ./be && \
rackup config.ru
The frontend is a React application and it is being built correctly when I type make frontend. No issues here.
But the backend is a Rack application written in Ruby. During the build process I need to enter its directory (be), make sure bundler is installed and run bundle install.
It happens that make creates its own shell to run the Makefile commands. And this shell does NOT inherit the environment variables of the console shell I am working. This turns out to be a problem to me, because I use rvm and rvm uses some environment variables to pick the correct gems. I use the .ruby-gemset and .ruby-version to create a private bundle to my application, not using the default gemset. This is necessary, because I have applications using many different Ruby versions and don't want to care about this.
This particular application uses JRuby 9.1.7.0 and then my Gemfile (inside be directory) has the following line:
ruby '2.3.1', :engine => 'jruby', :engine_version => '9.1.7.0'
I noticed all this issue because I got the following error when I typed make backend:
Your Ruby engine is ruby, but your Gemfile specified jruby
As far as I know rvm (and I know it quite well), this means it is trying to use my default Ruby version (which is set to Ruby 2.5.0) as engine, instead of the correct JRuby version.
Now, here is my question: Is there a way to force the make inner shell to use the same configuration of the console shell I am using?
This would solve the issue, because the console shell is already configured by rvm. I know it is because when I enter the be directory manually and run bundle install inside it, everything goes fine.
I used the .ONESHELL directive to force make to do this, but it seems it is not what I need.
Suggestions?
EDIT: make documentation says the -e flag may be used to force the make grab all console shell variables. I tried it and it didn't work.
EDIT: It all works if I do
rvm use --default jruby-9.1.7.0
before doing my make backend. But for many reasons it is not convenient to do this. Besides, I really believe this is a better way to do this. make is too smart not to allow something as simple as this.
I believe there are 2 problems:
1) when rvm creates its environment variables, it does not export them, so they are not visible to child shells, and
2) because the child shell is not started as a login shell, it does not see and process the rvm function declarations, and can therefore not execute rvm my-ruby-version.
Here are a couple of workarounds:
Before creating the child shell, do something like this:
export RVM_VERSION=`rvm current`
When starting the child shell, do so in login mode:
zsh --login
or
bash --login
Then, rvm should be a function, and this should work:
rvm $RVM_VERSION
First I added two lines to my backend: recipe, which now was reading
backend:
cd ./be && \
source $(HOME)/.rvm/scripts/rvm && \
rvm use jruby-9.1.7.0 && \
gem install bundler && \
bundle install
The idea was setting the correct Ruby (JRuby 9.1.7.0) without having to change it permanently at the console.
It happened this wouldn't work, because I wasn't choosing the shell I was using and it seems for some reason it wasn't /bin/bash and source wouldn't work. As a sequel, rvm wouldn't work either and I would receive that irritating message saying
rvm is not a function
Then I added
SHELL = /bin/bash
right after the .ONESHELL: directive.
And now everything worked fine!

Automatically run RSPec when plain-old Ruby (not Rails) files change

I am writing a Ruby script designed to run from the command line. The script has a corresponding RSpec file that verifies its functionality. The folder structure is:
./main_script.rb
./spec/main_script_spec.rb
Running rspec spec in the top level directory works as expected. Test results from the ./spec/main_script_spec.rb file are shown. I'd like to avoid running this manually every time I change either the main script file or the spec file. All my search results turn up things like guard which (as far as I can tell) are all designed for Rails apps.
How do I setup RSpec to watch for script or spec changes and run automatically with non-Rails Ruby code?
Like David said, Guard can be used to watch a wide variety of files and perform actions when those files are modified. It does not have to be used with a Rails app. I have set up something similar in the past using guard. Here is what I did:
Place the following in your Gemfile:
source 'https://rubygems.org'
gem 'guard'
gem 'guard-shell'
gem 'rspec'
gem 'rb-fsevent', '~> 0.9'
Then run:
$ bundle install
Create a Guardfile in your home directory with:
$ guard init
In the Guardfile, comment out the examples and add this:
guard :shell do
watch(%r{^*\.rb}) { `bundle exec rspec spec/` }
end
This tells guard to watch for modifications to any ruby files in the directory and execute the command bundle exec rspec spec/ when they change (the backticks are used to execute the command in the shell).
Then open up a new terminal window in your current directory and start a guard server to start watching the files:
$ bundle exec guard
Now your Rspec test suite should automatically run when you modify ruby files in the directory.
I used guard at the past, but now I'm using a combination of rspec focus feature and watch command.
It's very simple, just add an f before a describe of it block you want to run the test. So it would becomes fdescribe or fit block. This is the same as adding a tag :focus => true to your block.
We can then filter specs with the focus tag: rspec -t focus
Now, to keeping running theses specs (every 0.5 seconds) with focus tag we call it with watch command:
watch -n 0.5 rspec -t focus
But with that the output won't show colors. So, we need to use with unbuffer.
sudo apt-get install expect
With a little customization:
watch -n 0.5 --color 'unbuffer bundle exec rspec -t focus'
Since it's annoying to type this all, I made two alias at my ~/.bash_aliases file (your can use .bashrc as well):
alias focus="watch -n 0.5 --color 'unbuffer bundle exec rspec -t focus'"
alias focuss="bundle exec rspec -t focus"
Now I can type focus to keep running it, or for a single focus execution I type focuss
Guard can be used for plain old ruby. I generally have trouble with guard so I like to use watchr, another gem. With a few lines of code you can tell watchr to watch for changes to your files and run a command when they change.
For an example of guard with plain ruby, see the shuhari gem.
update on watchr gem: There appears to be an issue with this gem, perhaps with versions of ruby >= 2.0. The observr gem addresses this issue and works as expected in ruby 2.3.
I have used guard and the guard-rspec addition with great results, and I don't believe it to be Rails-specific. Other Ruby/RSpec projects should work equally well.
The guard documentation recommends the use of Bundler and to "always run Guard through Bundler to avoid errors". I.e. you install it through your Gemfile and always run it with bundle exec guard (or use rubygems-bundler to avoid the bundle exec part).

How to develop a Ruby GEM without having to install it first?

I'm developing a GEM that I've forked and I'm trying to modify it slightly for my app.
I'm finding it difficult and time consuming because for every change I make I have to
uninstall
build
re-install
run the app
Is there an easier way of which doesn't require repeating all steps above?
To use it in some app using bundler
If what you mean is for using it in a app to test it / use it, you can just specify a path for your gem or even point to a git repo in the Gemfile http://gembundler.com/gemfile.html
Like
gem "mygem", :path => "~/code/gems/mygem"
To use it as a standalone gem. i.e: like rspec or rake that can run outside of an app.
Just specify the path to your gem binary when running the gem command, like:
$ ~/path_to_my_gem/bin/mygem some args
If you can execute inside your gem directory (i.e: the command does not create files in the current directory, or needs any specific files from the current directory), just do this:
$ ./bin/mygem some args
Note that this last one is just for future reference, I think it's not applicable in the OP context.
use require_relative to include your files:
require_relative 'yourgem/yourclass'
This is the documentation for the function.

How to build and run a Ruby command-line tool found on github?

Hi cloned a simple app ( https://github.com/cfx/twix) on github that allows me to send Twitter messages from the console, but I'm not sure how to run it.
I now have a folder in my users/name director called Twix. Inside twix, I have these folders created by the app.
README Rakefile bin lib test
The readme doesn't provide a lot of instruction to get things started. It just tells you what to do after the program's running (see below).
Questions: What command do I use to get this started? What folder do I need to be in?
The README
Twix 0.0.1
Simple twitter client for console
Keys:
q - quit
t - write new message
If you want to post your twit, finish you message with !SEND
If you want abort and back to your feed, finish your message with !EXIT
new features soon
This program is packaged as a gem. The following command will install the gem (run it in the Twix directory):
rake package && gem install pkg/twix-0.0.1.gem
You can now run the program from anywhere with the twix command.
Edit
The first thing I saw in the repository was the Rakefile, meaning there were some rake tasks defined. When you find yourself in this case, running rake -T is your best bet as it will show you the rake tasks available. Its output was the following:
(in /home/benoit/code/clones/twix)
rake clobber_package # Remove package products
rake gem # Build the gem file twix-0.0.1.gem
rake package # Build all the packages
rake repackage # Force a rebuild of the package files
I picked the command that would be the most likely to build the gem: the package one. I guess the gem task would have worked as well.
Running rake package gave me the following output:
(in /home/benoit/code/clones/twix)
mkdir -p pkg
WARNING: no homepage specified
Successfully built RubyGem
Name: twix
Version: 0.0.1
File: twix-0.0.1.gem
mv twix-0.0.1.gem pkg/twix-0.0.1.gem
All that was left was to install the pkg/twix-0.0.1.gem created by the previous command:
gem install pkg/twix-0.0.1.gem
There's another case you could have encountered: the presence of a twix.gemspec in the root directory of the application. In this case, running:
gem build twix.gemspec
would have built the gem, the installation step remains the same.
ruby ./bin/twix
or
ruby -I lib ./bin/twix

what is the ideal directory structure of a ruby application

what is the ideal directory structure of a ruby application.
I want it to be deployed on EC2. It should have Gemfile and Rake file for executing rake tasks.
It should contain a lib file for utilities.
Should I just be using the command
newgem --simple test
Take a look at this RailsCast.
'bundle gem simple_test' works great for me.
$ bundle gem simple_test
create simple_test/Gemfile
create simple_test/Rakefile
create simple_test/.gitignore
create simple_test/simple_test.gemspec
create simple_test/lib/simple_test.rb
create simple_test/lib/simple_test/version.rb
Initializating git repo in /Users/sean/dev/foo/simple_test
$

Resources