Module: Ofe

Defined in:
lib/ofe.rb,
lib/ofe/t_sorted_files.rb

Overview


MODULE->OFE ————————————


Defined Under Namespace

Classes: TSortedFiles

Constant Summary collapse

VERSION =

CONSTANTS ————————————


::Gem.loaded_specs["ofe"].version.to_s

Class Method Summary collapse

Class Method Details

.config_file_exists?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/ofe.rb', line 177

def self.config_file_exists?
  File.exist? config_file_filename
end

.config_file_filenameObject


CONFIG-FILE ———————————-




173
174
175
# File 'lib/ofe.rb', line 173

def self.config_file_filename
  "ofe.json"
end

.current_file_basenameObject


UTILITY ————————————–




37
38
39
# File 'lib/ofe.rb', line 37

def self.current_file_basename
  File.basename $0
end

.editorObject



153
154
155
# File 'lib/ofe.rb', line 153

def self.editor
  ENV["EDITOR"]
end

.ensure_group_key_is_class_if_exists(group, group_config, key, compare_class) ⇒ Object



157
158
159
160
161
# File 'lib/ofe.rb', line 157

def self.ensure_group_key_is_class_if_exists(group, group_config, key, compare_class)
  if group_config[key] and group_config[key].class != compare_class
    raise "Config key '#{key}' is not class #{compare_class} for group '#{group}'."
  end
end

.file_should_be_excluded?(file, exclusions) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
# File 'lib/ofe.rb', line 163

def self.file_should_be_excluded?(file, exclusions)
  exclusions.each do |exclusion|
    return true if file.start_with?(exclusion)
  end
  false
end

.files_to_open_for_group(group, formatted: false) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/ofe.rb', line 235

def self.files_to_open_for_group(group, formatted: false)
  group_config = find_group_config(group)

  raise_group_not_found group unless group_config

  files      = group_config["files"]
  extensions = group_config["extensions"]
  exclusions = group_config["exclusions"]
  first_file = group_config["first_file"]
  command    = group_config["command"]
  topology   = group_config["topology"]

  to_open    = []

  # Check that at least one definition exists
  raise "Neither files: nor extensions: is defined for group '#{group}'." if !files and !group

  # Check that they're the right type if they exist
  ["files", "extensions", "exclusions"].each do |key|
    ensure_group_key_is_class_if_exists group, group_config, key, Array
  end

  ["first_file", "command"].each do |key|
    ensure_group_key_is_class_if_exists group, group_config, key, String
  end
  
  ["topology"].each do |key|
    ensure_group_key_is_class_if_exists group, group_config, key, Hash
  end

  # Add full path/glob files
  # --------------------------------------------
  if files
    files.each do |file|

      # This will support globbing and make sure the file exists
      to_open.concat Dir[file]
    end
  end

  # Add files by extension
  # --------------------------------------------
  if extensions
    extensions.each do |extension|

      # Glob the extension in current directory and all subdirectories
      files_found = Dir["**/*#{extension}"]
      to_open.concat files_found
    end
  end
  
  # Add files by command
  # --------------------------------------------
  if command
    to_open.concat(`#{command}`.split("\n"))
  end

  # Check for exclusions and exclude
  # --------------------------------------------
  if exclusions
    to_open = to_open.collect do |file|
      file unless file_should_be_excluded?(file, exclusions)
    end.compact
  end

  # Uniqify
  # --------------------------------------------
  to_open.uniq!
  
  # Remove any matches that are directories
  # --------------------------------------------
  to_open.delete_if {|file| Dir.exist? file }

  # Topologically sort files
  # --------------------------------------------
  if topology
    to_open = TSortedFiles.new(to_open, topology).sorted
  end
  
  # Move "first_file" to front
  # --------------------------------------------
  if first_file

    if to_open.member? first_file

      # Move to beginning of array
      to_open.delete first_file
      to_open.unshift first_file

    else
      puts "#{current_file_basename}: warning: Specified first file '#{first_file}' is not included in the list of files to open in group '#{group}'."
    end
    
  end
  
  # Ensure files were found
  # --------------------------------------------
  raise "No files found for group '#{group}'." if to_open.empty?

  # Return formatted or as-is
  # --------------------------------------------
  return format_files_to_open(to_open) if formatted

  to_open
end

.find_group_config(group) ⇒ Object



200
201
202
# File 'lib/ofe.rb', line 200

def self.find_group_config(group)
  configuration[group.to_s]
end

.format_files_to_open(files) ⇒ Object



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

def self.format_files_to_open(files)
  Shellwords.join(files)
end

.get_groupObject


GROUP —————————————-




207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ofe.rb', line 207

def self.get_group
  return :default if ARGV.count == 0

  group_match = ARGV.first

  # Return the group if we have an exact match
  return group_match.to_sym if group_names.member? group_match

  # Otherwise, try to fuzzy match
  group_names.each do |group_name|
    if group_name.start_with? group_match
      printf "Fuzzy matched group '#{group_name}'. Press ENTER to open: "
      begin
        Readline.readline
        return group_name.to_sym
      rescue Interrupt
        exit 0
      end
    end
  end

  raise_group_not_found group_match
end

.group_namesObject



101
102
103
104
105
# File 'lib/ofe.rb', line 101

def self.group_names
  require_and_parse_config_file

  configuration.keys
end

.helpObject


HELP/USAGE ———————————–




76
77
78
79
# File 'lib/ofe.rb', line 76

def self.help
  puts "usage: #{current_file_basename} [--version|-v] [--list|-l] [--groups|-g]"
  puts "           [--mk-example-config|-m] [--self|-s] [--help|-h]"
end

.list(argv) ⇒ Object


LISTING ————————————–




84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ofe.rb', line 84

def self.list(argv)
  require_and_parse_config_file

  # Support and check for "--list <group>"
  if group = argv.first
    to_puts = configuration[group]

    raise_group_not_found group unless to_puts

  # <group> was not passed
  else
    to_puts = configuration
  end

  puts JSON.pretty_generate(to_puts)
end

.list_group_namesObject



107
108
109
# File 'lib/ofe.rb', line 107

def self.list_group_names
  puts group_names
end

.mainObject


MAIN —————————————–




351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/ofe.rb', line 351

def self.main()

  begin

    require_editor_env_variable

    parse_and_execute_special_arguments
    require_and_parse_config_file

    open_group

  rescue => exception

    puts "#{current_file_basename}: fatal: #{exception}"
    exit 1

  end
end

.make_example_config_fileObject



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
# File 'lib/ofe.rb', line 111

def self.make_example_config_file
  raise "Cannot make config file because ofe.json already exists in this directory." if config_file_exists?

  empty_config_file = <<-EOS
{
"default": {
  "extensions": [],
  "files":      ["ofe.json"]
},
"docs": {
  "extensions": [".md"]
},
"git": {
  "files": [".git/config", ".gitignore"]
}
}
  EOS

  File.open(config_file_filename, "w") do |file|
    file.puts empty_config_file
  end

  puts "Example ofe.json file:"
  puts "--------------------------------------------------"
  puts empty_config_file
  puts "--------------------------------------------------"
  puts "Wrote example config file to ofe.json"

end

.open_group(group = get_group) ⇒ Object


OPENING ————————————–




344
345
346
# File 'lib/ofe.rb', line 344

def self.open_group(group=get_group)
  system "#{editor} #{files_to_open_for_group(group, formatted: true)}"
end

.open_selfObject



141
142
143
144
145
# File 'lib/ofe.rb', line 141

def self.open_self
  require_config_file

  system "#{editor} #{config_file_filename}"
end

.parse_and_execute_special_argumentsObject



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/ofe.rb', line 41

def self.parse_and_execute_special_arguments
  case ARGV.first
  when "--list", "-l"
    list ARGV[1..ARGV.length-1]
    exit

  when "--groups", "-g"
    list_group_names
    exit

  when "--mk-example-config", "-m"
    make_example_config_file
    exit
  
  when "--self", "-s"
    open_self
    exit
  
  when "--help", "-h"
    help
    exit

  end
end

.raise_group_not_found(group) ⇒ Object


UTILITY->ERRORS ——————————




69
70
71
# File 'lib/ofe.rb', line 69

def self.raise_group_not_found(group)
  raise "Group '#{group}' not found in ofe.json.  Try '#{current_file_basename} --list' to list groups or '#{current_file_basename} --help'."
end

.require_and_parse_config_fileObject



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ofe.rb', line 187

def self.require_and_parse_config_file
  require_config_file

  begin

    self.configuration = JSON.parse(File.open(config_file_filename).read)
  
  rescue => exception
    raise "Cannot parse ofe.json because its JSON is invalid."
  end

end

.require_config_fileObject



181
182
183
184
185
# File 'lib/ofe.rb', line 181

def self.require_config_file
  return true if config_file_exists?

  raise "fatal: No ofe.json config file found in this directory."
end

.require_editor_env_variableObject



147
148
149
150
151
# File 'lib/ofe.rb', line 147

def self.require_editor_env_variable
  unless ENV["EDITOR"]
    raise "EDITOR environment variable is not set."
  end
end