bin stub - missing dependencies from gem (executing rake tasks within gem)
Asked Answered
A

1

2

I've written a gem elastic-beanstalk which is to be used inside a rails project file structure, as well as in a standalone CI environment where the rails dir and files are not available (without unzipping etc). i.e. the Bamboo build process running eb:package will yield one primary artifact app.zip where the deploy plan at a later time and perhaps on another agent can take over and execute eb:deploy.

The goal

This is all running fine when inside a rails project structure, so my goal here is to also get this running for the standalone CI environment.

Given

an empty dir (CI environment) with just the app.zip, eb.yml, binstubs created, and the gem is available

When

I run elastic-beanstalk eb:deploy

Then

it should run the equivalent of rake eb:deploy using this gem's dependencies and lib files.

Update - Bin Stub

It appears that a bin stub may be what I'm looking for. Exploring another SO post, I have tried (to no avail so far) bin/elastic-beanstalk:

gem_dir = File.expand_path('..',File.dirname(__FILE__))
$LOAD_PATH.unshift gem_dir# Look in gem directory for resources first.

lib = File.expand_path('lib', gem_dir)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'elastic/beanstalk'
require 'rake'
require 'pp'

pwd=Dir.pwd
Dir.chdir("#{gem_dir}/bin") # We'll load rakefile from the gem's bin dir.
Rake.application.init
Rake.application.load_rakefile
Dir.chdir(pwd) # Revert to original pwd for any path args passed to task.

Rake.application.invoke_task(ARGV[0])

So this runs, but is still failing with the same dependency problem I began with undefined method 'safe_load_file' for Psych:Module (NoMethodError). While I think a binstub is the way to go:

How can I resolve the dependency problem with the bin stub?

Azerbaijan answered 20/8, 2013 at 16:55 Comment(0)
A
3

Ultimately, I needed a call to Bundler.setup to resolve dependencies.

After cleanup, the file below is the only thing I needed to call a rake task with a bin file inside a gem (external bin stub utilizes this file):

bin/elastic-beanstalk

#!/usr/bin/env ruby
require 'rake'
require 'bundler'

raise "Bundler is required.  Please install bundler with 'gem install bundler'" unless defined?(Bundler)

#
# Example:
#
#   elastic-beanstalk eb:show_config
#   elastic-beanstalk eb:show_config[1.1.1]
#

# init dependencies
Bundler.setup

# init rake
Rake.application.init

# load the rake tasks
gem_dir = File.expand_path('..',File.dirname(__FILE__))
load "#{gem_dir}/lib/elastic/beanstalk/tasks/eb.rake"

# invoke the given task
Rake.application.invoke_task(ARGV[0])
Azerbaijan answered 21/8, 2013 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.