Class: RBWatch::Configuration

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = !!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

Returns the value of attribute banner.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def banner
  @banner
end

#clearObject (readonly)

Returns the value of attribute clear.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def clear
  @clear
end

#colorObject (readonly)

Returns the value of attribute color.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def color
  @color
end

#commandObject (readonly)

Returns the value of attribute command.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def command
  @command
end

#config_fileObject (readonly)

Returns the value of attribute config_file.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def config_file
  @config_file
end

#delayObject (readonly)

Returns the value of attribute delay.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def delay
  @delay
end

#desktop_notifyObject (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_modeObject (readonly)

Returns the value of attribute exec_mode.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def exec_mode
  @exec_mode
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def extensions
  @extensions
end

#historyObject (readonly)

Returns the value of attribute history.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def history
  @history
end

#ignoreObject (readonly)

Returns the value of attribute ignore.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def ignore
  @ignore
end

#restart_on_exitObject (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

#soundObject (readonly)

Returns the value of attribute sound.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def sound
  @sound
end

#statsObject (readonly)

Returns the value of attribute stats.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def stats
  @stats
end

#stderrObject (readonly)

Returns the value of attribute stderr.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def stdout
  @stdout
end

#theme_nameObject (readonly)

Returns the value of attribute theme_name.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def theme_name
  @theme_name
end

#verboseObject (readonly)

Returns the value of attribute verbose.



15
16
17
# File 'lib/rbwatch/configuration.rb', line 15

def verbose
  @verbose
end

#watch_pathsObject (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_optionsObject



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 default_options
  {
    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 expand_command_shorthand(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:)
  cli_options = symbolize_keys(cli_options)
  explicit_config_file = cli_options.delete(:config_file)
  config_path = explicit_config_file || discover_config_file
  file_options = config_path ? load_file(config_path) : {}

  merged = default_options.merge(file_options).merge(cli_options.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.message}"
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
  expand_command_shorthand(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), options|
    case key.to_s
    when "delay"
      options[:delay] = value
    when "extensions"
      options[:extensions] = normalize_list(value)
    when "ignore"
      options[:ignore] = normalize_list(value)
    when "watch_paths", "watch"
      options[:watch_paths] = normalize_watch_paths(value)
    when "command"
      options[:command] = normalize_command(value)
    when "clear", "verbose", "banner", "color", "restart_on_exit", "sound", "desktop_notify", "stats", "history", "exec_mode"
      options[key.to_sym] = !!value
    when "theme", "theme_name"
      options[: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

Returns:

  • (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

Returns:

  • (Boolean)


215
216
217
# File 'lib/rbwatch/configuration.rb', line 215

def banner?
  @banner
end

#clear?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/rbwatch/configuration.rb', line 211

def clear?
  @clear
end

#color?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rbwatch/configuration.rb', line 203

def color?
  @color
end

#command_stringObject



251
252
253
# File 'lib/rbwatch/configuration.rb', line 251

def command_string
  Shellwords.join(command)
end

#delay_secondsObject



247
248
249
# File 'lib/rbwatch/configuration.rb', line 247

def delay_seconds
  delay / 1000.0
end

#desktop_notify?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/rbwatch/configuration.rb', line 227

def desktop_notify?
  @desktop_notify
end

#exec_mode?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/rbwatch/configuration.rb', line 239

def exec_mode?
  @exec_mode
end

#history?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/rbwatch/configuration.rb', line 235

def history?
  @history
end

#interactive_theme_prompt?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


219
220
221
# File 'lib/rbwatch/configuration.rb', line 219

def restart_on_exit?
  @restart_on_exit
end

#sound?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/rbwatch/configuration.rb', line 223

def sound?
  @sound
end

#stats?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/rbwatch/configuration.rb', line 231

def stats?
  @stats
end

#themeObject



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_hObject



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: 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

Returns:

  • (Boolean)


207
208
209
# File 'lib/rbwatch/configuration.rb', line 207

def verbose?
  @verbose
end