Allow specifing Agent gem in the Gemfile

Dominik Sander 8 years ago
parent
commit
811f9c27d1
4 changed files with 95 additions and 0 deletions
  1. 18 0
      .env.example
  2. 4 0
      Gemfile
  3. 25 0
      lib/gemfile_helper.rb
  4. 48 0
      spec/lib/gemfile_helper_spec.rb

+ 18 - 0
.env.example

@@ -162,6 +162,24 @@ AWS_ACCESS_KEY="your aws access key"
162 162
 # Set AWS_SANDBOX to true if you're developing Huginn code.
163 163
 AWS_SANDBOX=false
164 164
 
165
+#########################
166
+# Additional Agent gems #
167
+#########################
168
+
169
+# Agent gems can be added to Huginn by specifying them in a comma separated
170
+# list, the gem version and arguments for the gem command are optional.
171
+# When not providing a git(hub) repository the gem needs to be published to
172
+# https://rubygems.org.
173
+# Check http://bundler.io/v1.11/git.html for a list of valid arguments.
174
+#
175
+# Configuration examples:
176
+#
177
+# ADDITIONAL_GEMS=huginn_nlp_agents,test_agent
178
+# ADDITIONAL_GEMS=huginn_nlp_agents(~> 0.2.1),test_agent
179
+# ADDITIONAL_GEMS=huginn_nlp_agents(git: https://github.com/kreuzwerker/DKT.huginn_nlp_agents.git),test_agent
180
+# ADDITIONAL_GEMS=huginn_nlp_agents(github: kreuzwerker/DKT.huginn_nlp_agents),test_agent
181
+# ADDITIONAL_GEMS=huginn_nlp_agents(~> 0.2.1, git: https://github.com/kreuzwerker/DKT.huginn_nlp_agents.git),test_agent
182
+
165 183
 ########################
166 184
 #   Various Settings   #
167 185
 ########################

+ 4 - 0
Gemfile

@@ -195,3 +195,7 @@ end
195 195
 if_true(ENV['DATABASE_ADAPTER'].strip == 'mysql2') do
196 196
   gem 'mysql2', '~> 0.3.20'
197 197
 end
198
+
199
+GemfileHelper.parse_each_agent_gem(ENV['ADDITIONAL_GEMS']) do |args|
200
+  gem *args
201
+end

+ 25 - 0
lib/gemfile_helper.rb

@@ -19,8 +19,33 @@ class GemfileHelper
19 19
                               )
20 20
     end
21 21
 
22
+    GEM_NAME = '[A-Za-z0-9\.\-\_]+'.freeze
23
+    GEM_OPTIONS = '(.+?)\s*(?:,\s*(.+?)){0,1}'.freeze
24
+    GEM_SEPARATOR = '\s*(?:,|\z)'.freeze
25
+    GEM_REGULAR_EXPRESSION = /(#{GEM_NAME})(?:\(#{GEM_OPTIONS}\)){0,1}#{GEM_SEPARATOR}/
26
+
27
+    def parse_each_agent_gem(string)
28
+      return unless string
29
+      string.scan(GEM_REGULAR_EXPRESSION).each do |name, version, args|
30
+        if version =~ /\w+:/
31
+          args = "#{version},#{args}"
32
+          version = nil
33
+        end
34
+        yield [name, version, parse_gem_args(args)].compact
35
+      end
36
+    end
37
+
22 38
     private
23 39
 
40
+    def parse_gem_args(args)
41
+      return nil unless args
42
+      options = {}
43
+      args.scan(/(\w+):\s*(.+?)#{GEM_SEPARATOR}/).each do |key, value|
44
+        options[key.to_sym] = value
45
+      end
46
+      options
47
+    end
48
+
24 49
     def sanity_check(env)
25 50
       return if ENV['CI'] == 'true' || !env.empty?
26 51
       puts warning

+ 48 - 0
spec/lib/gemfile_helper_spec.rb

@@ -0,0 +1,48 @@
1
+require 'rails_helper'
2
+
3
+describe GemfileHelper do
4
+  context 'parse_each_agent_gem' do
5
+    VALID_STRINGS = [
6
+      ['huginn_nlp_agents(~> 0.2.1)', [
7
+        ['huginn_nlp_agents', '~> 0.2.1']
8
+      ]],
9
+      ['huginn_nlp_agents(~> 0.2.1, git: http://github.com/dsander/huginn.git, branch: agents_in_gems)',
10
+        [['huginn_nlp_agents', '~> 0.2.1', git: 'http://github.com/dsander/huginn.git', branch: 'agents_in_gems']]
11
+      ],
12
+      ['huginn_nlp_agents(~> 0.2.1, git: http://github.com/dsander/huginn.git, ref: 2342asdab)  , huginn_nlp_agents(~> 0.2.1)', [
13
+        ['huginn_nlp_agents', '~> 0.2.1', git: 'http://github.com/dsander/huginn.git', ref: '2342asdab'],
14
+        ['huginn_nlp_agents', '~> 0.2.1']
15
+      ]],
16
+      ['huginn_nlp_agents(~> 0.2.1, path: /tmp/test)', [
17
+        ['huginn_nlp_agents', '~> 0.2.1', path: '/tmp/test']
18
+      ]],
19
+      ['huginn_nlp_agents', [
20
+        ['huginn_nlp_agents']
21
+      ]],
22
+      ['huginn_nlp_agents, test(0.1), test2(github: test2/huginn_test)', [
23
+        ['huginn_nlp_agents'],
24
+        ['test', '0.1'],
25
+        ['test2', github: 'test2/huginn_test']
26
+      ]],
27
+      ['huginn_nlp_agents(git: http://github.com/dsander/huginn.git, ref: 2342asdab)', [
28
+        ['huginn_nlp_agents', git: 'http://github.com/dsander/huginn.git', ref: '2342asdab']
29
+      ]],
30
+    ]
31
+
32
+    it 'parses valid gem strings correctly' do
33
+      VALID_STRINGS.each do |string, outcomes|
34
+        GemfileHelper.parse_each_agent_gem(string) do |args|
35
+          expect(args).to eq(outcomes.shift)
36
+        end
37
+      end
38
+    end
39
+
40
+    it 'does nothing when nil is passed' do
41
+      expect { |b| GemfileHelper.parse_each_agent_gem(nil, &b) }.not_to yield_control
42
+    end
43
+
44
+    it 'does nothing when an empty string is passed' do
45
+      expect { |b| GemfileHelper.parse_each_agent_gem('', &b) }.not_to yield_control
46
+    end
47
+  end
48
+end