Move to buildtools' VersionTools for update-dependencies.

This commit is contained in:
Eric Erhardt 2016-07-15 19:09:38 -05:00
parent 69eed12132
commit a27d02eb66
8 changed files with 96 additions and 544 deletions

View file

@ -1,30 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using Microsoft.DotNet.Cli.Build.Framework;
namespace Microsoft.DotNet.Scripts
{
public static class BuildContextProperties
{
public static List<DependencyInfo> GetDependencyInfos(this BuildTargetContext c)
{
const string propertyName = "DependencyInfos";
List<DependencyInfo> dependencyInfos;
object dependencyInfosObj;
if (c.BuildContext.Properties.TryGetValue(propertyName, out dependencyInfosObj))
{
dependencyInfos = (List<DependencyInfo>)dependencyInfosObj;
}
else
{
dependencyInfos = new List<DependencyInfo>();
c.BuildContext[propertyName] = dependencyInfos;
}
return dependencyInfos;
}
}
}

View file

@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System; using System;
using Microsoft.DotNet.Cli.Build.Framework;
namespace Microsoft.DotNet.Scripts namespace Microsoft.DotNet.Scripts
{ {
@ -18,10 +17,10 @@ namespace Microsoft.DotNet.Scripts
/// ///
/// The following Environment Variables can optionally be specified: /// The following Environment Variables can optionally be specified:
/// ///
/// COREFX_VERSION_URL - The Url to get the current CoreFx package versions. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/corefx/release/1.0.0/Latest_Packages.txt") /// COREFX_VERSION_URL - The Url to get the current CoreFx package versions. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/corefx/release/1.0.0")
/// CORECLR_VERSION_URL - The Url to get the current CoreCLR version. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/coreclr/release/1.0.0/Latest_Packages.txt") /// CORECLR_VERSION_URL - The Url to get the current CoreCLR version. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/coreclr/release/1.0.0")
/// ROSLYN_VERSION_URL - The Url to get the current Roslyn version. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/roslyn/netcore1.0/Latest_Packages.txt") /// ROSLYN_VERSION_URL - The Url to get the current Roslyn version. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/roslyn/netcore1.0")
/// CORESETUP_VERSION_URL - The Url to get the current dotnet/core-setup package versions. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/core-setup/release/1.0.0/Latest_Packages.txt") /// CORESETUP_VERSION_URL - The Url to get the current dotnet/core-setup package versions. (ex. "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/core-setup/release/1.0.0")
/// GITHUB_ORIGIN_OWNER - The owner of the GitHub fork to push the commit and create the PR from. (ex. "dotnet-bot") /// GITHUB_ORIGIN_OWNER - The owner of the GitHub fork to push the commit and create the PR from. (ex. "dotnet-bot")
/// GITHUB_UPSTREAM_OWNER - The owner of the GitHub base repo to create the PR to. (ex. "dotnet") /// GITHUB_UPSTREAM_OWNER - The owner of the GitHub base repo to create the PR to. (ex. "dotnet")
/// GITHUB_PROJECT - The repo name under the ORIGIN and UPSTREAM owners. (ex. "cli") /// GITHUB_PROJECT - The repo name under the ORIGIN and UPSTREAM owners. (ex. "cli")
@ -36,11 +35,10 @@ namespace Microsoft.DotNet.Scripts
private Lazy<string> _email = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_EMAIL")); private Lazy<string> _email = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_EMAIL"));
private Lazy<string> _password = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_PASSWORD")); private Lazy<string> _password = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_PASSWORD"));
private Lazy<string> _coreFxVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("COREFX_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/corefx/release/1.0.0/Latest_Packages.txt")); private Lazy<string> _coreFxVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("COREFX_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/corefx/release/1.0.0"));
private Lazy<string> _coreClrVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("CORECLR_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/coreclr/release/1.0.0/Latest_Packages.txt")); private Lazy<string> _coreClrVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("CORECLR_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/coreclr/release/1.0.0"));
private Lazy<string> _roslynVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("ROSLYN_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/roslyn/netcore1.0/Latest_Packages.txt")); private Lazy<string> _roslynVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("ROSLYN_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/roslyn/netcore1.0"));
private Lazy<string> _coreSetupVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("CORESETUP_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/core-setup/release/1.0.0/Latest_Packages.txt")); private Lazy<string> _coreSetupVersionUrl = new Lazy<string>(() => GetEnvironmentVariable("CORESETUP_VERSION_URL", "https://raw.githubusercontent.com/dotnet/versions/master/build-info/dotnet/core-setup/release/1.0.0"));
private Lazy<string> _gitHubOriginOwner;
private Lazy<string> _gitHubUpstreamOwner = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_UPSTREAM_OWNER", "dotnet")); private Lazy<string> _gitHubUpstreamOwner = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_UPSTREAM_OWNER", "dotnet"));
private Lazy<string> _gitHubProject = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_PROJECT", "cli")); private Lazy<string> _gitHubProject = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_PROJECT", "cli"));
private Lazy<string> _gitHubUpstreamBranch = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_UPSTREAM_BRANCH", "rel/1.0.0")); private Lazy<string> _gitHubUpstreamBranch = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_UPSTREAM_BRANCH", "rel/1.0.0"));
@ -50,7 +48,6 @@ namespace Microsoft.DotNet.Scripts
private Config() private Config()
{ {
_gitHubOriginOwner = new Lazy<string>(() => GetEnvironmentVariable("GITHUB_ORIGIN_OWNER", UserName));
} }
public string UserName => _userName.Value; public string UserName => _userName.Value;
@ -60,7 +57,6 @@ namespace Microsoft.DotNet.Scripts
public string CoreClrVersionUrl => _coreClrVersionUrl.Value; public string CoreClrVersionUrl => _coreClrVersionUrl.Value;
public string RoslynVersionUrl => _roslynVersionUrl.Value; public string RoslynVersionUrl => _roslynVersionUrl.Value;
public string CoreSetupVersionUrl => _coreSetupVersionUrl.Value; public string CoreSetupVersionUrl => _coreSetupVersionUrl.Value;
public string GitHubOriginOwner => _gitHubOriginOwner.Value;
public string GitHubUpstreamOwner => _gitHubUpstreamOwner.Value; public string GitHubUpstreamOwner => _gitHubUpstreamOwner.Value;
public string GitHubProject => _gitHubProject.Value; public string GitHubProject => _gitHubProject.Value;
public string GitHubUpstreamBranch => _gitHubUpstreamBranch.Value; public string GitHubUpstreamBranch => _gitHubUpstreamBranch.Value;
@ -76,7 +72,7 @@ namespace Microsoft.DotNet.Scripts
if (value == null) if (value == null)
{ {
throw new BuildFailureException($"Can't find environment variable '{name}'."); throw new InvalidOperationException($"Can't find environment variable '{name}'.");
} }
return value; return value;

View file

@ -1,23 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using NuGet.Versioning;
namespace Microsoft.DotNet.Scripts
{
public class DependencyInfo
{
public string Name { get; set; }
public List<PackageInfo> NewVersions { get; set; }
public string NewReleaseVersion { get; set; }
public bool IsUpdated { get; set; }
}
public class PackageInfo
{
public string Id { get; set; }
public NuGetVersion Version { get; set; }
}
}

View file

@ -1,25 +1,92 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.Build.Framework; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.VersionTools;
using Microsoft.DotNet.VersionTools.Automation;
using Microsoft.DotNet.VersionTools.Dependencies;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Microsoft.DotNet.Scripts namespace Microsoft.DotNet.Scripts
{ {
public class Program public class Program
{ {
public static int Main(string[] args) private static readonly Config s_config = Config.Instance;
public static void Main(string[] args)
{ {
DebugHelper.HandleDebugSwitch(ref args); DebugHelper.HandleDebugSwitch(ref args);
return BuildSetup.Create(".NET CLI Dependency Updater") List<BuildInfo> buildInfos = new List<BuildInfo>();
.UseTargets(new[]
{ buildInfos.Add(BuildInfo.Get("CoreFx", s_config.CoreFxVersionUrl, fetchLatestReleaseFile: false));
new BuildTarget("Default", "Dependency Updater Goals", new [] { "UpdateFiles", "PushPR" }), buildInfos.Add(BuildInfo.Get("CoreClr", s_config.CoreClrVersionUrl, fetchLatestReleaseFile: false));
new BuildTarget("UpdateFiles", "Dependency Updater Goals"), buildInfos.Add(BuildInfo.Get("Roslyn", s_config.RoslynVersionUrl, fetchLatestReleaseFile: false));
new BuildTarget("PushPR", "Dependency Updater Goals"), buildInfos.Add(BuildInfo.Get("CoreSetup", s_config.CoreSetupVersionUrl, fetchLatestReleaseFile: false));
})
.UseAllTargetsFromAssembly<Program>() IEnumerable<IDependencyUpdater> updaters = GetUpdaters();
.Run(args);
GitHubAuth gitHubAuth = new GitHubAuth(s_config.Password, s_config.UserName, s_config.Email);
DependencyUpdater dependencyUpdater = new DependencyUpdater(
gitHubAuth,
s_config.GitHubProject,
s_config.GitHubUpstreamOwner,
s_config.GitHubUpstreamBranch,
s_config.UserName,
s_config.GitHubPullRequestNotifications);
if (args.Length > 0 && string.Equals("--Update", args[0], StringComparison.OrdinalIgnoreCase))
{
dependencyUpdater.Update(updaters, buildInfos);
}
else
{
dependencyUpdater.UpdateAndSubmitPullRequestAsync(updaters, buildInfos);
}
}
private static IEnumerable<IDependencyUpdater> GetUpdaters()
{
yield return CreateProjectJsonUpdater();
yield return CreateRegexUpdater(@"build_projects\shared-build-targets-utils\DependencyVersions.cs", "CoreCLRVersion", "Microsoft.NETCore.Runtime.CoreCLR");
yield return CreateRegexUpdater(@"build_projects\shared-build-targets-utils\DependencyVersions.cs", "JitVersion", "Microsoft.NETCore.Jit");
yield return CreateRegexUpdater(@"build_projects\dotnet-cli-build\CliDependencyVersions.cs", "SharedFrameworkVersion", "Microsoft.NETCore.App");
yield return CreateRegexUpdater(@"build_projects\dotnet-cli-build\CliDependencyVersions.cs", "HostFxrVersion", "Microsoft.NETCore.DotNetHostResolver");
yield return CreateRegexUpdater(@"build_projects\dotnet-cli-build\CliDependencyVersions.cs", "SharedHostVersion", "Microsoft.NETCore.DotNetHost");
}
private static IDependencyUpdater CreateProjectJsonUpdater()
{
const string noUpdateFileName = ".noautoupdate";
IEnumerable<string> projectJsonFiles = Enumerable.Union(
Directory.GetFiles(Dirs.RepoRoot, "project.json", SearchOption.AllDirectories),
Directory.GetFiles(Path.Combine(Dirs.RepoRoot, @"src\dotnet\commands\dotnet-new"), "project.json.template", SearchOption.AllDirectories))
.Where(p => !File.Exists(Path.Combine(Path.GetDirectoryName(p), noUpdateFileName)) &&
!Path.GetDirectoryName(p).EndsWith("CSharp_Web", StringComparison.Ordinal));
return new ProjectJsonUpdater(projectJsonFiles)
{
SkipStableVersions = false
};
}
private static IDependencyUpdater CreateRegexUpdater(string repoRelativePath, string dependencyPropertyName, string packageId)
{
return new FileRegexPackageUpdater()
{
Path = Path.Combine(Dirs.RepoRoot, repoRelativePath),
PackageId = packageId,
Regex = new Regex($@"{dependencyPropertyName} = ""(?<version>.*)"";"),
VersionGroupName = "version"
};
} }
} }
} }

View file

@ -1,162 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Linq;
using Microsoft.DotNet.Cli.Build.Framework;
using Octokit;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Scripts
{
/// <summary>
/// Creates a GitHub Pull Request for the current changes in the repo.
/// </summary>
public static class PushPRTargets
{
private static readonly Config s_config = Config.Instance;
[Target(nameof(CommitChanges), nameof(CreatePR))]
public static BuildTargetResult PushPR(BuildTargetContext c) => c.Success();
/// <summary>
/// Commits all the current changes in the repo and pushes the commit to a remote
/// so a PR can be created for it.
/// </summary>
[Target]
public static BuildTargetResult CommitChanges(BuildTargetContext c)
{
CommandResult statusResult = Cmd("git", "status", "--porcelain")
.CaptureStdOut()
.Execute();
statusResult.EnsureSuccessful();
bool hasModifiedFiles = !string.IsNullOrWhiteSpace(statusResult.StdOut);
bool hasUpdatedDependencies = c.GetDependencyInfos().Where(d => d.IsUpdated).Any();
if (hasModifiedFiles != hasUpdatedDependencies)
{
return c.Failed($"'git status' does not match DependencyInfo information. Git has modified files: {hasModifiedFiles}. DependencyInfo is updated: {hasUpdatedDependencies}.");
}
if (!hasUpdatedDependencies)
{
c.Warn("Dependencies are currently up to date");
return c.Success();
}
string userName = s_config.UserName;
string email = s_config.Email;
string commitMessage = GetCommitMessage(c);
Cmd("git", "commit", "-a", "-m", commitMessage, "--author", $"{userName} <{email}>")
.EnvironmentVariable("GIT_COMMITTER_NAME", userName)
.EnvironmentVariable("GIT_COMMITTER_EMAIL", email)
.Execute()
.EnsureSuccessful();
string remoteUrl = $"github.com/{s_config.GitHubOriginOwner}/{s_config.GitHubProject}.git";
string remoteBranchName = $"UpdateDependencies{DateTime.UtcNow.ToString("yyyyMMddhhmmss")}";
string refSpec = $"HEAD:refs/heads/{remoteBranchName}";
string logMessage = $"git push https://{remoteUrl} {refSpec}";
BuildReporter.BeginSection("EXEC", logMessage);
CommandResult pushResult =
Cmd("git", "push", $"https://{userName}:{s_config.Password}@{remoteUrl}", refSpec)
.QuietBuildReporter() // we don't want secrets showing up in our logs
.CaptureStdErr() // git push will write to StdErr upon success, disable that
.CaptureStdOut()
.Execute();
var message = logMessage + $" exited with {pushResult.ExitCode}";
if (pushResult.ExitCode == 0)
{
BuildReporter.EndSection("EXEC", message.Green(), success: true);
}
else
{
BuildReporter.EndSection("EXEC", message.Red().Bold(), success: false);
}
pushResult.EnsureSuccessful(suppressOutput: true);
c.SetRemoteBranchName(remoteBranchName);
return c.Success();
}
/// <summary>
/// Creates a GitHub PR for the remote branch created above.
/// </summary>
[Target]
public static BuildTargetResult CreatePR(BuildTargetContext c)
{
string remoteBranchName = c.GetRemoteBranchName();
string commitMessage = c.GetCommitMessage();
NewPullRequest prInfo = new NewPullRequest(
commitMessage,
s_config.GitHubOriginOwner + ":" + remoteBranchName,
s_config.GitHubUpstreamBranch);
string[] prNotifications = s_config.GitHubPullRequestNotifications;
if (prNotifications.Length > 0)
{
prInfo.Body = $"/cc @{string.Join(" @", prNotifications)}";
}
GitHubClient gitHub = new GitHubClient(new ProductHeaderValue("dotnetDependencyUpdater"));
gitHub.Credentials = new Credentials(s_config.Password);
PullRequest createdPR = gitHub.PullRequest.Create(s_config.GitHubUpstreamOwner, s_config.GitHubProject, prInfo).Result;
c.Info($"Created Pull Request: {createdPR.HtmlUrl}");
return c.Success();
}
private static string GetRemoteBranchName(this BuildTargetContext c)
{
return (string)c.BuildContext["RemoteBranchName"];
}
private static void SetRemoteBranchName(this BuildTargetContext c, string value)
{
c.BuildContext["RemoteBranchName"] = value;
}
private static string GetCommitMessage(this BuildTargetContext c)
{
const string commitMessagePropertyName = "CommitMessage";
string message;
object messageObject;
if (c.BuildContext.Properties.TryGetValue(commitMessagePropertyName, out messageObject))
{
message = (string)messageObject;
}
else
{
DependencyInfo[] updatedDependencies = c.GetDependencyInfos()
.Where(d => d.IsUpdated)
.ToArray();
string updatedDependencyNames = string.Join(", ", updatedDependencies.Select(d => d.Name));
string updatedDependencyVersions = string.Join(", ", updatedDependencies.Select(d => d.NewReleaseVersion));
message = $"Updating {updatedDependencyNames} to {updatedDependencyVersions}";
if (updatedDependencies.Count() > 1)
{
message += " respectively";
}
c.BuildContext[commitMessagePropertyName] = message;
}
return message;
}
}
}

View file

@ -1,283 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli.Build.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Versioning;
namespace Microsoft.DotNet.Scripts
{
public static class UpdateFilesTargets
{
private static HttpClient s_client = new HttpClient();
[Target(nameof(GetDependencies), nameof(ReplaceVersions))]
public static BuildTargetResult UpdateFiles(BuildTargetContext c) => c.Success();
/// <summary>
/// Gets all the dependency information and puts it in the build properties.
/// </summary>
[Target]
public static BuildTargetResult GetDependencies(BuildTargetContext c)
{
List<DependencyInfo> dependencyInfos = c.GetDependencyInfos();
dependencyInfos.Add(CreateDependencyInfo("CoreFx", Config.Instance.CoreFxVersionUrl).Result);
dependencyInfos.Add(CreateDependencyInfo("CoreClr", Config.Instance.CoreClrVersionUrl).Result);
dependencyInfos.Add(CreateDependencyInfo("Roslyn", Config.Instance.RoslynVersionUrl).Result);
dependencyInfos.Add(CreateDependencyInfo("CoreSetup", Config.Instance.CoreSetupVersionUrl).Result);
return c.Success();
}
private static async Task<DependencyInfo> CreateDependencyInfo(string name, string packageVersionsUrl)
{
List<PackageInfo> newPackageVersions = new List<PackageInfo>();
using (Stream versionsStream = await s_client.GetStreamAsync(packageVersionsUrl))
using (StreamReader reader = new StreamReader(versionsStream))
{
string currentLine;
while ((currentLine = await reader.ReadLineAsync()) != null)
{
int spaceIndex = currentLine.IndexOf(' ');
newPackageVersions.Add(new PackageInfo()
{
Id = currentLine.Substring(0, spaceIndex),
Version = new NuGetVersion(currentLine.Substring(spaceIndex + 1))
});
}
}
string newReleaseVersion = newPackageVersions
.Where(p => p.Version.IsPrerelease)
.Select(p => p.Version.Release)
.FirstOrDefault()
??
// if there are no prerelease versions, just grab the first version
newPackageVersions
.Select(p => p.Version.ToNormalizedString())
.FirstOrDefault();
return new DependencyInfo()
{
Name = name,
NewVersions = newPackageVersions,
NewReleaseVersion = newReleaseVersion
};
}
[Target(nameof(ReplaceProjectJson), nameof(ReplaceDependencyVersions))]
public static BuildTargetResult ReplaceVersions(BuildTargetContext c) => c.Success();
/// <summary>
/// Replaces all the dependency versions in the project.json files.
/// </summary>
[Target]
public static BuildTargetResult ReplaceProjectJson(BuildTargetContext c)
{
List<DependencyInfo> dependencyInfos = c.GetDependencyInfos();
const string noUpdateFileName = ".noautoupdate";
IEnumerable<string> projectJsonFiles = Enumerable.Union(
Directory.GetFiles(Dirs.RepoRoot, "project.json", SearchOption.AllDirectories),
Directory.GetFiles(Path.Combine(Dirs.RepoRoot, @"src\dotnet\commands\dotnet-new"), "project.json.template", SearchOption.AllDirectories))
.Where(p => !File.Exists(Path.Combine(Path.GetDirectoryName(p), noUpdateFileName)) &&
!Path.GetDirectoryName(p).EndsWith("CSharp_Web", StringComparison.Ordinal));
JObject projectRoot;
foreach (string projectJsonFile in projectJsonFiles)
{
try
{
projectRoot = ReadProject(projectJsonFile);
}
catch (Exception e)
{
c.Warn($"Non-fatal exception occurred reading '{projectJsonFile}'. Skipping file. Exception: {e}. ");
continue;
}
if (projectRoot == null)
{
c.Warn($"A non valid JSON file was encountered '{projectJsonFile}'. Skipping file.");
continue;
}
bool changedAnyPackage = FindAllDependencyProperties(projectRoot)
.Select(dependencyProperty => ReplaceDependencyVersion(dependencyProperty, dependencyInfos))
.ToArray()
.Any(shouldWrite => shouldWrite);
if (changedAnyPackage)
{
c.Info($"Writing changes to {projectJsonFile}");
WriteProject(projectRoot, projectJsonFile);
}
}
return c.Success();
}
/// <summary>
/// Replaces the single dependency with the updated version, if it matches any of the dependencies that need to be updated.
/// </summary>
private static bool ReplaceDependencyVersion(JProperty dependencyProperty, List<DependencyInfo> dependencyInfos)
{
string id = dependencyProperty.Name;
foreach (DependencyInfo dependencyInfo in dependencyInfos)
{
foreach (PackageInfo packageInfo in dependencyInfo.NewVersions)
{
if (id == packageInfo.Id)
{
string oldVersion;
if (dependencyProperty.Value is JObject)
{
oldVersion = (string)dependencyProperty.Value["version"];
}
else
{
oldVersion = (string)dependencyProperty.Value;
}
string newVersion = packageInfo.Version.ToNormalizedString();
if (oldVersion != newVersion)
{
if (dependencyProperty.Value is JObject)
{
dependencyProperty.Value["version"] = newVersion;
}
else
{
dependencyProperty.Value = newVersion;
}
// mark the DependencyInfo as updated so we can tell which dependencies were updated
dependencyInfo.IsUpdated = true;
return true;
}
}
}
}
return false;
}
private static JObject ReadProject(string projectJsonPath)
{
using (TextReader projectFileReader = File.OpenText(projectJsonPath))
{
var projectJsonReader = new JsonTextReader(projectFileReader);
var serializer = new JsonSerializer();
return serializer.Deserialize<JObject>(projectJsonReader);
}
}
private static void WriteProject(JObject projectRoot, string projectJsonPath)
{
string projectJson = JsonConvert.SerializeObject(projectRoot, Formatting.Indented);
File.WriteAllText(projectJsonPath, projectJson + Environment.NewLine);
}
private static IEnumerable<JProperty> FindAllDependencyProperties(JObject projectJsonRoot)
{
return projectJsonRoot
.Descendants()
.OfType<JProperty>()
.Where(property => property.Name == "dependencies")
.Select(property => property.Value)
.SelectMany(o => o.Children<JProperty>());
}
/// <summary>
/// Replaces version numbers that are hard-coded in DependencyVersions.cs and CliDependencyVersions.cs.
/// </summary>
[Target]
public static BuildTargetResult ReplaceDependencyVersions(BuildTargetContext c)
{
ReplaceFileContents(@"build_projects\shared-build-targets-utils\DependencyVersions.cs", fileContents =>
{
fileContents = ReplaceDependencyVersion(c, fileContents, "CoreCLRVersion", "Microsoft.NETCore.Runtime.CoreCLR");
fileContents = ReplaceDependencyVersion(c, fileContents, "JitVersion", "Microsoft.NETCore.Jit");
return fileContents;
});
ReplaceFileContents(@"build_projects\dotnet-cli-build\CliDependencyVersions.cs", fileContents =>
{
fileContents = ReplaceDependencyVersion(c, fileContents, "SharedFrameworkVersion", "Microsoft.NETCore.App");
fileContents = ReplaceDependencyVersion(c, fileContents, "HostFxrVersion", "Microsoft.NETCore.DotNetHostResolver");
fileContents = ReplaceDependencyVersion(c, fileContents, "SharedHostVersion", "Microsoft.NETCore.DotNetHost");
return fileContents;
});
return c.Success();
}
private static string ReplaceDependencyVersion(BuildTargetContext c, string fileContents, string dependencyPropertyName, string packageId)
{
Regex regex = new Regex($@"{dependencyPropertyName} = ""(?<version>.*)"";");
string newVersion = c.GetNewVersion(packageId);
return regex.ReplaceGroupValue(fileContents, "version", newVersion);
}
private static string GetNewVersion(this BuildTargetContext c, string packageId)
{
string newVersion = c.GetDependencyInfos()
.SelectMany(d => d.NewVersions)
.FirstOrDefault(p => p.Id == packageId)
?.Version
.ToNormalizedString();
if (string.IsNullOrEmpty(newVersion))
{
c.Error($"Could not find package version information for '{packageId}'");
return $"DEPENDENCY '{packageId}' NOT FOUND";
}
return newVersion;
}
private static void ReplaceFileContents(string repoRelativePath, Func<string, string> replacement)
{
string fullPath = Path.Combine(Dirs.RepoRoot, repoRelativePath);
string contents = File.ReadAllText(fullPath);
contents = replacement(contents);
File.WriteAllText(fullPath, contents, Encoding.UTF8);
}
private static string ReplaceGroupValue(this Regex regex, string input, string groupName, string newValue)
{
return regex.Replace(input, m =>
{
string replacedValue = m.Value;
Group group = m.Groups[groupName];
int startIndex = group.Index - m.Index;
replacedValue = replacedValue.Remove(startIndex, group.Length);
replacedValue = replacedValue.Insert(startIndex, newValue);
return replacedValue;
});
}
}
}

View file

@ -2,28 +2,17 @@
"version": "1.0.0-*", "version": "1.0.0-*",
"description": "Updates the repos dependencies", "description": "Updates the repos dependencies",
"buildOptions": { "buildOptions": {
"emitEntryPoint": true "emitEntryPoint": true,
"compile": [
"../../src/Microsoft.DotNet.Cli.Utils/DebugHelper.cs"
]
}, },
"dependencies": { "dependencies": {
"NETStandard.Library": "1.6.0", "Microsoft.NETCore.App": "1.0.0",
"Microsoft.CSharp": "4.0.1", "Microsoft.DotNet.VersionTools": "1.0.26-prerelease-00615-07"
"Microsoft.NETCore.Runtime.CoreCLR": "1.0.2",
"System.Runtime.Serialization.Primitives": "4.1.1",
"Microsoft.DotNet.Cli.Build.Framework": {
"target": "project"
},
"NuGet.Versioning": "3.5.0-beta2-1484",
"Newtonsoft.Json": "9.0.1",
"Octokit": "0.18.0",
"Microsoft.Net.Http": "2.2.29"
}, },
"frameworks": { "frameworks": {
"netcoreapp1.0": { "netcoreapp1.0": {}
"imports": [
"dnxcore50",
"portable-net45+win"
]
}
}, },
"runtimes": { "runtimes": {
"win7-x64": {}, "win7-x64": {},

View file

@ -4,15 +4,13 @@
# #
param( param(
[string[]]$Targets=@("Default"),
[switch]$Help) [switch]$Help)
if($Help) if($Help)
{ {
Write-Host "Usage: .\update-dependencies.ps1 [-Targets <TARGETS...>]" Write-Host "Usage: .\update-dependencies.ps1"
Write-Host "" Write-Host ""
Write-Host "Options:" Write-Host "Options:"
Write-Host " -Targets <TARGETS...> Comma separated build targets to run (UpdateFiles, PushPR; Default is everything)"
Write-Host " -Help Display this help message" Write-Host " -Help Display this help message"
exit 0 exit 0
} }
@ -51,5 +49,5 @@ if($LASTEXITCODE -ne 0) { throw "Failed to compile build scripts" }
# Run the app # Run the app
Write-Host "Invoking App $AppPath..." Write-Host "Invoking App $AppPath..."
Write-Host " Configuration: $env:CONFIGURATION" Write-Host " Configuration: $env:CONFIGURATION"
& "$AppPath\bin\update-dependencies.exe" @Targets & "$AppPath\bin\update-dependencies.exe"
if($LASTEXITCODE -ne 0) { throw "Build failed" } if($LASTEXITCODE -ne 0) { throw "Build failed" }