Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Powershell script to generate nuget packages with dependencies
param(
[string] $Major = "8",
[string] $Minor = "1",
[string] $Update = "0",
[string] $Date = "151003",
[string] $nugetPath = ".\tools\nuget.exe",
[string] $downloadsPath = ".\downloads",
[string] $nuspecTemplate = ".\tools\package.nuspec.xml",
[string] $apiKey,
[string] $nugetFeed = "http://use-your.own/nuget/Sitecore-Libs"
)
function Publish-NugetPackageFromWebsiteBin(
[string] $major,
[string] $minor,
[string] $update,
[string] $date,
[string] $nugetPath,
[string] $downloadsPath,
[string] $nuspecTemplate,
[string] $apiKey,
[string] $nugetFeed)
{
$bin = Resolve-Path ("$downloadsPath\Sitecore $major.$minor rev. $date\website\bin")
$nugetPath = Resolve-Path $nugetPath
$nuspecTemplate = Resolve-Path $nuspecTemplate
$updateText = "Initial Release"
if($update -gt 0)
{
$updateText = "$major.$minor Update-$update"
}
$description = "Sitecore Experience Platform $major.$minor rev. $date ($updateText)"
$version = "$major.$minor.$update.$date"
# clean-up
$output = New-Item -ItemType Directory -Path ($env:temp + "\" + [System.Guid]::NewGuid())
#call functions
Get-ChildItem $bin |
Where-Object {$_.Name -match "^Sitecore.*\.dll$"} |
Foreach { Publish-NugetPackageFromDLL $bin $_.Name $output.FullName $description $version $nugetPath $nugetFeed $apiKey $nuspecTemplate }
Remove-Item $output -Force -Recurse
}
function Publish-NugetPackageFromDLL(
$binPath, $assemblyName, $output, $description, $productVersion, $nugetPath, $nugetFeed, $apiKey, $nuspecTemplate)
{
$tempPath = $output + "\tmp"
# dlls
$file = Get-Item "$binPath\$assemblyName"
$id = [System.IO.Path]::GetFileNameWithoutExtension($file)
$version = $productVersion
$framework = Get-FrameworkVersion($file)
# create package directory
$tmpFolder =New-Item $tempPath -type directory -Force
$packageFolder = New-Item "$tmpFolder\$id" -type directory -Force
$libsFolder = New-Item "$packageFolder\lib" -type directory -Force
$frameworkFolder = New-Item "$libsFolder\$framework" -type directory -Force
# packages
$nuspeck = New-Nuspec $file $binPath $nuspecTemplate $id $productVersion $description $framework $packageFolder
# write files
Copy-Item $file $frameworkFolder -Force
$assemblyXml = "$binPath\$id.xml"
if(Test-Path $assemblyXml)
{
Copy-Item $assemblyXml $frameworkFolder -Force
}
$assemblyPub = "$binPath\$id.pub"
if(Test-Path $assemblyPub)
{
Copy-Item $assemblyPub $frameworkFolder -Force
}
$output = New-Item "$basePath\packages" -type directory -Force
& $nugetPath pack $nuspeck.FullName -OutputDirectory $output
if (-Not([string]::IsNullOrEmpty($apiKey)) -AND -Not([string]::IsNullOrEmpty($nugetFeed))){
& $nugetPath push "$output\$id.$productVersion.nupkg" -ApiKey $apiKey -Source $nugetFeed
}
}
function New-Nuspec(
$assemblyFile,
[string] $pathToDependencies,
[string] $nuspecTemplate,
[string] $packageId,
[string] $packageVerison,
[string] $packageDescription,
[string] $packageFramework,
[string] $output
)
{
$xml = [xml](Get-Content $nuspecTemplate)
# Set ID
$node = $xml.package.metadata
$node.ID = $packageId
$node.version = $packageVerison
$node.description = "Autogenerated NuGet package form $packageDescription"
$group = $xml.CreateElement('group');
# get framework version
$group.SetAttribute('targetFramework', $packageFramework )
$assemblyDependencies = Get-AssemblyDependencies($assemblyFile)
$dependencies = $xml.CreateElement('dependencies');
foreach ($assembly in $assemblyDependencies){
$depVersion = Find-AssemblyAndReturnVersion "$pathToDependencies\$($assembly.Name).dll" $packageVerison
if ($depVersion -ne "0.0.0")
{
$dep = $xml.CreateElement('dependency')
$dep.SetAttribute('id', $assembly.Name )
$dep.SetAttribute('version', '[' + $(Find-AssemblyAndReturnVersion "$binPath\$($assembly.Name).dll" $packageVerison) + ']')
$group.AppendChild($dep)
}
}
$dependencies.AppendChild($group)
$node.AppendChild($dependencies)
# write nuspeck
$xml.Save("$output\$packageId.nuspec")
return Get-Item "$output\$packageId.nuspec"
}
function Get-FrameworkVersion([string] $path)
{
$imageRuntimeVersion = [System.Reflection.Assembly]::LoadFrom($path).ImageRuntimeVersion.ToString()
if($imageRuntimeVersion.Contains("v4.5"))
{
return "net45"
}
return "net40"
}
function Get-AssemblyDependencies([string] $path)
{
return [System.Reflection.Assembly]::ReflectionOnlyLoadFrom($path).GetReferencedAssemblies() |
Where-Object {$_.Name -match "Sitecore\.[A-Za-z\.]*"} |
Select Name |
Where { -not([string]::IsNullOrEmpty($_)) } |
Sort-Object -Property Name |
Get-Unique
}
function Find-AssemblyAndReturnVersion([string] $pathToAssembly, [string] $productVesion)
{
if (Test-Path $pathToAssembly)
{
return $productVesion
}
return "0.0.0"
}
Publish-NugetPackageFromWebsiteBin $Major $Minor $Update $Date $nugetPath $downloadsPath $nuspecTemplate $apiKey $nugetFeed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.