Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-460) Amended Template
  (GH-623) Extract Nuspec on Install
  (GH-674)(GH-672) Color Overrides / Fix Write-Host
  (GH-181) log selection to log file only
  (GH-181)(GH-184) Short prompt/Prompt character
  (GH-675) Template include LICENSE/VERIFICATION
  (GH-258) Rename zip to detector.zip
  (GH-572) Fix: The handle is invalid - Output Redirection
  (maint) comments
  (GH-667) Provide How To Add Profile Manually
  (GH-667) PowerShell Custom Host - Set $Profile
  (GH-666) pass Write-ChocolateyError message param
  (GH-673) Ensure Get-BinRoot Warning is Shown
  (GH-666) setup logs colored write-host w/fallback
  • Loading branch information
ferventcoder committed Mar 29, 2016
2 parents 329c797 + ad27a72 commit b6b3433
Show file tree
Hide file tree
Showing 27 changed files with 484 additions and 36 deletions.
25 changes: 17 additions & 8 deletions nuget/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ param (
)

try {
$host.ui.RawUI.ForegroundColor = "Yellow"
Write-Output "WARNING: $message"
$host.ui.RawUI.ForegroundColor = $originalForegroundColor
Write-Host "WARNING: $message" -ForegroundColor "Yellow" -ErrorAction "Stop"
} catch {
Write-Output "WARNING: $message"
}
}

function Write-ChocolateyError {
param (
[string]$message = ''
)

try {
$host.ui.RawUI.ForegroundColor = "Red"
Write-Output "ERROR: $message"
$host.ui.RawUI.ForegroundColor = $originalForegroundColor
Write-Host "ERROR: $message" -ForegroundColor "Red" -ErrorAction "Stop"
} catch {
Write-Output "ERROR: $message"
}
Expand Down Expand Up @@ -518,14 +518,23 @@ if (Test-Path($ChocolateyProfile)) {
Write-Output 'Adding Chocolatey to the profile. This will provide tab completion, refreshenv, etc.'
$profileInstall | Out-File $profileFile -Append -Encoding (Get-FileEncoding $profileFile)
Write-ChocolateyWarning 'Chocolatey profile installed. Reload your profile - type . $profile'

if ($PSVersionTable.PSVersion.Major -lt 3) {
Write-ChocolateyWarning "Tab completion does not currently work in PowerShell v2. `n Please upgrade to a more recent version of PowerShell to take advantage of tab completion."
#Write-ChocolateyWarning "To load tab expansion, you need to install PowerTab. `n See https://powertab.codeplex.com/ for details."
}

} catch {
Write-ChocolateyWarning "Unable to add Chocolatey to the profile. You will need to do it manually. Error was '$_'"
@'
This is how add the Chocolatey Profile manually.
Find your $profile. Then add the following lines to it:
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
'@ | Write-Output
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ that chocolatey.licensed.dll exists at

if (config.RegularOutput)
{
"logfile".Log().Info(() => "".PadRight(60, '='));
"LogFileOnly".Log().Info(() => "".PadRight(60, '='));
#if DEBUG
"chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
#else
if (config.Information.ChocolateyVersion == config.Information.ChocolateyProductVersion && args.Any())
{
"logfile".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
"LogFileOnly".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty));
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey.console/chocolatey.console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<HintPath>..\packages\SimpleInjector.2.5.0\lib\net40-client\SimpleInjector.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<EmbeddedResource Include="helpers\functions\Get-ToolsLocation.ps1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="tools\die.zip" />
<EmbeddedResource Include="tools\detector.zip" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="helpers\chocolateyProfile.psm1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
function Get-ToolsLocation {
Write-Debug "Running 'Get-ToolsLocation'";
$invocation = $MyInvocation
if ($invocation -ne $null -and $invocation.InvocationName -ne $null -and $invocation.InvocationName.ToLower() -eq 'Get-BinRoot') {
if ($invocation -ne $null -and $invocation.InvocationName -ne $null -and $invocation.InvocationName.ToLower() -eq 'get-binroot') {
Write-Host "Get-BinRoot is going to be deprecated in v1 and removed in v2. It is being replaced with Get-ToolsLocation, however many packages no longer require a special separate directory since package folders no longer have versions on them. Some do though and should continue to use Get-ToolsLocation."
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,208 @@ public void should_error_when_any_choice_not_available_is_given()
console.Verify(c => c.ReadLine(), Times.AtLeast(8));
}
}

public class when_prompting_short_with_interactivePrompt_guard_errors : InteractivePromptSpecsBase
{
private Func<string> prompt;

public override void Because()
{
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, defaultChoice: null, requireAnswer:true, shortPrompt: true);
}

[Fact]
public void should_error_when_the_choicelist_is_null()
{
choices = null;
bool errored = false;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception)
{
errored = true;
}

errored.ShouldBeTrue();
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_choicelist_is_empty()
{
choices = new List<string>();
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("No choices passed in.");
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_prompt_input_is_null()
{
choices = new List<string> { "bob" };
prompt_value = null;
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("Value for prompt cannot be null.");
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_choicelist_contains_empty_values()
{
choices = new List<string> { "bob", "" };
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("Some choices are empty.");
console.Verify(c => c.ReadLine(), Times.Never);
}

[Fact]
public void should_error_when_the_choicelist_has_multiple_items_with_same_first_letter()
{
choices = new List<string> {"sally", "suzy"};
bool errored = false;
string errorMessage = string.Empty;
console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception ex)
{
errored = true;
errorMessage = ex.Message;
}

errored.ShouldBeTrue();
errorMessage.ShouldContain("Multiple choices have the same first letter.");
console.Verify(c => c.ReadLine(), Times.Never);
}
}

public class when_prompting_short_with_interactivePrompt : InteractivePromptSpecsBase
{
private Func<string> prompt;

public override void Because()
{
prompt = () => InteractivePrompt.prompt_for_confirmation(prompt_value, choices, defaultChoice: null, requireAnswer: true, shortPrompt: true);
}

public override void AfterObservations()
{
base.AfterObservations();
should_have_called_Console_ReadLine();
}

[Fact]
public void should_error_when_no_answer_given()
{
bool errored = false;

console.Setup(c => c.ReadLine()).Returns(""); //Enter pressed
try
{
prompt();
}
catch (Exception)
{
errored = true;
}
errored.ShouldBeTrue();
console.Verify(c => c.ReadLine(), Times.AtLeast(8));
}

[Fact]
public void should_return_yes_when_yes_is_given()
{
console.Setup(c => c.ReadLine()).Returns("yes");
var result = prompt();
result.ShouldEqual("yes");
}

[Fact]
public void should_return_yes_when_y_is_given()
{
console.Setup(c => c.ReadLine()).Returns("y");
var result = prompt();
result.ShouldEqual("yes");
}

[Fact]
public void should_return_no_choice_when_no_is_given()
{
console.Setup(c => c.ReadLine()).Returns("no");
var result = prompt();
result.ShouldEqual("no");
}

[Fact]
public void should_return_no_choice_when_n_is_given()
{
console.Setup(c => c.ReadLine()).Returns("n");
var result = prompt();
result.ShouldEqual("no");
}

[Fact]
public void should_error_when_any_choice_not_available_is_given()
{
bool errored = false;

console.Setup(c => c.ReadLine()).Returns("yup"); //Enter pressed
try
{
prompt();
}
catch (Exception)
{
errored = true;
}
errored.ShouldBeTrue();
console.Verify(c => c.ReadLine(), Times.AtLeast(8));
}
}
}
}
3 changes: 3 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<HintPath>..\packages\SimpleInjector.2.5.0\lib\net40-client\SimpleInjector.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Management.Automation">
Expand Down Expand Up @@ -91,6 +92,8 @@
<Compile Include="infrastructure.app\events\HandlePackageResultCompletedMessage.cs" />
<Compile Include="infrastructure.app\services\FileTypeDetectorService.cs" />
<Compile Include="infrastructure.app\services\IFileTypeDetectorService.cs" />
<Compile Include="infrastructure.app\templates\ChocolateyLicenseFileTemplate.cs" />
<Compile Include="infrastructure.app\templates\ChocolateyVerificationFileTemplate.cs" />
<Compile Include="infrastructure\commandline\ReadKeyTimeout.cs" />
<Compile Include="infrastructure\commands\Execute.cs" />
<Compile Include="GetChocolatey.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using NuGet;

// ReSharper disable InconsistentNaming
Expand Down Expand Up @@ -44,8 +49,30 @@ public ChocolateyLocalPackageRepository(IPackagePathResolver pathResolver, IFile
public override void AddPackage(IPackage package)
{
string packageFilePath = GetPackageFilePath(package);
if (PackageSaveMode.HasFlag(PackageSaveModes.Nuspec))
{
string manifestFilePath = GetManifestFilePath(package.Id, package.Version);
Manifest manifest = Manifest.Create(package);
manifest.Metadata.ReferenceSets = Enumerable.ToList<ManifestReferenceSet>(Enumerable.Select<IGrouping<FrameworkName, IPackageAssemblyReference>, ManifestReferenceSet>(Enumerable.GroupBy<IPackageAssemblyReference, FrameworkName>(package.AssemblyReferences, (Func<IPackageAssemblyReference, FrameworkName>)(f => f.TargetFramework)), (Func<IGrouping<FrameworkName, IPackageAssemblyReference>, ManifestReferenceSet>)(g => new ManifestReferenceSet()
{
TargetFramework = g.Key == (FrameworkName)null ? (string)null : VersionUtility.GetFrameworkString(g.Key),
References = Enumerable.ToList<ManifestReference>(Enumerable.Select<IPackageAssemblyReference, ManifestReference>((IEnumerable<IPackageAssemblyReference>)g, (Func<IPackageAssemblyReference, ManifestReference>)(p => new ManifestReference()
{
File = p.Name
})))
})));
FileSystem.AddFileWithCheck(manifestFilePath, manifest.Save);
}

FileSystem.AddFileWithCheck(packageFilePath, package.GetStream);
}

private string GetManifestFilePath(string packageId, SemanticVersion version)
{
string packageDirectory = PathResolver.GetPackageDirectory(packageId, version);
string path2 = packageDirectory + Constants.ManifestExtension;
return Path.Combine(packageDirectory, path2);
}
}

// ReSharper restore InconsistentNaming
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure.app/nuget/NugetCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static IPackagePathResolver GetPathResolver(ChocolateyConfiguration confi
public static IPackageRepository GetLocalRepository(IPackagePathResolver pathResolver, IFileSystem nugetPackagesFileSystem)
{
IPackageRepository localRepository = new ChocolateyLocalPackageRepository(pathResolver, nugetPackagesFileSystem);
localRepository.PackageSaveMode = PackageSaveModes.Nupkg;
localRepository.PackageSaveMode = PackageSaveModes.Nupkg | PackageSaveModes.Nuspec;

return localRepository;
}
Expand Down
11 changes: 8 additions & 3 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ require admin rights. Only advanced users should run choco w/out an
elevated shell. When you open the command shell, you should ensure
that you do so with ""Run as Administrator"" selected.
Do you want to continue?", new[] { "yes", "no" }, defaultChoice: null, requireAnswer: true);
Do you want to continue?", new[] { "yes", "no" },
defaultChoice: null,
requireAnswer: true,
allowShortAnswer: true,
shortPrompt: true
);

if (selection.is_equal_to("no"))
{
Expand All @@ -199,5 +204,5 @@ require admin rights. Only advanced users should run choco w/out an
}

}
}

}
Loading

0 comments on commit b6b3433

Please sign in to comment.