Class: Bake::BakeryOptions

Inherits:
Parser
  • Object
show all
Defined in:
lib/bakery/options/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#add_option, #get_block, #num_parameter?, #parse_internal, #valid?

Constructor Details

#initialize(argv) ⇒ BakeryOptions

Returns a new instance of BakeryOptions.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bakery/options/options.rb', line 14

def initialize(argv)
  super(argv)

  @collection_name = ""
  @collection_dir = nil
  @color = nil
  @error = false
  @roots = []
  @socket = 0

  add_option(["-b", ""      ], lambda { |x| set_collection_name(x)                  })
  add_option(["-m"          ], lambda { |x| @collection_dir = x                    })
  add_option(["-r"          ], lambda {     @error = true                           })
  add_option(["-a"          ], lambda { |x| Bake.formatter.setColorScheme(x.to_sym) })
  add_option(["-w"          ], lambda { |x| set_root(x)                             })
  add_option(["--socket"    ], lambda { |x| @socket = String === x ? x.to_i : x     })
  add_option(["-h", "--help"], lambda {     usage; ExitHelper.exit(0)               })
end

Instance Attribute Details

#collection_dirObject (readonly)

String



9
10
11
# File 'lib/bakery/options/options.rb', line 9

def collection_dir
  @collection_dir
end

#collection_nameObject (readonly)

String



9
10
11
# File 'lib/bakery/options/options.rb', line 9

def collection_name
  @collection_name
end

#colorObject (readonly)

Boolean



11
12
13
# File 'lib/bakery/options/options.rb', line 11

def color
  @color
end

#errorObject (readonly)

Boolean



11
12
13
# File 'lib/bakery/options/options.rb', line 11

def error
  @error
end

#rootsObject (readonly)

Root list



10
11
12
# File 'lib/bakery/options/options.rb', line 10

def roots
  @roots
end

#socketObject (readonly)

Fixnum



12
13
14
# File 'lib/bakery/options/options.rb', line 12

def socket
  @socket
end

Instance Method Details

#check_valid_dir(dir) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/bakery/options/options.rb', line 69

def check_valid_dir(dir)
 if not File.exist?(dir)
    Bake.formatter.printError("Error: Directory #{dir} does not exist")
    ExitHelper.exit(1)
  end
  if not File.directory?(dir)
    Bake.formatter.printError("Error: #{dir} is not a directory")
    ExitHelper.exit(1)
  end
end

#parse_options(bakeOptions) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bakery/options/options.rb', line 46

def parse_options(bakeOptions)
  parse_internal(true, bakeOptions)

  searchDir = @collection_dir.nil? ? Dir.pwd : @collection_dir
  dir = Bake.findDirOfFileToRoot(searchDir,"Collection.meta")
  if dir
    set_collection_dir(dir)
  else
    Bake.formatter.printError("Error: Collection.meta not found in #{searchDir} or upwards")
    ExitHelper.exit(1)
  end

  def_roots = Root.calc_roots_bake(@collection_dir)
  @roots += def_roots

  if @roots.empty?
    @roots = []
    @roots = Root.calc_def_roots(@collection_dir)
  end

  @roots = Root.uniq(@roots)
end

#set_collection_dir(dir) ⇒ Object



88
89
90
91
# File 'lib/bakery/options/options.rb', line 88

def set_collection_dir(dir)
  check_valid_dir(dir)
  @collection_dir = File.expand_path(dir.gsub(/[\\]/,'/'))
end

#set_collection_name(collection_name) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/bakery/options/options.rb', line 80

def set_collection_name(collection_name)
  if not @collection_name.empty?
    Bake.formatter.printError("Error: Cannot set collection name '#{collection_name}', because collection name is already set to '#{@collection_name}'")
    ExitHelper.exit(1)
  end
  @collection_name = collection_name
end

#set_root(dir) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/bakery/options/options.rb', line 93

def set_root(dir)
  if File.file?(dir)
    @roots += Root.calc_roots_bake(dir)
  else
    root = Root.extract_depth(dir)
    check_valid_dir(root.dir)
    root.dir  = File.expand_path(root.dir.gsub(/[\\]/,'/'))
    @roots << root
  end
end

#usageObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bakery/options/options.rb', line 33

def usage
  puts "\nUsage: bake <name> [options]"
  puts " [-b] <name>           Name of the collection to build."
  puts " -m <dir>              Directory containing the collection file (default is current directory)."
  puts " -r                    Stop on first error."
  puts " -a <scheme>           Use ansi color sequences (console must support it). Possible values are 'white', 'black' and 'none' (default)."
  puts " -h, --help            Print this help."
  puts " -w <root>[,<depth>]   Add a workspace root (can be used multiple times). Additionally the search depth can be specified (>=0)."
  puts "                       If no root is specified, the parent directory of Collection.meta is added automatically."
  puts " --socket <num>        Set socket for sending errors, receiving commands, etc. - used by e.g. Eclipse."
  puts "Note: all parameters except -b, -m and -h will be passed to bake - see bake help for more options."
end