I'm currently attempting to create a ruby gem out of a script and, while it works if I take the scripts and put them in the same directory, if I put them in the appropriate /bin and /lib directories after generating my gem structure, build the gem and then attempt to execute it I'm given the uninitialized constant error. Under /bin the relevant section in the file "cjp" is:
Cjb.new(crontabDir, logDir, allowedFrequency, printOnly, testRun).
find_violations autoFix
The class is defined in the file cjp.rb under /lib
class Cjb
def initialize(crontabDir, logDir, allowedFrequency, printOnly, testRun)
#crontabDir, #logDir, #allowedFrequency, #printOnly, #testRun =
crontabDir, logDir, allowedFrequency, printOnly, testRun
Despite this, the error I get is:
ERROR: uninitialized constant Cjb
I also noticed that if I attempt to manually execute the "cjp" script under /bin without the lib script being in the same directory that it also gives the same error. It appears that I'm missing something.
Any help on what I'm missing here to get this working would be appreciated.
bundle gem uses git to manage gem manifest.
If you do not see all your expected files when you run git ls-files in your gem project dir, then you need to add them.
To add individual files:
git add <filename>
To add everything (run from root of project):
git add .
This may have caught you out if you were not expecting to use git on your project. If you don't know git, and have time, it is really recommended to help you manage your project code. In fact so much so that bundle simply assumes that's what you want to do (although other code management tools are available)
Related
I'm writing a gem which needs a directory to store some config. I want to do this upon installation but I'm not sure how best to insert code into the installation process.
By adding the code to the Rakefile and adding spec.extensions = ["Rakefile"] to my gemspec, I've achieved what I want to do but this method means the installation process tells me I'm building native extensions; this is misleading.
Is there a built in way of doing this or via bundler? I'm assuming Bundler::GemHelper.install_tasks is close to what I'm after but I can't find helpful documentation.
EDIT: My current Rakefile.
It seems like the RubyGems designers intentionally left the option to run a script during installation... so I would say that the accepted way is to do the directory creation in the application.
The unix way is for the application to create the directory (vi and git do this).
FileUtils.mkdir_p is handy... it will save you from having to check if the directory is already there.
TL;DR:
Don't run bundle within an existing git repository. Weird things will happen without any error messages.
Original Question:
I've built a gem by adapting the steps in this tutorial:
http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/
As a final step, I've run gem build .gemspec
This succeeds, but when I install the gem I find the critical file, the one which contains my code, isn't in the gem. Another file in the same (lib) directory, "version.rb", does exist in the gem.
I don't know how to start debugging this...how does bundler/gem build decide which files to include in the gem?
Edit:
My workflow is:
gem build <project_name>.gemspec
gem unpack <project_name>
=> confirm file does not exist in <unpacked>/lib/
gem install <project name>
=> confirm file structure in ~/home/stefan/.rvm/... contains gem, but does not contain desired file
Edit 2 / Resolution:
I was finally able to get this working by committing all my code to a remote repository, creating a clean clone, and building the gem. The new gem included all the required files.
A bit of history...I originally created the code and committed it before thinking about making a gem (this is my first gem). I then used bundle inside the original repository, which didn't complain, but was probably the reason for the weirdness.
One of the things bundler did for you is start a local git repo to version-manage your gem code. Check that you have added the file in git
git add lib/gem_name/missing_file.rb
Bundler generated gems use git internally to track "membership" of source files for the gem. You can see this in the .gemspec where it uses backticks to call out to git and generate a file list:
gem.files = `git ls-files`.split($/)
Note this also means you should pay attention to what you list in .gitignore
I have a middleman server running fine on the trunk part of my repository.
when I try to start middleman on a branch, I get the following error:
Guard is now watching at '/Users/name.lastname/Foo/foo-html/branches/foo-html-1.2/src/main/resources/assets'
/Users/name.lastname/.rvm/gems/ruby-1.9.3-p327/gems/middleman-2.0.15.3/lib/middleman/core_extensions/features.rb:82:in `class_eval': cannot load such file -- helpers/application_helper (LoadError)
I use terminal to get to the relevant folder and use "bundle exec middleman". It looks like a path issue, something relative to the current directory when I run that command. What files should I look at to edit middleman's settings ?
that probably wont help anyone, but i had to comment out a few lines in config.rb, located in the root of the branch tree. like this :
# require 'helpers/application_helper'
# activate :application_helper
I've got a custom gem that has been working just fine with regards to bundling, building, distributing, & implementing. The gem is the core of a framework from which other gems are derived. Since most derived gems will have the same basic structure, I want to include a Ruby script in the bin path of the gem that can be used to basically copy files from a template folder into a new folder where the user will develop their own gem.
The problem I'm having is that the template folder has a gemspec file named $name$.gemspec with similarly named classes/modules in the file (e.g.: module $Name$), where the $name$ gets replaced with a name provided by the user.
Unfortunately, when I run bundle install from my gem's top-most path, I get an error:
There was a SyntaxError while evaluating $name$.gemspec:
C:/my_gem/template/$name$.gemspec:8: syntax error, unexpected tGVAR
gem.version = MyGem::$Name$::VERSION
It looks like Bundler is using the wrong Gemfile, even if I explicitly pass the Gemfile or path via one of the following:
bundle install --gemfile=Gemfile
bundle install --path=C:\my_gem
I also tried updating the gemspec line of my Gemfile to no avail:
gemspec name: 'my_gem'
Lastly, I've ensured that the template folder isn't even included in my_gem.gemspec, but that doesn't seem to matter:
gem.files = Dir.glob("lib/**/*") + %w(LICENSE.txt README.md)
Does anyone know why Bundler is trying to read the ./template/$name$.gemspec instead of ./my_gem.gemspec?
Inspecting the Bundler source, I may have spotted the culprit in lib/bundler/source/path.rb. There's GLOB used to find gemspecs in load_spec_files. The default glob is "{,*,*/*}.gemspec". This will find *.gemspec in the root directory of your gem or any directory one descendant from root (which will include your template dir).
If this is indeed the culprit, you could work around this by placing your template directory deeper in your gem's dir hierarchy or changing the name of the template file so it doesn't end in .gemspec. The Bundler::Source::Pathobject looks like it can take a different glob at initilization but I haven't dug deep enough to see if there's a viable way to specify this alternative glob in bundle execution via config or cmdline options.
I have git cloned a repo from Github, now I want to experiment with it, as in I want to poke around the code and mess with it. I've created a file test.rb that should load this gem, but I want to load my locally checked out version, what's the right way to do this?
Right now I'm just using a bunch of "require_relative 'the_gem_name/lib/file'", which feels wrong.
When you require 'foo' Ruby checks all the directories in the load path for a file foo.rb and loads the first one it finds. If no file named foo.rb is found, and you’re not using Rubygems, a LoadError is raised.
If you are using Rubygems (which is likely given that it is included in Ruby 1.9+), then instead of immediately raising a LoadError all the installed Gems are searched to see if one contains a file foo.rb. If such a Gem is found, then it is added to the load path and the file is loaded.
You can manipulate the load path yourself if you want to ensure a particular version of a library is used. Normally this isn’t something that’s recommended, but this is the kind of situation that you’d want to do it.
There are two ways of adding directories to the load path. First you can do it in the actual code, using the $LOAD_PATH (or $:) global variable:
$LOAD_PATH.unshift '/path/to/the/gems/lib/'
require 'the_gem'
Note that you normally want to add the lib dir of the gem, not the top level dir of the gem (actually this can vary depending on the actual Gem, and it’s possible to need to add more than one dir, but lib is the norm).
The other way is to use the -I command line switch to the ruby executable:
$ ruby -I/path/to/the/gems/lib/ test.rb
This way might be a bit cleaner, as normally you don’t want to be messing with the load path from inside your code, but if you’re just testing the library it probably doesn’t matter much.
Following apneadiving's suggestion in the comments, I created a Gemfile and added this line
source "http://rubygems.org"
gem 'gem_name', path: '~/path/to/gem/source/folder'
Then bundle install, and bundle exec ruby test.rb and it worked.