Repository navigation
Replies: 1 comment
|
I'm not seeing a major advantage here... Both |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
#!/usr/bin/python3
import os
import sys
import argparse
import subprocess
from xml.dom.minidom import parse as XMLOpen
import tempfile
import time
from dataclasses import dataclass
tdir = tempfile.gettempdir()
args = ["csc", "-unsafe", "-warnaserror", "-nostdlib", "-target:library"]
@DataClass
class PackageReference:
name: str
version: str
destination: str
@DataClass
class LibraryReference:
name: str
hintPath: str
@DataClass
class PublicizeTarget:
inputName: str
outputName: str
def parseArgs(argv):
argParser = argparse.ArgumentParser(description="Automate compiling C# projects on Linux, using Mono")
argParser.add_argument("--reference", type=str, metavar="PATH", help="Location of Rimworld Reference libraries")
argParser.add_argument("--output", "-o", type=str, default="Assemblies/CombatExtended.dll", help="Output filename")
argParser.add_argument("--csproj", type=str, default="Source/CombatExtended/CombatExtended.csproj", help="C# project to build.")
argParser.add_argument("--all-libs", action="store_true", default=False, help="Reference all dlls in $reference, even those not specified by the .csproj file")
argParser.add_argument("--verbose", "-v", action="count", default=0, help="Increase verbosity, specify more times for more verbosity")
argParser.add_argument("--download-libs", action="store_true", default=False, help=f"Automatically download referenced packages to {tdir}/downloads and unpack them to $reference")
argParser.add_argument("--publicizer", type=str, metavar="PATH", help="Location of AssemblyPublicizer source code or parent directory of AssemblyPublicizer.exe")
argParser.add_argument("--debug", action="store_true", default=False, help="Define
DEBUGwhen calling csc")argParser.add_argument("--refout", metavar="PATH", default=None, help="Specify where to save a reference library")
def run_command(command, verbose):
if verbose:
print(f"Running command: {command}")
result = subprocess.run(command, shell=True)
if result.returncode != 0:
raise Exception(f"Command failed: {command}")
def downloadLib(name, version, dest, verbose):
if verbose:
print(f"Downloading {version} of {name} from nuget")
quiet = "-q" if verbose < 5 else ""
run_command(f"wget {quiet} https://www.nuget.org/api/v2/package/{name}/{version} -O {dest}", verbose)
def unpackLib(path, dest, verbose):
quiet = "-q" if verbose < 6 else ""
run_command(f"unzip {quiet} -o {path} -d {dest}", verbose)
def resolveLibrary(library, reference, csproj, verbose):
if library.hintPath:
if verbose > 1:
print(f"Adding library via hintPath: {library.hintPath}")
base_dir = os.path.split(csproj)[0]
t_path = os.path.join(base_dir, library.hintPath)
if os.path.isabs(library.hintPath):
return os.path.abspath(t_path)
else:
return os.path.relpath(t_path)
if verbose > 1:
print(f"Searching for library named {library.name}.dll")
return os.path.join(reference, library.name + '.dll')
def publicize(p, publicizer, verbose):
if verbose:
print(f"Publicizing Assembly: {p}")
cwd = os.getcwd()
os.chdir(os.path.dirname(p))
run_command(f"mono {publicizer} --exit -i {p} -o {p[:-4]}_publicized.dll", verbose)
os.chdir(cwd)
return p[:-4] + '_publicized.dll'
def makePublicizer(p, verbose):
ap_path = os.path.join(p, "AssemblyPublicizer.exe")
def parse_packages(csproj):
packages = []
for idx, package in enumerate(csproj.getElementsByTagName("PackageReference")):
name = package.attributes['Include'].value
version = package.attributes['Version'].value
dest = f"{tdir}/downloads/rwref-{idx}.zip"
packages.append(PackageReference(name, version, dest))
return packages
def parse_libraries(csproj):
libraries = []
removed_libraries = []
for reference in csproj.getElementsByTagName("Reference"):
hintPath = reference.getElementsByTagName("HintPath")
if 'Remove' in reference.attributes:
assemblyName = reference.attributes['Remove'].value.rsplit('\', 1)[1]
removed_libraries.append(assemblyName)
continue
if hintPath:
hintPath = hintPath[0].firstChild.data.strip().replace("\", "/")
libraries.append(LibraryReference(reference.attributes['Include'].value, hintPath))
return libraries, removed_libraries
def parse_sources(csproj, base_dir, verbose):
sources = []
for d, _, files in os.walk(base_dir):
sources.extend(os.path.join(d, f).split(base_dir + '/', 1)[1] for f in files if f.endswith('.cs'))
def parse_publicize(csproj):
publicized_libraries = []
removed_libraries = []
publicize_task = [i for i in csproj.getElementsByTagName("Target") if 'Name' in i.attributes and i.attributes['Name'].value.startswith('Publi')]
variables = {}
def parse_csproj(csproj_path, verbose):
base_dir = os.path.split(csproj_path)[0]
if verbose:
print(f"Parsing .csproj: {csproj_path}")
with XMLOpen(csproj_path) as csproj:
packages = parse_packages(csproj)
libraries, removed_libraries = parse_libraries(csproj)
sources = parse_sources(csproj, base_dir, verbose)
publicized_libraries, removed_libraries_2 = parse_publicize(csproj)
removed_libraries.extend(removed_libraries_2)
return packages, libraries, removed_libraries, publicized_libraries, sources
def main(argv=sys.argv):
rest = []
if '--' in argv:
_idx = argv.index('--')
argv, rest = argv[:_idx], argv[_idx + 1:]
options = parseArgs(argv)
verbose = options.verbose
if name == 'main':
main(sys.argv)
All reactions