Top Level Namespace

Defined Under Namespace

Classes: Cameleon

Instance Method Summary collapse

Instance Method Details

#generateObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cameleon/cli/generate.rb', line 7

def generate
  unless Cameleon.config
    $stderr.puts "not cameleon home"
    exit(1)
  end
  
  path = ARGV.shift
  dir_path = File.join "response", path
  FileUtils.mkdir_p dir_path
  
  template_dir = File.join Cameleon.root, "/template/generate"
  Dir.glob("#{template_dir}/*").map do |path|
    basename = File.basename(path)
    target_file_path = File.join dir_path, basename
    src = File.read path
    erb = Erubis::Eruby.new src
    body = erb.result(binding)
    File.open(target_file_path, "w") do |f|
      f.puts body
    end
  end
end

#newObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cameleon/cli/new.rb', line 7

def new
  project_name = ARGV.shift
  FileUtils.mkdir_p project_name
  base_dir = File.join(Cameleon.root, 'template/new')
  Dir.glob("#{base_dir}/*").map do |f|
    if File.file? f
      src = File.read f
      erb = Erubis::Eruby.new src
      text = erb.result(binding)
      basename = File.basename(f)
      File.open(File.join(project_name, basename), 'w') do |out|
        out.write text
      end
    elsif File.directory? f
      FileUtils.cp_r f, project_name
    end
  end
end

#parse_options(argv) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cameleon/cli.rb', line 7

def parse_options(argv)
  OptionParser.new { |opt|
    opt.on("-v", "--version") {
      version = File.read "#{Cameleon.root}/VERSION"
      puts "cameleon #{version}"
      exit(0)
    }
    opt.on("-h", "--help") {
      usage
      exit(0)
    }
    opt.parse! argv
  }
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cameleon/cli.rb', line 39

def run
  aliases = {
    's' => 'server',
    'g' => 'generate'
  }
  
  parse_options ARGV
  
  command = ARGV.shift
  command = aliases[command] || command
  case command
  when 'server'
    require 'cameleon/cli/server'
  when 'new'
    require 'cameleon/cli/new'
  when 'generate'
    require 'cameleon/cli/generate'
  when 'gallery'
    require 'cameleon/cli/gallery'
  when 'help'
    usage
  else
    puts "unknown command: #{command}."
    puts "'cameleon help' for usage."
  end
end

#start(config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cameleon/cli/server.rb', line 7

def start(config)
  version = ">= 0"
  
  if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
    version = $1
    ARGV.shift
  end
  
  gem 'rack', version
  require 'rack'
  require 'rack/builder'
  require 'rack/handler/webrick'
  app = Rack::Builder.new {
    use Rack::Chunked
    run Cameleon::App.new
  }
  trap(:INT) {
    Rack::Handler::WEBrick.shutdown
  }
  Rack::Handler::WEBrick.run app, :Port => (config.port || 9292)
end

#usageObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cameleon/cli.rb', line 22

def usage
    puts usage_text = <<-"USAGE"
Cameleon - HTTP mock server framework.

Usage:
  cameleon [option] command [args]

Commands:
  new:             create new dummy application
  server:          launch server
  generate path:   generate scaffold for path
  
Options:
  -v:  show version
USAGE
end