Class: Uppercrust::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/uppercrust/generator.rb

Class Method Summary collapse

Class Method Details

.generate(path, base_only) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/uppercrust/generator.rb', line 9

def self.generate(path, base_only)
  unless File.readable?(path)
    puts "Cannot read path #{path}"
    return
  end

  files = read_files(path, base_only)
  if files.length > 0
    FileUtils.mkdir("output")
    output = []
    files.each do |generate|
      puts "Generate #{generate.file_name}"
      output << generate.generate_files
    end
    output.each do |generated|
      generated.each { |name, contents| File.open(File.join("output", name), "w") { |f| f.write(contents) } }
    end
  end
end

.glob_dir_for_json(path) ⇒ Object



40
41
42
# File 'lib/uppercrust/generator.rb', line 40

def self.glob_dir_for_json(path)
  Dir.glob(File.join(File.absolute_path(path), '**', '*.json'))
end

.read_files(path, base_only) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/uppercrust/generator.rb', line 29

def self.read_files(path, base_only)
  files = File.file?(File.absolute_path(path)) ? [File.absolute_path(path)] : glob_dir_for_json(path)

  output = []
  files.each do |json_file|
    data = JSON.parse(File.open(json_file, 'r') { |f| f.read })
    output << Parser.new(json_file, data, base_only)
  end
  output
end