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

Returns a new instance of 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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nesta/commands/plugin/create.rb', line 57

def execute
  make_directories
  copy_templates(
    'plugins/README.md' => gem_path('README.md'),
    'plugins/gitignore' => gem_path('.gitignore'),
    'plugins/plugin.gemspec' => gem_path("#{@gem_name}.gemspec"),
    'plugins/Gemfile' => gem_path('Gemfile'),
    'plugins/lib/required.rb' => gem_path("lib/#{@gem_name}.rb"),
    'plugins/lib/version.rb' => gem_path("lib/#{@gem_name}/version.rb"),
    'plugins/lib/init.rb' => gem_path("lib/#{@gem_name}/init.rb"),
    'plugins/Rakefile' => gem_path('Rakefile')
  )
  Dir.chdir(@gem_name) do
    run_process('git', 'init')
    run_process('git', 'add', '.')
  end
end

#gem_path(path) ⇒ Object



53
54
55
# File 'lib/nesta/commands/plugin/create.rb', line 53

def gem_path(path)
  File.join(@gem_name, path)
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

#make_directoriesObject



49
50
51
# File 'lib/nesta/commands/plugin/create.rb', line 49

def make_directories
  FileUtils.mkdir_p(File.join(@gem_name, 'lib', @gem_name))
end

#module_nameObject



23
24
25
# File 'lib/nesta/commands/plugin/create.rb', line 23

def module_name
  module_names.join('::')
end

#nested_module_definition_with_versionObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nesta/commands/plugin/create.rb', line 27

def nested_module_definition_with_version
  indent_level = 2
  indent_with = '  '

  lines = module_names.map { |name| "module #{name}\n" }
  indent_levels = 0.upto(module_names.size - 1).to_a

  lines << "VERSION = '0.1.0'\n"
  indent_levels << module_names.size

  (module_names.size - 1).downto(0).each do |indent_level|
    lines << "end\n"
    indent_levels << indent_level
  end

  ''.tap do |code|
    lines.each_with_index do |line, i|
      code << '  ' * (indent_levels[i] + 2) + line
    end
  end
end