Last active
October 19, 2016 12:30
-
-
Save mbildner/869aec825a296cbefbd79ac621446c2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'active_support/inflector' | |
require 'digest' | |
require 'fileutils' | |
PACKAGE_PATH = "com.mbildner.domain" | |
class JavaClass | |
attr_accessor :name, :body | |
def initialize(name, body) | |
@name = name | |
@body = body | |
end | |
def package_file_path | |
@_package_file_path ||= File.join(*PACKAGE_PATH.split(".")) | |
end | |
def file_name | |
"#{name}.java" | |
end | |
def path | |
File.join(package_file_path, file_name) | |
end | |
end | |
class Parser | |
def initialize(finalize) | |
@finalize = finalize | |
@classes = [] | |
end | |
attr_reader :classes | |
def parse(source_hash, base_class_name) | |
if source_hash.class == Array | |
collection_name = "#{base_class_name.capitalize}".pluralize | |
base_class_name = base_class_name + "Collection" | |
source_hash = { | |
collection_name => source_hash | |
} | |
end | |
js_to_java(source_hash, base_class_name) | |
end | |
private | |
def java_class(name, members=nil) | |
throw Exception.new("java class must have a name") if name == nil | |
java_class = <<-JAVA_CODE | |
package #{PACKAGE_PATH} | |
public class #{name} { | |
#{members} | |
} | |
JAVA_CODE | |
JavaClass.new(name, java_class) | |
end | |
def js_to_java(js, class_name=nil, root=false) | |
case(js) | |
when TrueClass | |
'boolean' | |
when FalseClass | |
'boolean' | |
when NilClass | |
'null' | |
when String | |
'String' | |
when Fixnum | |
'Number' | |
when Float | |
'Number' | |
when Array | |
types = js.map { |element| js_to_java(element, class_name) } | |
type = types.uniq.size == 1 ? types.first : "Object" | |
"List<#{type.singularize}>" | |
when Hash | |
members = js.map do |(key, value)| | |
"\t #{optional_final_keyword} #{js_to_java(value, key.capitalize)} #{root ? key.downcase : key};" | |
end.join("\n") | |
@classes << java_class(class_name, members) | |
class_name | |
else | |
throw Exception.new("Not clear what type this is, failing to build") | |
end | |
end | |
def optional_final_keyword | |
"final" if @finalize | |
end | |
end | |
finalize = ARGV.include? "--finalize" | |
source_json_file = ARGV[0] | |
source_string = File.read(source_json_file) | |
source_json = JSON.parse(source_string) | |
parser = Parser.new(finalize=finalize) | |
base_name = source_json_file.gsub(/\.json$/, "").capitalize | |
parser.parse(source_json, base_name) | |
parser.classes.uniq do |java_class| | |
java_class.name | |
end.map do |java_class| | |
FileUtils.mkdir_p(java_class.package_file_path) | |
File.open(java_class.path, 'w') { |f| f.write(java_class.body) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment