79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
from pathlib import Path
|
|
import subprocess
|
|
|
|
from core.table_formatter import fmt_table
|
|
from core.build_target import BuildTarget
|
|
from core.iter_utils import join_t
|
|
from core.visual_studio_utils import get_preferred_vs_context, VisualStudioContext
|
|
from typing import Optional
|
|
|
|
|
|
class CsprojTarget(BuildTarget):
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
source_path: Path,
|
|
out_path: Path,
|
|
aliases=[],
|
|
configuration: str = 'debug',
|
|
verbosity: str = 'minimal',
|
|
vs_context: Optional[VisualStudioContext] = None,
|
|
restore=True,
|
|
):
|
|
BuildTarget.__init__(self, name, source_path, out_path, aliases)
|
|
self.configuration = configuration
|
|
self.verbosity = verbosity
|
|
self.restore = restore
|
|
self.vs_context = vs_context or get_preferred_vs_context()
|
|
|
|
def _get_build_cmd(self):
|
|
cfg = self.configuration
|
|
build_cmd_steps = [self.vs_context.prompt_cmd()]
|
|
base_output_path_arg = self.vs_context.msbuild_prop(
|
|
'BaseOutputPath', self.out_path.joinpath(cfg.title()))
|
|
if self.restore:
|
|
# Restore build (download nugets)
|
|
restore_cmd_args = [
|
|
f'"{self.vs_context.msbuild_path}"',
|
|
f"{self.vs_context.msbuild_flag('m')}",
|
|
f"{self.vs_context.msbuild_prop('platform', 'x64')}",
|
|
f"{self.vs_context.msbuild_prop('configuration', cfg.title())}",
|
|
f"{self.vs_context.msbuild_target('restore')}",
|
|
f"{self.vs_context.msbuild_clp('verbosity', self.verbosity)}",
|
|
f"{self.vs_context.msbuild_clp('summary')}",
|
|
base_output_path_arg,
|
|
self.source_path,
|
|
]
|
|
restore_cmd = join_t(' ', restore_cmd_args)
|
|
build_cmd_steps.append(restore_cmd)
|
|
|
|
# Main build
|
|
dbg_type = 'portable' if cfg.lower() == 'debug' else 'none'
|
|
build_cmd_prefix = ' '.join(
|
|
[
|
|
f'"{self.vs_context.msbuild_path}"',
|
|
f"{self.vs_context.msbuild_flag('m')}",
|
|
f"{self.vs_context.msbuild_prop('platform', 'x64')}",
|
|
f"{self.vs_context.msbuild_prop('GenerateFullPaths', 'true')}",
|
|
f"{self.vs_context.msbuild_prop('configuration', cfg.title())}",
|
|
f"{self.vs_context.msbuild_prop('debugtype', dbg_type)}",
|
|
f"{self.vs_context.msbuild_clp('verbosity', self.verbosity)}",
|
|
f"{self.vs_context.msbuild_clp('summary')}"
|
|
])
|
|
build_cmd_steps.append(
|
|
f'{build_cmd_prefix} {base_output_path_arg} {self.source_path}')
|
|
return ' && '.join(build_cmd_steps)
|
|
|
|
def configure(self):
|
|
pass
|
|
|
|
def build(self):
|
|
subprocess.run(self._get_build_cmd()).check_returncode()
|
|
|
|
def __str__(self):
|
|
return fmt_table(
|
|
{
|
|
'name': self.name,
|
|
'build_cmd': self._get_build_cmd()
|
|
})
|