Update PinVersion tool use dotnet and v3 NuGet packages

This commit is contained in:
Pranav K 2016-05-17 14:42:22 -07:00
parent 6307533967
commit 0e4d154134
6 changed files with 119 additions and 96 deletions

View File

@ -101,20 +101,17 @@
-// Pin versions of packages in project.json and updated project.lock.json
-// More information https://github.com/aspnet/Universe/wiki/%23pin-version-:-Pinning-package-version-for-a-particular-release-in-project.json
@{
var koreBuildTag = GetEnvironmentParameter("KOREBUILD_TAG");
var coherenceFeed = GetEnvironmentParameter("COHERENCE_FEED");
if (string.IsNullOrEmpty(coherenceFeed))
{
throw new Exception("COHERENCE_FEED not specified. Usually this is Packages-NoTimestamp directory of Coherence-Signed.");
}
if (string.IsNullOrEmpty(koreBuildVersion))
if (string.IsNullOrEmpty(koreBuildTag))
{
throw new Exception("KOREBUILD_VERSION environment variable is not specified.");
}
if (string.IsNullOrEmpty(nugetVersion))
{
throw new Exception("NUGET_VERSION not specified. This is most recent stable build of NuGet from http://dist.nuget.org/index.html. e.g. v3.2");
throw new Exception("KOREBUILD_TAG environment variable is not specified.");
}
var excludeReposForJson = new[]
@ -127,7 +124,7 @@
"libuv-build",
};
Exec("cmd", "/C dnu restore", @"tools\PinVersion");
Exec("dotnet", "restore", "tools/PinVersion");
foreach (var repo in GetAllRepos())
{
@ -144,19 +141,33 @@
}
var reposToPin = GetAllRepos().Except(excludeReposForJson);
var repositoryNamesFile = Path.GetTempFileName();
File.WriteAllLines(repositoryNamesFile, reposToPin.Select(r => Path.Combine(Directory.GetCurrentDirectory(), r)));
var pinToolsArgs = string.Format(@"run -p tools/PinVersion ""{0}"" ""{1}"" ""{2}""",
coherenceFeed,
koreBuildTag,
repositoryNamesFile);
Exec("dotnet", pinToolsArgs, "");
try
{
File.Delete(repositoryNamesFile);
}
catch
{
}
foreach (var repo in reposToPin)
{
var repoPath = Path.Combine(Directory.GetCurrentDirectory(), repo);
Exec("dnx",
string.Format(@"run ""{0}"" ""{1}"" ""{2}""",
Path.Combine(Directory.GetCurrentDirectory(), repo),
coherenceFeed,
koreBuildVersion),
@"tools\PinVersion");
GitCommand(repo, "commit -am \"Updating json files to pin versions and build.cmd to pin KoreBuild and DNX\"");
try
{
GitCommand(repo, "commit -am \"Updating json files to pin versions and build files to pin KoreBuild\"");
}
catch
{
// Don't fail if there was nothing to add.
}
}
foreach (var repo in GetAllRepos())
@ -170,6 +181,7 @@
#update-prerelease-tags
-// Update tags on each repo to have the latest release tag
@{
var preReleaseTag = GetEnvironmentParameter("PRERELEASETAG");
if (string.IsNullOrEmpty(preReleaseTag))
{
throw new Exception("PRERELEASETAG tag not defined");
@ -180,6 +192,7 @@
foreach (var repo in GetAllRepos())
{
GitCommand(repo, "pull --tags");
string version = null;
try
{
@ -187,12 +200,15 @@
}
catch
{
Log.Warn(string.Format("{0} repo not tagged. Skipping....", repo));
continue;
version = "1.0.0-" + preReleaseTag;
Log.Warn(string.Format("{0} repo not tagged. Using default version {1}.", repo, version));
}
var version = File.ReadAllText(versionFile);
File.Delete(versionFile);
if (version == null)
{
version = File.ReadAllText(versionFile);
File.Delete(versionFile);
}
Log.Info(string.Format("Current version on repo {0} is {1}", repo, version));

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="AspNetVNext" value="https://www.myget.org/f/aspnetmaster/api/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>

View File

@ -35,11 +35,8 @@ functions
static string kBuildVersion = GetEnvironmentParameter("DNX_BUILD_VERSION");
static string ciVolatileShare = GetEnvironmentParameter("CI_VOLATILE_SHARE");
static string koreBuildTargets = GetEnvironmentParameter("KOREBUILD_BUILD_TARGETS");
static string coherenceFeed = GetEnvironmentParameter("COHERENCE_FEED");
static string koreBuildVersion = GetEnvironmentParameter("KOREBUILD_VERSION");
static string nugetVersion = GetEnvironmentParameter("NUGET_VERSION");
static string nugetExe = GetEnvironmentParameter("PUSH_NUGET_EXE");
static string preReleaseTag = GetEnvironmentParameter("PRERELEASETAG");
static string universeCommitsFile = GetEnvironmentParameter("UNIVERSE_COMMITS_FILE");
static bool skipNoCredentials = GetEnvironmentParameter("UNIVERSE_SKIP_NO_CREDENTIALS", value => value == "1");
static string repositoryInclude = GetEnvironmentParameter("KOREBUILD_REPOSITORY_INCLUDE");

View File

@ -9,9 +9,8 @@
<ProjectGuid>3d3b5750-0c24-4f1c-9304-b5e4d85cf5a0</ProjectGuid>
<RootNamespace>PinVersion</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>

View File

@ -2,38 +2,56 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet;
using NuGet.Common;
using NuGet.Protocol.Core.Types;
using NuGet.Protocol.Core.v3;
using NuGet.Versioning;
namespace PinVersion
{
public class Program
{
public void Main(string[] args)
private static ConcurrentDictionary<string, Task<NuGetVersion>> _packageVersionLookup =
new ConcurrentDictionary<string, Task<NuGetVersion>>(StringComparer.OrdinalIgnoreCase);
public static void Main(string[] args)
{
if (args.Length != 3)
{
Console.Error.WriteLine("Usage <repository-path> <package source> <Korebuild Version>");
Console.Error.WriteLine("Usage <package source> <Korebuild Tag> <repository-names-file>");
}
var repositoryPath = args[0];
var packageSource = args[1];
var korebuildVersion = args[2];
var packageSource = args[0];
var korebuildTag = args[1];
var packageRepository = PackageRepositoryFactory.Default.CreateRepository(packageSource);
var repositoryNames = File.ReadAllLines(args[2]);
Task.WaitAll(repositoryNames
.Select(repositoryPath => ExecuteAsync(repositoryPath, packageSource, korebuildTag))
.ToArray());
}
public static async Task ExecuteAsync(string repositoryPath, string packageSource, string korebuildTag)
{
var packageRepository = Repository.Factory.GetCoreV3(packageSource);
var metadataResource = await packageRepository.GetResourceAsync<MetadataResource>();
// Pin project.json files
foreach (var file in Directory.EnumerateFiles(repositoryPath, "project.json", SearchOption.AllDirectories))
{
var projectJson = JObject.Parse(File.ReadAllText(file));
var directoryName = Path.GetFileName(Path.GetDirectoryName(file));
var latestPackage = packageRepository.FindPackage(directoryName);
if (latestPackage != null)
var projectName = Path.GetFileName(Path.GetDirectoryName(file));
var latestPackageVersion = await GetOrAddVersion(metadataResource, projectName);
if (latestPackageVersion != null)
{
((JValue)projectJson["version"]).Value = latestPackage.Version.ToNormalizedString();
((JValue)projectJson["version"]).Value = latestPackageVersion.ToNormalizedString();
}
var frameworkDependencies = projectJson["frameworks"]
@ -47,19 +65,19 @@ namespace PinVersion
foreach (var dependency in dependencies)
{
latestPackage = packageRepository.FindPackage(dependency.Name);
if (latestPackage != null)
latestPackageVersion = await GetOrAddVersion(metadataResource, dependency.Name);
if (latestPackageVersion != null)
{
if (dependency.Value.Type == JTokenType.Object)
{
// "key": { "version": "1.0.0-*", "type": "build" }
var value = (JObject)dependency.Value;
value["version"] = latestPackage.Version.ToNormalizedString();
value["version"] = latestPackageVersion.ToNormalizedString();
}
else
{
// "key": "version"
dependency.Value = latestPackage.Version.ToNormalizedString();
dependency.Value = latestPackageVersion.ToNormalizedString();
}
}
}
@ -70,51 +88,38 @@ namespace PinVersion
fileWriter.Indentation = 2;
projectJson.WriteTo(fileWriter);
}
// Update KoreBuild path
var buildFiles = new[] { "build.ps1", "build.sh" };
foreach (var buildFile in buildFiles)
{
var buildFilePath = Path.Combine(repositoryPath, buildFile);
if (File.Exists(buildFilePath))
{
var content = File.ReadAllText(buildFilePath);
var replaced = content.Replace("KoreBuild/archive/release.zip", $"KoreBuild/archive/{korebuildTag}.zip");
if (content != replaced)
{
File.WriteAllText(buildFilePath, replaced);
}
}
}
}
}
// Pin the build scripts
//TODO: handle build.sh files too
var dnxPackageName = "dnx-clr-win-x86";
var dnxPackage = packageRepository.FindPackage(dnxPackageName);
if (dnxPackage == null)
private static Task<NuGetVersion> GetOrAddVersion(MetadataResource resource, string packageId)
{
return _packageVersionLookup.GetOrAdd(packageId, id =>
{
throw new InvalidOperationException(
$"Could not find the DNX package with name '{dnxPackageName}' to " +
"pin the build script");
}
var buildCmdFiles = Directory.GetFiles(repositoryPath, "build.cmd", SearchOption.TopDirectoryOnly);
if (buildCmdFiles == null || buildCmdFiles.Length == 0)
{
throw new InvalidOperationException($"No build.cmd files found at {repositoryPath}");
}
var buildCmdFile = buildCmdFiles[0];
var buildCmdFileContent = File.ReadAllText(buildCmdFile);
buildCmdFileContent = buildCmdFileContent.Replace(
"SET BUILDCMD_KOREBUILD_VERSION=\"\"",
$"SET BUILDCMD_KOREBUILD_VERSION={korebuildVersion}");
buildCmdFileContent = buildCmdFileContent.Replace(
"SET BUILDCMD_DNX_VERSION=\"\"",
$"SET BUILDCMD_DNX_VERSION={dnxPackage.Version.ToNormalizedString()}");
// Replace all content of the file
File.WriteAllText(buildCmdFile, buildCmdFileContent);
// Pin the global.json
var globalJsonPath = Path.Combine(repositoryPath, "global.json");
var globalJson = JObject.Parse(File.ReadAllText(globalJsonPath));
globalJson["sdk"] = new JObject
{
["version"] = dnxPackage.Version.ToNormalizedString()
};
using (var fileWriter = new JsonTextWriter(new StreamWriter(globalJsonPath)))
{
fileWriter.Formatting = Formatting.Indented;
fileWriter.Indentation = 2;
globalJson.WriteTo(fileWriter);
}
return resource.GetLatestVersion(
packageId,
includePrerelease: true,
includeUnlisted: false,
log: NullLogger.Instance,
token: default(CancellationToken));
});
}
}
}

View File

@ -1,15 +1,13 @@
{
"version": "1.0.0-*",
"dependencies": {
"Newtonsoft.Json": "8.0.2",
"NuGet.Core": "2.8.6"
},
"commands": {
"PinVersion": "PinVersion"
},
"frameworks": {
"dnx451": { }
}
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Newtonsoft.Json": "8.0.3",
"NuGet.Protocol.Core.v3": "3.5.0-beta-final"
},
"frameworks": {
"net451": { }
}
}