Last active
August 18, 2017 09:53
-
-
Save jackweldon/59c9bf61f650c1b01481392920ce5e42 to your computer and use it in GitHub Desktop.
Powershell script to ilmerge a project. Used in Jenkins to publish nuget package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Credit to https://gist.github.com/crmckenzie/4004375 | |
.SYNOPSIS | |
IlMerges an assembly with its dependencies. Depends on nuget being installed in the PATH. | |
.PARAMETER targetProject | |
The name of the project to be ilmerged | |
.PARAMETER outputAssembly | |
The name of the ilmerged assembly when it is created | |
.PARAMETER buildConfiguration | |
The build configuration used to create the assembly. Used to locate the assembly under the project. The usual format is Project/bin/Debug | |
.PARAMETER targetPlatform | |
Defaults to .NET 4 | |
.PARAMETER internalize | |
Adds the /internalize flag to the merged assembly to prevent namespace conflicts. | |
--Jenkins notes | |
Add powershell jenkins plugin https://wiki.jenkins.io/display/JENKINS/PowerShell+Plugin | |
command powershell -ExecutionPolicy Unrestricted -File ./scripts/ilmerge.ps1 | |
#> | |
param( | |
# [parameter(Mandatory=$true)] $targetProject, | |
$targetProject = "Ols.Core", | |
$outputAssembly = "$targetProject.dll", | |
$buildConfiguration = "..\bin\Release", ###Needs to be release for nguet | |
$targetPlatform = "v4,c:\windows\Microsoft.NET\Framework\v4.0.30319", | |
[switch] $internalize | |
) | |
function Get-ScriptDirectory | |
{ | |
$Invocation = (Get-Variable MyInvocation -Scope 1).Value | |
Split-Path $Invocation.MyCommand.Path | |
} | |
function Get-InputAssemblyNames($buildDirectory) | |
{ | |
$assemblyNames = Get-ChildItem -Path $buildDirectory | Where-Object {$_.extension -eq ".dll" -and $_.name -notlike "System*" -and $_.name -notlike "Microsoft*"} | ForEach-Object { """" + $_.FullName + """" } | |
write-host "Assemblies to merge: $assemblyNames" | |
$inArgument = [System.String]::Join(" ", $assemblyNames) | |
return $inArgument | |
} | |
function Get-BuildDirectory($solutionDirectoryFullName) | |
{ | |
$targetProjectDirectory = "$solutionDirectoryFullName\$targetProject" | |
$result = "$targetProjectDirectory\bin" | |
if ($buildConfiguration -ne "") | |
{ | |
$result = Join-Path $result $buildConfiguration | |
} | |
return $result | |
} | |
try | |
{ | |
$scriptPath= Get-ScriptDirectory | |
$scriptDirectory = new-object System.IO.DirectoryInfo $scriptPath | |
$solutionDirectory = $scriptDirectory.Parent | |
$solutionDirectoryFullName = $solutionDirectory.FullName | |
$ilMergeAssembly = "$solutionDirectoryFullName\scripts\.ilmerge\IlMerge\tools\IlMerge.exe" | |
#$publishDirectory = "$solutionDirectoryFullName\Ols.Core\bin\Release\" | |
$publishDirectory = "$solutionDirectoryFullName\Publish" | |
$outputAssemblyFullPath = "$publishDirectory\$outputAssembly" | |
$buildDirectory = Get-BuildDirectory $solutionDirectoryFullName | |
"Script Directory : $scriptPath" | |
"Solution Directory: $solutionDirectoryFullName" | |
"Build Directory : $buildDirectory" | |
"Publish Directory : $publishDirectory" | |
$outArgument = "/out:$publishDirectory/$outputAssembly" | |
$inArgument = Get-InputAssemblyNames $buildDirectory | |
$cmd = "$ilMergeAssembly /t:library /targetPlatform:""$targetPlatform"" $outArgument $inArgument" | |
if ($internalize) | |
{ | |
$cmd = $cmd + " /internalize" | |
} | |
"Installing ilmerge" | |
#Start-Process -FilePath 'nuget.exe'-ArgumentList 'install IlMerge -outputDirectory .ilmerge -ExcludeVersion' #UNCOMMENT IF YOU NEED TO INSTALL | |
"Ensuring that publication directory exists" | |
if ([System.IO.Directory]::Exists($publishDirectory) -eq $false) | |
{ | |
[System.IO.Directory]::CreateDirectory($publishDirectory) | |
} | |
$cmd = "$ilMergeAssembly /t:library /targetPlatform:""$targetPlatform"" $outArgument $inArgument" | |
$path = "$ilMergeAssembly" | |
$arg = "/t:library /targetPlatform:""$targetPlatform"" $outArgument $inArgument" | |
if ($internalize) | |
{ | |
$arg = $arg + " /internalize" | |
} | |
"Running Command: $cmd" | |
"Command Path: $path" | |
"Command Args: $arg" | |
$result = Start-Process $path $arg; | |
"Getting assembly info for $outputAssemblyFullPath" | |
$outputAssemblyInfo = New-Object System.IO.FileInfo $outputAssemblyFullPath | |
if ($outputAssemblyInfo.Length -eq 0) | |
{ | |
$outputAssemblyInfo.Delete | |
} | |
$outputAssemblyInfo | |
#Force copy to release folder | |
"Copying : $publishDirectory/$outputAssembly to $solutionDirectoryFullName\Ols.Core\bin\Release\" | |
Copy-Item "$publishDirectory/$outputAssembly" "$solutionDirectoryFullName\Ols.Core\bin\Release\" -force | |
if ($outputAssemblyInfo.Exists -eq $false) | |
{ | |
throw "Output assembly not created by ilmerge script." | |
Exit -1 | |
} | |
elseif ($outputAssemblyInfo.Length -eq 0) | |
{ | |
$outputAssemblyInfo.Delete(); | |
Exit -1; | |
} | |
else | |
{ | |
"Output assembly created successfully at $outputAssemblyFullPath." | |
Exit 0; | |
} | |
} | |
catch | |
{ | |
throw | |
Exit -1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment