Class: JSON2Ruby::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/json2ruby/cli.rb

Class Method Summary collapse

Class Method Details

.display_entity(hsh, ent) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/json2ruby/cli.rb', line 174

def self.display_entity(hsh, ent)
  puts "- #{ent.name} (#{ent.class.short_name} - #{hsh})"
  if ent.is_a?(Entity)
    ent.attributes.each { |ak,av| puts "  #{ak}: #{av.name}" }
  elsif ent.is_a?(Collection)
    puts "  (Types): #{ent.ruby_types.map { |h,ent| ent.name }.join(',')}"
  end
end

.ensure_output_dir(options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/json2ruby/cli.rb', line 29

def self.ensure_output_dir(options)
  puts "Output Directory: #{options[:outputdir]}" if options[:verbose]
  unless Dir.exists?(options[:outputdir])
    puts "Creating Output Directory..." if options[:verbose]
    Dir.mkdir(options[:outputdir])
  end
end

.get_cli_optionsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/json2ruby/cli.rb', line 37

def self.get_cli_options

  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options] <file.json> [<file.json>....]"

    opts.on("-o", "--outputdir OUTPUTDIR", "Output directory") do |v|
      options[:outputdir] = v
    end

    opts.on("-n", "--namespace MODULENAME", "Module namespace path") do |v|
      options[:namespace] = v
    end

    opts.on("-s", "--superclass SUPERCLASS", "Class ancestor") do |v|
      options[:superclass_name] = v
    end

    opts.on("-r", "--require REQUIRE", "Require module in file") do |v|
      options[:require] ||= []
      options[:require] << v
    end

    opts.on("-i", "--include INCLUDE", "Include Class/Module in file") do |v|
      options[:include] ||= []
      options[:include] << v
    end

    opts.on("-e", "--extend EXTEND", "Extend from Class/Module in file") do |v|
      options[:extend] ||= []
      options[:extend] << v
    end

    opts.on("-M", "--modules", "Create Modules, not classes") do |v|
      options[:modules] = true
    end

    opts.on("-a", "--attributemethod METHODNAME", "Use attribute method instead of attr_accessor") do |v|
      options[:attributemethod] = v
    end

    opts.on("-c", "--collectionmethod METHODNAME", "Use collection method instead of attr_accessor") do |v|
      options[:collectionmethod] = v
    end

    opts.on("-t", "--types", "Include type in attribute definition call") do |v|
      options[:includetypes] = true
    end

    opts.on("-b", "--baseless", "Don't generate classes/modules for the root JSON in each file") do |v|
      options[:baseless] = true
    end

    opts.on("-f", "--forceoverwrite", "Overwrite Existing files") do |v|
      options[:forceoverwrite] = v
    end

    opts.on("-N", "--forcenumeric", "Use Numeric instead of Integer/Float") do |v|
      options[:forcenumeric] = v
    end

    opts.on("-v", "--verbose", "Verbose") do |v|
      options[:verbose] = v
    end
  end.parse!

  # Defaults
  options[:outputdir] ||= File.expand_path("./classes")
  options[:namespace] ||= ""
  options[:attributemethod] ||= "attr_accessor"
  options[:collectionmethod] ||= "attr_accessor"
  options[:includetypes] ||= false
  options[:baseless] ||= false
  options[:forceoverwrite] ||= false
  options[:verbose] ||= false

  options[:modulenames] = options[:namespace].split("::")

  options
end

.parse_files(options) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/json2ruby/cli.rb', line 118

def self.parse_files(options)
  # Reset the object cache
  Entity.reset_parse

  # Load and parse each JSON file
  puts "Parsing Files..." if options[:verbose]

  rootclasses = []

  ARGV.each do |filename|
    filename = File.expand_path(filename)
    puts "Processing: #{filename}" if options[:verbose]

    file = File.read(filename)
    data_hash = JSON.parse(file)

    rootclasses << Entity.parse_from(File.basename(filename,'.*'), data_hash, options)
  end

  rootclasses
end

.runObject



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

def self.run

  puts "json2ruby v#{VERSION}\n"

  # Do the cmdline options
  options = get_cli_options

  # Ensure Output Directory
  options[:outputdir] = File.expand_path(options[:outputdir], File.dirname(__FILE__))      
  ensure_output_dir(options)

  # Parse Files
  rootclasses = parse_files(options)

  # Write out Ruby (see what I'm doing here?)
  writer = JSON2Ruby::RubyWriter

  # Write Output
  write_files(rootclasses, writer, options)
end

.write_files(rootclasses, writer, options) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/json2ruby/cli.rb', line 140

def self.write_files(rootclasses, writer, options)
  files = 0
  Entity.entities.each do |k,v|
    next if options[:baseless] and rootclasses.include?(v)

    display_entity(k,v) if options[:verbose] && !v.is_a?(Primitive)

    if v.is_a?(Entity)
      indent = 0
      out = ""
      options[:modulenames].each do |v|
        out += (' '*indent)+"module #{v}\r\n"
        indent += 2
      end
      out += writer.to_code(v, indent,options)
      while indent>0
        indent -= 2
        out += (' '*indent)+"end\r\n"
      end

      filename = options[:outputdir]+"/#{v.name}.rb"
      if File.exists?(filename) && !options[:forceoverwrite]
        $stderr.puts "File #{filename} exists. Use -f to overwrite."
      else
        File.write(filename, out)
        files += 1
      end
    end
  end

  # Done
  puts "Done, Generated #{files} file#{files==1 ? '' : 's'}"
end