Skip to content

Commit

Permalink
Merge pull request #42 from ferventcoder/ticket/master/GH-10-packages…
Browse files Browse the repository at this point in the history
….config

(GH-10) Packages.config
  • Loading branch information
ferventcoder committed Feb 4, 2015
2 parents 17e259a + 864a666 commit 4c3059f
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 56 deletions.
15 changes: 15 additions & 0 deletions src/chocolatey/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

namespace chocolatey
{
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
/// Extensions for Object
/// </summary>
Expand All @@ -31,5 +34,17 @@ public static string to_string(this object input)

return input.ToString();
}

public static T deep_copy<T>(this T other)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, other);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}

}
}
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<Compile Include="infrastructure.app\commands\ChocolateyUpgradeCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyVersionCommand.cs" />
<Compile Include="infrastructure.app\configuration\ConfigFileFeatureSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFilePackageSetting.cs" />
<Compile Include="infrastructure.app\configuration\PackagesConfigFileSettings.cs" />
<Compile Include="infrastructure.app\domain\FeatureCommandType.cs" />
<Compile Include="infrastructure.app\domain\PinCommandType.cs" />
<Compile Include="infrastructure.app\domain\SourceCommandType.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace chocolatey.infrastructure.app.configuration
/// <summary>
/// The chocolatey configuration.
/// </summary>
[Serializable]
public class ChocolateyConfiguration
{
public ChocolateyConfiguration()
Expand Down Expand Up @@ -202,6 +203,7 @@ private void output_tostring(StringBuilder propertyValues, IEnumerable<PropertyI
public PinCommandConfiguration PinCommand { get; private set; }
}

[Serializable]
public sealed class InformationCommandConfiguration
{
// application set variables
Expand All @@ -214,6 +216,7 @@ public sealed class InformationCommandConfiguration
public bool IsInteractive { get; set; }
}

[Serializable]
public sealed class FeaturesConfiguration
{
public bool AutoUninstaller { get; set; }
Expand All @@ -222,13 +225,15 @@ public sealed class FeaturesConfiguration

//todo: retrofit other command configs this way

[Serializable]
public sealed class ListCommandConfiguration
{
// list
public bool LocalOnly { get; set; }
public bool IncludeRegistryPrograms { get; set; }
}

[Serializable]
public sealed class NewCommandConfiguration
{
public NewCommandConfiguration()
Expand All @@ -241,31 +246,36 @@ public NewCommandConfiguration()
public IDictionary<string, string> TemplateProperties { get; private set; }
}

[Serializable]
public sealed class SourcesCommandConfiguration
{
public string Name { get; set; }
public SourceCommandType Command { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}

}

[Serializable]
public sealed class FeatureCommandConfiguration
{
public string Name { get; set; }
public FeatureCommandType Command { get; set; }
}

[Serializable]
public sealed class PinCommandConfiguration
{
public string Name { get; set; }
public PinCommandType Command { get; set; }
}

[Serializable]
public sealed class ApiKeyCommandConfiguration
{
public string Key { get; set; }
}

[Serializable]
public sealed class PushCommandConfiguration
{
public string Key { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.Xml.Serialization;

/// <summary>
/// XML packages.config file package element
/// </summary>
[Serializable]
//[XmlType("package")]
public sealed class PackagesConfigFilePackageSetting
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }

[XmlAttribute(AttributeName = "source")]
public string Source { get; set; }

[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }

[XmlAttribute(AttributeName = "installArguments")]
public string InstallArguments { get; set; }

[XmlAttribute(AttributeName = "packageParameters")]
public string PackageParameters { get; set; }

[XmlAttribute(AttributeName = "forceX86")]
public bool ForceX86 { get; set; }

[XmlAttribute(AttributeName = "allowMultipleVersions")]
public bool AllowMultipleVersions { get; set; }

[XmlAttribute(AttributeName = "ignoreDependencies")]
public bool IgnoreDependencies { get; set; }

[XmlAttribute(AttributeName = "disabled")]
public bool Disabled { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.Generic;
using System.Xml.Serialization;

/// <summary>
/// XML packages.config configuration file
/// </summary>
[Serializable]
[XmlRoot("packages")]
public class PackagesConfigFileSettings
{
[XmlElement("package")]
public HashSet<PackagesConfigFilePackageSetting> Packages { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace chocolatey.infrastructure.app.services
using configuration;
using domain;
using filesystem;
using infrastructure.services;
using logging;
using platforms;
using results;
Expand All @@ -36,8 +37,9 @@ public class ChocolateyPackageService : IChocolateyPackageService
private readonly IRegistryService _registryService;
private readonly IChocolateyPackageInformationService _packageInfoService;
private readonly IAutomaticUninstallerService _autoUninstallerService;
private readonly IXmlService _xmlService;

public ChocolateyPackageService(INugetService nugetService, IPowershellService powershellService, IShimGenerationService shimgenService, IFileSystem fileSystem, IRegistryService registryService, IChocolateyPackageInformationService packageInfoService, IAutomaticUninstallerService autoUninstallerService)
public ChocolateyPackageService(INugetService nugetService, IPowershellService powershellService, IShimGenerationService shimgenService, IFileSystem fileSystem, IRegistryService registryService, IChocolateyPackageInformationService packageInfoService, IAutomaticUninstallerService autoUninstallerService, IXmlService xmlService)
{
_nugetService = nugetService;
_powershellService = powershellService;
Expand All @@ -46,6 +48,7 @@ public ChocolateyPackageService(INugetService nugetService, IPowershellService p
_registryService = registryService;
_packageInfoService = packageInfoService;
_autoUninstallerService = autoUninstallerService;
_xmlService = xmlService;
}

public void list_noop(ChocolateyConfiguration config)
Expand Down Expand Up @@ -140,7 +143,11 @@ public void push_run(ChocolateyConfiguration config)

public void install_noop(ChocolateyConfiguration config)
{
_nugetService.install_noop(config, (pkg) => _powershellService.install_noop(pkg));
// each package can specify its own configuration values
foreach (var packageConfig in set_config_from_package_names_and_packages_config(config, new ConcurrentDictionary<string, PackageResult>()).or_empty_list_if_null())
{
_nugetService.install_noop(packageConfig, (pkg) => _powershellService.install_noop(pkg));
}
}

public void handle_package_result(PackageResult packageResult, ChocolateyConfiguration config, CommandNameType commandName)
Expand Down Expand Up @@ -210,10 +217,20 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
this.Log().Info(ChocolateyLoggers.Important, @"{0}".format_with(config.PackageNames));
this.Log().Info(@"By installing you accept licenses for the packages.");

var packageInstalls = _nugetService.install_run(
config,
(packageResult) => handle_package_result(packageResult, config, CommandNameType.install)
);

var packageInstalls = new ConcurrentDictionary<string, PackageResult>();

foreach (var packageConfig in set_config_from_package_names_and_packages_config(config, packageInstalls).or_empty_list_if_null())
{
var results = _nugetService.install_run(
packageConfig,
(packageResult) => handle_package_result(packageResult, packageConfig, CommandNameType.install)
);
foreach (var result in results)
{
packageInstalls.GetOrAdd(result.Key, result.Value);
}
}

var installFailures = packageInstalls.Count(p => !p.Value.Success);
this.Log().Warn(() => @"{0}{1} installed {2}/{3} packages. {4} packages failed.{0}See the log for details.".format_with(
Expand All @@ -240,6 +257,58 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
return packageInstalls;
}

private IEnumerable<ChocolateyConfiguration> set_config_from_package_names_and_packages_config(ChocolateyConfiguration config, ConcurrentDictionary<string, PackageResult> packageInstalls)
{
// if there are any .config files, split those off of the config. Then return the config without those package names.
foreach (var packageConfigFile in config.PackageNames.Split(new[] {ApplicationParameters.PackageNamesSeparator}, StringSplitOptions.RemoveEmptyEntries).or_empty_list_if_null().Where(p => p.Contains(".config")).ToList())
{
config.PackageNames = config.PackageNames.Replace(packageConfigFile, string.Empty);

foreach (var packageConfig in get_packages_from_config(packageConfigFile, config, packageInstalls).or_empty_list_if_null())
{
yield return packageConfig;
}
}

yield return config;
}

private IEnumerable<ChocolateyConfiguration> get_packages_from_config(string packageConfigFile, ChocolateyConfiguration config, ConcurrentDictionary<string, PackageResult> packageInstalls)
{
IList<ChocolateyConfiguration> packageConfigs = new List<ChocolateyConfiguration>();

if (!_fileSystem.file_exists(_fileSystem.get_full_path(packageConfigFile)))
{
var logMessage = "Could not find '{0}' in the location specified.".format_with(packageConfigFile);
this.Log().Error(ChocolateyLoggers.Important, logMessage);
var results = packageInstalls.GetOrAdd(packageConfigFile, new PackageResult(packageConfigFile, null, null));
results.Messages.Add(new ResultMessage(ResultType.Error, logMessage));

return packageConfigs;
}

var settings = _xmlService.deserialize<PackagesConfigFileSettings>(_fileSystem.get_full_path(packageConfigFile));
foreach (var pkgSettings in settings.Packages.or_empty_list_if_null())
{
if (!pkgSettings.Disabled)
{
var packageConfig = config.deep_copy();
packageConfig.PackageNames = pkgSettings.Id;
packageConfig.Sources = string.IsNullOrWhiteSpace(pkgSettings.Source) ? packageConfig.Sources : pkgSettings.Source;
packageConfig.Version = pkgSettings.Version;
packageConfig.InstallArguments = string.IsNullOrWhiteSpace(pkgSettings.InstallArguments) ? packageConfig.InstallArguments : pkgSettings.InstallArguments;
packageConfig.PackageParameters = string.IsNullOrWhiteSpace(pkgSettings.PackageParameters) ? packageConfig.PackageParameters : pkgSettings.PackageParameters;
if (pkgSettings.ForceX86) packageConfig.ForceX86 = true;
if (pkgSettings.AllowMultipleVersions) packageConfig.AllowMultipleVersions = true;
if (pkgSettings.IgnoreDependencies) packageConfig.IgnoreDependencies = true;

packageConfigs.Add(packageConfig);
}
}

return packageConfigs;
}

public void upgrade_noop(ChocolateyConfiguration config)
{
_nugetService.upgrade_noop(config, (pkg) => _powershellService.install_noop(pkg));
Expand Down
Loading

0 comments on commit 4c3059f

Please sign in to comment.