Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (specs) start setting up host for executing specs
  (GH-525) Improve write-progress
  (GH-525) Improve Write-Progress
  (GH-8) PowerShell host execution improvements
  • Loading branch information
ferventcoder committed Jan 2, 2016
2 parents 8ba0c8e + dbf0d22 commit f3ae282
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ param(
}

if ($total -eq $goal) {
Write-Progress "Completed download of $url." "Completed a total of $total bytes of $fileName" -id 0 -Completed
Write-Progress "Completed download of $url." "Completed download of $fileName ($goalFormatted)." -id 0 -Completed
}
}
} while ($count -gt 0)
Write-Host ""
Write-Host "Download of $([System.IO.Path]::GetFileName($fileName)) ($goalFormatted) completed."
} catch {
throw $_.Exception
} finally {
Expand Down
3 changes: 3 additions & 0 deletions src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ private static ChocolateyConfiguration baseline_configuration()
config.Verbose = false;
config.Input = config.PackageNames = string.Empty;
config.ListCommand.LocalOnly = false;
//config.Features.UsePowerShellHost = true;
//config.Features.AutoUninstaller = true;
//config.Features.CheckSumFiles = true;

return config;
}
Expand Down
19 changes: 18 additions & 1 deletion src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,27 @@ private PowerShellExecutionResults run_host(ChocolateyConfiguration config, stri
{
pipeline.Invoke();
}
catch (RuntimeException ex)
{
var errorStackTrace = ex.StackTrace;
var record = ex.ErrorRecord;
if (record != null)
{
// not available in v1
//errorStackTrace = record.ScriptStackTrace;
var scriptStackTrace = record.GetType().GetProperty("ScriptStackTrace");
if (scriptStackTrace != null)
{
var scriptError = scriptStackTrace.GetValue(record, null).to_string();
if (!string.IsNullOrWhiteSpace(scriptError)) errorStackTrace = scriptError;
}
}
this.Log().Error("ERROR: {0}{1}".format_with(ex.Message, !config.Debug ? string.Empty : "{0} {1}".format_with(Environment.NewLine,errorStackTrace)));
}
catch (Exception ex)
{
// Unfortunately this doesn't print line number and character. It might be nice to get back to those items unless it involves tons of work.
this.Log().Error("ERROR: {0}".format_with(ex.Message)); //, !config.Debug ? string.Empty : "{0} {1}".format_with(Environment.NewLine,ex.StackTrace)));
this.Log().Error("ERROR: {0}{1}".format_with(ex.Message, !config.Debug ? string.Empty : "{0} {1}".format_with(Environment.NewLine,ex.StackTrace)));
}

if (pipeline.PipelineStateInfo != null)
Expand Down
10 changes: 8 additions & 2 deletions src/chocolatey/infrastructure/commands/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.commands
{
using System;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
Expand Down Expand Up @@ -61,10 +62,15 @@ public T command<T>(Func<T> function, T timeoutDefaultValue)
{
if (function == null) return timeoutDefaultValue;

var task = Task<T>.Factory.StartNew(function);
var cancelToken = new CancellationTokenSource();
cancelToken.Token.ThrowIfCancellationRequested();
var task = Task<T>.Factory.StartNew(function, cancelToken.Token); //,TaskCreationOptions.LongRunning| TaskCreationOptions.AttachedToParent);

task.Wait(_timespan);

if (task.IsCompleted) return task.Result;

cancelToken.Cancel();

return timeoutDefaultValue;

//T result = timeoutDefaultValue;
Expand Down
23 changes: 18 additions & 5 deletions src/chocolatey/infrastructure/powershell/PoshHostUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override void Write(ConsoleColor foregroundColor, ConsoleColor background

public override void WriteLine()
{
base.WriteLine();
this.Log().Info("");
}

public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
Expand Down Expand Up @@ -118,14 +118,27 @@ public override void WriteDebugLine(string message)
this.Log().Debug(message);
}

private bool hasLoggedStartProgress = false;
private bool hasLoggedFinalProgress = false;
public override void WriteProgress(long sourceId, ProgressRecord record)
{
{
if (record.PercentComplete == -1) return;

if (record.PercentComplete == 100) this.Log().Debug(() => "Progress: 100%{0}".format_with(" ".PadRight(20)));
if (hasLoggedFinalProgress) return;
if (!hasLoggedStartProgress)
{
hasLoggedStartProgress = true;
this.Log().Debug(record.Activity);
}

// http://stackoverflow.com/a/888569/18475
Console.Write("\rProgress: {0}%{1}".format_with(record.PercentComplete, " ".PadRight(20)));
Console.Write("\rProgress: {0}% - {1}".format_with(record.PercentComplete.to_string(), record.StatusDescription.PadRight(50, ' ')));

if (record.PercentComplete == 100 && !hasLoggedFinalProgress)
{
hasLoggedFinalProgress = true;
//this.Log().Info("");
//this.Log().Info(record.StatusDescription.Replace("Saving","Finished downloading. Saved"));
}
}

public override void WriteVerboseLine(string message)
Expand Down

0 comments on commit f3ae282

Please sign in to comment.