Skip to content

Commit

Permalink
(GH-584) License loading occurs in DLL
Browse files Browse the repository at this point in the history
Allow the license loading to occur in the DLL in the same way it does
in the executable.
  • Loading branch information
ferventcoder committed May 27, 2016
1 parent 9ea851e commit 347753b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 35 deletions.
33 changes: 2 additions & 31 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ namespace chocolatey.console
using infrastructure.commandline;
using infrastructure.configuration;
using infrastructure.extractors;
using infrastructure.filesystem;
using infrastructure.information;
using infrastructure.licensing;
using infrastructure.logging;
using infrastructure.registration;
using resources;
using SimpleInjector;
using Console = System.Console;
using Environment = System.Environment;
using IFileSystem = infrastructure.filesystem.IFileSystem;

public sealed class Program
{
Expand All @@ -52,33 +50,7 @@ private static void Main(string[] args)
Log4NetAppenderConfiguration.configure(loggingLocation);
Bootstrap.initialize();
Bootstrap.startup();

var license = LicenseValidation.validate();
if (license.is_licensed_version())
{
try
{
var licensedAssembly = Assembly.LoadFile(ApplicationParameters.LicensedAssemblyLocation);
license.AssemblyLoaded = true;
license.Assembly = licensedAssembly;
license.Version = VersionInformation.get_current_informational_version(licensedAssembly);
Type licensedComponent = licensedAssembly.GetType(ApplicationParameters.LicensedComponentRegistry, throwOnError: false, ignoreCase: true);
SimpleInjectorContainer.add_component_registry_class(licensedComponent);
}
catch (Exception ex)
{
"chocolatey".Log().Error(
@"Error when attempting to load chocolatey licensed assembly. Ensure
that chocolatey.licensed.dll exists at
'{0}'.
Install with `choco install chocolatey.extension`.
The error message itself may be helpful as well:{1} {2}".format_with(
ApplicationParameters.LicensedAssemblyLocation,
Environment.NewLine,
ex.Message
));
}
}
var license = License.validate_license();
var container = SimpleInjectorContainer.Container;

var config = container.GetInstance<ChocolateyConfiguration>();
Expand Down Expand Up @@ -185,7 +157,6 @@ Install with `choco install chocolatey.extension`.
#endif
pause_execution_if_debug();
Bootstrap.shutdown();

Environment.Exit(Environment.ExitCode);
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/chocolatey/GetChocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey
using System;
using System.Collections.Generic;
using infrastructure.licensing;
using NuGet;
using SimpleInjector;
using infrastructure.adapters;
using infrastructure.app;
Expand All @@ -26,11 +27,10 @@ namespace chocolatey
using infrastructure.app.runners;
using infrastructure.configuration;
using infrastructure.extractors;
using infrastructure.filesystem;
using infrastructure.logging;
using infrastructure.registration;
using infrastructure.services;
using resources;
using IFileSystem = infrastructure.filesystem.IFileSystem;

// ReSharper disable InconsistentNaming

Expand Down Expand Up @@ -61,7 +61,7 @@ public GetChocolatey()
{
Log4NetAppenderConfiguration.configure();
Bootstrap.initialize();
_license = LicenseValidation.validate();
_license = License.validate_license();
_container = SimpleInjectorContainer.Container;
}

Expand Down Expand Up @@ -221,7 +221,13 @@ public int ListCount()
private ChocolateyConfiguration create_configuration(IList<string> args)
{
var configuration = new ChocolateyConfiguration();
ConfigurationBuilder.set_up_configuration(args, configuration, _container, _license, null);
ConfigurationBuilder.set_up_configuration(
args,
configuration,
_container,
_license,
null);

Config.initialize_with(configuration);

configuration.PromptForConfirmation = false;
Expand Down
1 change: 1 addition & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
<Compile Include="infrastructure.app\services\ISourceRunner.cs" />
<Compile Include="infrastructure\licensing\ChocolateyLicense.cs" />
<Compile Include="infrastructure\licensing\ChocolateyLicenseType.cs" />
<Compile Include="infrastructure\licensing\License.cs" />
<Compile Include="infrastructure\powershell\PoshHost.cs" />
<Compile Include="infrastructure\powershell\PoshHostRawUserInterface.cs" />
<Compile Include="infrastructure\powershell\PoshHostUserInterface.cs" />
Expand Down
59 changes: 59 additions & 0 deletions src/chocolatey/infrastructure/licensing/License.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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.licensing
{
using System;
using adapters;
using app;
using information;
using registration;
using Environment = System.Environment;

public static class License
{
public static ChocolateyLicense validate_license()
{
var license = LicenseValidation.validate();
if (license.is_licensed_version())
{
try
{
var licensedAssembly = Assembly.LoadFile(ApplicationParameters.LicensedAssemblyLocation);
license.AssemblyLoaded = true;
license.Assembly = licensedAssembly;
license.Version = VersionInformation.get_current_informational_version(licensedAssembly);
Type licensedComponent = licensedAssembly.GetType(ApplicationParameters.LicensedComponentRegistry, throwOnError: false, ignoreCase: true);
SimpleInjectorContainer.add_component_registry_class(licensedComponent);
}
catch (Exception ex)
{
"chocolatey".Log().Error(
@"Error when attempting to load chocolatey licensed assembly. Ensure
that chocolatey.licensed.dll exists at
'{0}'.
Install with `choco install chocolatey.extension`.
The error message itself may be helpful as well:{1} {2}".format_with(
ApplicationParameters.LicensedAssemblyLocation,
Environment.NewLine,
ex.Message
));
}
}

return license;
}
}
}

0 comments on commit 347753b

Please sign in to comment.