Module: Ofe

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

Overview


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


Defined Under Namespace

Classes: TSortedFiles

Constant Summary collapse

@@config_json =

MODULE->VARIABLES —————————-


nil

Class Method Summary collapse

Class Method Details

.config_file_exists?Boolean

Returns:

  • (Boolean)


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

def self.config_file_exists?
  File.exist? config_file_filename
end

.config_file_filenameObject


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




137
138
139
# File 'lib/ofe.rb', line 137

def self.config_file_filename
  "ofe.json"
end

.current_file_basenameObject


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




24
25
26
# File 'lib/ofe.rb', line 24

def self.current_file_basename
  File.basename $0
end

.editorObject



117
118
119
# File 'lib/ofe.rb', line 117

def self.editor
  ENV["EDITOR"]
end

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



121
122
123
124
125
# File 'lib/ofe.rb', line 121

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)


127
128
129
130
131
132
# File 'lib/ofe.rb', line 127

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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
# File 'lib/ofe.rb', line 199

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

  raise "Group '#{group}' is not defined in your ofe.json config file." 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



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

def self.find_group_config(group)
  @@config_json[group.to_s]
end

.format_files_to_open(files) ⇒ Object



195
196
197
# File 'lib/ofe.rb', line 195

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

.get_groupObject


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




171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ofe.rb', line 171

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 '#{group_match}' was not found."
end

.group_namesObject



65
66
67
68
69
# File 'lib/ofe.rb', line 65

def self.group_names
  require_and_parse_config_file

  @@config_json.keys
end

.list(argv) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ofe.rb', line 49

def self.list(argv)
  require_and_parse_config_file

  # Support and check for "--list <group>"
  if group = argv.first
    to_puts = @@config_json[group]
    raise "Group '#{group}' not found in ofe.json." unless to_puts

  # <group> was not passed
  else
    to_puts = @@config_json
  end

  puts JSON.pretty_generate(to_puts)
end

.list_group_namesObject



71
72
73
# File 'lib/ofe.rb', line 71

def self.list_group_names
  puts group_names
end

.mainObject


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




315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/ofe.rb', line 315

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



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

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 = "{\n\"default\": {\n  \"extensions\": [],\n  \"files\":      [\"ofe.json\"]\n},\n\"docs\": {\n  \"extensions\": [\".md\"]\n},\n\"git\": {\n  \"files\": [\".git/config\", \".gitignore\"]\n}\n}\n  EOS\n\n  File.open(config_file_filename, \"w\") do |file|\n    file.puts empty_config_file\n  end\n\n  puts \"Example ofe.json file:\"\n  puts \"--------------------------------------------------\"\n  puts empty_config_file\n  puts \"--------------------------------------------------\"\n  puts \"Wrote example config file to ofe.json\"\n\nend\n"

.open_group(group = get_group) ⇒ Object


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




308
309
310
# File 'lib/ofe.rb', line 308

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

.open_selfObject



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

def self.open_self
  require_config_file

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

.parse_and_execute_special_argumentsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ofe.rb', line 28

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

  end
end

.require_and_parse_config_fileObject



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ofe.rb', line 151

def self.require_and_parse_config_file
  require_config_file

  begin

    @@config_json = 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



145
146
147
148
149
# File 'lib/ofe.rb', line 145

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



111
112
113
114
115
# File 'lib/ofe.rb', line 111

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