18 parser = optparse.OptionParser()
24 help=
"Absolute path to generate files",
31 help=
"Absolute path to fbcode directory",
38 help=
"Relative path of input file",
45 help=
"namespace / package of output file",
51 choices=[
"java",
"cpp"],
53 help=
"File type to generate",
60 help=
"Relative path to cpp header",
62 options, _ = parser.parse_args()
64 assert options.install_dir
is not None,
"Missing arg: --install_dir" 65 assert options.fbcode_dir
is not None,
"Missing arg: --fbcode_dir" 66 assert options.input_files
is not None,
"Missing arg: --input_files" 67 assert options.output_scope
is not None,
"Missing arg: --output_scope" 68 assert options.output_type
is not None,
"Missing arg: --output_type" 70 file_names = options.input_files.split(
",")
71 for file_name
in file_names:
73 class_name = os.path.basename(file_name).
split(
".")[0]
77 with open(file_name,
"r") as inf: 79 sp = re.match(
r"(.*), \"(.*)\"", line, re.I)
81 items.append((sp.group(1), sp.group(2)))
83 if options.output_type ==
"java":
84 gen_java(items, class_name, options.install_dir, options.output_scope)
86 elif options.output_type ==
"cpp":
87 assert options.header_path
is not None,
"Missing arg: --header_path" 89 gen_cpp_header(items, class_name, options.install_dir, options.output_scope)
100 Generate java interface class 104 def gen_java(items, class_name, install_dir, output_scope):
105 packages = output_scope.split(
".")
106 file_name =
"%s.java" % class_name
107 file_path = os.path.join(*([install_dir,
"src"] + packages))
108 output_file = os.path.join(file_path, file_name)
110 if not os.path.exists(file_path):
111 os.makedirs(file_path)
113 with open(output_file,
"w+")
as outf:
114 outf.write(
"// Copyright 2015-present Facebook. All Rights Reserved.\n")
115 outf.write(
"// ** AUTOGENERATED FILE. DO NOT HAND-EDIT **\n\n")
116 outf.write(
"package %s;\n\n" %
".".
join(packages))
117 outf.write(
"public interface %s {\n" % class_name)
121 ' public static final String %s = "%s";\n' % (item[0], item[1])
128 Generate cpp enum class and provide convert function from / to string 133 namespaces = output_scope.split(
"::")
134 file_name =
"%s.h" % class_name
135 output_file = os.path.join(install_dir, file_name)
137 with open(output_file,
"w+")
as outf:
138 outf.write(
"// Copyright 2015-present Facebook. All Rights Reserved.\n")
139 outf.write(
"// ** AUTOGENERATED FILE. DO NOT HAND-EDIT **\n\n")
140 outf.write(
"#pragma once\n\n")
141 outf.write(
"#include <string>\n\n")
142 for ns
in namespaces:
143 outf.write(
"namespace %s { " % ns)
147 outf.write(
"enum class %s {\n" % class_name)
149 outf.write(
" %s,\n" % item[0])
154 "extern const std::string& get%sString(%s);\n" % (class_name, class_name)
158 "extern %s get%sFromString(const std::string&);\n" 159 % (class_name, class_name)
167 Generate cpp const string and implement convert function 172 namespaces = output_scope.split(
"::")
173 file_name =
"%s.cpp" % class_name
174 output_file = os.path.join(install_dir, file_name)
176 with open(output_file,
"w+")
as outf:
177 outf.write(
"// Copyright 2015-present Facebook. All Rights Reserved.\n")
178 outf.write(
"// ** AUTOGENERATED FILE. DO NOT HAND-EDIT **\n\n")
179 outf.write(
'#include "%s/%s.h"\n\n' % (header_path, class_name))
180 outf.write(
"#include <stdexcept>\n\n")
182 for ns
in namespaces:
183 outf.write(
"namespace %s { " % ns)
189 'static const std::string k%s%s = "%s";\n' 190 % (class_name, item[0], item[1])
195 "const std::string& get%sString(%s type) {\n" % (class_name, class_name)
198 outf.write(
' static const std::string k%sInvalidType = "";\n' % class_name)
200 outf.write(
"\n switch (type) {\n")
203 " case %s::%s : return k%s%s;\n" 204 % (class_name, item[0], class_name, item[0])
207 outf.write(
" return k%sInvalidType;\n" % class_name)
211 " %s get%sFromString(const std::string& str) {\n" 212 % (class_name, class_name)
216 " if (str == k%s%s) return %s::%s;\n" 217 % (class_name, item[0], class_name, item[0])
220 " throw std::invalid_argument" 221 ' ("No matching %s from string");\n' % (class_name)
230 if __name__ ==
"__main__":
231 sys.exit(
main(sys.argv))
S split(const StringPiece source, char delimiter)
def gen_cpp_header(items, class_name, install_dir, output_scope)
def gen_java(items, class_name, install_dir, output_scope)
def gen_cpp_source(items, class_name, install_dir, header_path, output_scope)