2
0

build-nuget.ps1 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright The OpenTelemetry Authors
  2. # SPDX-License-Identifier: Apache-2.0
  3. # Function returns 4-part 'classic' version string from SemVer 2.0 string
  4. function GenVer4Part([String] $Version)
  5. {
  6. if ($Version[0] -eq 'v')
  7. {
  8. # If tag contains 'v' then strip it
  9. $Version = $Version.substring(1)
  10. }
  11. # Converts from 1.2.3-build4 to 1.2.3.4
  12. $Version = $Version -replace "-build", "."
  13. $Version = $Version -replace "-", "."
  14. $parts = $Version.Split('.');
  15. # Add missing tuples
  16. $i = $parts.Count
  17. while($i -lt 4)
  18. {
  19. $parts += "0"
  20. $i++
  21. }
  22. ,$parts
  23. }
  24. function New-TemporaryDirectory
  25. {
  26. $parent = [System.IO.Path]::GetTempPath()
  27. [string] $name = [System.Guid]::NewGuid()
  28. New-Item -ItemType Directory -Path (Join-Path $parent $name)
  29. }
  30. function GetGitWorkTree()
  31. {
  32. # TODO: presently we assume that GIT_WORK_TREE is 1-level up.
  33. # Uncomment the following line if this is not the case:
  34. # $result = (git rev-parse --show-toplevel) -join ''
  35. $result = ( Get-Item -Path .. ).Fullname
  36. $result = $result -replace '[\\/]', '\'
  37. return $result
  38. }
  39. function CopyAll([String] $src, [String] $dest)
  40. {
  41. $what = @("/COPYALL","/B","/SEC","/MIR")
  42. $options = @("/R:0","/W:0","/NFL","/NDL","/NJH","/NJS","/NC","/NS")
  43. $cmdArgs = @("$src","$dest",$what,$options)
  44. robocopy @cmdArgs
  45. }
  46. $tempDir = New-TemporaryDirectory
  47. $gitWorktree = GetGitWorkTree
  48. $items = Get-ChildItem .\nuget -include *.nuspec -Recurse
  49. foreach ($item in $items)
  50. {
  51. $nugetName = $item.Basename
  52. $v = GenVer4Part $env:PackageVersion
  53. $version = [string]::Join(".", $v, 0, 3)
  54. Write-Output "Creating nuget $nugetName-$version ..."
  55. # Copy all files from Git
  56. CopyAll $gitWorkTree $tempDir.Fullname
  57. # Append extra nuget package file
  58. Copy-Item -Path ".\nuget\$nugetName.nuspec" -Destination $tempDir.Fullname
  59. Copy-Item -Path ".\nuget\opentelemetry-icon-color.png" -Destination $tempDir.Fullname
  60. # Change to temporary directory
  61. Push-Location -Path $tempDir.Fullname
  62. # Pack the nuget package
  63. nuget pack $nugetName.nuspec -Version $version -NoDefaultExcludes
  64. Pop-Location
  65. $nupkgFileName = "$tempDir\$nugetName.$version.nupkg"
  66. Get-Item $nupkgFileName | Copy-Item -Destination "..\packages"
  67. }