Class: Nesta::Commands::Plugin::Create

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/nesta/commands/plugin/create.rb

Instance Method Summary collapse

Methods included from Command

#copy_template, #copy_templates, #fail, #run_process, #template_root, #update_config_yaml

Constructor Details

#initialize(*args) ⇒ Create



9
10
11
12
13
14
15
16
17
# File 'lib/nesta/commands/plugin/create.rb', line 9

def initialize(*args)
  name = args.shift
  name.nil? && (raise UsageError.new('name not specified'))
  @name = name
  @gem_name = "nesta-plugin-#{name}"
  if File.exist?(@gem_name)
    raise RuntimeError.new("#{@gem_name} already exists")
  end
end

Instance Method Details

#executeObject



71
72
73
74
75
76
77
# File 'lib/nesta/commands/plugin/create.rb', line 71

def execute
  run_process('bundle', 'gem', @gem_name)
  modify_required_file
  modify_init_file
  specify_gem_dependency
  Dir.chdir(@gem_name) { run_process('git', 'add', '.') }
end

#lib_path(*parts) ⇒ Object



19
20
21
# File 'lib/nesta/commands/plugin/create.rb', line 19

def lib_path(*parts)
  File.join(@gem_name, 'lib', *parts)
end

#modify_init_fileObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nesta/commands/plugin/create.rb', line 33

def modify_init_file
  module_name = @name.split('-').map { |name| name.capitalize }.join('::')
  File.open(lib_path(@gem_name, 'init.rb'), 'w') do |file|
    file.puts <<-EOF
module Nesta
  module Plugin
    module #{module_name}
      module Helpers
# If your plugin needs any helper methods, add them here...
      end
    end
  end

  class App
    helpers Nesta::Plugin::#{module_name}::Helpers
  end
end
    EOF
  end
end

#modify_required_fileObject



23
24
25
26
27
28
29
30
31
# File 'lib/nesta/commands/plugin/create.rb', line 23

def modify_required_file
  File.open(lib_path("#{@gem_name}.rb"), 'w') do |file|
    file.write <<-EOF
require "#{@gem_name}/version"

Nesta::Plugin.register(__FILE__)
    EOF
  end
end

#specify_gem_dependencyObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nesta/commands/plugin/create.rb', line 54

def specify_gem_dependency
  gemspec = File.join(@gem_name, "#{@gem_name}.gemspec")
  File.open(gemspec, 'r+') do |file|
    output = ''
    file.each_line do |line|
      if line =~ /^end/
        output << '  gem.add_dependency("nesta", ">= 0.9.11")' + "\n"
        output << '  gem.add_development_dependency("rake")' + "\n"
      end
      output << line
    end
    file.pos = 0
    file.print(output)
    file.truncate(file.pos)
  end
end