I’m working on extension which using should be as easy as possible, and I have following issue.
I’m using external gems in my ruby code and when I move extension into Plugins folder for new setup of sketchup I’m receiving an error like this one:
Error: #<LoadError: cannot load such file – rubyXL.rb>
I know that i could just install gem through console but this extension will be used by non technical user, on many pc’s.
My idea is to keep locally gems in the extension folder. Is this possible? How to do this? Where i can download package with gem? If it’s not possible, do you have any other ideas for this issue?
If you can do something manually through a console, you can automate through code.
You can require ‘rubgems’, then try to require the gems that you need and if they are not installed (exception), you invoke something like Gems.install .... However this is synchronous and freezesSketchUp for a while.
See here:
Alternatively, there might be ways to bundle them, but ensure they are in your namespace.
ok so basically I could install gems in pre-loaded stage. Should i first check if they are existing?
Is there any other way to just keep ruby gems in extension and call them without installation? I could for example download all needed tree gems and then put it to proper sketchup folder.
@DanRathbun - My name is Tomasz, but I’m very new to Sketchup and I’m not yet on SketchUcation
So you can trap that LoadError exception in a rescue clause …
begin
require "zip"
rescue LoadError
begin
Gem::install("rubyzip")
rescue Gem::InstallError => error
puts "ERROR! RubyZip could not be installed."
puts error.message
else
require "zip"
end
end
Now, RubyZip is a weird variance that does not have the same load name as it’s gemname.
Normally gems names and the name of their loader is the same so you can “require” the same string.
The folder can be gotten from SketchUp Ruby thus …
ENV["GEM_HOME"]
… but you should be an expert at how rubygems are setup so as not to break the user’s installation.
This code works for me (Windows SU2017). But doesn’t explain the underlying fault.
begin
require "rubyXL"
puts '1'
rescue LoadError
begin
puts '2'
Gem::install('rubyXL')
puts '3'
rescue LoadError => error
puts '4'
puts "ERROR! RubyXL could not be installed."
puts error.message
end
puts '5'
require "rubyXL"
end
The error that is caught at step- 4 reads:
ERROR! RubyXL could not be installed.
Could not find 'nokogiri' (>= 1.4.4) among 1 total gem(s)
Checked in 'GEM_PATH=C:/Users/USER1/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Gems64', execute `gem env` for more information
All those puts slow down the execution, that may allow Windows to “catch up”.
Sometimes it transparently caches filesystem updates, and Ruby calls to load newly created or copied files fail because the OS reports that the file(s) do not exist.
I often add in calls to return a directory listing in hopes that it “kicks” WIndows into updating what it thinks are in the source directory.
Dir::chdir(gem_directory) { Dir["*.*"] }
As we’ve been discussing here,… RubyGems is very poorly integrated into SketchUp.
It is very likely that a custom Gem::Installer subclass is needed for SketchUp.
But there is another issue and that is Nokogiri stopped supporting Ruby versions less than 2.2, so if one is to install on Ruby 2.0 (SketchUp 2016) you would need to know the last Nokogiri version that supported Ruby 2.0 so you could specify that when you install the gem.
Checking out the CHANGELOG at the Nokogiri Git repo, I find that it is v1.6.8.1 that is the last to support Ruby 2.0 (SketchUp 2016), so at the console I got no errors …