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

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/nesta/commands.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



171
172
173
174
175
176
177
178
179
# File 'lib/nesta/commands.rb', line 171

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



233
234
235
236
237
238
239
# File 'lib/nesta/commands.rb', line 233

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



181
182
183
# File 'lib/nesta/commands.rb', line 181

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

#modify_init_fileObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/nesta/commands.rb', line 195

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



185
186
187
188
189
190
191
192
193
# File 'lib/nesta/commands.rb', line 185

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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/nesta/commands.rb', line 216

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