Module: Nesta::Commands::Command

Included in:
Demo::Content, Edit, New, Plugin::Create, Theme::Create, Theme::Enable, Theme::Install
Defined in:
lib/nesta/commands.rb

Instance Method Summary collapse

Instance Method Details

#copy_template(src, dest) ⇒ Object



36
37
38
39
40
# File 'lib/nesta/commands.rb', line 36

def copy_template(src, dest)
  FileUtils.mkdir_p(File.dirname(dest))
  template = ERB.new(File.read(File.join(template_root, src)))
  File.open(dest, 'w') { |file| file.puts template.result(binding) }
end

#copy_templates(templates) ⇒ Object



42
43
44
# File 'lib/nesta/commands.rb', line 42

def copy_templates(templates)
  templates.each { |src, dest| copy_template(src, dest) }
end

#fail(message) ⇒ Object



27
28
29
30
# File 'lib/nesta/commands.rb', line 27

def fail(message)
  $stderr.puts "Error: #{message}"
  exit 1
end

#run_process(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nesta/commands.rb', line 14

def run_process(*args)
  system(*args)
  if ! $?.success?
    message = if $?.exitstatus == 127
                "#{args[0]} not found"
              else
                "'#{args.join(' ')}' failed with status #{$?.exitstatus}"
              end
    $stderr.puts "Error: #{message}"
    exit 1
  end
end

#template_rootObject



32
33
34
# File 'lib/nesta/commands.rb', line 32

def template_root
  File.expand_path('../../templates', File.dirname(__FILE__))
end

#update_config_yaml(pattern, replacement) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nesta/commands.rb', line 46

def update_config_yaml(pattern, replacement)
  configured = false
  File.open(Nesta::Config.yaml_path, 'r+') do |file|
    output = ''
    file.each_line do |line|
      if configured
        output << line
      else
        output << line.sub(pattern, replacement)
        configured = true if line =~ pattern
      end
    end
    output << "#{replacement}\n" unless configured
    file.pos = 0
    file.print(output)
    file.truncate(file.pos)
  end
end