Parsing direct and indirect Gems of Gemfile.lock file [duplicate] - ruby

This question already has answers here:
How to list ruby production only dependencies using Gemfile.lock and LockfileParser class
(2 answers)
Closed 1 year ago.
I'm trying to parse the following Gemfile.lock to include ALL Gems (direct and indirect dependencies) out of GEM specs:
GEM
remote: http://rubygems.org/
specs:
coderay (1.1.3)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
http-accept (1.7.0)
http-cookie (1.0.4)
domain_name (~> 0.5)
json (2.5.1)
method_source (1.0.0)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2021.0704)
netrc (0.11.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
yaml (0.1.1)
PLATFORMS
ruby
x86_64-darwin-20
DEPENDENCIES
json
rest-client
yaml
RUBY VERSION
ruby 2.7.3p183
BUNDLED WITH
2.2.23
But using my function I can only get the direct dependencies without their indirect ones.
e.g: direct dependency: http-cookie (1.0.4)... indirect dependency: domain_name (~> 0.5)
My code:
require 'bundler'
def gemlock(file_path)
file = file_path
gemlock_array = []
context = Bundler::LockfileParser.new(Bundler.read_file(file))
# Gems
context.specs.each do |spec|
name = spec.name
version = spec.version.to_s
gemlock_array << {'name' => name, 'version' => version}
end
puts gemlock_array
end
gemlock('Gemfile.lock')
I'm getting the following hash back:
{"name"=>"coderay", "version"=>"1.1.3"}
{"name"=>"domain_name", "version"=>"0.5.20190701"}
{"name"=>"http-accept", "version"=>"1.7.0"}
...
As you can see indirect dependencies were automatically ignored! But I still need to get it.
I don't have any experience with bundler and don't know how to solve this problem.
Any help in this matter would be much appreciated!
Thanks in advance.

indirect dependencies were automatically ignored
It would have helped if you'd included the full output. I just ran it myself, and you get:
[{"name"=>"coderay", "version"=>"1.1.3"},
{"name"=>"domain_name", "version"=>"0.5.20190701"},
{"name"=>"http-accept", "version"=>"1.7.0"},
{"name"=>"http-cookie", "version"=>"1.0.4"},
{"name"=>"json", "version"=>"2.5.1"},
{"name"=>"method_source", "version"=>"1.0.0"},
{"name"=>"mime-types", "version"=>"3.3.1"},
{"name"=>"mime-types-data", "version"=>"3.2021.0704"},
{"name"=>"netrc", "version"=>"0.11.0"},
{"name"=>"rest-client", "version"=>"2.1.0"},
{"name"=>"unf", "version"=>"0.1.4"}, # <------ !!!!!!!!!!!!
{"name"=>"unf_ext", "version"=>"0.0.7.7"},
{"name"=>"yaml", "version"=>"0.1.1"}]
So in summary, the indirect dependency of domain_name, i.e. unf, which is presumably what you were referring to, is included further down the list. Your code already works exactly as you intended.
One minor point, though: You can simplify the implementation a little by doing this:
gemlock_array = context.specs.map { |s| {'name' => s.name, 'version' => s.version.to_s} }
In ruby, assigning a temporary array and appending to it within a .each loop is usually sub-optimal. Use map instead, to just construct the array directly.

Related

tzinfo conflict with mongoid and rufus-scheduler

I try to use mongoid 3.x together with rufus-scheduler 2.x and im always getting a gem conflict over tzinfo.
Unable to activate mongoid-3.1.4, because tzinfo-1.0.1 conflicts with tzinfo (~> 0.3.22)
Looks like mongoid has some dependency on some legacy tzinfo version.
How can I solve this problem?
In your Gemfile, specify the version of TzInfo that suits Mongoid, before Mongoid and before rufus-scheduler (rufus-scheduler accepts any version of TzInfo).
source 'https://rubygems.org'
gem 'tzinfo', '0.3.22'
gem 'mongoid', '3.1.4'
gem 'rufus-scheduler'
UPDATE
Updated rufus-scheduler 2.0.x so that it accepts >= 0.3.22 (https://github.com/jmettraux/rufus-scheduler/commit/18c98010)
Unfortunately, can't seem to be able to push to RubyGems.org for now (it goes 500).
Until I find a workaround, you can point to that new rufus-scheduler with
gem 'rufus-scheduler', :git => 'git://github.com/jmettraux/rufus-scheduler.git', :branch => 'two'
UPDATE
Could push rufus-scheduler 2.0.24 to rubygems https://rubygems.org/gems/rufus-scheduler
It should be OK now.

Guard not detecting file changes until I interact with terminal

I'm trying to get Guard to detect file system changes while I'm working with SASS files. I want them to be compiled to CSS. I don't want to interact with the terminal every time I make a change though. It should just happen automatically. Right now, I have to press enter at the terminal to get it to compile.
When I do gem query --loacl I get this:
coderay (1.0.9)
ffi (1.8.1)
formatador (0.2.4)
guard (1.8.0)
guard-sass (1.1.0)
listen (1.0.3)
lumberjack (1.0.3)
method_source (0.8.1)
pry (0.9.12.1)
rb-fsevent (0.9.3)
rb-inotify (0.9.0)
rb-kqueue (0.2.0)
sass (3.2.9, 3.2.8)
slop (3.4.4)
thor (0.18.1)
I should note that this will eventually be a guard init on a Samba share; the directory is not on my physical machine, but I am testing on my local hard drive.
To initialize the guard project, I did guard init and then guard on my 'Portfolio' directory. This my directory structure:
-- Portfolio
-- sass
-- css
-- Guardfile (file)
And this is the contents of my guard file:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'sass', :input => 'sass', :output => 'css'
I read something about Ruby being compiled against libedit instead of readline. How can I check if this is the issue?
It appears the issue was in fact that Ruby wasn't using readline. I am not a Ruby expert, so I don't know for sure if this was the issue or not. I did follow the directions here and installed the Ruby Version Manager (RVM) and reinstalled Ruby to version 1.9.3 (although I think I could have used the latest 2.0.0) and I reinstalled the guard and guard-sass gems and now my filesystem changes are being detected automatically.

Ruby Builder conflict

I have a small ruby app that has been working. I am now getting a conflict error with builder. It looks like I updated my gems and now it is conflicting. I have it set in the Gemfile to use v2.1.2. But that is not working either. Thanks for any help you can give me.
Gemfile
gem 'builder', '2.1.2'
Bundler output
$ bundle
Using builder (2.1.2)
Using bundler (1.2.3)
$ bundle show builder
/Users/covard/.rvm/gems/ruby-1.9.3-p327/gems/builder-2.1.2
Conflict message
`raise_if_conflicts': Unable to activate actionpack-3.2.11, because rack-1.5.0 conflicts with rack (~> 1.4.0), builder-3.1.4 conflicts with builder (~> 3.0.0) (Gem::LoadError)
Figured out what my problem was even though I had my Gemfile I wasn't using it. Since rails handles that automatically I didn't know I had to add a require to use it. After adding this to my ruby files it worked perfectly.
require "bundler/setup"

Cucumber + Capybara file upload not working for me

I'm trying to test a simple file upload with Capybara. Here is my
Gemfile.lock
capybara (1.1.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
selenium-webdriver (~> 2.0)
xpath (~> 0.1.4)
and my selenium-webdriver version is 2.18. Here is my web_steps file (it's generated):
When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
with_scope(selector) do
attach_file(field, path)
end
end
This is my feature on the line to upload file:
Then I attach the file "features/resources/empty.file" to "file" within "#uploadForm"
Actually it ran fine and green on the line, but the input didn't pick up any file so the test failed after that because there was no file selected.
Here is my form:
%form#uploadForm{:action => "/upload", :method => "POST", :enctype => "multipart/form-data"}
%input{:type => "file", :name => "file", :id => "file"}
It's pretty basic, but I'm not sure why it's not working.
Toy,
I suggest to:
attach_file(field, File.expand_path(path))
Instead of using absolute path, especially if you collaborating.
edit: File.expand_path not File.extend_path
I just wanted to answer my question, the problem is I didn't use the full path. Somehow capybara doesn't recognise relative path.

Twitter entities no longer recognized in ruby script

I have a ruby script that scans each type of entity in a given tweet:
status = Twitter::Client.new.status(tweet[:id_str], {:include_entities => "1"})
status[:entities].each do |x|
#job on the entity
end
It was doing good until yesterday. Now I get NoMethodError: undefined method 'entities' for #<Twitter::Status:0x000001033e1800>
I can't figure it out since I've checked that status does include entities after the first line.
Any clues?
EDIT: turns out it's the new version of the twitter gem (v2.0.0) which is in cause. First I'd like to downgrade it to the last version working (v1.7.2), but I'm getting an annoying gem version error:
Bundler could not find compatible versions for gem "hashie":
In Gemfile:
topsy (~> 0.3.6) depends on
hashie (~> 1.0.0)
twitter (= 1.7.2) depends on
hashie (1.1.0)
How can I work it out?
If you need specific gem's version, you can forcely set it throught Gemfile:
gem "rack", "1.0.1"
gem "rails", ">=2.3.2"
In the end it was a conflict in the Gemfile:
gem 'topsy', '~> 0.3.6'
gem 'twitter', '1.7.2'
were requesting different versions of hashie, so I just deleted the version of topsy and it worked.

Resources