Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: build/android/gyp/java_cpp_enum.py

Issue 934873002: Improve CLI interface of C++ enums to Java generator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments & rebased Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | build/android/gyp/java_cpp_enum_tests.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright 2014 The Chromium Authors. All rights reserved. 3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 import collections 7 import collections
8 import re 8 import re
9 import optparse 9 import optparse
10 import os 10 import os
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 original_enum_name=enum_start.groups()[1], 221 original_enum_name=enum_start.groups()[1],
222 fixed_type=enum_start.groups()[3]) 222 fixed_type=enum_start.groups()[3])
223 self._in_enum = True 223 self._in_enum = True
224 224
225 def GetScriptName(): 225 def GetScriptName():
226 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep) 226 script_components = os.path.abspath(sys.argv[0]).split(os.path.sep)
227 build_index = script_components.index('build') 227 build_index = script_components.index('build')
228 return os.sep.join(script_components[build_index:]) 228 return os.sep.join(script_components[build_index:])
229 229
230 230
231 def DoGenerate(options, source_paths): 231 def DoGenerate(output_dir, source_paths, print_output_only=False):
232 output_paths = [] 232 output_paths = []
233 for source_path in source_paths: 233 for source_path in source_paths:
234 enum_definitions = DoParseHeaderFile(source_path) 234 enum_definitions = DoParseHeaderFile(source_path)
235 if not enum_definitions:
236 raise Exception('No enums found in %s\n'
237 'Did you forget prefixing enums with '
238 '"// GENERATED_JAVA_ENUM_PACKAGE: foo"?' %
239 source_path)
235 for enum_definition in enum_definitions: 240 for enum_definition in enum_definitions:
236 package_path = enum_definition.enum_package.replace('.', os.path.sep) 241 package_path = enum_definition.enum_package.replace('.', os.path.sep)
237 file_name = enum_definition.class_name + '.java' 242 file_name = enum_definition.class_name + '.java'
238 output_path = os.path.join(options.output_dir, package_path, file_name) 243 output_path = os.path.join(output_dir, package_path, file_name)
239 output_paths.append(output_path) 244 output_paths.append(output_path)
240 if not options.print_output_only: 245 if not print_output_only:
241 build_utils.MakeDirectory(os.path.dirname(output_path)) 246 build_utils.MakeDirectory(os.path.dirname(output_path))
242 DoWriteOutput(source_path, output_path, enum_definition) 247 DoWriteOutput(source_path, output_path, enum_definition)
243 return output_paths 248 return output_paths
244 249
245 250
246 def DoParseHeaderFile(path): 251 def DoParseHeaderFile(path):
247 with open(path) as f: 252 with open(path) as f:
248 return HeaderParser(f.readlines(), path).ParseDefinitions() 253 return HeaderParser(f.readlines(), path).ParseDefinitions()
249 254
250 255
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 def AssertFilesList(output_paths, assert_files_list): 298 def AssertFilesList(output_paths, assert_files_list):
294 actual = set(output_paths) 299 actual = set(output_paths)
295 expected = set(assert_files_list) 300 expected = set(assert_files_list)
296 if not actual == expected: 301 if not actual == expected:
297 need_to_add = list(actual - expected) 302 need_to_add = list(actual - expected)
298 need_to_remove = list(expected - actual) 303 need_to_remove = list(expected - actual)
299 raise Exception('Output files list does not match expectations. Please ' 304 raise Exception('Output files list does not match expectations. Please '
300 'add %s and remove %s.' % (need_to_add, need_to_remove)) 305 'add %s and remove %s.' % (need_to_add, need_to_remove))
301 306
302 def DoMain(argv): 307 def DoMain(argv):
303 parser = optparse.OptionParser() 308 usage = 'usage: %prog [options] output_dir input_file(s)...'
309 parser = optparse.OptionParser(usage=usage)
304 310
305 parser.add_option('--assert_file', action="append", default=[], 311 parser.add_option('--assert_file', action="append", default=[],
306 dest="assert_files_list", help='Assert that the given ' 312 dest="assert_files_list", help='Assert that the given '
307 'file is an output. There can be multiple occurrences of ' 313 'file is an output. There can be multiple occurrences of '
308 'this flag.') 314 'this flag.')
309 parser.add_option('--output_dir', help='Base path for generated files.')
310 parser.add_option('--print_output_only', help='Only print output paths.', 315 parser.add_option('--print_output_only', help='Only print output paths.',
311 action='store_true') 316 action='store_true')
317 parser.add_option('--verbose', help='Print more information.',
318 action='store_true')
312 319
313 options, args = parser.parse_args(argv) 320 options, args = parser.parse_args(argv)
314 321 if len(args) < 2:
315 output_paths = DoGenerate(options, args) 322 parser.error('Need to specify output directory and at least one input file')
323 output_paths = DoGenerate(args[0], args[1:],
324 print_output_only=options.print_output_only)
316 325
317 if options.assert_files_list: 326 if options.assert_files_list:
318 AssertFilesList(output_paths, options.assert_files_list) 327 AssertFilesList(output_paths, options.assert_files_list)
319 328
320 return " ".join(output_paths) 329 if options.verbose:
330 print 'Output paths:'
331 print '\n'.join(output_paths)
332
333 return ' '.join(output_paths)
321 334
322 if __name__ == '__main__': 335 if __name__ == '__main__':
323 DoMain(sys.argv[1:]) 336 DoMain(sys.argv[1:])
OLDNEW
« no previous file with comments | « no previous file | build/android/gyp/java_cpp_enum_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698