Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next attempt to pull Agents into gems #1366

Merged
merged 6 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ gem 'geokit-rails', '~> 2.0.1'
gem 'httparty', '~> 0.13'
gem 'httmultiparty', '~> 0.3.16'
gem 'jquery-rails', '~> 3.1.3'
gem 'huginn_agent', '~> 0.4.0'
gem 'json', '~> 1.8.1'
gem 'jsonpath', '~> 0.5.6'
gem 'kaminari', '~> 0.16.1'
Expand Down Expand Up @@ -194,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
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ GEM
httparty (0.13.7)
json (~> 1.8)
multi_xml (>= 0.5.2)
huginn_agent (0.4.0)
thor
hypdf (1.0.10)
httmultiparty (~> 0.3)
httparty (~> 0.13)
Expand Down Expand Up @@ -629,6 +631,7 @@ DEPENDENCIES
hipchat (~> 1.2.0)
httmultiparty (~> 0.3.16)
httparty (~> 0.13)
huginn_agent (~> 0.4.0)
hypdf (~> 1.0.10)
jquery-rails (~> 3.1.3)
json (~> 1.8.1)
Expand Down
3 changes: 2 additions & 1 deletion config/initializers/requires.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'pp'
require 'pp'
HuginnAgent.require!
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be \s*(?:,\s*|\z) in case there are spaces after the comma?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not needed, spaces are not valid in the GEM_NAME expression they will be .scan will skip over them and start the next match when any character that is allowed in GEM_NAME is found. In the specs gem are separated with spaces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!

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|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe [^,]+? so it skips the separator, or does it not matter because of the ??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not matter, the ? makes it non-greedy, so it will never match ,.

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