Class: NginxTestHelper::CommandLineTool

Inherits:
Object
  • Object
show all
Defined in:
lib/nginx_test_helper/command_line_tool.rb

Instance Method Summary collapse

Instance Method Details

#copy_unless_exists(relative_path, dest_path = nil) ⇒ Object



19
20
21
22
23
# File 'lib/nginx_test_helper/command_line_tool.rb', line 19

def copy_unless_exists(relative_path, dest_path = nil)
  unless File.exist?(dest_path(relative_path))
    FileUtils.copy(template_path(relative_path), dest_path(dest_path || relative_path))
  end
end

#cwdObject



3
4
5
# File 'lib/nginx_test_helper/command_line_tool.rb', line 3

def cwd
  File.expand_path(File.join(File.dirname(__FILE__), '../../'))
end

#dest_path(filepath) ⇒ Object



15
16
17
# File 'lib/nginx_test_helper/command_line_tool.rb', line 15

def dest_path(filepath)
  expand(Dir.pwd, filepath)
end

#expand(*paths) ⇒ Object



7
8
9
# File 'lib/nginx_test_helper/command_line_tool.rb', line 7

def expand(*paths)
  File.expand_path(File.join(*paths))
end

#process(argv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nginx_test_helper/command_line_tool.rb', line 25

def process(argv)
  if argv[0] == 'init'
    require 'fileutils'

    FileUtils.makedirs(dest_path('spec'))

    copy_unless_exists('spec/nginx_configuration.rb')
    copy_unless_exists('spec/example_spec.rb')
    copy_unless_exists('spec/example2_spec.rb')

    write_mode = 'w'
    if File.exist?(dest_path('spec/spec_helper.rb'))
      load dest_path('spec/spec_helper.rb')
      write_mode = 'a'
    end

    unless Object.const_defined?('NginxConfiguration') && write_mode == 'a'
      File.open(dest_path('spec/spec_helper.rb'), write_mode) do |f|
        f.write("\nrequire File.expand_path('nginx_configuration', File.dirname(__FILE__))")
      end
    end

    File.open(template_path('INSTALL'), 'r').each_line do |line|
      puts line
    end
  elsif argv[0] == "license"
    puts File.new(expand(cwd, "LICENSE")).read
  else
    puts "unknown command #{argv}"
    puts "Usage: nginx_test_helper init"
    puts "                         license"
  end
end

#template_path(filepath) ⇒ Object



11
12
13
# File 'lib/nginx_test_helper/command_line_tool.rb', line 11

def template_path(filepath)
  expand(cwd, File.join("templates", filepath))
end