I'm following this tutorial while creating my first Ruby gem. At the end of step 4 described there, there's a file my_gem-0.0.0.gem. For the time being, I'd like to keep this gem locally, so I tried putting it in a gems directory at the root of my rails project.
In the Gemfile I added this line:
gem 'my_gem', path: 'gems'
We use Docker, and when I try starting this container, I see my new gem is not found:
Bundler::GemNotFound: Could not find gem 'my_gem' in the source at `gems`
The source does not contain any versions of 'my_gem'
I tried adding/removing the gem extension and the -0.0.1 part both in the filename and in the Gemfile, but nothing worked. What's wrong here?
To works with Docker you need to clone the repository from github to your custom folder. In example below the steps to reproduce how I use custom path to edit gems in a separeted folder:
In my local machine I use a /gems folder inside my rails application folder: mkdir <my-app>/gems.
cd <my-app>/gems.
git clone <gem-repository-source>. Is necessary to clone because if you copy from other folder in your computer probably some files are missed.
Set in your Gemfile: gem '<gem-name>', path: '/bundle/custom_gems/<gem-name>'.
If you use docker-compose, you need to bind folders with volumes config, like below:
volumes:
- ./app/gems:/bundle/custom_gems
With this, your local folder (your machine) copy files inside ./app/gems to /bundle/custom_gems in Docker container.
Restart service.
If you NOT use docker-compose, you need add in Dockerfile some like:
ADD ./app/gems /bundle/custom_gems
Related
I have a gem in my Gemfile which reads from a custom source, and requires authentication -- as below:
source "https://gems.xxxxx.com/gems" do
gem "xyz", "~> 1.2.4"
end
I want to remove the dependancy on this provider and on the "remote" gem, incase something happens to the remote source.
Is there a way for me to fork/clone a private gem (I have authentication details), and host it myself?
Bundler allows you to configure credentials for any gem source, which allows you to avoid putting secrets into your Gemfile.
$ bundle config xxxxx.com username:password
See the documentation for the bundle config command. You can also use an ENV var:
export BUNDLE_GEMS__XXXXX__COM="username:password"
This is per domain (not sure how it works with subdomains). If you need to set credentials for a specific repo use:
bundle config https://gems.xxxxx.com/gems/awesome_gem.git username:password
Is there a way for me to fork/clone a private gem (I have authentication details), and host it myself?
Yes. Provided you can access a git repo then you should be able to clone it (practically - not legally). Forking is actually a workflow and not a specific command so the the details may vary depending on the host. Github allows you to fork private repo's which enables the fork & pull model - your fork might not actually be private by default and you may need to pay to be able to make it private. With bundler you can provide gems from either your local machine or any http(s) address.
I found the way this is possible, without necessarily cloning it:
Look in your Gemfile for the gem name.
Run: gem unpack --target vendor/gems
Take a look in the vendor/gems directory and see what the vendored gem directory name is for the next step.
Update the Gemfile to point the gem reference to the newly vendored gem path.
gem '', 'VERSION_NUMBER_GOES_HERE', path:
"vendor/gems/"
Run: bundle update
This will remove the reliance on the remote vendor and store it locally, although it will not receive version updates.
I am trying to build a command line ruby gem. I am using bundler to create the gem and install the gem locally. It generated the needed directories. I also was able to test that if I require my Gem I can use methods inside of it. I am trying to get the command line piece working now and can't seem to get it working. I want to be able to do something like
gemname command
Similar to how rspec works:
rspec test/whatever.rb
Any help on how to be able to execute through the command line would be great.
In order to declared the executables you have to just make a proper line in your yourgem.gemspec:
`git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
This line, along with an other useful line is generated by bundle gem yourgem command. Just execute it, and then fix the yourgem.gemspec according your needs. Put executables into bin/ folder of your gem, and all libraries, including the version, into lib/ folder.
The next step is to use the binary. When you are installing the gem into a system, the binary folder is automatically included into binary search path. So your gem is avaiable to execute from anywere. But when your gem isn't installed you are still able to simelate the case with a bundler's exec as follows:
bundle exec bin/your_exec
It picks the require librarires up from lib/ folder, and the executable will work properly.
To make sure that the executable will work, build the gem with gem build yourgem.gemspec, then install it with gem install yourgem.gem, and try.
I think the problem is that you've not commited your code yet.
Check if git ls-files -- bin/* shows you the script you want to execute.
When you commit your file, then git ls-files will return it and the spec will be able to load the script.
Checkout these blog posts:
http://robdodson.me/blog/2012/06/14/how-to-write-a-command-line-ruby-gem/
http://rubylearning.com/blog/2011/01/03/how-do-i-make-a-command-line-tool-in-ruby/
I'm not really sure what you've built so far but here are a few points of interest:
#!/usr/bin/env ruby for first line in the executable file (in the /bin folder).
chmod +x filname to make it executable
ARGV[0] Variables passed in are retrieved from ARGV[0]
s.executables << 'your_file' Add executable to gemspec
I figured this out at least for my specific issue. I just need to call out the bin command directly instead of that git ls loop. The top one works the bottom one doesn't for some reason.
spec.executables = ["toolshed"]
vs
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
I'm building a project and I need to override the default styles of bootstrap in the source code . So how do I access the source code ?
find the location of your gem files with gem env.
mine is
INSTALLATION DIRECTORY: /Users/xxx/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0
then the .less files are in /gems/twitter-bootstrap-rails-2.2.8/vendor/toolkit/twitter
I don't know what you want to do, but there are many variables for customizing bootstrap without changing any of the source, see - http://getbootstrap.com/2.3.2/customize.html#variables
just fork the gem in github, clone it to your local, modify it, then push it, after that if you want to use it in your rails project, just add your forked git source in your Gemfile like this:
gem "twitter-bootstrap-rails", git: "https://github.com/xxxx/twitter-bootstrap-rails.git"
Where are the gem files located ?
I'm new to rails and trying o understand how the whole gem functionality works.
My question is how can i follow a gem installation in order to confirm a gem is been installed ?
Where are the installed files located ?
From within your rails app, you can list out all of the gems being used, their versions, and the local path:
bundle show --paths
There's no reason to modify any of these files though. Configuration is typically done through an initializer in /app/initializers, but it depends on the gem being used.
If you need to modify something about the gem, you should fork it on Github and then reference the git location in your Gemfile until your pull request makes it back into the gem:
gem 'some_gem', '4.1.1', git: 'https://github.com/some_github_repo/some_gem.git'
So I have a gem in /vendor/bundle that needed to be fixed, and when I edit one of the files and then run bundle install it links up properly and all is good. But the problem is that when I deploy my project, the edits to my gem are lost because it runs bundle and saves the gems in a separate shared directory.
It seems that the solution to this is to make a vendor/static_gems directory, and place the gem inside of there, and link it like:
gem 'ruby-mysql', :path => "vendor/static_gems/ruby-mysql-2.9.3"
But I get an error that the gem is not found. is there something wrong with my path? Or something wrong with what I am placing there? When I copy this gem from the gems/ folder all that is in there is a lib/ directory with some .rb files.
Most probably you don't deploy your Gemfile.lock with your project, so Bundler runs without (or more probably) with an outdated one that links to the other gem path.
Be sure to update the Gemfile.lock with the rest of your project and you shouldn't need to worry about a static gem path or something like that.
I think I had the right idea, but I wasn't putting the whole gem into the static_gem folder, just the lib directory. It works great when the whole gem is in there.