Skip to content
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
2 changes: 1 addition & 1 deletion src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ private void SignApk(string apkPath, string apksigner)
{
Utils.RunProcess(logger, "keytool", "-genkey -v -keystore debug.keystore -storepass android -alias " +
"androiddebugkey -keypass android -keyalg RSA -keysize 2048 -noprompt " +
"-dname \"CN=Android Debug,O=Android,C=US\"", workingDir: OutputDir, silent: true);
"-dname \"CN=Android Debug,O=Android,C=US\"", workingDir: OutputDir, messageImportance: MessageImportance.Low);
}
else if (Path.GetFullPath(signingKey) != Path.GetFullPath(defaultKey))
{
Expand Down
3 changes: 1 addition & 2 deletions src/tasks/AotCompilerTask/MonoAOTCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,7 @@ private bool PrecompileLibrary(PrecompileArguments args)
$"--response=\"{args.ResponseFilePath}\"",
args.EnvironmentVariables,
args.WorkingDir,
silent: true,
debugMessageImportance: MessageImportance.Low,
messageImportance: MessageImportance.Low,
label: Path.GetFileName(assembly));

var importance = exitCode == 0 ? MessageImportance.Low : MessageImportance.High;
Expand Down
81 changes: 35 additions & 46 deletions src/tasks/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public static (int exitCode, string output) RunShellCommand(
string command,
IDictionary<string, string> envVars,
string workingDir,
bool silent=false,
bool logStdErrAsMessage=false,
MessageImportance debugMessageImportance=MessageImportance.Low,
bool logErrorsAndWarningsFromOutput=false,
MessageImportance messageImportance=MessageImportance.Low,
string? label=null)
{
string scriptFileName = CreateTemporaryBatchFile(command);
Expand All @@ -39,18 +38,17 @@ public static (int exitCode, string output) RunShellCommand(
: ("/bin/sh", $"\"{scriptFileName}\"");

string msgPrefix = label == null ? string.Empty : $"[{label}] ";
logger.LogMessage(debugMessageImportance, $"{msgPrefix}Running {command} via script {scriptFileName}:", msgPrefix);
logger.LogMessage(debugMessageImportance, File.ReadAllText(scriptFileName), msgPrefix);
logger.LogMessage(messageImportance, $"{msgPrefix}Running {command} via script {scriptFileName}:", msgPrefix);
logger.LogMessage(messageImportance, File.ReadAllText(scriptFileName), msgPrefix);

return TryRunProcess(logger,
shell,
args,
envVars,
workingDir,
silent: silent,
logStdErrAsMessage: logStdErrAsMessage,
logErrorsAndWarningsFromOutput: logErrorsAndWarningsFromOutput,
label: label,
debugMessageImportance: debugMessageImportance);
messageImportance: messageImportance);

static string CreateTemporaryBatchFile(string command)
{
Expand Down Expand Up @@ -82,17 +80,15 @@ public static string RunProcess(
IDictionary<string, string>? envVars = null,
string? workingDir = null,
bool ignoreErrors = false,
bool silent = true,
MessageImportance debugMessageImportance=MessageImportance.High)
MessageImportance messageImportance=MessageImportance.High)
{
(int exitCode, string output) = TryRunProcess(
logger,
path,
args,
envVars,
workingDir,
silent: silent,
debugMessageImportance: debugMessageImportance);
messageImportance: messageImportance);

if (exitCode != 0 && !ignoreErrors)
throw new Exception("Error: Process returned non-zero exit code: " + output);
Expand All @@ -106,13 +102,12 @@ public static (int, string) TryRunProcess(
string args = "",
IDictionary<string, string>? envVars = null,
string? workingDir = null,
bool silent = true,
bool logStdErrAsMessage = false,
MessageImportance debugMessageImportance=MessageImportance.High,
bool logErrorsAndWarningsFromOutput = false,
MessageImportance messageImportance = MessageImportance.High,
string? label=null)
{
string msgPrefix = label == null ? string.Empty : $"[{label}] ";
logger.LogMessage(debugMessageImportance, $"{msgPrefix}Running: {path} {args}");
logger.LogMessage(messageImportance, $"{msgPrefix}Running: {path} {args}");
var outputBuilder = new StringBuilder();
var processStartInfo = new ProcessStartInfo
{
Expand All @@ -127,7 +122,7 @@ public static (int, string) TryRunProcess(
if (workingDir != null)
processStartInfo.WorkingDirectory = workingDir;

logger.LogMessage(debugMessageImportance, $"{msgPrefix}Using working directory: {workingDir ?? Environment.CurrentDirectory}", msgPrefix);
logger.LogMessage(messageImportance, $"{msgPrefix}Using working directory: {workingDir ?? Environment.CurrentDirectory}", msgPrefix);

if (envVars != null)
{
Expand All @@ -145,42 +140,36 @@ public static (int, string) TryRunProcess(
if (process == null)
throw new ArgumentException($"{msgPrefix}Process.Start({path} {args}) returned null process");

process.ErrorDataReceived += (sender, e) =>
{
lock (s_SyncObj)
{
if (string.IsNullOrEmpty(e.Data))
return;
process.ErrorDataReceived += LogOutput;
process.OutputDataReceived += LogOutput;

string msg = $"{msgPrefix}{e.Data}";
if (!silent)
{
if (logStdErrAsMessage)
logger.LogMessage(debugMessageImportance, e.Data, msgPrefix);
else
logger.LogWarning(msg);
}
outputBuilder.AppendLine(e.Data);
}
};
process.OutputDataReceived += (sender, e) =>
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

process.ErrorDataReceived -= LogOutput;
process.OutputDataReceived -= LogOutput;

logger.LogMessage(messageImportance, $"{msgPrefix}Exit code: {process.ExitCode}");
return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n'));

void LogOutput(object sender, DataReceivedEventArgs args)
{
string? line = args.Data;
lock (s_SyncObj)
{
if (string.IsNullOrEmpty(e.Data))
if (string.IsNullOrEmpty(line))
return;

if (!silent)
logger.LogMessage(debugMessageImportance, e.Data, msgPrefix);
outputBuilder.AppendLine(e.Data);
}
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
string message = $"{msgPrefix}{line}";
if (logErrorsAndWarningsFromOutput)
logger.LogMessageFromText(message, messageImportance);
else
logger.LogMessage(messageImportance, message);

logger.LogMessage(debugMessageImportance, $"{msgPrefix}Exit code: {process.ExitCode}");
return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n'));
outputBuilder.AppendLine(line);
}
}
}

internal static string CreateTemporaryBatchFile(string command)
Expand Down
3 changes: 1 addition & 2 deletions src/tasks/WasmAppBuilder/EmccCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ bool ProcessSourceFile(string srcFile, string objFile)
command,
envVarsDict,
workingDir: Environment.CurrentDirectory,
logStdErrAsMessage: true,
debugMessageImportance: messageImportance,
messageImportance: messageImportance,
label: Path.GetFileName(srcFile));

var endTime = DateTime.Now;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ private bool ExecuteInternal()
Path.Combine(SdkDir, "dotnet"),
$"workload install --skip-manifest-update --no-cache --configfile \"{nugetConfigPath}\" {WorkloadId.ItemSpec}",
workingDir: Path.GetTempPath(),
silent: false,
debugMessageImportance: MessageImportance.High);
messageImportance: MessageImportance.High);
if (exitCode != 0)
{
Log.LogError($"workload install failed: {output}");
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/WorkloadBuildTasks/PackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private bool InstallActual(PackageReference[] references, bool stopOnMissing)
_logger.LogMessage(MessageImportance.Low, $"Restoring packages: {string.Join(", ", references.Select(r => $"{r.Name}/{r.Version}"))}");

string args = $"restore \"{projectPath}\" /p:RestorePackagesPath=\"{_packagesDir}\"";
(int exitCode, string output) = Utils.TryRunProcess(_logger, "dotnet", args, silent: false, debugMessageImportance: MessageImportance.Low);
(int exitCode, string output) = Utils.TryRunProcess(_logger, "dotnet", args, messageImportance: MessageImportance.Low);
if (exitCode != 0)
{
LogErrorOrWarning($"Restoring packages failed with exit code: {exitCode}. Output:{Environment.NewLine}{output}", stopOnMissing);
Expand Down