Skip to content

Commit

Permalink
(chocolateyGH-14)(refactor) move consts to WebPiService
Browse files Browse the repository at this point in the history
The constants for WebPI Service are only used by WebPi Service, so
there is no reason to put them and all of the other special source's
arguments inside of ApplicationParameters. Move those over to the
actual source service.
  • Loading branch information
ferventcoder committed Jun 4, 2015
1 parent dd2afd2 commit 3661276
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 42 deletions.
20 changes: 0 additions & 20 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,6 @@ public static class Messages
public static readonly string NugetEventActionHeader = "Nuget called an event";
}

public static class SourceRunner
{
public static readonly string WebPiName = "Web Platform Installer";
public static readonly string WebPiPackage = "webpicmd";
public static readonly string WebPiExe = "webpicmd.exe";
}

public static class OutputParser
{
public static class WebPi
{
public const string PACKAGE_NAME_GROUP = "PkgName";
public static readonly Regex Installing =new Regex(@"Started installing:", RegexOptions.Compiled);
public static readonly Regex Installed = new Regex(@"Install completed \(Success\):", RegexOptions.Compiled);
public static readonly Regex AlreadyInstalled = new Regex(@"No products to be installed \(either not available or already installed\)", RegexOptions.Compiled);
//public static readonly Regex NotInstalled = new Regex(@"not installed", RegexOptions.Compiled);
public static readonly Regex PackageName = new Regex(@"'(?<{0}>[^']*)'".format_with(PACKAGE_NAME_GROUP), RegexOptions.Compiled);
}
}

private static T try_get_config<T>(Func<T> func, T defaultValue)
{
try
Expand Down
50 changes: 28 additions & 22 deletions src/chocolatey/infrastructure.app/services/WebPiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ public sealed class WebPiService : ISourceRunner
private readonly ICommandExecutor _commandExecutor;
private readonly INugetService _nugetService;
private const string PACKAGE_NAME_TOKEN = "{{packagename}}";
private readonly string _exePath = ApplicationParameters.SourceRunner.WebPiExe;
private readonly string _appName = ApplicationParameters.SourceRunner.WebPiName;
private const string EXE_PATH = "webpicmd.exe";
private const string APP_NAME = "Web Platform Installer";
public const string WEB_PI_PACKAGE = "webpicmd";
public const string PACKAGE_NAME_GROUP = "PkgName";
public static readonly Regex InstallingRegex = new Regex(@"Started installing:", RegexOptions.Compiled);
public static readonly Regex InstalledRegex = new Regex(@"Install completed \(Success\):", RegexOptions.Compiled);
public static readonly Regex AlreadyInstalledRegex = new Regex(@"No products to be installed \(either not available or already installed\)", RegexOptions.Compiled);
//public static readonly Regex NotInstalled = new Regex(@"not installed", RegexOptions.Compiled);
public static readonly Regex PackageNameRegex = new Regex(@"'(?<{0}>[^']*)'".format_with(PACKAGE_NAME_GROUP), RegexOptions.Compiled);

private readonly IDictionary<string, ExternalCommandArgument> _listArguments = new Dictionary<string, ExternalCommandArgument>(StringComparer.InvariantCultureIgnoreCase);
private readonly IDictionary<string, ExternalCommandArgument> _installArguments = new Dictionary<string, ExternalCommandArgument>(StringComparer.InvariantCultureIgnoreCase);

Expand Down Expand Up @@ -86,7 +94,7 @@ public void ensure_source_app_installed(ChocolateyConfiguration config, Action<P
{
var runnerConfig = new ChocolateyConfiguration
{
PackageNames = ApplicationParameters.SourceRunner.WebPiPackage,
PackageNames = WEB_PI_PACKAGE,
Sources = ApplicationParameters.PackagesLocation,
Debug = config.Debug,
Force = config.Force,
Expand All @@ -101,7 +109,7 @@ public void ensure_source_app_installed(ChocolateyConfiguration config, Action<P

var localPackages = _nugetService.list_run(runnerConfig, logResults: false);

if (!localPackages.ContainsKey(ApplicationParameters.SourceRunner.WebPiPackage))
if (!localPackages.ContainsKey(WEB_PI_PACKAGE))
{
runnerConfig.Sources = ApplicationParameters.ChocolateyCommunityFeedSource;

Expand All @@ -115,7 +123,7 @@ public void ensure_source_app_installed(ChocolateyConfiguration config, Action<P
public void list_noop(ChocolateyConfiguration config)
{
var args = ExternalCommandArgsBuilder.build_arguments(config, _listArguments);
this.Log().Info("Would have run '{0} {1}'".format_with(_exePath, args));
this.Log().Info("Would have run '{0} {1}'".format_with(EXE_PATH, args));
}

public ConcurrentDictionary<string, PackageResult> list_run(ChocolateyConfiguration config, bool logResults)
Expand All @@ -128,7 +136,7 @@ public ConcurrentDictionary<string, PackageResult> list_run(ChocolateyConfigurat
//var recordingValues = false;

Environment.ExitCode = _commandExecutor.execute(
_exePath,
EXE_PATH,
args,
config.CommandExecutionTimeoutSeconds,
workingDirectory: ApplicationParameters.ShimsLocation,
Expand All @@ -142,7 +150,7 @@ public ConcurrentDictionary<string, PackageResult> list_run(ChocolateyConfigurat
}
else
{
this.Log().Debug(() => "[{0}] {1}".format_with(_appName, logMessage));
this.Log().Debug(() => "[{0}] {1}".format_with(APP_NAME, logMessage));
}

//if (recordingValues)
Expand Down Expand Up @@ -172,7 +180,7 @@ public void install_noop(ChocolateyConfiguration config, Action<PackageResult> c
{
var args = ExternalCommandArgsBuilder.build_arguments(config, _installArguments);
args = args.Replace(PACKAGE_NAME_TOKEN, config.PackageNames.Replace(';', ','));
this.Log().Info("Would have run '{0} {1}'".format_with(_exePath, args));
this.Log().Info("Would have run '{0} {1}'".format_with(EXE_PATH, args));
}

public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfiguration configuration, Action<PackageResult> continueAction)
Expand All @@ -184,42 +192,40 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu
{
var argsForPackage = args.Replace(PACKAGE_NAME_TOKEN, packageToInstall);
var exitCode = _commandExecutor.execute(
_exePath,
EXE_PATH,
argsForPackage,
configuration.CommandExecutionTimeoutSeconds,
ApplicationParameters.ShimsLocation,
(s, e) =>
{
var logMessage = e.Data;
if (string.IsNullOrWhiteSpace(logMessage)) return;
this.Log().Info(() => " [{0}] {1}".format_with(_appName, logMessage));
this.Log().Info(() => " [{0}] {1}".format_with(APP_NAME, logMessage));

var packageName = get_value_from_output(logMessage, ApplicationParameters.OutputParser.WebPi.PackageName, ApplicationParameters.OutputParser.WebPi.PACKAGE_NAME_GROUP);
var packageName = get_value_from_output(logMessage, PackageNameRegex, PACKAGE_NAME_GROUP);
var results = packageInstalls.GetOrAdd(packageName, new PackageResult(packageName, null, null));
if (ApplicationParameters.OutputParser.WebPi.AlreadyInstalled.IsMatch(logMessage))
if (AlreadyInstalledRegex.IsMatch(logMessage))
{
results.Messages.Add(new ResultMessage(ResultType.Inconclusive, packageName));
this.Log().Warn(ChocolateyLoggers.Important, " [{0}] {1} already installed or doesn't exist. --force has no effect.".format_with(_appName, string.IsNullOrWhiteSpace(packageName) ? packageToInstall : packageName));
this.Log().Warn(ChocolateyLoggers.Important, " [{0}] {1} already installed or doesn't exist. --force has no effect.".format_with(APP_NAME, string.IsNullOrWhiteSpace(packageName) ? packageToInstall : packageName));
return;
}

if (ApplicationParameters.OutputParser.WebPi.Installing.IsMatch(logMessage))
if (InstallingRegex.IsMatch(logMessage))
{
this.Log().Info(ChocolateyLoggers.Important, "{0}".format_with(packageName));
return;
}

//if (string.IsNullOrWhiteSpace(packageName)) return;

if (ApplicationParameters.OutputParser.WebPi.Installed.IsMatch(logMessage))
if (InstalledRegex.IsMatch(logMessage))
{
this.Log().Info(ChocolateyLoggers.Important, " {0} has been installed successfully.".format_with(string.IsNullOrWhiteSpace(packageName) ? packageToInstall : packageName));
}
},
(s, e) =>
{
if (string.IsNullOrWhiteSpace(e.Data)) return;
this.Log().Error(() => "[{0}] {1}".format_with(_appName, e.Data));
this.Log().Error(() => "[{0}] {1}".format_with(APP_NAME, e.Data));
},
updateProcessPath: false
);
Expand All @@ -234,23 +240,23 @@ public ConcurrentDictionary<string, PackageResult> install_run(ChocolateyConfigu

public ConcurrentDictionary<string, PackageResult> upgrade_noop(ChocolateyConfiguration config, Action<PackageResult> continueAction)
{
this.Log().Warn(ChocolateyLoggers.Important, "{0} does not implement upgrade".format_with(_appName));
this.Log().Warn(ChocolateyLoggers.Important, "{0} does not implement upgrade".format_with(APP_NAME));
return new ConcurrentDictionary<string, PackageResult>();
}

public ConcurrentDictionary<string, PackageResult> upgrade_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
{
throw new NotImplementedException("{0} does not implement upgrade".format_with(_appName));
throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME));
}

public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult> continueAction)
{
this.Log().Warn(ChocolateyLoggers.Important, "{0} does not implement uninstall".format_with(_appName));
this.Log().Warn(ChocolateyLoggers.Important, "{0} does not implement uninstall".format_with(APP_NAME));
}

public ConcurrentDictionary<string, PackageResult> uninstall_run(ChocolateyConfiguration config, Action<PackageResult> continueAction)
{
throw new NotImplementedException("{0} does not implement uninstall".format_with(_appName));
throw new NotImplementedException("{0} does not implement uninstall".format_with(APP_NAME));
}

/// <summary>
Expand Down

0 comments on commit 3661276

Please sign in to comment.