Class: RBWatch::Configuration
- Inherits:
-
Object
- Object
- RBWatch::Configuration
- Defined in:
- lib/rbwatch/configuration.rb
Constant Summary collapse
- DEFAULT_DELAY =
300- DEFAULT_EXTENSIONS =
%w[rb erb haml slim rake yml yaml json].freeze
- DEFAULT_IGNORE =
%w[log tmp .git node_modules].freeze
- DEFAULT_WATCH_PATHS =
["."].freeze
- CONFIG_FILENAMES =
%w[rbwatch.yml rbwatch.yaml .rbwatch.yml .rbwatch.yaml].freeze
- SHORTHAND_RUBY_EXTENSIONS =
%w[.rb .ru].freeze
Instance Attribute Summary collapse
-
#banner ⇒ Object
readonly
Returns the value of attribute banner.
-
#clear ⇒ Object
readonly
Returns the value of attribute clear.
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#config_file ⇒ Object
readonly
Returns the value of attribute config_file.
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#desktop_notify ⇒ Object
readonly
Returns the value of attribute desktop_notify.
-
#exec_mode ⇒ Object
readonly
Returns the value of attribute exec_mode.
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#ignore ⇒ Object
readonly
Returns the value of attribute ignore.
-
#restart_on_exit ⇒ Object
readonly
Returns the value of attribute restart_on_exit.
-
#sound ⇒ Object
readonly
Returns the value of attribute sound.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
-
#theme_name ⇒ Object
readonly
Returns the value of attribute theme_name.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
-
#watch_paths ⇒ Object
readonly
Returns the value of attribute watch_paths.
Class Method Summary collapse
- .default_options ⇒ Object
- .discover_config_file(cwd = Dir.pwd) ⇒ Object
- .expand_command_shorthand(command) ⇒ Object
- .from_sources(cli_options:, command:, stdin:, stdout:, stderr:) ⇒ Object
- .load_file(path) ⇒ Object
- .normalize_command(value) ⇒ Object
- .normalize_config_hash(hash) ⇒ Object
- .normalize_list(value) ⇒ Object
- .normalize_watch_paths(value) ⇒ Object
- .ruby_script_path?(path) ⇒ Boolean
- .symbolize_keys(hash) ⇒ Object
Instance Method Summary collapse
- #banner? ⇒ Boolean
- #clear? ⇒ Boolean
- #color? ⇒ Boolean
- #command_string ⇒ Object
- #delay_seconds ⇒ Object
- #desktop_notify? ⇒ Boolean
- #exec_mode? ⇒ Boolean
- #history? ⇒ Boolean
-
#initialize(delay: DEFAULT_DELAY, extensions: DEFAULT_EXTENSIONS, ignore: DEFAULT_IGNORE, watch_paths: DEFAULT_WATCH_PATHS, command: [], clear: nil, verbose: false, banner: true, color: true, theme_name: "ruby", restart_on_exit: true, sound: false, desktop_notify: false, stats: false, history: false, config_file: nil, exec_mode: false, stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ Configuration
constructor
A new instance of Configuration.
- #interactive_theme_prompt? ⇒ Boolean
- #restart_on_exit? ⇒ Boolean
- #sound? ⇒ Boolean
- #stats? ⇒ Boolean
- #theme ⇒ Object
- #to_h ⇒ Object
- #verbose? ⇒ Boolean
Constructor Details
#initialize(delay: DEFAULT_DELAY, extensions: DEFAULT_EXTENSIONS, ignore: DEFAULT_IGNORE, watch_paths: DEFAULT_WATCH_PATHS, command: [], clear: nil, verbose: false, banner: true, color: true, theme_name: "ruby", restart_on_exit: true, sound: false, desktop_notify: false, stats: false, history: false, config_file: nil, exec_mode: false, stdin: $stdin, stdout: $stdout, stderr: $stderr) ⇒ Configuration
Returns a new instance of Configuration.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/rbwatch/configuration.rb', line 158 def initialize( delay: DEFAULT_DELAY, extensions: DEFAULT_EXTENSIONS, ignore: DEFAULT_IGNORE, watch_paths: DEFAULT_WATCH_PATHS, command: [], clear: nil, verbose: false, banner: true, color: true, theme_name: "ruby", restart_on_exit: true, sound: false, desktop_notify: false, stats: false, history: false, config_file: nil, exec_mode: false, stdin: $stdin, stdout: $stdout, stderr: $stderr ) @delay = Integer(delay) @extensions = normalize_list(extensions) @ignore = normalize_list(ignore) @watch_paths = normalize_watch_paths(watch_paths) @command = normalize_command(command) @verbose = !!verbose @banner = !! @color = !!color @theme_name = Theme.normalize_name(theme_name) || :ruby @restart_on_exit = !!restart_on_exit @sound = !!sound @desktop_notify = !!desktop_notify @stats = !!stats @history = !!history @config_file = config_file @exec_mode = !!exec_mode @stdin = stdin @stdout = stdout @stderr = stderr @clear = clear.nil? ? clear_by_default?(stderr) : !!clear @theme = nil end |
Instance Attribute Details
#banner ⇒ Object (readonly)
Returns the value of attribute banner.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def @banner end |
#clear ⇒ Object (readonly)
Returns the value of attribute clear.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def clear @clear end |
#color ⇒ Object (readonly)
Returns the value of attribute color.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def color @color end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def command @command end |
#config_file ⇒ Object (readonly)
Returns the value of attribute config_file.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def config_file @config_file end |
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def delay @delay end |
#desktop_notify ⇒ Object (readonly)
Returns the value of attribute desktop_notify.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def desktop_notify @desktop_notify end |
#exec_mode ⇒ Object (readonly)
Returns the value of attribute exec_mode.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def exec_mode @exec_mode end |
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def extensions @extensions end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def history @history end |
#ignore ⇒ Object (readonly)
Returns the value of attribute ignore.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def ignore @ignore end |
#restart_on_exit ⇒ Object (readonly)
Returns the value of attribute restart_on_exit.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def restart_on_exit @restart_on_exit end |
#sound ⇒ Object (readonly)
Returns the value of attribute sound.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def sound @sound end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def stats @stats end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def stdout @stdout end |
#theme_name ⇒ Object (readonly)
Returns the value of attribute theme_name.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def theme_name @theme_name end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def verbose @verbose end |
#watch_paths ⇒ Object (readonly)
Returns the value of attribute watch_paths.
15 16 17 |
# File 'lib/rbwatch/configuration.rb', line 15 def watch_paths @watch_paths end |
Class Method Details
.default_options ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rbwatch/configuration.rb', line 69 def { delay: DEFAULT_DELAY, extensions: DEFAULT_EXTENSIONS.dup, ignore: DEFAULT_IGNORE.dup, watch_paths: DEFAULT_WATCH_PATHS.dup, command: [], clear: nil, verbose: false, banner: true, color: true, theme_name: :ruby, restart_on_exit: true, sound: false, desktop_notify: false, stats: false, history: false, config_file: nil, exec_mode: false } end |
.discover_config_file(cwd = Dir.pwd) ⇒ Object
37 38 39 |
# File 'lib/rbwatch/configuration.rb', line 37 def discover_config_file(cwd = Dir.pwd) CONFIG_FILENAMES.map { |name| File.join(cwd, name) }.find { |path| File.file?(path) } end |
.expand_command_shorthand(command) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/rbwatch/configuration.rb', line 140 def (command) return command if command.length != 1 first = command.first.to_s return command unless ruby_script_path?(first) ["ruby", first] end |
.from_sources(cli_options:, command:, stdin:, stdout:, stderr:) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rbwatch/configuration.rb', line 52 def from_sources(cli_options:, command:, stdin:, stdout:, stderr:) = symbolize_keys() explicit_config_file = .delete(:config_file) config_path = explicit_config_file || discover_config_file = config_path ? load_file(config_path) : {} merged = .merge().merge(.compact) resolved_command = command.empty? ? Array(merged[:command]) : command new( **merged.merge(command: resolved_command, config_file: config_path), stdin: stdin, stdout: stdout, stderr: stderr ) end |
.load_file(path) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rbwatch/configuration.rb', line 41 def load_file(path) return {} if path.nil? || path.to_s.strip.empty? raw = YAML.safe_load(File.read(path), permitted_classes: [Symbol], aliases: true) raise ArgumentError, "Config file must contain a YAML mapping" unless raw.nil? || raw.is_a?(Hash) normalize_config_hash(raw || {}) rescue Psych::SyntaxError => e raise ArgumentError, "Unable to parse config file #{path}: #{e.}" end |
.normalize_command(value) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rbwatch/configuration.rb', line 126 def normalize_command(value) command = case value when nil [] when String Shellwords.split(value) else Array(value).flat_map do |entry| entry.is_a?(String) ? Shellwords.split(entry) : entry end.map(&:to_s) end (command) end |
.normalize_config_hash(hash) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rbwatch/configuration.rb', line 91 def normalize_config_hash(hash) hash.each_with_object({}) do |(key, value), | case key.to_s when "delay" [:delay] = value when "extensions" [:extensions] = normalize_list(value) when "ignore" [:ignore] = normalize_list(value) when "watch_paths", "watch" [:watch_paths] = normalize_watch_paths(value) when "command" [:command] = normalize_command(value) when "clear", "verbose", "banner", "color", "restart_on_exit", "sound", "desktop_notify", "stats", "history", "exec_mode" [key.to_sym] = !!value when "theme", "theme_name" [:theme_name] = Theme.normalize_name(value) || :ruby end end end |
.normalize_list(value) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/rbwatch/configuration.rb', line 112 def normalize_list(value) Array(value).flat_map { |item| item.to_s.split(",") } .map { |entry| entry.strip.downcase } .reject(&:empty?) .uniq end |
.normalize_watch_paths(value) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/rbwatch/configuration.rb', line 119 def normalize_watch_paths(value) paths = Array(value).flat_map { |item| item.to_s.split(",") } normalized = paths.map { |path| path.strip }.reject(&:empty?) normalized = ["."] if normalized.empty? normalized.uniq end |
.ruby_script_path?(path) ⇒ Boolean
149 150 151 |
# File 'lib/rbwatch/configuration.rb', line 149 def ruby_script_path?(path) SHORTHAND_RUBY_EXTENSIONS.include?(File.extname(path).downcase) end |
.symbolize_keys(hash) ⇒ Object
153 154 155 |
# File 'lib/rbwatch/configuration.rb', line 153 def symbolize_keys(hash) hash.each_with_object({}) { |(key, value), result| result[key.to_sym] = value } end |
Instance Method Details
#banner? ⇒ Boolean
215 216 217 |
# File 'lib/rbwatch/configuration.rb', line 215 def @banner end |
#clear? ⇒ Boolean
211 212 213 |
# File 'lib/rbwatch/configuration.rb', line 211 def clear? @clear end |
#color? ⇒ Boolean
203 204 205 |
# File 'lib/rbwatch/configuration.rb', line 203 def color? @color end |
#command_string ⇒ Object
251 252 253 |
# File 'lib/rbwatch/configuration.rb', line 251 def command_string Shellwords.join(command) end |
#delay_seconds ⇒ Object
247 248 249 |
# File 'lib/rbwatch/configuration.rb', line 247 def delay_seconds delay / 1000.0 end |
#desktop_notify? ⇒ Boolean
227 228 229 |
# File 'lib/rbwatch/configuration.rb', line 227 def desktop_notify? @desktop_notify end |
#exec_mode? ⇒ Boolean
239 240 241 |
# File 'lib/rbwatch/configuration.rb', line 239 def exec_mode? @exec_mode end |
#history? ⇒ Boolean
235 236 237 |
# File 'lib/rbwatch/configuration.rb', line 235 def history? @history end |
#interactive_theme_prompt? ⇒ Boolean
255 256 257 |
# File 'lib/rbwatch/configuration.rb', line 255 def interactive_theme_prompt? stdin.respond_to?(:tty?) && stdout.respond_to?(:tty?) && stdin.tty? && stdout.tty? end |
#restart_on_exit? ⇒ Boolean
219 220 221 |
# File 'lib/rbwatch/configuration.rb', line 219 def restart_on_exit? @restart_on_exit end |
#sound? ⇒ Boolean
223 224 225 |
# File 'lib/rbwatch/configuration.rb', line 223 def sound? @sound end |
#stats? ⇒ Boolean
231 232 233 |
# File 'lib/rbwatch/configuration.rb', line 231 def stats? @stats end |
#theme ⇒ Object
243 244 245 |
# File 'lib/rbwatch/configuration.rb', line 243 def theme @theme ||= Theme.resolve(theme_name, color: color?, interactive: interactive_theme_prompt?, input: stdin, output: stderr) end |
#to_h ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/rbwatch/configuration.rb', line 259 def to_h { delay: delay, extensions: extensions.dup, ignore: ignore.dup, watch_paths: watch_paths.dup, command: command.dup, clear: clear?, verbose: verbose?, banner: , color: color?, theme_name: theme_name, restart_on_exit: restart_on_exit?, sound: sound?, desktop_notify: desktop_notify?, stats: stats?, history: history?, config_file: config_file, exec_mode: exec_mode? } end |
#verbose? ⇒ Boolean
207 208 209 |
# File 'lib/rbwatch/configuration.rb', line 207 def verbose? @verbose end |