Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added scons tool to run black automatically during the build #52765

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,6 @@ gcov.css
# https://clangd.llvm.org/ cache folder
.clangd/
.cache/

# auto black format tool
.black.out
4 changes: 4 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ opts.Add(BoolVariable("modules_enabled_by_default", "If no, disable all modules
opts.Add(BoolVariable("no_editor_splash", "Don't use the custom splash screen for the editor", False))
opts.Add("system_certs_path", "Use this path as SSL certificates default for editor (for package maintainers)", "")
opts.Add(BoolVariable("use_precise_math_checks", "Math checks use very precise epsilon (debug option)", False))
opts.Add(BoolVariable("auto_black", "Run black automatically every build.", True))

# Thirdparty libraries
opts.Add(BoolVariable("builtin_bullet", "Use the built-in Bullet library", True))
Expand Down Expand Up @@ -193,6 +194,9 @@ opts.Add("LINKFLAGS", "Custom flags for the linker")
# in following code (especially platform and custom_modules).
opts.Update(env_base)

if env_base["auto_black"]:
env_base.Tool("black")

# Platform selection: validate input, and add options.

selected_platform = ""
Expand Down
63 changes: 63 additions & 0 deletions site_scons/site_tools/black.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
import subprocess
from git import Repo
import SCons
from SCons.Script import *

black_bin = None


def run_changeset_black(env, target, source):
subprocess.getoutput("black -l 120 " + " ".join([s.path for s in source]) + " > " + target[0].path)
# because we have modified the files during a build, we should clear the nodes.
# this prevent unnecessary rebuids.
for s in source:
s.del_binfo()
s.clear_memoized_values()
s.ninfo = s.new_ninfo()
s.executor_cleanup()


def exists(env):
global black_bin

black_bin = env.WhereIs("black")
if not black_bin:
black_bin = env.WhereIs("black", os.environ["PATH"])

return black_bin


def generate(env):

if not exists(env):
print("black not found, not running black formatter.")
return

env["BLACK_OUTPUT"] = env.get("BLACK_OUTPUT", ".black.out")
repo = Repo(env.Dir("#").abspath)

files = [
file
for file in repo.git.diff("HEAD~1..HEAD", name_only=True).split("\n") + repo.untracked_files
if (
not file.startswith("thirdparty")
and (file.endswith("SConstruct") or file.endswith("SCsub") or file.endswith(".py"))
)
]

if files:
changeset_black = env.Command(
target="$BLACK_OUTPUT",
source=files,
action=SCons.Action.Action(run_changeset_black, cmdstr="Formatting changeset with black..."),
)

# these next few lines for scons to always run the black formatter task
Default(changeset_black)
for target in COMMAND_LINE_TARGETS:
if target == "run-black":
continue
env.Depends(target, changeset_black)

env.Alias("run-black", changeset_black)