Skip to content

Commit

Permalink
(GH-584) Licensed ConfigurationBuilder hooks
Browse files Browse the repository at this point in the history
Hook in and set configuration for licensed version from the licensed
version. Remove configuration items that are for licensed only so they
can be set and adjusted appropriately and independently in the licensed
version.
  • Loading branch information
ferventcoder committed Feb 10, 2016
1 parent 0ad1baa commit 89691e2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static class ApplicationParameters
public static readonly string LicenseFileLocation = _fileSystem.combine_paths(InstallLocation, "license", "chocolatey.license.xml");
public static readonly string LicensedAssemblyLocation = _fileSystem.combine_paths(InstallLocation, "extensions", "chocolatey", "chocolatey.licensed.dll");
public static readonly string LicensedComponentRegistry = @"chocolatey.licensed.infrastructure.app.registration.ContainerBinding";
public static readonly string LicensedConfigurationBuilder = @"chocolatey.licensed.infrastructure.app.builders.ConfigurationBuilder";
public static readonly string PackageNamesSeparator = ";";
public static readonly string OfficialChocolateyPublicKey = "79d02ea9cad655eb";

Expand Down Expand Up @@ -97,7 +98,6 @@ public static class ConfigSettings
public static readonly string Proxy = "proxy";
public static readonly string ProxyUser = "proxyUser";
public static readonly string ProxyPassword = "proxyPassword";
public static readonly string VirusCheckMinimumPositives = "virusCheckMinimumPositives";
}

public static class Features
Expand Down
66 changes: 42 additions & 24 deletions src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.app.builders
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using adapters;
using attributes;
Expand All @@ -33,6 +34,7 @@ namespace chocolatey.infrastructure.app.builders
using nuget;
using platforms;
using tolerance;
using Assembly = adapters.Assembly;
using Container = SimpleInjector.Container;
using Environment = adapters.Environment;

Expand All @@ -41,6 +43,7 @@ namespace chocolatey.infrastructure.app.builders
/// </summary>
public static class ConfigurationBuilder
{
private const string SET_CONFIGURATION_METHOD = "SetConfiguration";
private static Lazy<IEnvironment> _environmentInitializer = new Lazy<IEnvironment>(() => new Environment());

[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down Expand Up @@ -71,8 +74,9 @@ public static void set_up_configuration(IList<string> args, ChocolateyConfigurat
ConfigurationOptions.reset_options();
set_global_options(args, config, container);
set_environment_options(config);
set_license_options(config, license);
set_environment_variables(config);
// must be done last for overrides
set_licensed_options(config, license, configFileSettings);
set_config_file_settings(configFileSettings, xmlService);
}

Expand Down Expand Up @@ -172,10 +176,6 @@ private static void set_config_items(ChocolateyConfiguration config, ConfigFileS
config.Proxy.Location = set_config_item(ApplicationParameters.ConfigSettings.Proxy, configFileSettings, string.Empty, "Explicit proxy location.");
config.Proxy.User = set_config_item(ApplicationParameters.ConfigSettings.ProxyUser, configFileSettings, string.Empty, "Optional proxy user.");
config.Proxy.EncryptedPassword = set_config_item(ApplicationParameters.ConfigSettings.ProxyPassword, configFileSettings, string.Empty, "Optional proxy password. Encrypted.");

int minPositives=0;
int.TryParse(set_config_item(ApplicationParameters.ConfigSettings.VirusCheckMinimumPositives, configFileSettings, "5", "Optional proxy password. Encrypted."), out minPositives);
config.VirusCheckMinimumPositives = minPositives == 0 ? 5 : minPositives;
}

private static string set_config_item(string configName, ConfigFileSettings configFileSettings, string defaultValue, string description, bool forceSettingValue = false)
Expand Down Expand Up @@ -367,21 +367,6 @@ private static void set_environment_options(ChocolateyConfiguration config)
config.Information.IsProcessElevated = ProcessInformation.process_is_elevated();
}

private static void set_license_options(ChocolateyConfiguration config, ChocolateyLicense license)
{
config.Information.LicenseExpirationDate = license.ExpirationDate;
config.Information.LicenseIsValid = license.IsValid;
config.Information.LicenseVersion = license.Version ?? string.Empty;
config.Information.LicenseType = license.is_licensed_version() ? license.LicenseType.get_description_or_value() : string.Empty;

var licenseName = license.Name.to_string();
if (licenseName.Contains("@"))
{
licenseName = licenseName.Remove(licenseName.IndexOf("@", StringComparison.InvariantCulture)) + "[at REDACTED])";
}
config.Information.LicenseUserName = licenseName;
}

public static void set_environment_variables(ChocolateyConfiguration config)
{
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, ApplicationParameters.InstallLocation);
Expand Down Expand Up @@ -419,13 +404,46 @@ public static void set_environment_variables(ChocolateyConfiguration config)
}

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

private static void set_licensed_options(ChocolateyConfiguration config, ChocolateyLicense license, ConfigFileSettings configFileSettings)
{
if (license.AssemblyLoaded)
{
Environment.SetEnvironmentVariable("ChocolateyVirusCheckFiles", "true");
Environment.SetEnvironmentVariable("ChocolateyVirusCheckMinimumPositives", config.VirusCheckMinimumPositives.to_string());
Type licensedConfigBuilder = license.Assembly.GetType(ApplicationParameters.LicensedConfigurationBuilder, throwOnError: true, ignoreCase: true);

if (licensedConfigBuilder == null)
{
"chocolatey".Log().Error(
@"Type expected for registering components was null. Unable to provide
name due to it being null.");
return;
}
try
{
object componentClass = Activator.CreateInstance(licensedConfigBuilder);

licensedConfigBuilder.InvokeMember(
SET_CONFIGURATION_METHOD,
BindingFlags.InvokeMethod,
null,
componentClass,
new Object[] { config, configFileSettings }
);
}
catch (Exception ex)
{
"chocolatey".Log().Error(
ChocolateyLoggers.Important,
@"Error when setting configuration for '{0}':{1} {2}".format_with(
licensedConfigBuilder.FullName,
Environment.NewLine,
ex.Message
));
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace chocolatey.infrastructure.app.configuration
using System.Reflection;
using System.Text;
using domain;
using licensing;
using logging;
using platforms;

Expand Down Expand Up @@ -207,9 +206,6 @@ private void append_output(StringBuilder propertyValues, string append)
public bool AllowDowngrade { get; set; }
public bool ForceDependencies { get; set; }

//licensed versions only
public int VirusCheckMinimumPositives { get; set; }

/// <summary>
/// Configuration values provided by choco.
/// </summary>
Expand Down Expand Up @@ -330,12 +326,6 @@ public sealed class InformationCommandConfiguration
public bool IsInteractive { get; set; }
public bool IsUserAdministrator { get; set; }
public bool IsProcessElevated { get; set; }
public string LicenseType { get; set; }
public bool LicenseIsValid { get; set; }
public string LicenseVersion { get; set; }
public string LicenseUserName { get; set; }
public DateTime? LicenseExpirationDate { get; set; }

}

[Serializable]
Expand Down

0 comments on commit 89691e2

Please sign in to comment.