Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choco0.10.3 #219

Closed
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions Boxstarter.Chocolatey/Boxstarter.Chocolatey.pssproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Get-PackageRoot.ps1" />
<Compile Include="Install-BoxstarterPackage.ps1" />
<Compile Include="Invoke-BoxstarterBuild.ps1" />
<Compile Include="invoke-chocolatey.ps1" />
<Compile Include="Invoke-ChocolateyBoxstarter.ps1" />
<Compile Include="New-BoxstarterPackage.ps1" />
<Compile Include="New-PackageFromScript.ps1" />
Expand Down
76 changes: 43 additions & 33 deletions Boxstarter.Chocolatey/invoke-chocolatey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ function Invoke-Chocolatey($chocoArgs) {
Write-BoxstarterMessage "Current runtime is $($PSVersionTable.CLRVersion)" -Verbose
$refs = @(
"$($Boxstarter.BaseDir)/boxstarter.chocolatey/chocolatey/log4net.dll",
"$($Boxstarter.BaseDir)/boxstarter.chocolatey/chocolatey/chocolatey.dll"
"$($Boxstarter.BaseDir)/boxstarter.chocolatey/chocolatey/chocolatey.dll",
"$($Boxstarter.BaseDir)/boxstarter.chocolatey/chocolatey/AlphaFS.dll"
Copy link
Member

Choose a reason for hiding this comment

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

Hopefully this won't be needed for long - chocolatey/choco#1102

)
$refs | % {
Write-BoxstarterMessage "Adding types from $_" -Verbose
Expand All @@ -16,7 +17,6 @@ function Invoke-Chocolatey($chocoArgs) {
}
}
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.ReferencedAssemblies.Add([System.Reflection.Assembly]::Load('System.Management.Automation').location) | Out-Null
$refs | % { $cpar.ReferencedAssemblies.Add($_) | Out-Null }
Write-BoxstarterMessage "Adding boxstarter choco wrapper types..." -Verbose
Add-Type @"
Expand All @@ -28,17 +28,24 @@ namespace Boxstarter
using chocolatey.infrastructure.logging;
using System;
using System.IO;
using System.Management.Automation.Host;

public class ChocolateyWrapper
{
private GetChocolatey _choco;
private static GetChocolatey _choco;

public ChocolateyWrapper(string boxstarterSetup, PSHostUserInterface ui, bool logDebug, string logPath, bool quiet) {
_choco = Lets.GetChocolatey();
var psService = new PowershellService(new DotNetFileSystem(), boxstarterSetup);
_choco.RegisterContainerComponent<IPowershellService>(() => psService);
_choco.SetCustomLogging(new PsLogger(ui, logDebug, logPath, quiet));
public ChocolateyWrapper(string boxstarterSetup, bool logDebug, string logPath, bool quiet) {
if (_choco == null)
{
_choco = Lets.GetChocolatey();

// Because chocolatey uses a static container which gets locked
// after first resolve we may only register our custom PowershellService once.
var psService = new PowershellService(new DotNetFileSystem(), boxstarterSetup);
_choco.RegisterContainerComponent<IPowershellService>(() => psService);
}

_choco.SetCustomLogging(new PsLogger(logDebug, logPath, quiet));

}

public void Run(string[] args) {
Expand All @@ -48,14 +55,12 @@ namespace Boxstarter

public class PsLogger : ILog
{
private PSHostUserInterface _ui;
private string _path;
private bool _logDebug;
private bool _quiet;

public PsLogger(PSHostUserInterface ui, bool logDebug, string path, bool quiet)
public PsLogger(bool logDebug, string path, bool quiet)
{
_ui = ui;
_logDebug = logDebug;
_path = path;
_quiet = quiet;
Expand All @@ -69,7 +74,7 @@ namespace Boxstarter
{
WriteLog(
message,
x => { if(_logDebug) _ui.WriteDebugLine(x); },
x => { if(_logDebug) Console.WriteLine(x); },
formatting
);
}
Expand All @@ -78,22 +83,28 @@ namespace Boxstarter
{
WriteLog(
message,
x => { if(_logDebug) _ui.WriteDebugLine(x); }
x => { if(_logDebug) Console.WriteLine(x); }
);
}

public void Info(string message, params object[] formatting)
{
if (message.StartsWith("VERBOSE: "))
{
Debug(message, formatting);
return;
}

WriteLog(
message,
x => {
if(x.Trim().StartsWith("Boxstarter: ") || x.Replace("+","").Trim().StartsWith("Boxstarter ")){
_ui.RawUI.ForegroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.Green;
}
else {
_ui.RawUI.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.White;
}
_ui.WriteLine(x);
Console.WriteLine(x);
},
formatting
);
Expand All @@ -105,12 +116,12 @@ namespace Boxstarter
message,
x => {
if(x.Trim().StartsWith("Boxstarter: ") || x.Replace("+","").Trim().StartsWith("Boxstarter ")){
_ui.RawUI.ForegroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.Green;
}
else {
_ui.RawUI.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.White;
}
_ui.WriteLine(x);
Console.WriteLine(x);
}
);
}
Expand All @@ -120,8 +131,8 @@ namespace Boxstarter
WriteLog(
message,
x => {
_ui.RawUI.ForegroundColor = ConsoleColor.Yellow;
_ui.WriteLine(x);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(x);
},
formatting
);
Expand All @@ -132,8 +143,8 @@ namespace Boxstarter
WriteLog(
message,
x => {
_ui.RawUI.ForegroundColor = ConsoleColor.Yellow;
_ui.WriteLine(x);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(x);
}
);
}
Expand All @@ -142,7 +153,7 @@ namespace Boxstarter
{
WriteLog(
message,
x => _ui.WriteErrorLine(x),
x => Console.Error.WriteLine(x),
formatting
);
}
Expand All @@ -151,15 +162,15 @@ namespace Boxstarter
{
WriteLog(
message,
x => _ui.WriteErrorLine(x)
x => Console.Error.WriteLine(x)
);
}

public void Fatal(string message, params object[] formatting)
{
WriteLog(
message,
x => _ui.WriteErrorLine(x),
x => Console.Error.WriteLine(x),
formatting
);
}
Expand All @@ -168,7 +179,7 @@ namespace Boxstarter
{
WriteLog(
message,
x => _ui.WriteErrorLine(x)
x => Console.Error.WriteLine(x)
);
}

Expand All @@ -185,7 +196,7 @@ namespace Boxstarter
private void WriteFormattedLog(Func<string> formatMessage, Action<String> logAction)
{
if(_quiet) return;
var origColor = _ui.RawUI.ForegroundColor;
var origColor = Console.ForegroundColor;
try {
var msg = formatMessage.Invoke();
logAction.Invoke(msg);
Expand All @@ -195,7 +206,7 @@ namespace Boxstarter
WriteRaw(e.ToString());
}
finally{
_ui.RawUI.ForegroundColor = origColor;
Console.ForegroundColor = origColor;
}
}

Expand All @@ -214,7 +225,7 @@ namespace Boxstarter
}
catch(Exception e)
{
_ui.WriteErrorLine(e.ToString());
Console.Error.WriteLine(e.ToString());
}
}
}
Expand All @@ -231,8 +242,7 @@ namespace Boxstarter
Write-BoxstarterMessage "instantiating choco wrapper..." -Verbose
$global:choco = New-Object -TypeName boxstarter.ChocolateyWrapper -ArgumentList `
(Get-BoxstarterSetup),`
$host.UI,`
($global:DebugPreference -eq "Continue"),`
$false,`
$boxstarter.log,`
$boxstarter.SuppressLogging
}
Expand Down
22 changes: 15 additions & 7 deletions BuildScripts/default.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,21 @@ task Install-WebDeploy {
}

task Install-ChocoLib {
exec { .$nugetExe install chocolatey.lib -Version 0.9.10-beta-20151210 -Pre -OutputDirectory $basedir\Boxstarter.Chocolatey\ }
exec { .$nugetExe install log4net -Version 2.0.3 -OutputDirectory $basedir\Boxstarter.Chocolatey\ }
MkDir $basedir\Boxstarter.Chocolatey\chocolatey -ErrorAction SilentlyContinue
Copy-Item $basedir\Boxstarter.Chocolatey\log4net.2.0.3\lib\net40-full\* $basedir\Boxstarter.Chocolatey\chocolatey
Copy-Item $basedir\Boxstarter.Chocolatey\chocolatey.lib.0.9.10-beta-20151210\lib\* $basedir\Boxstarter.Chocolatey\chocolatey
Remove-Item $basedir\Boxstarter.Chocolatey\log4net.2.0.3 -Recurse -Force
Remove-Item $basedir\Boxstarter.Chocolatey\chocolatey.lib.0.9.10-beta-20151210 -Recurse -Force
$project = Join-Path $basedir "Boxstarter.Chocolatey"
$temp = Join-Path $project "temp"

exec { .$nugetExe install chocolatey.lib -Version 0.10.3 -OutputDirectory $temp }
exec { .$nugetExe install alphafs -Version 2.0.0 -OutputDirectory $temp }

$output = Join-Path $project "chocolatey"
Remove-Item $output -Force -Recurse
New-Item $output -ItemType Directory

Copy-Item $temp\log4net.*\lib\net40-full\* $output
Copy-Item $temp\chocolatey.lib.*\lib\* $output
Copy-Item $temp\alphafs.*\lib\net451\* $output

Remove-Item $temp -Recurse -Force
}

function PackDirectory($path, [switch]$AddReleaseNotes){
Expand Down
131 changes: 131 additions & 0 deletions tests/Chocolatey/Invoke-Chocolatey.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
if(get-module Boxstarter.Chocolatey){Remove-Module boxstarter.Chocolatey}
Resolve-Path $here\..\..\Boxstarter.Common\*.ps1 |
% { . $_.ProviderPath }
Resolve-Path $here\..\..\Boxstarter.Bootstrapper\*.ps1 |
% { . $_.ProviderPath }

$Boxstarter.BaseDir=(split-path -parent (split-path -parent $here))
$Boxstarter.SuppressLogging=$true
Resolve-Path $here\..\..\Boxstarter.Chocolatey\*.ps1 |
% { . $_.ProviderPath }


# When you change the ChocolateyWrapper type you'll get this error for a
# subsequent test run:
# > Cannot add type. The type name 'Boxstarter.ChocolateyWrapper' already exists.
# You'll have to kill the testrunner to solve this.
# Package Manager Console:
# Get-Process -Name vstest.executionengine.x86 | Stop-Process
Describe "Invoke-Chocolatey" {
$global:choco = $null

Context "adds chocolatey wrapper types" {
Mock Write-BoxstarterMessage
Mock Enter-BoxstarterLogable

Invoke-Chocolatey -chocoArgs "--version"

it "has message written" {
Assert-MockCalled Write-BoxstarterMessage -ParameterFilter { $message -eq "Types added..." }
}
}

Context "invokes created type" {
Invoke-Chocolatey -chocoArgs "--version"
}

Context "subsequent calls" {
$global:choco = $null
Invoke-Chocolatey -chocoArgs "--version"

$global:choco = $null
Invoke-Chocolatey -chocoArgs "--version"
}

Context "logger" {
# Just invoke to make sure the types are created and loaded.
Invoke-Chocolatey -chocoArgs "--version"

$logfile = Join-Path $env:TEMP "test.log"
$log = New-Object -TypeName boxstarter.PsLogger -ArgumentList `
$true,`
$logfile,`
$false

$log.Debug("output {0}", "debug")
$log.Debug({ "debug" });
$log.Info("output {0}", "info")
$log.Info({ "info" });
$log.Warn("output {0}", "warn")
$log.Warn({ "warn" });
$log.Error("output {0}", "error")
$log.Error({ "error" });
$log.Fatal("output {0}", "fatal")
$log.Fatal({ "fatal" });

it "has written to file" {
Test-Path -Path $logfile | Should Be $true
}
}
}

Describe "PsLogger" {
# Just invoke to make sure the types are created and loaded.
Invoke-Chocolatey -chocoArgs "--version"

Add-Type @"
namespace Boxstarter.Tests
{
using System;
using System.IO;

public class RedirectConsole
{
public string Execute(Action a)
{
using (var writer = new StringWriter())
{
Console.SetOut(writer);
a();

return writer.ToString();
}
}
}
}
"@
$redirect = New-Object Boxstarter.Tests.RedirectConsole
$logfile = Join-Path $env:TEMP "test.log"

Context "logDebug is false" {
$log = New-Object -TypeName boxstarter.PsLogger -ArgumentList `
$false,`
$logfile,`
$false

It "should hide VERBOSE info" {
$redirect.Execute({ $log.Info("VERBOSE: {0}", "info") }) | Should Be ""
}

It "should print normal info" {
$redirect.Execute({ $log.Info("Hello: {0}", "info") }) | Should BeLike "Hello: info*"
}
}


Context "logDebug is true" {
$log = New-Object -TypeName boxstarter.PsLogger -ArgumentList `
$true,`
$logfile,`
$false

It "should print VERBOSE info" {
$redirect.Execute({ $log.Info("VERBOSE: {0}", "info") }) | Should BeLike "VERBOSE: info*"
}

It "should print normal info" {
$redirect.Execute({ $log.Info("Hello: {0}", "info") }) | Should BeLike "Hello: info*"
}
}
}
1 change: 1 addition & 0 deletions tests/Tests.pssproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="Chocolatey\New-PackageFromScript.tests.ps1" />
<Compile Include="Chocolatey\Set-BoxstarterShare.Tests.ps1" />
<Compile Include="Common\Invoke-FromTask.tests.ps1" />
<Compile Include="Chocolatey\Invoke-Chocolatey.tests.ps1" />
<Compile Include="TestRunner\Install-BoxstarterScripts.tests.ps1" />
<Compile Include="TestRunner\Set-BoxstarterFeedAPIKey.Tests.ps1" />
</ItemGroup>
Expand Down