Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-708) PowerShell Host.Version Returns Actual
  (maint) formatting
  (GH-708) Retrieve generic registry values
  (GH-198) pass configuration in messaging
  • Loading branch information
ferventcoder committed May 29, 2016
2 parents 0f46075 + 084af74 commit 46858b3
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="infrastructure.app\domain\installers\SetupFactoryInstaller.cs" />
<Compile Include="infrastructure.app\domain\installers\SquirrelInstaller.cs" />
<Compile Include="infrastructure.app\domain\installers\WiseInstaller.cs" />
<Compile Include="infrastructure.app\domain\RegistryHiveType.cs" />
<Compile Include="infrastructure.app\domain\RegistryValueKindType.cs" />
<Compile Include="infrastructure.app\events\HandlePackageResultCompletedMessage.cs" />
<Compile Include="infrastructure.app\services\FileTypeDetectorService.cs" />
Expand Down
28 changes: 28 additions & 0 deletions src/chocolatey/infrastructure.app/domain/RegistryHiveType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace chocolatey.infrastructure.app.domain
{
public enum RegistryHiveType
{
ClassesRoot,
CurrentUser,
LocalMachine,
Users,
PerformanceData,
CurrentConfig,
DynData,
}
}
11 changes: 10 additions & 1 deletion src/chocolatey/infrastructure.app/events/PostRunMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@

namespace chocolatey.infrastructure.app.events
{
using configuration;
using infrastructure.commands;
using infrastructure.events;

public class PostRunMessage : IMessage
{
public ChocolateyConfiguration Configuration { get; private set; }

public PostRunMessage(ChocolateyConfiguration configuration)
{
this.Configuration = configuration;
}
}

public class PostRunMessage<TCommand> : IMessage where TCommand : ICommand
{
public TCommand Command { get; private set; }
public ChocolateyConfiguration Configuration { get; set; }
public object[] State { get; private set; }

public PostRunMessage(TCommand command, object[] state)
public PostRunMessage(TCommand command, ChocolateyConfiguration configuration, object[] state)
{
Command = command;
this.Configuration = configuration;
State = state;
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/chocolatey/infrastructure.app/events/PreRunMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@

namespace chocolatey.infrastructure.app.events
{
using configuration;
using infrastructure.commands;
using infrastructure.events;

public class PreRunMessage : IMessage
{
public ChocolateyConfiguration Configuration { get; private set; }

public PreRunMessage(ChocolateyConfiguration configuration)
{
this.Configuration = configuration;
}
}

public class PreRunMessage<TCommand> : IMessage where TCommand : ICommand
{
public TCommand Command { get; private set; }
public ChocolateyConfiguration Configuration { get; private set; }
public object[] State { get; private set; }

public PreRunMessage(TCommand command, object[] state)
public PreRunMessage(TCommand command, ChocolateyConfiguration configuration, object[] state)
{
Command = command;
this.Configuration = configuration;
State = state;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
fail_when_license_is_missing_or_invalid_if_requested(config);


EventManager.publish(new PreRunMessage());
EventManager.publish(new PreRunMessage(config));

var command = find_command(config, container, isConsole, parseArgs);
if (command != null)
Expand All @@ -153,7 +153,7 @@ public void run(ChocolateyConfiguration config, Container container, bool isCons
command.run(config);
}

EventManager.publish(new PostRunMessage());
EventManager.publish(new PostRunMessage(config));

foreach (var task in tasks.or_empty_list_if_null())
{
Expand Down
57 changes: 52 additions & 5 deletions src/chocolatey/infrastructure.app/services/RegistryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public RegistryService(IXmlService xmlService, IFileSystem fileSystem)

private RegistryKey open_key(RegistryHive hive, RegistryView view)
{
return FaultTolerance.try_catch_with_logging_exception(
() => RegistryKey.OpenBaseKey(hive, view),
"Could not open registry hive '{0}' for view '{1}'".format_with(hive.to_string(), view.to_string()),
logWarningInsteadOfError: true);
return FaultTolerance.try_catch_with_logging_exception(
() => RegistryKey.OpenBaseKey(hive, view),
"Could not open registry hive '{0}' for view '{1}'".format_with(hive.to_string(), view.to_string()),
logWarningInsteadOfError: true);
}

private void add_key(IList<RegistryKey> keys, RegistryHive hive, RegistryView view)
Expand Down Expand Up @@ -281,7 +281,7 @@ private void get_values(RegistryKey key, string subKeyName, IList<GenericRegistr
{
Name = valueName,
ParentKeyName = subKey.Name,
Type = (RegistryValueKindType)Enum.Parse(typeof(RegistryValueKindType), subKey.GetValueKind(valueName).to_string(), ignoreCase:true),
Type = (RegistryValueKindType)Enum.Parse(typeof(RegistryValueKindType), subKey.GetValueKind(valueName).to_string(), ignoreCase: true),
Value = subKey.GetValue(valueName, expandValues ? RegistryValueOptions.None : RegistryValueOptions.DoNotExpandEnvironmentNames).to_string(),
});
}
Expand Down Expand Up @@ -344,6 +344,53 @@ public RegistryKey get_key(RegistryHive hive, string subKeyPath)

return null;
}

public static GenericRegistryValue get_value(RegistryHiveType hive, string subKeyPath, string registryValue)
{
var hiveActual = (RegistryHive)Enum.Parse(typeof(RegistryHive), hive.to_string(), ignoreCase: true);
IList<RegistryKey> keyLocations = new List<RegistryKey>();
if (Environment.Is64BitOperatingSystem)
{
keyLocations.Add(RegistryKey.OpenBaseKey(hiveActual, RegistryView.Registry64));
}

keyLocations.Add(RegistryKey.OpenBaseKey(hiveActual, RegistryView.Registry32));

GenericRegistryValue value = null;

foreach (var topLevelRegistryKey in keyLocations)
{
using (topLevelRegistryKey)
{
var key = topLevelRegistryKey.OpenSubKey(subKeyPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);
if (key != null)
{
value = FaultTolerance.try_catch_with_logging_exception(
() =>
{
if (key.GetValueNames().Contains(registryValue,StringComparer.InvariantCultureIgnoreCase))
{
return new GenericRegistryValue
{
Name = registryValue,
ParentKeyName = key.Name,
Type = (RegistryValueKindType)Enum.Parse(typeof(RegistryValueKindType), key.GetValueKind(registryValue).to_string(), ignoreCase: true),
Value = key.GetValue(registryValue).to_string(),
};
}

return null;
},
"Could not get registry value '{0}' from key '{1}'".format_with(registryValue, key.Name),
logWarningInsteadOfError: true);

if (value != null) break;
}
}
}

return value;
}
}

}
25 changes: 24 additions & 1 deletion src/chocolatey/infrastructure/powershell/PoshHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace chocolatey.infrastructure.powershell
using System.Management.Automation.Host;
using app;
using app.configuration;
using app.domain;
using app.services;

public class PoshHost : PSHost
{
Expand All @@ -28,6 +30,7 @@ public class PoshHost : PSHost
private readonly PoshHostUserInterface _psUI;
private readonly CultureInfo _cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
private readonly CultureInfo _cultureUiInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
private readonly Version _version;

private bool _isClosing;

Expand All @@ -48,6 +51,26 @@ public PoshHost(ChocolateyConfiguration configuration)
ExitCode = -1;
_configuration = configuration;
_psUI = new PoshHostUserInterface(configuration);
_version = get_current_version();
}

/// <summary>
/// Grabs the current version of PowerShell from the registry
/// </summary>
/// <returns></returns>
/// <remarks>We can cheat because we require at least v2, which takes us down to just the check for v3</remarks>
private Version get_current_version()
{
// users need at least v2 to even use Chocolatey
// this allows us to shortcut the check for the v1/2 key
var version = new Version(2, 0);
var majorMinor = RegistryService.get_value(RegistryHiveType.LocalMachine, "SOFTWARE\\Microsoft\\PowerShell\\3\\PowerShellEngine", "PowerShellVersion");
if (majorMinor != null)
{
version = new Version(majorMinor.Value);
}

return version;
}

public override void SetShouldExit(int exitCode)
Expand Down Expand Up @@ -87,7 +110,7 @@ public override PSHostUserInterface UI

public override Version Version
{
get { return new Version(_configuration.Information.ChocolateyVersion); }
get { return _version; }
}

#region Not Implemented / Empty
Expand Down

0 comments on commit 46858b3

Please sign in to comment.