Skip to content

Commit

Permalink
(chocolateyGH-268) Add a before_modify action to the PowershellService
Browse files Browse the repository at this point in the history
This action will trigger the execution of chocolateyBeforeModify.ps1 if that script is present in the package.
  • Loading branch information
Richard J Foster authored and Richard J Foster committed Mar 9, 2016
1 parent 7a34e23 commit 4e2f9fc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/chocolatey/infrastructure.app/services/IPowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,19 @@ public interface IPowershellService
/// <param name="packageResult">The package result.</param>
/// <returns>true if the chocolateyUninstall.ps1 was found, even if it has failures</returns>
bool uninstall(ChocolateyConfiguration configuration, PackageResult packageResult);

/// <summary>
/// Noops the specified package before modify operation.
/// </summary>
/// <param name="packageResult">The package result.</param>
void before_modify_noop(PackageResult packageResult);

/// <summary>
/// Runs any before modification script on the specified package.
/// </summary>
/// <param name="configuration">The configuration</param>
/// <param name="packageResult">The package result.</param>
/// <returns>true if the chocolateyBeforeModify.ps1 was found, even if it has failures</returns>
bool before_modify(ChocolateyConfiguration configuration, PackageResult packageResult);
}
}
45 changes: 32 additions & 13 deletions src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.app.services
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
Expand Down Expand Up @@ -59,22 +60,30 @@ public PowershellService(IFileSystem fileSystem, CustomString customImports)
_customImports = customImports;
}

public void noop_action(PackageResult packageResult, CommandNameType command)
private string get_script_for_action(PackageResult packageResult, CommandNameType command)
{
var file = "chocolateyInstall.ps1";
switch (command)
{
case CommandNameType.uninstall:
file = "chocolateyUninstall.ps1";
break;

case CommandNameType.upgrade:
file = "chocolateyBeforeModify.ps1";
break;
}

var packageDirectory = packageResult.InstallLocation;
var installScript = _fileSystem.get_files(packageDirectory, file, SearchOption.AllDirectories).Where(p => !p.to_lower().contains("\\templates\\"));
if (installScript.Count() != 0)
{
var chocoInstall = installScript.FirstOrDefault();
var installScript = _fileSystem.get_files(packageDirectory, file, SearchOption.AllDirectories).FirstOrDefault();
return installScript;
}

public void noop_action(PackageResult packageResult, CommandNameType command)
{
var chocoInstall = get_script_for_action(packageResult, command);
if (!string.IsNullOrEmpty(chocoInstall))
{
this.Log().Info("Would have run '{0}':".format_with(chocoInstall));
this.Log().Warn(_fileSystem.read_file(chocoInstall).escape_curly_braces());
}
Expand All @@ -100,15 +109,27 @@ public bool uninstall(ChocolateyConfiguration configuration, PackageResult packa
return run_action(configuration, packageResult, CommandNameType.uninstall);
}

public void before_modify_noop(PackageResult packageResult)
{
noop_action(packageResult, CommandNameType.upgrade);
}

public bool before_modify(ChocolateyConfiguration configuration, PackageResult packageResult)
{
return run_action(configuration, packageResult, CommandNameType.upgrade);
}

private string get_helpers_folder()
{
return _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "helpers");
}

public string wrap_script_with_module(string script, ChocolateyConfiguration config)
{
var installerModule = _fileSystem.combine_paths(get_helpers_folder(), "chocolateyInstaller.psm1");
var scriptRunner = _fileSystem.combine_paths(get_helpers_folder(), "chocolateyScriptRunner.ps1");
var installerModules = _fileSystem.get_files(ApplicationParameters.InstallLocation, "chocolateyInstaller.psm1", SearchOption.AllDirectories);
var installerModule = installerModules.FirstOrDefault();
var scriptRunners = _fileSystem.get_files(ApplicationParameters.InstallLocation, "chocolateyScriptRunner.ps1", SearchOption.AllDirectories);
var scriptRunner = scriptRunners.FirstOrDefault();
// removed setting all errors to terminating. Will cause too
// many issues in existing packages, including upgrading
// Chocolatey from older POSH client due to log errors
Expand Down Expand Up @@ -169,11 +190,9 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
return installerRun;
}

var powershellScript = _fileSystem.get_files(packageDirectory, file, SearchOption.AllDirectories).Where(p => !p.to_lower().contains("\\templates\\"));
if (powershellScript.Count() != 0)
var chocoPowerShellScript = get_script_for_action(packageResult, command);
if (!string.IsNullOrEmpty(chocoPowerShellScript))
{
var chocoPowerShellScript = powershellScript.FirstOrDefault();

var failure = false;

//todo: this is here for any possible compatibility issues. Should be reviewed and removed.
Expand Down Expand Up @@ -248,7 +267,7 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
if (selection.is_equal_to("no"))
{
Environment.ExitCode = 1;
packageResult.Messages.Add(new ResultMessage(ResultType.Error, "User cancelled powershell portion of installation for '{0}'.{1} Specify -n to skip automated script actions.".format_with(powershellScript.FirstOrDefault(), Environment.NewLine)));
packageResult.Messages.Add(new ResultMessage(ResultType.Error, "User cancelled powershell portion of installation for '{0}'.{1} Specify -n to skip automated script actions.".format_with(chocoPowerShellScript, Environment.NewLine)));
}
}

Expand Down Expand Up @@ -304,7 +323,7 @@ public bool run_action(ChocolateyConfiguration configuration, PackageResult pack
if (failure)
{
Environment.ExitCode = result.ExitCode;
packageResult.Messages.Add(new ResultMessage(ResultType.Error, "Error while running '{0}'.{1} See log for details.".format_with(powershellScript.FirstOrDefault(), Environment.NewLine)));
packageResult.Messages.Add(new ResultMessage(ResultType.Error, "Error while running '{0}'.{1} See log for details.".format_with(chocoPowerShellScript, Environment.NewLine)));
}
packageResult.Messages.Add(new ResultMessage(ResultType.Note, "Ran '{0}'".format_with(chocoPowerShellScript)));
}
Expand Down

0 comments on commit 4e2f9fc

Please sign in to comment.