Skip to content

Commit

Permalink
(GH-420) Set env vars once config is complete
Browse files Browse the repository at this point in the history
To start moving out environment variables that may be needed by several
execution engines, move the setting of those out.
  • Loading branch information
ferventcoder committed Sep 30, 2015
1 parent 51d1800 commit ff7e4c4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
47 changes: 47 additions & 0 deletions src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace chocolatey.infrastructure.app.builders
using information;
using infrastructure.services;
using logging;
using nuget;
using platforms;
using tolerance;
using Environment = adapters.Environment;
Expand Down Expand Up @@ -64,6 +65,7 @@ public static void set_up_configuration(IList<string> args, ChocolateyConfigurat
ConfigurationOptions.reset_options();
set_global_options(args, config);
set_environment_options(config);
set_environment_variables(config);
}

private static void set_file_configuration(ChocolateyConfiguration config, IFileSystem fileSystem, IXmlService xmlService, Action<string> notifyWarnLoggingAction)
Expand Down Expand Up @@ -324,5 +326,50 @@ private static void set_environment_options(ChocolateyConfiguration config)
config.Information.IsUserAdministrator = ProcessInformation.user_is_administrator();
config.Information.IsProcessElevated = ProcessInformation.process_is_elevated();
}

public static void set_environment_variables(ChocolateyConfiguration 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");
}
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);
}
}
}
}
48 changes: 6 additions & 42 deletions src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.app.services
using System.IO;
using System.Linq;
using adapters;
using builders;
using commandline;
using configuration;
using domain;
Expand Down Expand Up @@ -165,17 +166,10 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack

var failure = false;

//todo: this is here for any possible compatibility issues. Should be reviewed and removed.
ConfigurationBuilder.set_environment_variables(configuration);

var package = packageResult.Package;
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, ApplicationParameters.InstallLocation);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION", configuration.Information.ChocolateyVersion);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION_PRODUCT", configuration.Information.ChocolateyProductVersion);
Environment.SetEnvironmentVariable("OS_PLATFORM", configuration.Information.PlatformType.get_description_or_value());
Environment.SetEnvironmentVariable("OS_VERSION", configuration.Information.PlatformVersion.to_string());
Environment.SetEnvironmentVariable("OS_NAME", configuration.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", configuration.Information.Is64Bit ? "true" : "false");
Environment.SetEnvironmentVariable("IS_ADMIN", configuration.Information.IsUserAdministrator ? "true" : "false");
Environment.SetEnvironmentVariable("IS_PROCESSELEVATED", configuration.Information.IsProcessElevated ? "true" : "false");
Environment.SetEnvironmentVariable("chocolateyPackageName", package.Id);
Environment.SetEnvironmentVariable("packageName", package.Id);
Environment.SetEnvironmentVariable("chocolateyPackageVersion", package.Version.to_string());
Expand All @@ -195,42 +189,12 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
{
Environment.SetEnvironmentVariable("chocolateyInstallOverride", "true");
}

Environment.SetEnvironmentVariable("TEMP", configuration.CacheLocation);


if (configuration.NotSilent)
{
Environment.SetEnvironmentVariable("chocolateyInstallOverride", "true");
}
if (configuration.Debug)
{
Environment.SetEnvironmentVariable("ChocolateyEnvironmentDebug", "true");
}
if (configuration.Verbose)
{
Environment.SetEnvironmentVariable("ChocolateyEnvironmentVerbose", "true");
}
if (!configuration.Features.CheckSumFiles)
{
Environment.SetEnvironmentVariable("ChocolateyIgnoreChecksums", "true");
}
if (!string.IsNullOrWhiteSpace(configuration.Proxy.Location))
{
var proxy_creds = string.Empty;
if (!string.IsNullOrWhiteSpace(configuration.Proxy.User) &&
!string.IsNullOrWhiteSpace(configuration.Proxy.EncryptedPassword)
)
{
proxy_creds = "{0}:{1}@".format_with(configuration.Proxy.User, NugetEncryptionUtility.DecryptString(configuration.Proxy.EncryptedPassword));

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

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

//todo:if (configuration.NoOutput)
//{
// Environment.SetEnvironmentVariable("ChocolateyEnvironmentQuiet","true");
Expand Down

0 comments on commit ff7e4c4

Please sign in to comment.