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

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Create

Returns a new instance of Create.



5
6
7
8
9
10
11
12
13
# File 'lib/nesta/commands/plugin/create.rb', line 5

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

#execute(process) ⇒ Object



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

def execute(process)
  make_directories
  {
    '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')
  }.each do |src, dest|
    Nesta::Commands::Template.new(src).copy_to(dest, binding)
  end
  Dir.chdir(@gem_name) do
    process.run('git', 'init')
    process.run('git', 'add', '.')
  end
end

#gem_path(path) ⇒ Object



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

def gem_path(path)
  File.join(@gem_name, path)
end

#lib_path(*parts) ⇒ Object



15
16
17
# File 'lib/nesta/commands/plugin/create.rb', line 15

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

#make_directoriesObject



45
46
47
# File 'lib/nesta/commands/plugin/create.rb', line 45

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

#module_nameObject



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

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

#nested_module_definition_with_versionObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nesta/commands/plugin/create.rb', line 23

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