-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.py
More file actions
65 lines (54 loc) · 2.42 KB
/
format.py
File metadata and controls
65 lines (54 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from helpers import env, click
from helpers.util import *
import subprocess
def format_files(files, verbose):
if not files:
print(f"No files to format.")
return
print(f"Formatting {len(files)} files...")
# Run clang-format on each file
any_errors = False
for file in files:
try:
run(["clang-format", "-style=file", "-i", str(file)])
if verbose:
print(f"Formatted: {file}")
except subprocess.CalledProcessError as e:
print(f"Error formatting {file}: {e}")
any_errors = True
if any_errors:
print("-- Failed")
sys.exit(-1)
print("-- Succeeded")
@click.command()
@click.option('-n', '--name', envvar="CI_NAME", help="Name of the project or plugin (without .uproject/.uplugin)")
@click.option('-p', '--path', envvar="CI_PATH", type=click.Path(exists=True), help=f"{colors.OKCYAN}(default: current path){colors.ENDC} Path that contains all project/plugin files")
@click.option('-f', '--filters', type=str, multiple=True, envvar="CI_FILTER", help=f"{colors.OKCYAN}(default: *.h *.hpp *.c *.cpp){colors.ENDC} Glob filter of files to include")
@click.option('-v', '--verbose', is_flag=True)
def format_cli(name, path, filters, verbose):
"""Formats all source files in a project. """
if not filters:
filters = ("*.h", "*.hpp", "*.c", "*.cpp")
files = set()
try:
plugin = env.Plugin(name, path)
path = Path(os.path.join(plugin.path, "Source"))
click.echo(f"{colors.WARNING}-- Formatting plugin {colors.OKGREEN}{plugin.name} {colors.WARNING}({path}){colors.ENDC}")
except:
try:
project = env.Project(name, path)
path = Path(os.path.join(project.path, "Source"))
click.echo(f"{colors.WARNING}-- Formatting project {colors.OKGREEN}{project.name} {colors.WARNING}({path}){colors.ENDC}")
except:
path = Path(os.path.abspath(path) if path else os.getcwd())
if path.is_file():
click.echo(f"{colors.WARNING}-- Formatting file {colors.OKGREEN}{path}{colors.ENDC}")
files.add(path)
else:
click.echo(f"{colors.WARNING}-- Formatting folder {colors.OKGREEN}{path}{colors.ENDC}")
if not path.is_file():
for ext in filters:
files.update(path.rglob(ext))
format_files(files, verbose)
if __name__ == '__main__':
format_cli()