Skip to content

Commit

Permalink
Allow specifing Agent gem in the Gemfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dsander committed Jun 20, 2016
1 parent 603f3e7 commit 811f9c2
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ AWS_ACCESS_KEY="your aws access key"
# Set AWS_SANDBOX to true if you're developing Huginn code.
AWS_SANDBOX=false

#########################
# Additional Agent gems #
#########################

# Agent gems can be added to Huginn by specifying them in a comma separated
# list, the gem version and arguments for the gem command are optional.
# When not providing a git(hub) repository the gem needs to be published to
# https://rubygems.org.
# Check http://bundler.io/v1.11/git.html for a list of valid arguments.
#
# Configuration examples:
#
# ADDITIONAL_GEMS=huginn_nlp_agents,test_agent
# ADDITIONAL_GEMS=huginn_nlp_agents(~> 0.2.1),test_agent
# ADDITIONAL_GEMS=huginn_nlp_agents(git: https://github.com/kreuzwerker/DKT.huginn_nlp_agents.git),test_agent
# ADDITIONAL_GEMS=huginn_nlp_agents(github: kreuzwerker/DKT.huginn_nlp_agents),test_agent
# ADDITIONAL_GEMS=huginn_nlp_agents(~> 0.2.1, git: https://github.com/kreuzwerker/DKT.huginn_nlp_agents.git),test_agent

########################
# Various Settings #
########################
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,7 @@ end
if_true(ENV['DATABASE_ADAPTER'].strip == 'mysql2') do
gem 'mysql2', '~> 0.3.20'
end

GemfileHelper.parse_each_agent_gem(ENV['ADDITIONAL_GEMS']) do |args|
gem *args
end
25 changes: 25 additions & 0 deletions lib/gemfile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,33 @@ def load_dotenv
)
end

GEM_NAME = '[A-Za-z0-9\.\-\_]+'.freeze
GEM_OPTIONS = '(.+?)\s*(?:,\s*(.+?)){0,1}'.freeze
GEM_SEPARATOR = '\s*(?:,|\z)'.freeze
GEM_REGULAR_EXPRESSION = /(#{GEM_NAME})(?:\(#{GEM_OPTIONS}\)){0,1}#{GEM_SEPARATOR}/

def parse_each_agent_gem(string)
return unless string
string.scan(GEM_REGULAR_EXPRESSION).each do |name, version, args|
if version =~ /\w+:/
args = "#{version},#{args}"
version = nil
end
yield [name, version, parse_gem_args(args)].compact
end
end

private

def parse_gem_args(args)
return nil unless args
options = {}
args.scan(/(\w+):\s*(.+?)#{GEM_SEPARATOR}/).each do |key, value|
options[key.to_sym] = value
end
options
end

def sanity_check(env)
return if ENV['CI'] == 'true' || !env.empty?
puts warning
Expand Down
48 changes: 48 additions & 0 deletions spec/lib/gemfile_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rails_helper'

describe GemfileHelper do
context 'parse_each_agent_gem' do
VALID_STRINGS = [
['huginn_nlp_agents(~> 0.2.1)', [
['huginn_nlp_agents', '~> 0.2.1']
]],
['huginn_nlp_agents(~> 0.2.1, git: http://github.com/dsander/huginn.git, branch: agents_in_gems)',
[['huginn_nlp_agents', '~> 0.2.1', git: 'http://github.com/dsander/huginn.git', branch: 'agents_in_gems']]
],
['huginn_nlp_agents(~> 0.2.1, git: http://github.com/dsander/huginn.git, ref: 2342asdab) , huginn_nlp_agents(~> 0.2.1)', [
['huginn_nlp_agents', '~> 0.2.1', git: 'http://github.com/dsander/huginn.git', ref: '2342asdab'],
['huginn_nlp_agents', '~> 0.2.1']
]],
['huginn_nlp_agents(~> 0.2.1, path: /tmp/test)', [
['huginn_nlp_agents', '~> 0.2.1', path: '/tmp/test']
]],
['huginn_nlp_agents', [
['huginn_nlp_agents']
]],
['huginn_nlp_agents, test(0.1), test2(github: test2/huginn_test)', [
['huginn_nlp_agents'],
['test', '0.1'],
['test2', github: 'test2/huginn_test']
]],
['huginn_nlp_agents(git: http://github.com/dsander/huginn.git, ref: 2342asdab)', [
['huginn_nlp_agents', git: 'http://github.com/dsander/huginn.git', ref: '2342asdab']
]],
]

it 'parses valid gem strings correctly' do
VALID_STRINGS.each do |string, outcomes|
GemfileHelper.parse_each_agent_gem(string) do |args|
expect(args).to eq(outcomes.shift)
end
end
end

it 'does nothing when nil is passed' do
expect { |b| GemfileHelper.parse_each_agent_gem(nil, &b) }.not_to yield_control
end

it 'does nothing when an empty string is passed' do
expect { |b| GemfileHelper.parse_each_agent_gem('', &b) }.not_to yield_control
end
end
end

0 comments on commit 811f9c2

Please sign in to comment.