Class: Awestruct::CLI::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/cli/options.rb

Constant Summary collapse

DEFAULT_BIND_ADDR =
'localhost'
DEFAULT_PORT =
4242
DEFAULT_BASE_URL =
%(http://#{DEFAULT_BIND_ADDR}:#{DEFAULT_PORT})
DEFAULT_GENERATE_ON_ACCESS =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Options

Returns a new instance of Options.



36
37
38
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/awestruct/cli/options.rb', line 36

def initialize(opts = {})
  default_opts = { server: false, port: DEFAULT_PORT, bind_addr: DEFAULT_BIND_ADDR, auto: false, force: false,
                   init: false, framework: 'compass', scaffold: true, base_url: DEFAULT_BASE_URL, deploy: false,
                   verbose: false, quiet: false, livereload: false, source_dir: Dir.pwd, debug: false,
                   output_dir: File.expand_path('_site'), generate_on_access: DEFAULT_GENERATE_ON_ACCESS,
                   perf_log: false
                 }.merge opts
  @generate   = default_opts[:generate]
  @server     = default_opts[:server]
  @port       = default_opts[:port]
  @bind_addr  = default_opts[:bind_addr]
  @auto       = default_opts[:auto]
  @force      = default_opts[:force]
  @init       = default_opts[:init]
  @framework  = default_opts[:framework]
  @scaffold   = default_opts[:scaffold]
  @base_url   = default_opts[:base_url]
  @profile    = default_opts[:profile]
  @deploy     = default_opts[:deploy]
  @script     = default_opts[:script]
  @verbose    = default_opts[:verbose]
  @quiet      = default_opts[:quiet]
  @livereload = default_opts[:livereload]
  @source_dir = default_opts[:source_dir]
  @output_dir = default_opts[:output_dir]
  @generate_on_access = default_opts[:generate_on_access]
  @perf_log = default_opts[:perf_log]
  @debug = default_opts[:debug]
end

Instance Attribute Details

#autoObject

Returns the value of attribute auto.



18
19
20
# File 'lib/awestruct/cli/options.rb', line 18

def auto
  @auto
end

#base_urlObject

Returns the value of attribute base_url.



23
24
25
# File 'lib/awestruct/cli/options.rb', line 23

def base_url
  @base_url
end

#bind_addrObject

Returns the value of attribute bind_addr.



17
18
19
# File 'lib/awestruct/cli/options.rb', line 17

def bind_addr
  @bind_addr
end

#debugObject

Returns the value of attribute debug.



32
33
34
# File 'lib/awestruct/cli/options.rb', line 32

def debug
  @debug
end

#deployObject

Returns the value of attribute deploy.



25
26
27
# File 'lib/awestruct/cli/options.rb', line 25

def deploy
  @deploy
end

#forceObject

Returns the value of attribute force.



19
20
21
# File 'lib/awestruct/cli/options.rb', line 19

def force
  @force
end

#frameworkObject

Returns the value of attribute framework.



21
22
23
# File 'lib/awestruct/cli/options.rb', line 21

def framework
  @framework
end

#generateObject

Returns the value of attribute generate.



14
15
16
# File 'lib/awestruct/cli/options.rb', line 14

def generate
  @generate
end

#generate_on_accessObject

Returns the value of attribute generate_on_access.



33
34
35
# File 'lib/awestruct/cli/options.rb', line 33

def generate_on_access
  @generate_on_access
end

#initObject

Returns the value of attribute init.



20
21
22
# File 'lib/awestruct/cli/options.rb', line 20

def init
  @init
end

#livereloadObject

Returns the value of attribute livereload.



31
32
33
# File 'lib/awestruct/cli/options.rb', line 31

def livereload
  @livereload
end

#output_dirObject

Returns the value of attribute output_dir.



30
31
32
# File 'lib/awestruct/cli/options.rb', line 30

def output_dir
  @output_dir
end

#perf_logObject

Returns the value of attribute perf_log.



34
35
36
# File 'lib/awestruct/cli/options.rb', line 34

def perf_log
  @perf_log
end

#portObject

Returns the value of attribute port.



16
17
18
# File 'lib/awestruct/cli/options.rb', line 16

def port
  @port
end

#profileObject

Returns the value of attribute profile.



24
25
26
# File 'lib/awestruct/cli/options.rb', line 24

def profile
  @profile
end

#quietObject

Returns the value of attribute quiet.



28
29
30
# File 'lib/awestruct/cli/options.rb', line 28

def quiet
  @quiet
end

#scaffoldObject

Returns the value of attribute scaffold.



22
23
24
# File 'lib/awestruct/cli/options.rb', line 22

def scaffold
  @scaffold
end

#scriptObject

Returns the value of attribute script.



26
27
28
# File 'lib/awestruct/cli/options.rb', line 26

def script
  @script
end

#serverObject

Returns the value of attribute server.



15
16
17
# File 'lib/awestruct/cli/options.rb', line 15

def server
  @server
end

#source_dirObject

Returns the value of attribute source_dir.



29
30
31
# File 'lib/awestruct/cli/options.rb', line 29

def source_dir
  @source_dir
end

#verboseObject

Returns the value of attribute verbose.



27
28
29
# File 'lib/awestruct/cli/options.rb', line 27

def verbose
  @verbose
end

Class Method Details

.parse!(args) ⇒ Object



66
67
68
# File 'lib/awestruct/cli/options.rb', line 66

def self.parse!(args)
  Options.new({output_dir: nil}).parse! args
end

Instance Method Details

#parse!(args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/awestruct/cli/options.rb', line 70

def parse!(args)
  opts = OptionParser.new do |opts|
    opts.on('-D', '--debug', 'Enable debug logging') do |verbose|
      self.debug = true
    end
    opts.on('-w', '--verbose', 'Enable verbose mode') do |verbose|
      self.verbose = true
    end
    opts.on('-q', '--quiet', 'Only display warnings and errors') do |quiet|
      self.quiet = true
    end
    opts.on( '-i', '--init', 'Initialize a new project in the current directory' ) do |init|
      self.init     = init
      self.generate = false
    end
    opts.on( '-f', '--framework FRAMEWORK', 'Specify a compass framework during initialization (bootstrap, foundation)' ) do |framework|
      self.framework = framework
    end
    opts.on( '--[no-]scaffold', 'Create scaffolding during initialization (default: true)' ) do |s|
      self.scaffold = s
    end
    opts.on( '--force', 'Force a regeneration' ) do |force|
      self.force = force
    end
    opts.on( '-s', '--server', 'Serve generated site' ) do |s|
      self.server = s
    end
    opts.on( '-u', '--url URL', 'Set site.base_url' ) do |url|
      self.base_url = url
    end
    opts.on( '-d', '--dev',     "Run site in development mode (--auto, --server, --port #{DEFAULT_PORT}, --profile development, --livereload and --generate_on_access)" ) do |url|
      self.server   = true
      self.auto     = true
      self.port     = DEFAULT_PORT
      self.profile  = 'development'
      self.livereload = true
      self.generate_on_access = true
    end
    opts.on( '-a', '--auto', 'Auto-generate when changes are noticed' ) do |a|
      self.auto = a
      self.livereload = true
    end
    opts.on( '--[no-]livereload', 'Support for browser livereload' ) do |livereload|
      self.livereload = livereload
      self.generate_on_access = true  if self.livereload
    end

    opts.on( '--[no-]generate-on-access', 'Support for calling generate on HTTP access' ) do |generate_on_access|
      self.generate_on_access = generate_on_access
    end

    opts.on( '-P', '--profile PROFILE', 'Activate a configuration profile' ) do |profile|
      self.profile = profile
    end

    opts.on( '--deploy', 'Deploy site' ) do |deploy|
      self.deploy = deploy
      self.generate = false if self.generate.nil?
    end

    opts.on( '-p', '--port PORT', Integer, "Server port (default: #{DEFAULT_PORT})" ) do |port|
      self.port = port
    end
    opts.on( '-b', '--bind ADDR', "Server address (default: #{DEFAULT_BIND_ADDR})" ) do |bind_addr|
      self.bind_addr = bind_addr
    end
    opts.on( '-g', '--[no-]generate', 'Generate site' ) do |g|
      self.generate = g
    end
    #opts.on( '--run SCRIPT', 'Invoke a script after initialization' ) do |script|
    #  self.script = script
    #end
    opts.on( '--source-dir DIR', 'Location of sources (default: .)' ) do |source_dir|
      self.source_dir = File.expand_path source_dir
    end

    opts.on( '--output-dir DIR', 'Location to output generated site (default: _site)' ) do |output_dir|
      self.output_dir = File.expand_path output_dir
    end

    opts.on('--perf', 'Enable performance logging to .awestruct/perf.log') do
      self.perf_log = true
    end

    opts.separator ''
    opts.separator "Common options:"

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("-v", "--version", "Display the version") do
      puts "Awestruct: #{Awestruct::VERSION}"
      puts "https://awestruct.github.io/"
      exit
    end
  end

  opts.parse!(args)
  self.port ||= DEFAULT_PORT
  self.base_url = %(http://#{self.bind_addr}:#{self.port}) if self.base_url === DEFAULT_BASE_URL
  self.output_dir ||= File.expand_path(File.join(self.source_dir, '_site'))

  self.generate = true if self.generate.nil?
  self
end