diff --git a/.gitignore b/.gitignore index d5950872a2..1d308dafc6 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ node_modules global.json msbuild.ProjectImports.zip .env +scripts/tmp/ diff --git a/scripts/UpdateDependencies.ps1 b/scripts/UpdateDependencies.ps1 index 4447719d6f..1c09a9982b 100755 --- a/scripts/UpdateDependencies.ps1 +++ b/scripts/UpdateDependencies.ps1 @@ -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 diff --git a/scripts/UpdateDependenciesCoreFx.ps1 b/scripts/UpdateDependenciesCoreFx.ps1 new file mode 100644 index 0000000000..762583ddd8 --- /dev/null +++ b/scripts/UpdateDependenciesCoreFx.ps1 @@ -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 diff --git a/scripts/common.psm1 b/scripts/common.psm1 index 4f80800211..4272ccb7a5 100644 --- a/scripts/common.psm1 +++ b/scripts/common.psm1 @@ -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 +} diff --git a/scripts/update-dependencies/Config.cs b/scripts/update-dependencies/Config.cs deleted file mode 100644 index 38a59ac062..0000000000 --- a/scripts/update-dependencies/Config.cs +++ /dev/null @@ -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); - } - } - } -} diff --git a/scripts/update-dependencies/Program.cs b/scripts/update-dependencies/Program.cs deleted file mode 100644 index 06ec564689..0000000000 --- a/scripts/update-dependencies/Program.cs +++ /dev/null @@ -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 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; - } - } - } - } -} diff --git a/scripts/update-dependencies/update-dependencies.csproj b/scripts/update-dependencies/update-dependencies.csproj deleted file mode 100644 index 2887812fbb..0000000000 --- a/scripts/update-dependencies/update-dependencies.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - Exe - netcoreapp2.0 - 7.1 - - https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json; - https://api.nuget.org/v3/index.json; - - - - - - - - - - - -