Skip to content

Instantly share code, notes, and snippets.

@sergejusb
Forked from crmckenzie/gist:4004375
Last active December 14, 2015 08:09
Show Gist options
  • Save sergejusb/5055519 to your computer and use it in GitHub Desktop.
Save sergejusb/5055519 to your computer and use it in GitHub Desktop.
PowerShell script used for IL Merging. Useful with CI servers like TeamCity
<#
.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 framework
Defaults to .NET 4 (v4)
.PARAMETER mvc3
If the project is an Mvc3 project, the MVC3 assemblies need to be added to the ilmerge list. The script assumes that the MVC3 assemblies are installed in the default location.
.PARAMETER internalize
Adds the /internalize flag to the merged assembly to prevent namespace conflicts.
.EXAMPLE
ilmerge.ps1 -targetProject "MyMVC3Project" -mvc3
#>
param(
[parameter(Mandatory=$true)] $targetProject,
$outputAssembly = "$targetProject.dll",
$buildConfiguration = "",
$framework = "v4",
[switch] $mvc3,
[switch] $internalize
)
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
function Get-Mvc3Dependencies()
{
$system_web_mvc = """c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll"""
$system_web_webpages = """c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll"""
$system_web_razor = """c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.Razor.dll"""
$system_web_webpages_razor = """c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.Razor.dll"""
$mvc3Assemblies = "$system_web_mvc $system_web_webpages $system_web_razor $system_web_webpages_razor"
return $mvc3Assemblies
}
function Get-InputAssemblyNames($buildDirectory)
{
$assemblyNames = Get-ChildItem -Path $buildDirectory -Filter *.dll | 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
$targetPlatform = "$framework,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\$framework"
$ilMergeAssembly = "$solutionDirectoryFullName\.ilmerge\IlMerge\IlMerge.exe"
$publishDirectory = "$solutionDirectoryFullName\Publish"
$outputAssemblyFullPath = "$publishDirectory\$outputAssembly"
$buildDirectory = Get-BuildDirectory $solutionDirectoryFullName
"Script Directory : $scriptPath"
"Solution Directory: $solutionDirectoryFullName"
"Build Directory : $buildDirectory"
"Publish Directory : $publishDirectory"
"Target Platform : $targetPlatform"
$outArgument = "/out:$publishDirectory/$outputAssembly"
$inArgument = Get-InputAssemblyNames $buildDirectory
# MVC3 assemblies are not a part of the .NET Framework, but neither are they a nuget package.
# They have to be referenced directly.
if ($mvc3 -eq $true)
{
$mvcAssemblies = Get-Mvc3Dependencies
$inArgument = "$inArgument $mvcAssemblies"
}
$cmd = "$ilMergeAssembly /t:library /targetPlatform:""$targetPlatform"" $outArgument $inArgument"
if ($internalize)
{
$cmd = $cmd + " /internalize"
}
"Installing ilmerge"
$nugetInstall = "$solutionDirectoryFullName\.nuget\nuget.exe install IlMerge -outputDirectory .ilmerge -ExcludeVersion"
"$nugetInstall"
Invoke-Expression $nugetInstall
"Ensuring that publication directory exists"
if ([System.IO.Directory]::Exists($publishDirectory) -eq $false)
{
[System.IO.Directory]::CreateDirectory($publishDirectory)
}
"Running Command: $cmd"
$result = Invoke-Expression $cmd
"Getting assembly info for $outputAssemblyFullPath"
$outputAssemblyInfo = New-Object System.IO.FileInfo $outputAssemblyFullPath
if ($outputAssemblyInfo.Length -eq 0)
{
$outputAssemblyInfo.Delete
}
$outputAssemblyInfo
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