From e627a019f4e44651d320394b3c6b8dc9ec50012f Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 5 Jun 2016 16:29:33 -0500 Subject: [PATCH] (GH-620) Set environment variables after options After all options and configuration have been set, then set the environment variables. --- src/chocolatey/chocolatey.csproj | 1 + .../builders/ConfigurationBuilder.cs | 52 +--------- .../configuration/EnvironmentSettings.cs | 96 +++++++++++++++++++ .../runners/GenericRunner.cs | 2 + .../services/ChocolateyPackageService.cs | 2 +- .../services/PowershellService.cs | 2 +- 6 files changed, 102 insertions(+), 53 deletions(-) create mode 100644 src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index 5a32e48c7a..80948c78e1 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -84,6 +84,7 @@ + diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index 4a08d4f562..ef3dbe6883 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -78,7 +78,7 @@ public static void set_up_configuration(IList 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 @@ -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(); diff --git a/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs b/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs new file mode 100644 index 0000000000..fdb81aa6a8 --- /dev/null +++ b/src/chocolatey/infrastructure.app/configuration/EnvironmentSettings.cs @@ -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 _environmentInitializer = new Lazy(() => new Environment()); + + [EditorBrowsable(EditorBrowsableState.Never)] + public static void initialize_with(Lazy 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"); + } + } +} diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index 773c212985..c7bd2c0321 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -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())); diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index cbf01b76e2..45e5a00959 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -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) diff --git a/src/chocolatey/infrastructure.app/services/PowershellService.cs b/src/chocolatey/infrastructure.app/services/PowershellService.cs index ab25e317d2..f57fb03ba3 100644 --- a/src/chocolatey/infrastructure.app/services/PowershellService.cs +++ b/src/chocolatey/infrastructure.app/services/PowershellService.cs @@ -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);