Update UpdateDependencies script

This commit is contained in:
Ryan Brandenburg 2018-03-23 17:00:06 -07:00
parent f51d2ec1ae
commit a5b8be2ad3
7 changed files with 201 additions and 182 deletions

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ node_modules
global.json
msbuild.ProjectImports.zip
.env
scripts/tmp/

View File

@ -13,6 +13,7 @@ param(
$ErrorActionPreference = 'Stop'
Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
Set-StrictMode -Version 1
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$depsPath = Resolve-Path "$PSScriptRoot/../build/dependencies.props"
[xml] $dependencies = LoadXml $depsPath
@ -20,13 +21,12 @@ $depsPath = Resolve-Path "$PSScriptRoot/../build/dependencies.props"
if ($BuildXml -like 'http*') {
$url = $BuildXml
New-Item -Type Directory "$PSScriptRoot/../obj/" -ErrorAction Ignore
$localXml = "$PSScriptRoot/../obj/build.xml"
$BuildXml = "$PSScriptRoot/../obj/build.xml"
Write-Verbose "Downloading from $url to $BuildXml"
Invoke-WebRequest -OutFile $localXml $url
Invoke-WebRequest -OutFile $BuildXml $url
}
[xml] $remoteDeps = LoadXml $localXml
$count = 0
[xml] $remoteDeps = LoadXml $BuildXml
$variables = @{}
@ -46,50 +46,5 @@ foreach ($package in $remoteDeps.SelectNodes('//Package')) {
}
}
$updatedVars = @{}
foreach ($varName in ($variables.Keys | sort)) {
$packageVersions = $variables[$varName]
if ($packageVersions.Length -gt 1) {
Write-Warning "Skipped $varName. Multiple version found. { $($packageVersions -join ', ') }."
continue
}
$packageVersion = $packageVersions | Select-Object -First 1
$depVarNode = $dependencies.SelectSingleNode("//PropertyGroup[`@Label=`"Package Versions: Auto`"]/$varName")
if ($depVarNode -and $depVarNode.InnerText -ne $packageVersion) {
$depVarNode.InnerText = $packageVersion
$count++
Write-Host -f DarkGray " Updating $varName to $packageVersion"
$updatedVars[$varName] = $packageVersion
}
}
if ($count -gt 0) {
Write-Host -f Cyan "Updating $count version variables in $depsPath"
SaveXml $dependencies $depsPath
# Ensure dotnet is installed
& "$PSScriptRoot\..\run.ps1" install-tools
$ProjectPath = "$PSScriptRoot\update-dependencies\update-dependencies.csproj"
$ConfigVars += "--BuildXml"
$ConfigVars += $BuildXml
$ConfigVars += "--UpdatedVersions"
$varString = ""
foreach ($updatedVar in $updatedVars.GetEnumerator()) {
$varString += "$($updatedVar.Name)=$($updatedVar.Value)+"
}
$ConfigVars += $varString
# Restore and run the app
Write-Host "Invoking App $ProjectPath..."
Invoke-Expression "dotnet run -p `"$ProjectPath`" $ConfigVars"
if ($LASTEXITCODE -ne 0) { throw "Build failed" }
}
else {
Write-Host -f Green "No changes found"
}
$updatedVars = UpdateVersions $variables $dependencies
CommitUpdatedVersions $updatedVars $dependencies $depsPath

View File

@ -0,0 +1,100 @@
[CmdletBinding()]
param()
$ErrorActionPreference = 'Stop'
Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
Set-StrictMode -Version 1
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$githubRaw = "https://raw.githubusercontent.com"
$versionsRepo = "dotnet/versions"
$versionsBranch = "master"
$coreSetupRepo = "dotnet/core-setup"
$coreFxRepo = "dotnet/corefx"
$coreSetupVersions = "$githubRaw/$versionsRepo/$versionsBranch/build-info/$coreSetupRepo/master/Latest_Packages.txt"
$tempDir = "$PSScriptRoot/../obj"
$localCoreSetupVersions = "$tempDir/coresetup.packages"
Write-Host "Downloading $coreSetupVersions to $localCoreSetupVersions"
Invoke-WebRequest -OutFile $localCoreSetupVersions -Uri $coreSetupVersions
$msNetCoreAppPackageVersion = $null
$msNetCoreAppPackageName = "Microsoft.NETCore.App"
$variables = @{}
foreach ($line in Get-Content $localCoreSetupVersions) {
if ($line.StartsWith("$msNetCoreAppPackageName ")) {
$msNetCoreAppPackageVersion = $line.Trim("$msNetCoreAppPackageName ")
}
$parts = $line.Split(' ')
$packageName = $parts[0]
$varName = "$packageName" + "PackageVersion"
$varName = $varName.Replace('.', '')
$packageVersion = $parts[1]
if ($variables[$varName]) {
if ($variables[$varName].Where( {$_ -eq $packageVersion}, 'First').Count -eq 0) {
$variables[$varName] += $packageVersion
}
}
else {
$variables[$varName] = @($packageVersion)
}
}
if (!$msNetCoreAppPackageVersion) {
Throw "$msNetCoreAppPackageName was not in $coreSetupVersions"
}
$coreAppDownloadLink = "https://dotnet.myget.org/F/dotnet-core/api/v2/package/$msNetCoreAppPackageName/$msNetCoreAppPackageVersion"
$netCoreAppNupkg = "$tempDir/microsoft.netcore.app.zip"
Invoke-WebRequest -OutFile $netCoreAppNupkg -Uri $coreAppDownloadLink
$expandedNetCoreApp = "$tempDir/microsoft.netcore.app/"
Expand-Archive -Path $netCoreAppNupkg -DestinationPath $expandedNetCoreApp -Force
$versionsTxt = "$expandedNetCoreApp/$msNetCoreAppPackageName.versions.txt"
$versionsCoreFxCommit = $null
foreach ($line in Get-Content $versionsTxt) {
if ($line.StartsWith("dotnet/versions/corefx")) {
$versionsCoreFxCommit = $line.Split(' ')[1]
break
}
}
if (!$versionsCoreFxCommit) {
Throw "no 'dotnet/versions/corefx' in versions.txt of Microsoft.NETCore.App"
}
$coreFxVersionsUrl = "$githubRaw/$versionsRepo/$versionsCoreFxCommit/build-info/$coreFxRepo/$versionsBranch/Latest_Packages.txt"
$localCoreFxVersions = "$tempDir/$corefx.packages"
Invoke-WebRequest -OutFile $localCoreFxVersions -Uri $coreFxVersionsUrl
foreach ($line in Get-Content $localCoreFxVersions) {
$parts = $line.Split(' ')
$packageName = $parts[0]
$varName = "$packageName" + "PackageVersion"
$varName = $varName.Replace('.', '')
$packageVersion = $parts[1]
if ($variables[$varName]) {
if ($variables[$varName].Where( {$_ -eq $packageVersion}, 'First').Count -eq 0) {
$variables[$varName] += $packageVersion
}
}
else {
$variables[$varName] = @($packageVersion)
}
}
$depsPath = Resolve-Path "$PSScriptRoot/../build/dependencies.props"
Write-Host "Loading deps from $depsPath"
[xml] $dependencies = LoadXml $depsPath
$updatedVars = UpdateVersions $variables $dependencies $depsPath
CommitUpdatedVersions $updatedVars $dependencies $depsPath

View File

@ -1,3 +1,5 @@
$ErrorActionPreference = 'Stop'
function Assert-Git {
if (!(Get-Command git -ErrorAction Ignore)) {
Write-Error 'git is required to execute this script'
@ -110,3 +112,95 @@ function PackageIdVarName([string]$packageId) {
$canonicalVarName += "PackageVersion"
return $canonicalVarName
}
function Ensure-Hub() {
$tmpDir = "$PSScriptRoot\tmp"
$zipDir = "$tmpDir\Hub\"
$hubLocation = "$zipDir\bin\hub.exe"
if (-Not (Test-Path $hubLocation) ) {
$source = "https://github.com/github/hub/releases/download/v2.3.0-pre9/hub-windows-amd64-2.3.0-pre9.zip"
$zipLocation = "$tmpDir\hub.zip"
Invoke-WebRequest -OutFile $zipLocation -Uri $source
Expand-Archive -Path $zipLocation -DestinationPath $zipDir -Force
if (-Not (Test-Path $hubLocation)) {
throw "Hub couldn't be downloaded"
}
}
return $hubLocation
}
function CommitUpdatedVersions([hashtable]$updatedVars, [xml]$dependencies, [string]$depsPath) {
$count = $updatedVars.Count
if ($count -gt 0) {
$hubLocation = Ensure-Hub
$destinationBranch = "rybrande/UpgradeDepsTest"
$currentBranch = & git rev-parse --abbrev-ref HEAD
$remote = "origin"
$baseBranch = "dev"
Invoke-Block { & git checkout -tb $destinationBranch "$remote/$baseBranch" }
try
{
& git add build\dependencies.props
$subject = "Updating external dependencies"
& git commit -m $subject
$body = "$subject`n`n"
$body += "New versions:`n"
foreach ($var in $updatedVars.GetEnumerator()) {
$body += " $($var.Name)`n"
}
Invoke-Block { & git push -f origin $destinationBranch }
Invoke-Block { & $hubLocation pull-request -b $baseBranch -h $destinationBranch -m $body }
}
finally{
& git checkout $currentBranch
}
}
}
function UpdateVersions([hashtable]$variables, [xml]$dependencies, [string]$depsPath) {
$updatedVars = @{}
foreach ($varName in ($variables.Keys | sort)) {
$packageVersions = $variables[$varName]
if ($packageVersions.Length -gt 1) {
Write-Warning "Skipped $varName. Multiple version found. { $($packageVersions -join ', ') }."
continue
}
$packageVersion = $packageVersions | Select-Object -First 1
$depVarNode = $dependencies.SelectSingleNode("//PropertyGroup[`@Label=`"Package Versions: Auto`"]/$varName")
if ($depVarNode -and $depVarNode.InnerText -ne $packageVersion) {
$depVarNode.InnerText = $packageVersion
Write-Host -f DarkGray " Updating $varName to $packageVersion"
$updatedVars[$varName] = $packageVersion
}
elseif ($depVarNode) {
Write-Host -f DarkBlue " Didn't update $varName to $packageVersion because it was $($depVarNode.InnerText)"
}
else {
# This isn't a dependency we use
}
}
if ($updatedVars.Count -gt 0) {
Write-Host -f Cyan "Updating $count version variables in $depsPath"
SaveXml $dependencies $depsPath
}
else {
Write-Host -f Green "No changes found"
}
return $updatedVars
}

View File

@ -1,26 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.Dotnet.Scripts
{
public class Config
{
public string UpdatedVersions { get; set; }
public string BuildXml { get; set; }
public string GithubUsername {get; set;}
public string GithubEmail {get; set;}
public string GithubToken {get; set;}
public string GithubUpstreamOwner {get; set;} = "aspnet";
public string GithubProject {get; set;} = "Universe";
public string GithubUpstreamBranch {get; set;} = "dev";
public string[] GitHubPullRequestNotifications { get; set; } = new string[] { };
public string[] UpdatedVersionsList
{
get
{
return UpdatedVersions.Split('+',System.StringSplitOptions.RemoveEmptyEntries);
}
}
}
}

View File

@ -1,84 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.DotNet.VersionTools.Automation;
using Microsoft.Extensions.Configuration;
namespace Microsoft.Dotnet.Scripts
{
public static class Program
{
private static readonly Config _config = new Config();
public static async Task Main(string[] args)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
ParseArgs(args);
await CreatePullRequest();
}
private static void ParseArgs(string[] args)
{
var builder = new ConfigurationBuilder().AddCommandLine(args);
var configRoot = builder.Build();
configRoot.Bind(_config);
}
private static async Task CreatePullRequest()
{
var gitHubAuth = new GitHubAuth(_config.GithubToken, _config.GithubUsername, _config.GithubEmail);
var origin = new GitHubProject(_config.GithubProject, _config.GithubUsername);
var upstreamBranch = new GitHubBranch(_config.GithubUpstreamBranch, new GitHubProject(_config.GithubProject, _config.GithubUpstreamOwner));
var commitMessage = $"Updating external dependencies to '{ await GetOrchestratedBuildId() }'";
var body = string.Empty;
if (_config.GitHubPullRequestNotifications.Any())
{
body += PullRequestCreator.NotificationString(_config.GitHubPullRequestNotifications);
}
body += $"New versions:{Environment.NewLine}";
foreach (var updatedVersion in _config.UpdatedVersionsList)
{
body += $" {updatedVersion}{Environment.NewLine}";
}
await new PullRequestCreator(gitHubAuth, origin, upstreamBranch)
.CreateOrUpdateAsync(commitMessage, commitMessage + $" ({upstreamBranch.Name})", body);
}
private static async Task<string> GetOrchestratedBuildId()
{
var xmlUrl = _config.BuildXml;
using (var client = new HttpClient())
{
var response = await client.GetAsync(xmlUrl);
using (var bodyStream = await response.Content.ReadAsStreamAsync())
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(bodyStream);
var orcBuilds = xmlDoc.GetElementsByTagName("OrchestratedBuild");
if (orcBuilds.Count < 1)
{
throw new ArgumentException($"{xmlUrl} didn't have an 'OrchestratedBuild' element.");
}
var orcBuild = orcBuilds[0];
return orcBuild.Attributes["BuildId"].Value;
}
}
}
}
}

View File

@ -1,21 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
<RestoreSources>
https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json;
https://api.nuget.org/v3/index.json;
</RestoreSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.DotNet.VersionTools" Version="1.0.27-prerelease-01723-01" />
<PackageReference Include="System.Diagnostics.TextWriterTraceListener" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.0.0" />
</ItemGroup>
</Project>