comparison mxtool/mx.py @ 7524:bbaa734b3627

added 'mx eclipseformat' command for applying the Eclipse Code Formatter to the Java source files in a suite
author Doug Simon <doug.simon@oracle.com>
date Tue, 22 Jan 2013 22:46:13 +0100
parents 442668d41bc2
children 4584ca2618d5
comparison
equal deleted inserted replaced
7523:e018bde4b89d 7524:bbaa734b3627
1461 1461
1462 if suppliedParser: 1462 if suppliedParser:
1463 return args 1463 return args
1464 return None 1464 return None
1465 1465
1466 def eclipseformat(args):
1467 """run the Eclipse Code Formatter on the Java sources
1468
1469 The exit code 1 denotes that at least one file was modified."""
1470
1471 parser = ArgumentParser(prog='mx eclipseformat')
1472 parser.add_argument('-e', '--eclipse-exe', help='location of the Eclipse executable')
1473 parser.add_argument('-C', '--no-backup', action='store_false', dest='backup', help='do not save backup of modified files')
1474
1475 args = parser.parse_args(args)
1476 if args.eclipse_exe is None:
1477 args.eclipse_exe = os.environ.get('ECLIPSE_EXE')
1478 if args.eclipse_exe is None:
1479 abort('Could not find Eclipse executable. Use -e option or ensure ECLIPSE_EXE environment variable is set.')
1480
1481 eclipseinit([], buildProcessorJars=False)
1482
1483 modified = dict()
1484 for p in sorted_deps():
1485 if p.native:
1486 continue
1487 sourceDirs = p.source_dirs()
1488 prefsFile = join(p.dir, '.settings', 'org.eclipse.jdt.core.prefs')
1489
1490 if not exists(prefsFile):
1491 log('[no Eclipse Code Formatter preferences at {0} - skipping]'.format(prefsFile))
1492 continue
1493
1494 for sourceDir in sourceDirs:
1495 javafiles = dict()
1496 for root, _, files in os.walk(sourceDir):
1497 for f in [join(root, name) for name in files if name.endswith('.java') and name != 'package-info.java']:
1498 with open(f) as fp:
1499 content = fp.read()
1500 javafiles[f] = content
1501 if len(javafiles) == 0:
1502 log('[no Java sources in {0} - skipping]'.format(sourceDir))
1503 continue
1504
1505 run([args.eclipse_exe, '-nosplash', '-application', 'org.eclipse.jdt.core.JavaCodeFormatter', '-config', prefsFile] + javafiles.keys())
1506
1507 for f, oldContent in javafiles.iteritems():
1508 with open(f) as fp:
1509 newContent = fp.read()
1510 if oldContent != newContent:
1511 modified[f] = oldContent
1512
1513 log('{0} files were modified'.format(len(modified)))
1514 if len(modified) != 0:
1515 if args.backup:
1516 backup = os.path.abspath('eclipseformat.backup.zip')
1517 arcbase = _mainSuite.dir
1518 zf = zipfile.ZipFile(backup, 'w', zipfile.ZIP_DEFLATED)
1519 for f, content in modified.iteritems():
1520 arcname = os.path.relpath(f, arcbase).replace(os.sep, '/')
1521 zf.writestr(arcname, content)
1522 zf.close()
1523 log('Wrote backup of {0} modified files to {1}'.format(len(modified), backup))
1524 return 1
1525
1466 def processorjars(): 1526 def processorjars():
1467 projects = set([]) 1527 projects = set([])
1468 1528
1469 for p in sorted_deps(): 1529 for p in sorted_deps():
1470 if _needsEclipseJarBuild(p): 1530 if _needsEclipseJarBuild(p):
2814 'build': [build, '[options]'], 2874 'build': [build, '[options]'],
2815 'checkstyle': [checkstyle, ''], 2875 'checkstyle': [checkstyle, ''],
2816 'canonicalizeprojects': [canonicalizeprojects, ''], 2876 'canonicalizeprojects': [canonicalizeprojects, ''],
2817 'clean': [clean, ''], 2877 'clean': [clean, ''],
2818 'eclipseinit': [eclipseinit, ''], 2878 'eclipseinit': [eclipseinit, ''],
2879 'eclipseformat': [eclipseformat, ''],
2819 'findclass': [findclass, ''], 2880 'findclass': [findclass, ''],
2820 'help': [help_, '[command]'], 2881 'help': [help_, '[command]'],
2821 'ideclean': [ideclean, ''], 2882 'ideclean': [ideclean, ''],
2822 'ideinit': [ideinit, ''], 2883 'ideinit': [ideinit, ''],
2823 'projectgraph': [projectgraph, ''], 2884 'projectgraph': [projectgraph, ''],