Implement upgrade-deps and generate-deps CLI commands (#438)

This commit is contained in:
Nate McMaster 2017-10-23 15:31:13 -07:00 коммит произвёл GitHub
Родитель 448eca8e5d
Коммит 2714091e0d
7 изменённых файлов: 236 добавлений и 14 удалений

Просмотреть файл

@ -9,6 +9,7 @@
</ApplyNuGetPoliciesDependsOn>
<DisablePackageReferenceRestrictions Condition=" '$(DisablePackageReferenceRestrictions)' == '' ">false</DisablePackageReferenceRestrictions>
<DependencyVersionsFile Condition="'$(DependencyVersionsFile)' == ''">$(RepositoryRoot)build\dependencies.props</DependencyVersionsFile>
</PropertyGroup>
<ItemGroup Condition="@(PackageLineup->Count()) != 0 AND '$(RestrictVersionOnPackageReference)' != 'false' ">
@ -77,23 +78,24 @@
<!-- At some future date, we will make this always run, even if LineupPackageId is not set. We're using LineupPackageId as the way to tell if a repo has started using this pattern yet. -->
<CheckPackageReferences Projects="@(Solutions);@(CommonlyImportedFiles)"
DependenciesFile="$(RepositoryRoot)build\dependencies.props"
DependenciesFile="$(DependencyVersionsFile)"
Properties="$(SolutionProperties)"
Condition=" '$(LineupPackageId)' != '' "/>
</Target>
<!-- See also DependenciesUpgradeCommand.cs in KoreBuild.Console for a more discoverable way to run this. -->
<Target Name="UpgradeDependencies">
<Error Text="LineupPackageId was not set." Condition="'$(LineupPackageId)' == ''" />
<Error Text="LineupPackageRestoreSource was not set." Condition="'$(LineupPackageRestoreSource)' == ''" />
<UpgradeDependenciesPropsFile
DependenciesFile="$(RepositoryRoot)build\dependencies.props"
DependenciesFile="$(DependencyVersionsFile)"
LineupPackageId="$(LineupPackageId)"
LineupPackageVersion="$(LineupPackageVersion)"
LineupPackageRestoreSource="$(LineupPackageRestoreSource)" />
</Target>
<!-- See also DependenciesGenerateCommand.cs in KoreBuild.Console for a more discoverable way to run this. -->
<Target Name="GenerateDependenciesPropsFile" DependsOnTargets="ResolveSolutions;Pin">
<ItemGroup>
<CommonlyImportedFiles Remove="@(CommonlyImportedFiles)" Condition="!Exists(%(CommonlyImportedFiles.Identity))" />
@ -102,7 +104,7 @@
<GenerateDependenciesPropsFile
Projects="@(Solutions)"
OtherImports="@(CommonlyImportedFiles)"
DependenciesFile="$(RepositoryRoot)build\dependencies.props"
DependenciesFile="$(DependencyVersionsFile)"
Properties="$(SolutionProperties)" />
</Target>

Просмотреть файл

@ -18,7 +18,7 @@ namespace KoreBuild.Console.Commands
application.OnExecute(
() =>
{
if(IsValid())
if (IsValid())
{
return Execute();
}

Просмотреть файл

@ -0,0 +1,72 @@
// 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.Collections.Generic;
using System.IO;
using Microsoft.Extensions.CommandLineUtils;
namespace KoreBuild.Console.Commands
{
internal class DependenciesGenerateCommand : SubCommandBase
{
private CommandOption _configOpt;
private CommandOption _fileOpt;
public override void Configure(CommandLineApplication application)
{
application.Description = "Generates a build/dependencies.props file and updates csproj files to use variables";
application.ExtendedHelpText = @"
MORE INFO:
This command will generate a dependencies.props file and adjust all PackageReference's in csproj files
to use the MSBuild variables it generates.
Example output:
<Project>
<PropertyGroup Label=""Package Versions"">
<MyPackageVersion>1.0.0</MyPackageVersion>
</PropertyGroup>
</Project>
";
_configOpt = application.Option("-c|--configuration <CONFIG>", "The MSBuild configuration. Defaults to 'Debug'.", CommandOptionType.SingleValue);
_fileOpt = application.Option("--deps-file <FILEPATH>", "The dependencies.props file to upgrade.", CommandOptionType.SingleValue);
base.Configure(application);
}
protected override int Execute()
{
var args = new List<string>
{
"msbuild",
Path.Combine(KoreBuildDir, "KoreBuild.proj"),
"-t:GenerateDependenciesPropsFile",
};
if (_configOpt.HasValue())
{
args.Add("-p:Configuration=" + _configOpt.Value());
}
if (_fileOpt.HasValue())
{
var filePath = _fileOpt.Value();
if (!Path.IsPathRooted(filePath))
{
filePath = Path.GetFullPath(filePath);
}
args.Add("-p:DependencyVersionsFile=" + filePath);
}
if (Reporter.IsVerbose)
{
args.Add("-v:n");
}
return RunDotnet(args, RepoPath);
}
}
}

Просмотреть файл

@ -0,0 +1,88 @@
// 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.Collections.Generic;
using System.IO;
using Microsoft.Extensions.CommandLineUtils;
namespace KoreBuild.Console.Commands
{
internal class DependenciesUpgradeCommand : SubCommandBase
{
private CommandOption _sourceOpt;
private CommandOption _packageIdOpt;
private CommandOption _packageVersionOpt;
private CommandOption _fileOpt;
public override void Configure(CommandLineApplication application)
{
application.Description = "Upgrades the build/dependencies.props file to the latest package versions";
application.ExtendedHelpText = @"
MORE INFO:
The upgrade uses a 'lineup' package as the source of information about which versions to use.
A lineup package is simply a nuget package that contains a file in build/dependencies.props.
Just like the version of the file in this local repo, this file is an MSBuild project file
with a list of MSBuild variables. Example:
<Project>
<PropertyGroup Label=""Package Versions"">
<MyPackageVersion>1.0.0</MyPackageVersion>
</PropertyGroup>
</Project>
";
_sourceOpt = application.Option("-s|--source <SOURCE>",
"Specifies a NuGet package source to use to upgrade dependencies to the latest lineup package.", CommandOptionType.SingleValue);
_packageIdOpt = application.Option("--id <PACKAGE_ID>", "Specifies the lineup package id to use.", CommandOptionType.SingleValue);
_packageVersionOpt = application.Option("--version <PACKAGE_VERISON>", "Specifies the lineup package version to use.", CommandOptionType.SingleValue);
_fileOpt = application.Option("--deps-file <FILEPATH>", "The dependencies.props file to upgrade.", CommandOptionType.SingleValue);
base.Configure(application);
}
protected override int Execute()
{
var args = new List<string>
{
"msbuild",
Path.Combine(KoreBuildDir, "KoreBuild.proj"),
"-t:UpgradeDependencies",
};
if (_sourceOpt.HasValue())
{
args.Add("-p:LineupPackageRestoreSource=" + _sourceOpt.Value());
}
if (_packageIdOpt.HasValue())
{
args.Add("-p:LineupPackageId=" + _packageIdOpt.Value());
}
if (_packageVersionOpt.HasValue())
{
args.Add("-p:LineupPackageVersion=" + _packageVersionOpt.Value());
}
if (_fileOpt.HasValue())
{
var filePath = _fileOpt.Value();
if (!Path.IsPathRooted(filePath))
{
filePath = Path.GetFullPath(filePath);
}
args.Add("-p:DependencyVersionsFile=" + filePath);
}
if (Reporter.IsVerbose)
{
args.Add("-v:n");
}
return RunDotnet(args, RepoPath);
}
}
}

Просмотреть файл

@ -12,10 +12,36 @@ namespace KoreBuild.Console.Commands
{
application.FullName = "korebuild";
application.Command("install-tools", new InstallToolsCommand().Configure, throwOnUnexpectedArg:false);
application.Command("msbuild", new MSBuildCommand().Configure, throwOnUnexpectedArg:false);
application.Command("install-tools", new InstallToolsCommand().Configure, throwOnUnexpectedArg: false);
application.Command("msbuild", new MSBuildCommand().Configure, throwOnUnexpectedArg: false);
application.Command("docker-build", new DockerBuildCommand().Configure, throwOnUnexpectedArg: false);
// Commands that upgrade things
application.Command("upgrade", c =>
{
c.HelpOption("-h|--help");
c.Command("deps", new DependenciesUpgradeCommand().Configure);
c.OnExecute(() =>
{
c.ShowHelp();
return 2;
});
});
// Commands that generate code and files
application.Command("generate", c =>
{
c.HelpOption("-h|--help");
c.Command("deps", new DependenciesGenerateCommand().Configure);
c.OnExecute(() =>
{
c.ShowHelp();
return 2;
});
});
application.VersionOption("--version", GetVersion);
base.Configure(application);

Просмотреть файл

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
@ -23,8 +24,20 @@ namespace KoreBuild.Console.Commands
private CommandOption _verbose;
private string _koreBuildDir;
private CommandOption _korebuildOverrideOpt;
public string KoreBuildDir
{
get
{
if (_koreBuildDir == null)
{
_koreBuildDir = FindKoreBuildDirectory();
}
return _koreBuildDir;
}
}
public string KoreBuildDir => _koreBuildDir;
public string ConfigDirectory => Path.Combine(KoreBuildDir, "config");
public string RepoPath => _repoPathOption.HasValue() ? _repoPathOption.Value() : Directory.GetCurrentDirectory();
public string DotNetHome => GetDotNetHome();
@ -35,10 +48,12 @@ namespace KoreBuild.Console.Commands
public override void Configure(CommandLineApplication application)
{
_koreBuildDir = FindKoreBuildDirectory();
base.Configure(application);
_korebuildOverrideOpt = application.Option("--korebuild-override <PATH>", "Where is KoreBuild?", CommandOptionType.SingleValue);
// for local development only
_korebuildOverrideOpt.ShowInHelpText = false;
_verbose = application.Option("-v|--verbose", "Show verbose output", CommandOptionType.NoValue);
_toolsSourceOption = application.Option("--tools-source", "The source to draw tools from.", CommandOptionType.SingleValue);
_repoPathOption = application.Option("--repo-path", "The path to the repo to work on.", CommandOptionType.SingleValue);
@ -57,16 +72,29 @@ namespace KoreBuild.Console.Commands
return base.IsValid();
}
protected int RunDotnet(string[] arugments)
protected int RunDotnet(params string[] arguments)
=> RunDotnet(arguments, Directory.GetCurrentDirectory());
protected int RunDotnet(IEnumerable<string> arguments, string workingDir)
{
var args = ArgumentEscaper.EscapeAndConcatenate(arugments);
var args = ArgumentEscaper.EscapeAndConcatenate(arguments);
// use the dotnet.exe file used to start this process
var dotnet = DotNetMuxer.MuxerPath;
// if it could not be found, fallback to detecting DOTNET_HOME or PATH
dotnet = string.IsNullOrEmpty(dotnet) || !Path.IsPathRooted(dotnet)
? GetDotNetExecutable()
: dotnet;
var psi = new ProcessStartInfo
{
FileName = GetDotNetExecutable(),
Arguments = args
FileName = dotnet,
Arguments = args,
WorkingDirectory = workingDir,
};
Reporter.Verbose($"Executing '{psi.FileName} {psi.Arguments}'");
var process = Process.Start(psi);
process.WaitForExit();
@ -116,6 +144,11 @@ namespace KoreBuild.Console.Commands
private string FindKoreBuildDirectory()
{
if (_korebuildOverrideOpt.HasValue())
{
return Path.GetFullPath(_korebuildOverrideOpt.Value());
}
var executingDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var root = Directory.GetDirectoryRoot(executingDir);
while (executingDir != root)

Просмотреть файл

@ -9,5 +9,6 @@ namespace Microsoft.Extensions.Tools.Internal
void Output(string message);
void Warn(string message);
void Error(string message);
bool IsVerbose { get; }
}
}