Skip to content

Commit

Permalink
(GH-604) Add licensed source automatically
Browse files Browse the repository at this point in the history
When a valid license is found, add the pro/business source
automatically. This smooths out the the process of
installing/upgrading the pro/business version.
  • Loading branch information
ferventcoder committed Feb 4, 2016
1 parent 98c9f7a commit 45bd4c1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
60 changes: 47 additions & 13 deletions src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace chocolatey.console
using infrastructure.app.builders;
using infrastructure.app.configuration;
using infrastructure.app.runners;
using infrastructure.app.services;
using infrastructure.commandline;
using infrastructure.configuration;
using infrastructure.extractors;
Expand All @@ -33,14 +34,15 @@ namespace chocolatey.console
using infrastructure.logging;
using infrastructure.registration;
using resources;
using SimpleInjector;
using Console = System.Console;
using Environment = System.Environment;

public sealed class Program
{
// ReSharper disable InconsistentNaming
// ReSharper disable InconsistentNaming
private static void Main(string[] args)
// ReSharper restore InconsistentNaming
// ReSharper restore InconsistentNaming
{
try
{
Expand Down Expand Up @@ -77,19 +79,21 @@ that chocolatey.licensed.dll exists at
}
}
var container = SimpleInjectorContainer.Container;


add_or_remove_licensed_source(license, container);

var config = container.GetInstance<ChocolateyConfiguration>();
var fileSystem = container.GetInstance<IFileSystem>();

var warnings = new List<string>();

ConfigurationBuilder.set_up_configuration(
args,
config,
container,
license,
warning => { warnings.Add(warning); }
);
ConfigurationBuilder.set_up_configuration(
args,
config,
container,
license,
warning => { warnings.Add(warning); }
);
Config.initialize_with(config);

report_version_and_exit_if_requested(args, config);
Expand All @@ -112,12 +116,12 @@ that chocolatey.licensed.dll exists at
}
#endif
}

if (warnings.Count != 0 && config.RegularOutput)
{
foreach (var warning in warnings.or_empty_list_if_null())
{
"chocolatey".Log().Warn(ChocolateyLoggers.Important, warning);
"chocolatey".Log().Warn(ChocolateyLoggers.Important, warning);
}
}

Expand All @@ -142,7 +146,7 @@ that chocolatey.licensed.dll exists at
"redirects",
"tools"
};
AssemblyFileExtractor.extract_all_resources_to_relative_directory(fileSystem, Assembly.GetAssembly(typeof (ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources);
AssemblyFileExtractor.extract_all_resources_to_relative_directory(fileSystem, Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources);

var application = new ConsoleApplication();
application.run(args, config, container);
Expand Down Expand Up @@ -218,6 +222,36 @@ private static void remove_old_chocolatey_exe(IFileSystem fileSystem)
}
}

private static void add_or_remove_licensed_source(ChocolateyLicense license, Container container)
{
var addOrUpdate = license.IsValid;
var config = new ChocolateyConfiguration {
RegularOutput = false,
};

var sourceService = container.GetInstance<IChocolateyConfigSettingsService>();
var sources = sourceService.source_list(config);

config.SourceCommand.Name = ApplicationParameters.ChocolateyLicensedFeedSourceName;
config.Sources = ApplicationParameters.ChocolateyLicensedFeedSource;
config.SourceCommand.Username = "customer";
config.SourceCommand.Password = license.Id;
config.SourceCommand.Priority = 10;

if (addOrUpdate && !sources.Any(s =>
s.Id.is_equal_to(ApplicationParameters.ChocolateyLicensedFeedSourceName)
&& s.Authenticated)
)
{
sourceService.source_add(config);
}

if (!addOrUpdate)
{
sourceService.source_remove(config);
}
}

private static void pause_execution_if_debug()
{
#if DEBUG
Expand Down
2 changes: 2 additions & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static class ApplicationParameters
public static readonly string TemplatesLocation = _fileSystem.combine_paths(InstallLocation, "templates");
public static readonly string ChocolateyCommunityFeedPushSource = "https://chocolatey.org/";
public static readonly string ChocolateyCommunityFeedSource = "https://chocolatey.org/api/v2/";
public static readonly string ChocolateyLicensedFeedSource = "https://licensedpackages.chocolatey.org/api/v2/";
public static readonly string ChocolateyLicensedFeedSourceName = "chocolatey.licensed";
public static readonly string UserAgent = "Chocolatey Command Line";
public static readonly string RegistryValueInstallLocation = "InstallLocation";
public static readonly string AllPackages = "all";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.licensing

public sealed class ChocolateyLicense
{
public string Id { get; set; }
public string Name { get; set; }
public ChocolateyLicenseType LicenseType { get; set; }
public bool IsValid { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/chocolatey/infrastructure/licensing/LicenseValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static ChocolateyLicense validate()

chocolateyLicense.ExpirationDate = license.ExpirationDate;
chocolateyLicense.Name = license.Name;

chocolateyLicense.Id = license.UserId.to_string();

//todo: if it is expired, provide a warning.
// one month after it should stop working
}
Expand Down

0 comments on commit 45bd4c1

Please sign in to comment.