Skip to content

Commit

Permalink
(GH-620) Set environment variables after options
Browse files Browse the repository at this point in the history
After all options and configuration have been set, then set the
environment variables.
  • Loading branch information
ferventcoder committed Jun 5, 2016
1 parent 29de194 commit e627a01
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
</Compile>
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyInfoCommand.cs" />
<Compile Include="infrastructure.app\configuration\EnvironmentSettings.cs" />
<Compile Include="infrastructure.app\domain\GenericRegistryKey.cs" />
<Compile Include="infrastructure.app\domain\GenericRegistryValue.cs" />
<Compile Include="infrastructure.app\domain\installers\BitRockInstaller.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void set_up_configuration(IList<string> args, ChocolateyConfigurat
ConfigurationOptions.reset_options();
set_global_options(args, config, container);
set_environment_options(config);
set_environment_variables(config);
EnvironmentSettings.set_environment_variables(config);
// must be done last for overrides
set_licensed_options(config, license, configFileSettings);
// save all changes if there are any
Expand Down Expand Up @@ -464,56 +464,6 @@ private static void set_environment_options(ChocolateyConfiguration config)
config.Information.IsProcessElevated = ProcessInformation.process_is_elevated();
}

public static void reset_environment_variables(ChocolateyConfiguration config)
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageInstallLocationEnvironmentVariableName, null);
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageInstallerTypeEnvironmentVariableName, null);
}

public static void set_environment_variables(ChocolateyConfiguration config)
{
reset_environment_variables(config);

Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, ApplicationParameters.InstallLocation);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION", config.Information.ChocolateyVersion);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION_PRODUCT", config.Information.ChocolateyProductVersion);
Environment.SetEnvironmentVariable("OS_PLATFORM", config.Information.PlatformType.get_description_or_value());
Environment.SetEnvironmentVariable("OS_VERSION", config.Information.PlatformVersion.to_string());
Environment.SetEnvironmentVariable("OS_NAME", config.Information.PlatformName.to_string());
// experimental until we know if this value returns correctly based on the OS and not the current process.
Environment.SetEnvironmentVariable("OS_IS64BIT", config.Information.Is64Bit ? "true" : "false");
Environment.SetEnvironmentVariable("IS_ADMIN", config.Information.IsUserAdministrator ? "true" : "false");
Environment.SetEnvironmentVariable("IS_PROCESSELEVATED", config.Information.IsProcessElevated ? "true" : "false");
Environment.SetEnvironmentVariable("TEMP", config.CacheLocation);

if (config.Debug) Environment.SetEnvironmentVariable("ChocolateyEnvironmentDebug", "true");
if (config.Verbose) Environment.SetEnvironmentVariable("ChocolateyEnvironmentVerbose", "true");
if (!config.Features.CheckSumFiles) Environment.SetEnvironmentVariable("ChocolateyIgnoreChecksums", "true");
Environment.SetEnvironmentVariable("chocolateyRequestTimeout", config.WebRequestTimeoutSeconds.to_string() + "000");
Environment.SetEnvironmentVariable("chocolateyResponseTimeout", config.CommandExecutionTimeoutSeconds.to_string() + "000");

if (!string.IsNullOrWhiteSpace(config.Proxy.Location))
{
var proxyCreds = string.Empty;
if (!string.IsNullOrWhiteSpace(config.Proxy.User) &&
!string.IsNullOrWhiteSpace(config.Proxy.EncryptedPassword)
)
{
proxyCreds = "{0}:{1}@".format_with(config.Proxy.User, NugetEncryptionUtility.DecryptString(config.Proxy.EncryptedPassword));

Environment.SetEnvironmentVariable("chocolateyProxyUser", config.Proxy.User);
Environment.SetEnvironmentVariable("chocolateyProxyPassword", NugetEncryptionUtility.DecryptString(config.Proxy.EncryptedPassword));
}

Environment.SetEnvironmentVariable("http_proxy", "{0}{1}".format_with(proxyCreds, config.Proxy.Location));
Environment.SetEnvironmentVariable("https_proxy", "{0}{1}".format_with(proxyCreds, config.Proxy.Location));
Environment.SetEnvironmentVariable("chocolateyProxyLocation", config.Proxy.Location);
}

if (config.Features.UsePowerShellHost) Environment.SetEnvironmentVariable("ChocolateyPowerShellHost", "true");
if (config.Force) Environment.SetEnvironmentVariable("ChocolateyForce", "true");
}

private static void set_licensed_options(ChocolateyConfiguration config, ChocolateyLicense license, ConfigFileSettings configFileSettings)
{
config.Information.IsLicensedVersion = license.is_licensed_version();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.configuration
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using adapters;
using logging;
using nuget;
using Environment = adapters.Environment;

public static class EnvironmentSettings
{
private const string SET_ENVIRONMENT_METHOD = "SetEnvironment";
private static Lazy<IEnvironment> _environmentInitializer = new Lazy<IEnvironment>(() => new Environment());

[EditorBrowsable(EditorBrowsableState.Never)]
public static void initialize_with(Lazy<IEnvironment> environment)
{
_environmentInitializer = environment;
}

private static IEnvironment Environment
{
get { return _environmentInitializer.Value; }
}

public static void reset_environment_variables(ChocolateyConfiguration config)
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageInstallLocationEnvironmentVariableName, null);
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyPackageInstallerTypeEnvironmentVariableName, null);
}

public static void set_environment_variables(ChocolateyConfiguration config)
{
reset_environment_variables(config);

Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, ApplicationParameters.InstallLocation);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION", config.Information.ChocolateyVersion);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION_PRODUCT", config.Information.ChocolateyProductVersion);
Environment.SetEnvironmentVariable("OS_PLATFORM", config.Information.PlatformType.get_description_or_value());
Environment.SetEnvironmentVariable("OS_VERSION", config.Information.PlatformVersion.to_string());
Environment.SetEnvironmentVariable("OS_NAME", config.Information.PlatformName.to_string());
// experimental until we know if this value returns correctly based on the OS and not the current process.
Environment.SetEnvironmentVariable("OS_IS64BIT", config.Information.Is64Bit ? "true" : "false");
Environment.SetEnvironmentVariable("IS_ADMIN", config.Information.IsUserAdministrator ? "true" : "false");
Environment.SetEnvironmentVariable("IS_PROCESSELEVATED", config.Information.IsProcessElevated ? "true" : "false");
Environment.SetEnvironmentVariable("TEMP", config.CacheLocation);

if (config.Debug) Environment.SetEnvironmentVariable("ChocolateyEnvironmentDebug", "true");
if (config.Verbose) Environment.SetEnvironmentVariable("ChocolateyEnvironmentVerbose", "true");
if (!config.Features.CheckSumFiles) Environment.SetEnvironmentVariable("ChocolateyIgnoreChecksums", "true");
Environment.SetEnvironmentVariable("chocolateyRequestTimeout", config.WebRequestTimeoutSeconds.to_string() + "000");
Environment.SetEnvironmentVariable("chocolateyResponseTimeout", config.CommandExecutionTimeoutSeconds.to_string() + "000");

if (!string.IsNullOrWhiteSpace(config.Proxy.Location))
{
var proxyCreds = string.Empty;
if (!string.IsNullOrWhiteSpace(config.Proxy.User) &&
!string.IsNullOrWhiteSpace(config.Proxy.EncryptedPassword)
)
{
proxyCreds = "{0}:{1}@".format_with(config.Proxy.User, NugetEncryptionUtility.DecryptString(config.Proxy.EncryptedPassword));

Environment.SetEnvironmentVariable("chocolateyProxyUser", config.Proxy.User);
Environment.SetEnvironmentVariable("chocolateyProxyPassword", NugetEncryptionUtility.DecryptString(config.Proxy.EncryptedPassword));
}

Environment.SetEnvironmentVariable("http_proxy", "{0}{1}".format_with(proxyCreds, config.Proxy.Location));
Environment.SetEnvironmentVariable("https_proxy", "{0}{1}".format_with(proxyCreds, config.Proxy.Location));
Environment.SetEnvironmentVariable("chocolateyProxyLocation", config.Proxy.Location);
}

if (config.Features.UsePowerShellHost) Environment.SetEnvironmentVariable("ChocolateyPowerShellHost", "true");
if (config.Force) Environment.SetEnvironmentVariable("ChocolateyForce", "true");
}
}
}
2 changes: 2 additions & 0 deletions src/chocolatey/infrastructure.app/runners/GenericRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ private ICommand find_command(ChocolateyConfiguration config, Container containe
}

set_source_type(config);
// guaranteed that all settings are set.
EnvironmentSettings.set_environment_variables(config);

this.Log().Debug(() => "Configuration: {0}".format_with(config.ToString()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void randomly_notify_about_pro_business(ChocolateyConfiguration config, s

public void handle_package_result(PackageResult packageResult, ChocolateyConfiguration config, CommandNameType commandName)
{
ConfigurationBuilder.reset_environment_variables(config);
EnvironmentSettings.reset_environment_variables(config);
set_pending(packageResult, config);
var pkgInfo = _packageInfoService.get_package_information(packageResult.Package);
if (config.AllowMultipleVersions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
{
var failure = false;

ConfigurationBuilder.set_environment_variables(configuration);
EnvironmentSettings.set_environment_variables(configuration);

var package = packageResult.Package;
Environment.SetEnvironmentVariable("chocolateyPackageName", package.Id);
Expand Down

0 comments on commit e627a01

Please sign in to comment.