Class: JSON2Ruby::CLI

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

Overview

The CLI (Command Line Interface) functionality class for the json2ruby executable

Class Method Summary collapse

Class Method Details

.display_entity(hsh, ent) ⇒ Object

Display the Entity supplied in ent with the supplied hash value hsh to STDOUT



181
182
183
184
185
186
187
188
# File 'lib/json2ruby/cli.rb', line 181

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

Create the output directory with the options if it does not exist.



32
33
34
35
36
37
38
# File 'lib/json2ruby/cli.rb', line 32

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

Process ARGV for command line switches and return the options hash.



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
117
118
119
# File 'lib/json2ruby/cli.rb', line 41

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

Parse all JSON files in ARGV and build the Entity cache, using the supplied options Hash.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/json2ruby/cli.rb', line 122

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

Run the json2ruby command, using arguments in ARGV.



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

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

Write out all types in the Entity cache, except primitives and those contained in the rootclasses array, to the provided writer with the supplied options Hash.



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
173
174
175
176
177
178
# File 'lib/json2ruby/cli.rb', line 146

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