From ce67371b1246260b034a354c052e73c63618fcb8 Mon Sep 17 00:00:00 2001 From: Paul Ritter Date: Wed, 5 May 2021 14:30:46 +0200 Subject: [PATCH] Revert "Deprecates RUN_THIS.py in favor of manually doin' it (#3930)" (#3933) This reverts commit 94082e07a76357df0efe54552d4685d1ddda95da. --- BuildChecker/.gitignore | 5 + BuildChecker/BuildChecker.csproj | 46 +++++++++ BuildChecker/git_helper.py | 104 ++++++++++++++++++++ {Hooks => BuildChecker/hooks}/post-checkout | 0 {Hooks => BuildChecker/hooks}/post-merge | 0 README.md | 5 +- RUN_THIS.py | 18 ++++ 7 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 BuildChecker/.gitignore create mode 100644 BuildChecker/BuildChecker.csproj create mode 100644 BuildChecker/git_helper.py rename {Hooks => BuildChecker/hooks}/post-checkout (100%) mode change 100644 => 100755 rename {Hooks => BuildChecker/hooks}/post-merge (100%) mode change 100644 => 100755 create mode 100755 RUN_THIS.py diff --git a/BuildChecker/.gitignore b/BuildChecker/.gitignore new file mode 100644 index 0000000000..5eeecc18fc --- /dev/null +++ b/BuildChecker/.gitignore @@ -0,0 +1,5 @@ +INSTALLED_HOOKS_VERSION +DISABLE_SUBMODULE_AUTOUPDATE +*.nuget* +project.assets.json +project.packagespec.json \ No newline at end of file diff --git a/BuildChecker/BuildChecker.csproj b/BuildChecker/BuildChecker.csproj new file mode 100644 index 0000000000..7acdbe9935 --- /dev/null +++ b/BuildChecker/BuildChecker.csproj @@ -0,0 +1,46 @@ + + + + + python3 + py -3 + {C899FCA4-7037-4E49-ABC2-44DE72487110} + .NETFramework, Version=v4.7.2 + false + + + Library + + + + + + bin\Debug\ + + + bin\Release\ + + + + + + + + + + + + + diff --git a/BuildChecker/git_helper.py b/BuildChecker/git_helper.py new file mode 100644 index 0000000000..00fd93a43d --- /dev/null +++ b/BuildChecker/git_helper.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +# Installs git hooks, updates them, updates submodules, that kind of thing. + +import subprocess +import sys +import os +import shutil +from pathlib import Path +from typing import List + +SOLUTION_PATH = Path("..") / "SpaceStation14.sln" +# If this doesn't match the saved version we overwrite them all. +CURRENT_HOOKS_VERSION = "2" +QUIET = len(sys.argv) == 2 and sys.argv[1] == "--quiet" + + +def run_command(command: List[str], capture: bool = False) -> subprocess.CompletedProcess: + """ + Runs a command with pretty output. + """ + text = ' '.join(command) + if not QUIET: + print("$ {}".format(text)) + + sys.stdout.flush() + + completed = None + + if capture: + completed = subprocess.run(command, cwd="..", stdout=subprocess.PIPE) + else: + completed = subprocess.run(command, cwd="..") + + if completed.returncode != 0: + print("Error: command exited with code {}!".format(completed.returncode)) + + return completed + + +def update_submodules(): + """ + Updates all submodules. + """ + + if os.path.isfile("DISABLE_SUBMODULE_AUTOUPDATE"): + return + + # If the status doesn't match, force VS to reload the solution. + # status = run_command(["git", "submodule", "status"], capture=True) + run_command(["git", "submodule", "update", "--init", "--recursive"]) + # status2 = run_command(["git", "submodule", "status"], capture=True) + + # Something changed. + # if status.stdout != status2.stdout: + # print("Git submodules changed. Reloading solution.") + # reset_solution() + + +def install_hooks(): + """ + Installs the necessary git hooks into .git/hooks. + """ + + # Read version file. + if os.path.isfile("INSTALLED_HOOKS_VERSION"): + with open("INSTALLED_HOOKS_VERSION", "r") as f: + if f.read() == CURRENT_HOOKS_VERSION: + if not QUIET: + print("No hooks change detected.") + return + + with open("INSTALLED_HOOKS_VERSION", "w") as f: + f.write(CURRENT_HOOKS_VERSION) + + print("Hooks need updating.") + + hooks_target_dir = Path("..")/".git"/"hooks" + hooks_source_dir = Path("hooks") + + # Clear entire tree since we need to kill deleted files too. + for filename in os.listdir(str(hooks_target_dir)): + os.remove(str(hooks_target_dir/filename)) + + for filename in os.listdir(str(hooks_source_dir)): + print("Copying hook {}".format(filename)) + shutil.copy2(str(hooks_source_dir/filename), + str(hooks_target_dir/filename)) + + +def reset_solution(): + """ + Force VS to think the solution has been changed to prompt the user to reload it, thus fixing any load errors. + """ + + with SOLUTION_PATH.open("r") as f: + content = f.read() + + with SOLUTION_PATH.open("w") as f: + f.write(content) + + +if __name__ == '__main__': + install_hooks() + update_submodules() diff --git a/Hooks/post-checkout b/BuildChecker/hooks/post-checkout old mode 100644 new mode 100755 similarity index 100% rename from Hooks/post-checkout rename to BuildChecker/hooks/post-checkout diff --git a/Hooks/post-merge b/BuildChecker/hooks/post-merge old mode 100644 new mode 100755 similarity index 100% rename from Hooks/post-merge rename to BuildChecker/hooks/post-merge diff --git a/README.md b/README.md index 8752889c2e..6332e7ccf0 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,8 @@ We are happy to accept contributions from anybody. Get in Discord if you want to ## Building 1. Clone this repo. -2. In the root folder run `git submodule update --init --recursive` -3. Copy the two files from the `Hooks` folder to `.git/hooks` -4. Compile the solution. +2. Run `RUN_THIS.py` to init submodules and download the engine. +3. Compile the solution. [More detailed instructions on building the project.](https://hackmd.io/@ss14/docs/%2FBZkI4RlUQbm09QWrXCZ3kg) diff --git a/RUN_THIS.py b/RUN_THIS.py new file mode 100755 index 0000000000..b7a8fcaa35 --- /dev/null +++ b/RUN_THIS.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Import future so people on py2 still get the clear error that they need to upgrade. +from __future__ import print_function +import sys +import subprocess + +IS_WINDOWS = sys.platform in ("win32", "cygwin") + +version = sys.version_info +if version.major < 3 or (version.major == 3 and version.minor < 5): + print("ERROR: You need at least Python 3.5 to build SS14.") + sys.exit(1) + +if IS_WINDOWS: + subprocess.run(["py", "-3", "git_helper.py"], cwd="BuildChecker") +else: + subprocess.run(["python3", "git_helper.py"], cwd="BuildChecker")