Skip to content
Open
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
84 changes: 84 additions & 0 deletions CSharpWasm/buildAndCopy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
import subprocess
import sys
from pathlib import Path
import shutil

# Paths are defined relative to this script's location
HERE = Path(__file__).resolve().parent
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing. maybe we can name it something more clear like SCRIPT_DIR etc. that way it follows the standards in the below constants and is more explanatory as to what "here" means


BIN_DIR = HERE / "bin" / "Debug" / "net8.0"
TARGET_DIR = HERE.parent / "CSharpWasmExpo" / "bin"
FRAMEWORK_SRC = BIN_DIR / "wwwroot" / "_framework"
FRAMEWORK_DEST = HERE.parent / "CSharpWasmExpo" / "wwwroot" / "_framework"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and a few other places some comments are included in the original which are not explained here. I would recommend copying them for consistency. however some you replaced with function names which I believe make it much more readable and understandable

FILES = [
"mscorlib.dll",
"netstandard.dll",
"CSharpWasm.dll",
"System.Console.dll",
"System.Private.CoreLib.dll",
"System.Runtime.dll",
]


def run(cmd, cwd=None):
"""Run a command and fail loudly if it errors."""
print("> " + " ".join(cmd))
subprocess.run(cmd, cwd=cwd, check=True)


def copy_required_files():
print(f"Copying files to {TARGET_DIR}...")
TARGET_DIR.mkdir(parents=True, exist_ok=True)

for filename in FILES:
src = BIN_DIR / filename
dest = TARGET_DIR / filename

if src.is_file():
shutil.copy2(src, dest)
print(f"Copied {filename} to {TARGET_DIR}")
else:
print(f"Warning: {filename} not found in {BIN_DIR}")


def copy_framework_directory():
print("Copying _framework directory...")

if not FRAMEWORK_SRC.is_dir():
print(f"Error: Framework directory not found at {FRAMEWORK_SRC}", file=sys.stderr)
sys.exit(1)

FRAMEWORK_DEST.mkdir(parents=True, exist_ok=True)

# Copy contents of _framework (not the directory itself)
for item in FRAMEWORK_SRC.iterdir():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is a bit complex for the task. also the rmtree is there but this isn't present in the original. If this is for stale files from an earlier build then we should delete everything in the folder to start with since files that aren't in the new build but were in old wont be deleted and will remain there forever.

however if not then rmtree should be removed.

also to address the complexity of the loop and better follow the cp -r from the original you can add this flag to the end of copytree dirs_exist_ok=True and copy the full directory with no loop. otherwise if you are going ahead with removing stale files/folder structures then you could remove the full loop, remove the FRAMEWORK_DEST.mkdir(parents=True, exist_ok=True) and just do a copytree allowing it to make the directory and copy the contents with no previous build beforehand

dest = FRAMEWORK_DEST / item.name
if item.is_dir():
if dest.exists():
shutil.rmtree(dest)
shutil.copytree(item, dest)
else:
shutil.copy2(item, dest)

print(f"Copied _framework to {FRAMEWORK_DEST}")


def main():
print("Building the project...")
run(["dotnet", "build"], cwd=HERE)
print("Build succeeded!")

copy_required_files()
copy_framework_directory()

print("Build and copy process completed successfully!")


if __name__ == "__main__":
try:
main()
except subprocess.CalledProcessError as e:
print(f"Build failed with exit code {e.returncode}", file=sys.stderr)
sys.exit(e.returncode)
Loading