add dotnet add p2p command
This commit is contained in:
parent
e4ccc0ff9b
commit
ca33d98531
3 changed files with 210 additions and 12 deletions
|
@ -10,14 +10,218 @@ using System.Linq;
|
|||
using Microsoft.Build.Construction;
|
||||
using Microsoft.DotNet.ProjectJsonMigration;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Ref
|
||||
namespace Microsoft.DotNet.Tools.Add.ProjectToProjectReference
|
||||
{
|
||||
public class RefCommand
|
||||
public class AddProjectToProjectReferenceCommand
|
||||
{
|
||||
public static int Run(string[] args)
|
||||
{
|
||||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false)
|
||||
{
|
||||
Name = "dotnet add p2p",
|
||||
FullName = ".NET Add Project to Project (p2p) reference Command",
|
||||
Description = "Command to add project to project (p2p) reference"
|
||||
};
|
||||
app.HelpOption("-h|--help");
|
||||
|
||||
CommandArgument projectArgument = app.Argument("<PROJECT>",
|
||||
"The MSBuild project file to modify. If a project file is not specified," +
|
||||
" it searches the current working directory for an MSBuild file that has a file extension that ends in `proj` and uses that file.");
|
||||
|
||||
CommandOption frameworkOption = app.Option("-f|--framework <FRAMEWORK>", "Add reference only when targetting a specific framework", CommandOptionType.SingleValue);
|
||||
CommandOption referenceOption = app.Option("-r|--reference <REFERENCE>", "Add project to project <REFERENCE> to <PROJECT>", CommandOptionType.MultipleValue);
|
||||
|
||||
|
||||
app.OnExecute(() => {
|
||||
ProjectRootElement project = projectArgument.Value != null ?
|
||||
GetProjectFromFileOrThrow(projectArgument.Value) :
|
||||
GetProjectFromCurrentDirectoryOrThrow();
|
||||
|
||||
if (referenceOption.Values.Count == 0)
|
||||
{
|
||||
throw new GracefulException("You must specify at least one reference to add.");
|
||||
}
|
||||
|
||||
AddProjectToProjectReference(project, frameworkOption.Value(), referenceOption.Values);
|
||||
|
||||
//project.Save();
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
return app.Execute(args);
|
||||
}
|
||||
catch (GracefulException e)
|
||||
{
|
||||
Reporter.Error.WriteLine(e.Message);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// There is ProjectRootElement.TryOpen but it does not work as expected
|
||||
// I.e. it returns null for some valid projects
|
||||
public static ProjectRootElement TryOpenProject(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
return ProjectRootElement.Open(filename);
|
||||
}
|
||||
catch (Microsoft.Build.Exceptions.InvalidProjectFileException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ProjectRootElement GetProjectFromFileOrThrow(string filename)
|
||||
{
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
throw new GracefulException($"Provided project `{filename}` does not exist.");
|
||||
}
|
||||
|
||||
var project = TryOpenProject(filename);
|
||||
if (project == null)
|
||||
{
|
||||
throw new GracefulException($"Invalid MSBuild project `{filename}`.");
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
public static ProjectRootElement GetProjectFromCurrentDirectoryOrThrow()
|
||||
{
|
||||
DirectoryInfo currDir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||
FileInfo[] files = currDir.GetFiles("*proj");
|
||||
if (files.Length == 0)
|
||||
{
|
||||
throw new GracefulException("Could not find any MSBuild project in the current directory.");
|
||||
}
|
||||
|
||||
if (files.Length > 1)
|
||||
{
|
||||
throw new GracefulException("Found more than one MSBuild project in the current directory. Please specify which one to use.");
|
||||
}
|
||||
|
||||
FileInfo projectFile = files.First();
|
||||
|
||||
if (!projectFile.Exists)
|
||||
{
|
||||
throw new GracefulException("Could not find any project in the current directory.");
|
||||
}
|
||||
|
||||
var ret = TryOpenProject(projectFile.FullName);
|
||||
if (ret == null)
|
||||
{
|
||||
throw new GracefulException($"Found an MSBuild project `{projectFile.FullName}` in the current directory but it is invalid.");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static Func<T, bool> AndPred<T>(params Func<T, bool>[] preds)
|
||||
{
|
||||
return (el) => preds.All((pred) => pred == null || pred(el));
|
||||
}
|
||||
|
||||
public static string GetFrameworkConditionString(string framework)
|
||||
{
|
||||
return $" '$(TargetFramework)' == '{framework}' ";
|
||||
}
|
||||
|
||||
public static Func<T, bool> FrameworkPred<T>(string framework) where T : ProjectElement
|
||||
{
|
||||
if (string.IsNullOrEmpty(framework))
|
||||
{
|
||||
return (ig) => {
|
||||
var condChain = ig.ConditionChain();
|
||||
return condChain.Count == 0;
|
||||
};
|
||||
}
|
||||
|
||||
string conditionStr = GetFrameworkConditionString(framework).Trim();
|
||||
return (ig) => {
|
||||
var condChain = ig.ConditionChain();
|
||||
return condChain.Count == 1 && condChain.First().Trim() == conditionStr;
|
||||
};
|
||||
}
|
||||
|
||||
public static Func<ProjectItemGroupElement, bool> UniformItemElementTypePred(string projectItemElementType)
|
||||
{
|
||||
return (ig) => ig.Items.All((it) => it.ItemType == projectItemElementType);
|
||||
}
|
||||
|
||||
public static Func<ProjectItemElement, bool> IncludePred(string include)
|
||||
{
|
||||
return (it) => it.Include == include;
|
||||
}
|
||||
|
||||
public static ProjectItemElement[] FindExistingItemsWithCondition(ProjectRootElement root, string framework, string include)
|
||||
{
|
||||
return root.Items
|
||||
.Where(
|
||||
AndPred(
|
||||
FrameworkPred<ProjectItemElement>(framework),
|
||||
IncludePred(include)))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public static ProjectItemGroupElement FindExistingUniformItemGroupWithCondition(ProjectRootElement root, string projectItemElementType, string framework)
|
||||
{
|
||||
return root.ItemGroupsReversed
|
||||
.FirstOrDefault(
|
||||
AndPred(
|
||||
// When adding more predicates which operate on ItemGroup.Condition
|
||||
// some slightly more advanced logic need to be used:
|
||||
// i.e. ConditionPred(FrameworkConditionPred(framework), RuntimeConditionPred(runtime))
|
||||
// FrameworkConditionPred and RuntimeConditionPred would need to operate on a single condition
|
||||
// and ConditionPred would need to check if whole Condition Chain is satisfied
|
||||
FrameworkPred<ProjectItemGroupElement>(framework),
|
||||
UniformItemElementTypePred(projectItemElementType)));
|
||||
}
|
||||
|
||||
public static ProjectItemGroupElement FindUniformOrCreateItemGroupWithCondition(ProjectRootElement root, string projectItemElementType, string framework)
|
||||
{
|
||||
var lastMatchingItemGroup = FindExistingUniformItemGroupWithCondition(root, projectItemElementType, framework);
|
||||
|
||||
if (lastMatchingItemGroup != null)
|
||||
{
|
||||
return lastMatchingItemGroup;
|
||||
}
|
||||
|
||||
ProjectItemGroupElement ret = root.CreateItemGroupElement();
|
||||
ret.Condition = GetFrameworkConditionString(framework);
|
||||
root.AppendChild(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void AddProjectToProjectReference(ProjectRootElement root, string framework, IEnumerable<string> refs)
|
||||
{
|
||||
const string ProjectItemElementType = "ProjectReference";
|
||||
|
||||
ProjectItemGroupElement ig = null;
|
||||
foreach (var @ref in refs)
|
||||
{
|
||||
if (FindExistingItemsWithCondition(root, framework, @ref).Length == 0)
|
||||
{
|
||||
Reporter.Output.WriteLine($"Item {ProjectItemElementType} including `{@ref}` is already present.");
|
||||
continue;
|
||||
}
|
||||
|
||||
ig = ig ?? FindUniformOrCreateItemGroupWithCondition(root, ProjectItemElementType, framework);
|
||||
ig.AppendChild(root.CreateItemElement(ProjectItemElementType, @ref));
|
||||
|
||||
Reporter.Output.WriteLine($"Item {ProjectItemElementType} including `{@ref}` added to project.");
|
||||
}
|
||||
}
|
||||
|
||||
public static int Run2(string[] args)
|
||||
{
|
||||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
DirectoryInfo currDir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||
FileInfo projectFile = currDir.GetFiles("*.csproj").FirstOrDefault();
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ using System.Linq;
|
|||
using Microsoft.Build.Construction;
|
||||
using Microsoft.DotNet.ProjectJsonMigration;
|
||||
using NuGet.Frameworks;
|
||||
using Microsoft.DotNet.Tools.Add.ProjectToProjectReference;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Add
|
||||
{
|
||||
|
@ -27,7 +28,7 @@ Commands:
|
|||
|
||||
private static Dictionary<string, Func<string[], int>> s_builtIns = new Dictionary<string, Func<string[], int>>
|
||||
{
|
||||
["p2p"] = null,
|
||||
["p2p"] = AddProjectToProjectReferenceCommand.Run,
|
||||
};
|
||||
|
||||
public static int Run(string[] args)
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>1.0.0-preview4</VersionPrefix>
|
||||
<TargetFramework>netcoreapp1.0</TargetFramework>
|
||||
|
@ -9,15 +8,13 @@
|
|||
<AssemblyOriginatorKeyFile>../../tools/Key.snk</AssemblyOriginatorKeyFile>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
|
||||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dotnet5.4</PackageTargetFallback>
|
||||
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">$(PackageTargetFallback);dotnet5.4</PackageTargetFallback>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="**\*.cs" Exclude="commands\dotnet-new\CSharp_Console\**;commands\dotnet-new\CSharp_Lib\**;commands\dotnet-new\CSharp_Mstest\**;commands\dotnet-new\CSharp_Web\**;commands\dotnet-new\CSharp_Xunittest\**;bin\**;obj\**;**\*.xproj;packages\**" />
|
||||
<EmbeddedResource Include="**\*.resx" Exclude="bin\**;obj\**;**\*.xproj;packages\**" />
|
||||
<EmbeddedResource Include="commands\dotnet-new\CSharp_Console.zip;commands\dotnet-new\CSharp_Lib.zip;commands\dotnet-new\CSharp_Mstest.zip;commands\dotnet-new\CSharp_Xunittest.zip;commands\dotnet-new\CSharp_Web.zip;compiler\resources\**\*" Exclude="bin\**;obj\**;**\*.xproj;packages\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../Microsoft.DotNet.Configurer/Microsoft.DotNet.Configurer.csproj" />
|
||||
<ProjectReference Include="../Microsoft.DotNet.ProjectJsonMigration/Microsoft.DotNet.ProjectJsonMigration.csproj" />
|
||||
|
@ -25,7 +22,6 @@
|
|||
<ProjectReference Include="../Microsoft.DotNet.Archive/Microsoft.DotNet.Archive.csproj" />
|
||||
<ProjectReference Include="../Microsoft.DotNet.Cli.Utils/Microsoft.DotNet.Cli.Utils.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Sdk">
|
||||
<Version>1.0.0-alpha-20161104-2</Version>
|
||||
|
@ -78,14 +74,11 @@
|
|||
<Version>1.0.1-beta-000933</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
|
||||
<DefineConstants>$(DefineConstants);NETCOREAPP1_0</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
</Project>
|
Loading…
Add table
Reference in a new issue