Skip to content

Commit

Permalink
(GH-207) Set up variables for powershell scripts
Browse files Browse the repository at this point in the history
Prior to calling the script, we need to set up variables so they are
available. To do that we redirect the call to a known powershell
script that will call the install/uninstall script. In this known
script we set up all of the variables appropriately so they are already
set when we call the actual script from the known script.
  • Loading branch information
ferventcoder committed Mar 29, 2015
1 parent 8f5ee90 commit aa89825
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/chocolatey.resources/chocolatey.resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<EmbeddedResource Include="helpers\functions\Generate-BinFile.ps1" />
<EmbeddedResource Include="helpers\functions\Remove-BinFile.ps1" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="helpers\chocolateyScriptRunner.ps1" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand All @@ -127,4 +130,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
28 changes: 28 additions & 0 deletions src/chocolatey.resources/helpers/chocolateyScriptRunner.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
param(
[alias("ia","installArgs")][string] $installArguments = '',
[alias("o","override","overrideArguments","notSilent")]
[switch] $overrideArgs = $false,
[alias("x86")][switch] $forceX86 = $false,
[alias("params")][alias("parameters")][alias("pkgParams")][string]$packageParameters = '',
[string]$packageScript
)

Write-Debug "Running 'ChocolateyScriptRunner' for $($env:packageName) v$($env:packageVersion) with packageScript `'$packageScript`', packageFolder:`'$($env:packageFolder)`', installArguments: `'$installArguments`', packageParameters: `'$packageParameters`',"

## Set the culture to invariant
$currentThread = [System.Threading.Thread]::CurrentThread;
$culture = [System.Globalization.CultureInfo]::InvariantCulture;
$currentThread.CurrentCulture = $culture;
$currentThread.CurrentUICulture = $culture;

$RunNote = "DarkCyan"
$Warning = "Magenta"
$ErrorColor = "Red"
$Note = "Green"

$version = $env:packageVersion
$packageName = $env:packageName
$packageFolder = $env:packageFolder

Write-Debug "Running `'$packageScript`'";
& "$packageScript"
24 changes: 19 additions & 5 deletions src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,36 @@ public bool uninstall(ChocolateyConfiguration configuration, PackageResult packa
return run_action(configuration, packageResult, CommandNameType.uninstall);
}

public string wrap_script_with_module(string script)
public string wrap_script_with_module(string script, ChocolateyConfiguration config)
{
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
//$ErrorActionPreference = 'Stop';
return "[System.Threading.Thread]::CurrentThread.CurrentCulture = '';[System.Threading.Thread]::CurrentThread.CurrentUICulture = ''; & import-module -name '{0}';{2} & '{1}'"
return "[System.Threading.Thread]::CurrentThread.CurrentCulture = '';[System.Threading.Thread]::CurrentThread.CurrentUICulture = ''; & import-module -name '{0}';{2} & '{1}' {3}"
.format_with(
installerModule,
script,
string.IsNullOrWhiteSpace(_customImports) ? string.Empty : "& {0}".format_with(_customImports.EndsWith(";") ? _customImports : _customImports + ";")
scriptRunner,
string.IsNullOrWhiteSpace(_customImports) ? string.Empty : "& {0}".format_with(_customImports.EndsWith(";") ? _customImports : _customImports + ";"),
get_script_arguments(script,config)
);
}

private string get_script_arguments(string script, ChocolateyConfiguration config)
{
return "-packageScript '{0}' -installArguments '{1}' -forceX86 {2} -packageParameters '{3}' -overrideArgs {4}".format_with(
script,
config.InstallArguments,
config.ForceX86 ? "$true" : "$false",
config.PackageParameters,
config.OverrideArguments ? "$true" : "$false"
);
}

public bool run_action(ChocolateyConfiguration configuration, PackageResult packageResult, CommandNameType command)
{
var installerRun = false;
Expand Down Expand Up @@ -225,7 +239,7 @@ Skip is an advanced option and most likely will never be wanted.
{
installerRun = true;
var exitCode = PowershellExecutor.execute(
wrap_script_with_module(chocoPowerShellScript),
wrap_script_with_module(chocoPowerShellScript, configuration),
_fileSystem,
configuration.CommandExecutionTimeoutSeconds,
(s, e) =>
Expand Down

1 comment on commit aa89825

@ferventcoder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gep13 this

Please sign in to comment.