2016-03-07 20:24:36 +00:00
|
|
|
|
// 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;
|
2016-05-25 13:45:18 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-03-07 20:24:36 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2016-05-25 13:45:18 +00:00
|
|
|
|
List<DependencyInfo> dependencyInfos = c.GetDependencyInfos();
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-05-25 13:45:18 +00:00
|
|
|
|
dependencyInfos.Add(CreateDependencyInfo("CoreFx", Config.Instance.CoreFxVersionUrl).Result);
|
2016-05-26 13:41:18 +00:00
|
|
|
|
dependencyInfos.Add(CreateDependencyInfo("CoreSetup", Config.Instance.CoreSetupVersionUrl).Result);
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-05-25 13:45:18 +00:00
|
|
|
|
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))
|
2016-03-07 20:24:36 +00:00
|
|
|
|
{
|
2016-05-25 13:45:18 +00:00
|
|
|
|
string currentLine;
|
|
|
|
|
while ((currentLine = await reader.ReadLineAsync()) != null)
|
|
|
|
|
{
|
|
|
|
|
int spaceIndex = currentLine.IndexOf(' ');
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-05-25 13:45:18 +00:00
|
|
|
|
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
|
|
|
|
|
};
|
2016-03-07 20:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 13:41:18 +00:00
|
|
|
|
[Target(nameof(ReplaceProjectJson), nameof(ReplaceDependencyVersions))]
|
2016-03-07 20:24:36 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2016-04-04 23:38:49 +00:00
|
|
|
|
List<DependencyInfo> dependencyInfos = c.GetDependencyInfos();
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-05-25 14:11:54 +00:00
|
|
|
|
const string noUpdateFileName = ".noautoupdate";
|
|
|
|
|
|
2016-04-11 19:01:59 +00:00
|
|
|
|
IEnumerable<string> projectJsonFiles = Enumerable.Union(
|
|
|
|
|
Directory.GetFiles(Dirs.RepoRoot, "project.json", SearchOption.AllDirectories),
|
2016-05-26 14:43:37 +00:00
|
|
|
|
Directory.GetFiles(Path.Combine(Dirs.RepoRoot, @"src\dotnet\commands\dotnet-new"), "project.json.template", SearchOption.AllDirectories))
|
2016-05-25 14:11:54 +00:00
|
|
|
|
.Where(p => !File.Exists(Path.Combine(Path.GetDirectoryName(p), noUpdateFileName)));
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 23:39:44 +00:00
|
|
|
|
if (projectRoot == null)
|
|
|
|
|
{
|
|
|
|
|
c.Warn($"A non valid JSON file was encountered '{projectJsonFile}'. Skipping file.");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 20:24:36 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2016-05-25 13:45:18 +00:00
|
|
|
|
foreach (PackageInfo packageInfo in dependencyInfo.NewVersions)
|
2016-03-07 20:24:36 +00:00
|
|
|
|
{
|
2016-05-25 13:45:18 +00:00
|
|
|
|
if (id == packageInfo.Id)
|
2016-03-07 20:24:36 +00:00
|
|
|
|
{
|
|
|
|
|
if (dependencyProperty.Value is JObject)
|
|
|
|
|
{
|
2016-05-25 13:45:18 +00:00
|
|
|
|
dependencyProperty.Value["version"] = packageInfo.Version.ToNormalizedString();
|
2016-03-07 20:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-25 13:45:18 +00:00
|
|
|
|
dependencyProperty.Value = packageInfo.Version.ToNormalizedString();
|
2016-03-07 20:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-25 13:45:18 +00:00
|
|
|
|
// mark the DependencyInfo as updated so we can tell which dependencies were updated
|
|
|
|
|
dependencyInfo.IsUpdated = true;
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-05-25 13:45:18 +00:00
|
|
|
|
return true;
|
2016-03-07 20:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
2016-05-27 20:24:39 +00:00
|
|
|
|
/// Replaces version numbers that are hard-coded in DependencyVersions.cs and CliDependencyVersions.cs.
|
2016-03-07 20:24:36 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
[Target]
|
2016-05-26 13:41:18 +00:00
|
|
|
|
public static BuildTargetResult ReplaceDependencyVersions(BuildTargetContext c)
|
2016-03-07 20:24:36 +00:00
|
|
|
|
{
|
2016-05-26 13:41:18 +00:00
|
|
|
|
ReplaceFileContents(@"build_projects\shared-build-targets-utils\DependencyVersions.cs", fileContents =>
|
2016-04-05 15:46:08 +00:00
|
|
|
|
{
|
|
|
|
|
DependencyInfo coreFXInfo = c.GetCoreFXDependency();
|
|
|
|
|
|
2016-05-26 13:41:18 +00:00
|
|
|
|
fileContents = ReplaceDependencyVersion(fileContents, coreFXInfo, "CoreCLRVersion", "Microsoft.NETCore.Runtime.CoreCLR");
|
2016-05-27 20:24:39 +00:00
|
|
|
|
|
|
|
|
|
return fileContents;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ReplaceFileContents(@"build_projects\dotnet-cli-build\CliDependencyVersions.cs", fileContents =>
|
|
|
|
|
{
|
|
|
|
|
DependencyInfo coreSetupInfo = c.GetCoreSetupDependency();
|
|
|
|
|
|
2016-05-26 13:41:18 +00:00
|
|
|
|
fileContents = ReplaceDependencyVersion(fileContents, coreSetupInfo, "SharedFrameworkVersion", "Microsoft.NETCore.App");
|
|
|
|
|
fileContents = ReplaceDependencyVersion(fileContents, coreSetupInfo, "SharedHostVersion", "Microsoft.NETCore.DotNetHost");
|
|
|
|
|
|
|
|
|
|
return fileContents;
|
2016-04-05 15:46:08 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return c.Success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static DependencyInfo GetCoreFXDependency(this BuildTargetContext c)
|
|
|
|
|
{
|
|
|
|
|
return c.GetDependencyInfos().Single(d => d.Name == "CoreFx");
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 13:41:18 +00:00
|
|
|
|
private static DependencyInfo GetCoreSetupDependency(this BuildTargetContext c)
|
|
|
|
|
{
|
|
|
|
|
return c.GetDependencyInfos().Single(d => d.Name == "CoreSetup");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string ReplaceDependencyVersion(string fileContents, DependencyInfo dependencyInfo, string dependencyPropertyName, string packageId)
|
|
|
|
|
{
|
|
|
|
|
Regex regex = new Regex($@"{dependencyPropertyName} = ""(?<version>.*)"";");
|
|
|
|
|
|
|
|
|
|
string newVersion = dependencyInfo
|
|
|
|
|
.NewVersions
|
|
|
|
|
.FirstOrDefault(p => p.Id == packageId)
|
|
|
|
|
?.Version
|
|
|
|
|
.ToNormalizedString();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(newVersion))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Could not find package version information for '{packageId}'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return regex.ReplaceGroupValue(fileContents, "version", newVersion);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 15:46:08 +00:00
|
|
|
|
private static void ReplaceFileContents(string repoRelativePath, Func<string, string> replacement)
|
|
|
|
|
{
|
|
|
|
|
string fullPath = Path.Combine(Dirs.RepoRoot, repoRelativePath);
|
|
|
|
|
string contents = File.ReadAllText(fullPath);
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-04-05 15:46:08 +00:00
|
|
|
|
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 =>
|
2016-03-07 20:24:36 +00:00
|
|
|
|
{
|
|
|
|
|
string replacedValue = m.Value;
|
2016-04-05 15:46:08 +00:00
|
|
|
|
Group group = m.Groups[groupName];
|
|
|
|
|
int startIndex = group.Index - m.Index;
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
2016-04-05 15:46:08 +00:00
|
|
|
|
replacedValue = replacedValue.Remove(startIndex, group.Length);
|
|
|
|
|
replacedValue = replacedValue.Insert(startIndex, newValue);
|
2016-03-07 20:24:36 +00:00
|
|
|
|
|
|
|
|
|
return replacedValue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|