Class: HuginnAgent::CLI::New

Inherits:
Object
  • Object
show all
Defined in:
lib/huginn_agent/cli/new.rb

Constant Summary collapse

PREFIX_QUESTION =
"We recommend prefixing all Huginn agent gem names with 'huginn_' to make them easily discoverable.\nPrefix gem name with 'huginn_'?".freeze
MIT_QUESTION =
"Do you want to license your code permissively under the MIT license?".freeze
DOT_ENV_QUESTION =
'Which .env file do you want to use?'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, gem_name, thor) ⇒ New

Returns a new instance of New.



12
13
14
15
16
17
18
19
20
21
# File 'lib/huginn_agent/cli/new.rb', line 12

def initialize(options, gem_name, thor)
  @options = options
  @thor = thor
  if !gem_name.start_with?('huginn_') &&
     thor.yes?(PREFIX_QUESTION)
    gem_name = "huginn_#{gem_name}"
  end
  @target = Pathname.pwd.join(gem_name)
  @gem_name = target.basename.to_s
end

Instance Attribute Details

#gem_nameObject (readonly)

Returns the value of attribute gem_name.



10
11
12
# File 'lib/huginn_agent/cli/new.rb', line 10

def gem_name
  @gem_name
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/huginn_agent/cli/new.rb', line 10

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



10
11
12
# File 'lib/huginn_agent/cli/new.rb', line 10

def target
  @target
end

#thorObject (readonly)

Returns the value of attribute thor.



10
11
12
# File 'lib/huginn_agent/cli/new.rb', line 10

def thor
  @thor
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/huginn_agent/cli/new.rb', line 23

def run
  thor.say "Creating Huginn agent '#{gem_name}'"

  agent_file_name = gem_name.gsub('huginn_', '')
  namespaced_path = gem_name.tr('-', '/')
  constant_name   = gem_name.split('_')[1..-1].map{|p| p[0..0].upcase + p[1..-1] unless p.empty?}.join
  constant_name   = constant_name.split('-').map{|q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/
  git_user_name   = `git config user.name`.chomp
  git_user_email  = `git config user.email`.chomp

  opts = {
    :gem_name         => gem_name,
    :agent_file_name  => agent_file_name,
    :namespaced_path  => namespaced_path,
    :constant_name    => constant_name,
    :author           => git_user_name.empty? ? "TODO: Write your name" : git_user_name,
    :email            => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
  }

  templates = {
    "Gemfile.tt" => "Gemfile",
    "gitignore.tt" => ".gitignore",
    "lib/new_agent.rb.tt" => "lib/#{namespaced_path}.rb",
    "lib/new_agent/new_agent.rb.tt" => "lib/#{namespaced_path}/#{agent_file_name}.rb",
    "spec/new_agent_spec.rb.tt" => "spec/#{agent_file_name}_spec.rb",
    "newagent.gemspec.tt" => "#{gem_name}.gemspec",
    "Rakefile.tt" => "Rakefile",
    "README.md.tt" => "README.md",
    "travis.yml.tt" => ".travis.yml"
  }

  if thor.yes?(MIT_QUESTION)
    opts[:mit] = true
    templates.merge!("LICENSE.txt.tt" => "LICENSE.txt")
  end


  templates.each do |src, dst|
    thor.template("newagent/#{src}", target.join(dst), opts)
  end

  thor.say "To run the specs of your agent you need to add a .env which configures the database for Huginn"

  possible_paths = Dir['.env', './huginn/.env', '~/huginn/.env']
  if possible_paths.length > 0
    thor.say 'Found possible preconfigured .env files please choose which one you want to use'
    possible_paths.each_with_index do |path, i|
      thor.say "#{i+1} #{path}"
    end

    if (i = thor.ask(DOT_ENV_QUESTION).to_i) != 0
      path = possible_paths[i-1]
      `cp #{path} #{target}`
      thor.say "Copied '#{path}' to '#{target}'"
    end
  end

  thor.say "Initializing git repo in #{target}"
  Dir.chdir(target) { `git init`; `git add .` }

  thor.say 'Installing dependencies'
  Dir.chdir(target) { HuginnAgent::Helper.open3('bundle install') }
end