Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaintSkeeta/5c15a47538d9ac00e0af7d604021d2ea to your computer and use it in GitHub Desktop.
Save SaintSkeeta/5c15a47538d9ac00e0af7d604021d2ea to your computer and use it in GitHub Desktop.
Powershell script to generate nuget packages with dependencies
param(
[string] $Major = "9",
[string] $Minor = "0",
[string] $Update = "0",
[string] $Date = "170622",
[string] $nugetPath = ".\tools\nuget.exe",
[string] $downloadsPath = "C:\\inetpub\\wwwroot\\sitecore9",
[string] $outputPath = "C:\\LocalNuGet\\Generated",
[string] $nuspecTemplate = ".\tools\package.nuspec.xml"
)
function Publish-NugetPackageFromWebsiteBin(
[string] $major,
[string] $minor,
[string] $update,
[string] $date,
[string] $nugetPath,
[string] $downloadsPath,
[string] $outputPath,
[string] $nuspecTemplate)
{
$bin = Resolve-Path ("$downloadsPath\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) Preview Release"
$version = "$major.$minor.$update.$date"
# clean-up
$output = New-Item -ItemType Directory -Path ($outputPath + "\" + [System.Guid]::NewGuid())
echo 'Output is ' + $outputPath
#call functions
Get-ChildItem $bin |
Where-Object {$_.Name -match "^Sitecore.*\.dll$"} |
Foreach { Publish-NugetPackageFromDLL $bin $_.Name $output.FullName $description $version $nugetPath $nuspecTemplate }
# Remove-Item $output -Force -Recurse
}
function Publish-NugetPackageFromDLL(
$binPath, $assemblyName, $output, $description, $productVersion, $nugetPath, $nuspecTemplate)
{
$tempPath = $output + "\tmp"
# dlls
$file = Get-Item "$binPath\$assemblyName"
$id = [System.IO.Path]::GetFileNameWithoutExtension($file) + ".NoReferences"
$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
}
# echo "FullName is " $tempPath\$Id\$Id.nuspec
$output = New-Item "$output\packages" -type directory -Force
& $nugetPath pack $tempPath\$Id\$Id.nuspec -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] $binPath,
[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.title = $packageId
$node.version = $packageVerison
$node.description = "Autogenerated NuGet package form $packageDescription"
$node.summary = "Summary : " + $packageId + "."
$files = $xml.CreateElement('files');
$file = $xml.CreateElement('file');
echo "Bin Path " $binPath
echo "AssemblyPath " $assemblyFile
$assemblyPath = "lib\" + $packageFramework + "\" + $assemblyFile.Name;
$file.SetAttribute('src', $assemblyPath );
$file.SetAttribute('target', $assemblyPath );
$files.AppendChild($file);
$xml.package.AppendChild($files);
# 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)
{
Write-Host "DLL is " $path
$bytes = [System.IO.File]::ReadAllBytes($path)
$assembly = [System.Reflection.Assembly]::Load($bytes)
$methodSig = $assembly.GetCustomAttributes([System.Runtime.Versioning.TargetFrameworkAttribute], $false)
if ($methodSig -eq $null -Or $methodSig.FrameworkName -eq $null)
{
Write-Host "Framework Version is 2.0"
return "net20"
}
Write-Host "Framework Version is " $methodSig.FrameworkName
if($methodSig.FrameworkName.Contains("v4.6.2"))
{
return "NET462"
}
elseif($methodSig.FrameworkName.Contains("v4.5.2"))
{
return "NET452"
}
elseif($methodSig.FrameworkName.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 $outputPath $nuspecTemplate
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/01/nuspec.xsd">
<metadata minClientVersion="2.8">
<id>Sitecore.Kernel.NoReferences</id>
<version>9.0.170662</version>
<title>Sitecore.Kernel.NoReferences</title>
<authors>Sitecore Corporation A/S</authors>
<owners>Sitecore Corporation A/S</owners>
<licenseUrl>https://doc.sitecore.net/~/media/C23E989268EC4FA588108F839675A5B6.pdf</licenseUrl>
<projectUrl>http://doc.sitecore.net/</projectUrl>
<iconUrl>https://mygetwwwsitecore.blob.core.windows.net/feedicons/sc-packages.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<developmentDependency>true</developmentDependency>
<description>Description: .</description>
<summary>Summary : .</summary>
<copyright>© 2017 Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.</copyright>
<language>en-US</language>
</metadata>
</package>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment