Jump to content

Ruby Script Workflow that requires a Gem


Recommended Posts

I have spent almost 8 hours on this and I know it's an easy fix.

 

I want to use this fork of the Sonos Ruby gem.

 

The problem: I don't know how to require/include the gem, neither locally as a cloned copy or as a regular "require gem_name". The script runs fine in IRB, but when I try to run it in a workflow the sound doesn't pause. 

 

I thought it might be because my system Ruby is old, but I use Mavericks so that shouldn't be a problem.

 

You can download the Workflow here from my Dropbox. And here's a SO question with more info, too.

 

 

Any help is really appreciated. I am ready to tear my hair out on this.

 

 

 

Link to post
Try building the gem and install it locally.
 
cd to the directory of the gem (I'm assuming you placed it in '/Users/Trevor/Documents/Development/code/alfred/sonos'). Then build the gem from the gemspec and install it:
 
gem build sonos.gemspec
sudo gem install sonos-0.3.4.gem

Now you should be able to require it as normal. If you plan on releasing the workflow for other people to use, you'll probably want to bundle it with the workflow rather than installing it on your system. Unfortunately, it's not obvious how to do that, so feel free to ask if you need any help with that.

Link to post

Thank you so much for the answer.

 

I get an error when I try to build the gem:

sudo gem install sonos-0.3.4.gem
Building native extensions.  This could take a while...
ERROR:  Error installing sonos-0.3.4.gem:
	ERROR: Failed to build gem native extension.

    /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/bin/ruby extconf.rb
Extracting libxml2-2.8.0.tar.gz into tmp/x86_64-apple-darwin13.0.2/ports/libxml2/2.8.0... OK
Running 'configure' for libxml2 2.8.0... OK
Running 'compile' for libxml2 2.8.0... ERROR, review 'tmp/x86_64-apple-darwin13.0.2/ports/libxml2/2.8.0/compile.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
/Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/mini_portile-0.5.2/lib/mini_portile.rb:265:in `block in execute': Failed to complete compile task (RuntimeError)
	from /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/mini_portile-0.5.2/lib/mini_portile.rb:257:in `chdir'
	from /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/mini_portile-0.5.2/lib/mini_portile.rb:257:in `execute'
	from /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/mini_portile-0.5.2/lib/mini_portile.rb:69:in `compile'
	from /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/mini_portile-0.5.2/lib/mini_portile.rb:109:in `cook'
	from extconf.rb:101:in `block in <main>'
	from extconf.rb:119:in `call'
	from extconf.rb:119:in `block in <main>'
	from extconf.rb:109:in `tap'
	from extconf.rb:109:in `<main>'


Gem files will remain installed in /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.1 for inspection.
Results logged to /Users/Trevor/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/gems/nokogiri-1.6.1/ext/nokogiri/gem_make.out

Is this an easy fix, or is there another way to include gems?

 

Thanks again in advance!

Link to post

This might be an easy fix, if you only want to use the workflow yourself (and since you’re including the full path in require, with your user account name and everything, it seems to be the case).
 
Problem is that Alfred does not use your shell’s environment, so the solution is to tell it to. If your shell can use it via irb but Alfred cannot, than it likely means it has some specific information to allow it to do it (specially relevant if you use something like rvm).

What you can do is (and for brevity I’ll post commands as if your default shell is bash — if it is zsh or something else, you should edit accordingly), instead of placing your ruby code directly inside Alfred’s window, tell it to use /bin/bash, and place your code inside an executable ruby file (let’s call it myscript.rb) inside the workflow’s directory. Use require as you’d normally do inside irb. Now, the code you tell Alfred to execute should be something like (again, adapt to your specific shell and startup file needs)

source "${HOME}/.bashrc"
./myscript.rb

This way you’ll be telling it to load your shell’s startup files, so it’ll see whatever it sees, and then run the ruby script with that information.

You can also install the gem and all it’s dependencies to the workflow’s directory, but since that would required a bit more setup, we can discuss that after if you’d rather do it that way.

Link to post
  • 8 months later...

Hey,

 

I'm also trying to install a gem in my Alfred workflow. I built  the gem from the gemspec and installed it.

 

My directory structure is like this:

 

user.workflow.8773xxx

--gem (directory)

   --Gemfile

   -- etc.

--main.rb

--info.plist

 

When i try to `require gem` in `main.rb`, I get this error:

 

```

[ERROR: alfred.workflow.input.scriptfilter] Code 1: /Users/andyjiang/.rbenv/versions/2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- muni.rb (LoadError)

from /Users/andyjiang/.rbenv/versions/2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'

from main.rb:4:in `<main>'

```

 

Any idea what I'm doing wrong? Do i need to tell Alfred to use a specific version of Ruby?

 

Thanks!

```

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...