format_app.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. # format_app.py
  3. import argparse
  4. import os
  5. import re
  6. import subprocess
  7. import sys
  8. def handle_cli_args():
  9. parser = argparse.ArgumentParser(description='A script for formatting a given app within emqx')
  10. parser.add_argument("-a", "--application", required=True, help="The application which is to be formatted.")
  11. parser.add_argument('-b', "--branch", help="The git branch to be switched to before formatting the code. Required unless the -f option is passed in which case the value will be ignored even if provided.")
  12. parser.add_argument('-f', "--format_in_place", default=False, action="store_true", help="Pass the -f option to format on the current git branch, otherwise a branch name must be provided by passing the -b option.")
  13. args = parser.parse_args()
  14. if not args.format_in_place and not args.branch:
  15. sys.exit("A new git branch name must be provided with the -b option unless the -f option is given to format the code in place.")
  16. return args
  17. def get_app_path(application):
  18. root_path = os.path.dirname(os.path.realpath(__file__))
  19. full_path = f"{root_path}/../apps/{application}"
  20. isdir = os.path.isdir(full_path)
  21. if isdir:
  22. return full_path
  23. sys.exit(f"The application provided ({application}) does not appear at the expected location: {full_path}")
  24. def maybe_switch_git_branch(format_in_place, branch):
  25. if not format_in_place:
  26. PIPE = subprocess.PIPE
  27. process = subprocess.Popen(["git", "status"], stdout=PIPE, text=True)
  28. stdoutput, stderroutput = process.communicate()
  29. if f"On branch {branch}" in stdoutput:
  30. return
  31. elif "working tree clean" in stdoutput:
  32. subprocess.call(["git", "checkout", "-b", branch])
  33. else:
  34. sys.exit("Cannot switch git branches while there are changes waiting to be committed.")
  35. def maybe_add_rebar_plugin():
  36. with open("rebar.config", "r+") as f:
  37. text = f.read()
  38. erlfmt_pattern = "\{project_plugins.+?erlfmt.+\}"
  39. plugin_pattern = "\{project_plugins.+?\}"
  40. if not re.search(erlfmt_pattern, text) and not re.search(plugin_pattern, text):
  41. f.write("\n{project_plugins, [erlfmt]}.\n")
  42. elif not re.search(erlfmt_pattern, text) and re.search(plugin_pattern, text):
  43. sys.exit("Could not find the erlfmt plugin but the 'project_plugins' declaration already exists. Please add 'erlfmt' to the plugins list manually and rerun the script.")
  44. def execute_formatting():
  45. subprocess.call(["rebar3", "fmt", "-w"])
  46. def main():
  47. args = handle_cli_args()
  48. app_path = get_app_path(args.application)
  49. os.chdir(app_path)
  50. maybe_switch_git_branch(args.format_in_place, args.branch)
  51. maybe_add_rebar_plugin()
  52. execute_formatting()
  53. if __name__ == "__main__":
  54. main()