Implement 'run3' command which will execute the MSBuild project.
Also create a 'dotnet new -t msbuild' template.
This commit is contained in:
parent
bccff16c18
commit
b068687413
15 changed files with 331 additions and 37 deletions
|
@ -0,0 +1,17 @@
|
|||
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
|
||||
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="**\*.cs" Exclude="$(GlobalExclude)" />
|
||||
<EmbeddedResource Include="**\*.resx" Exclude="$(GlobalExclude)" />
|
||||
<None Include="project.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
12
src/dotnet/commands/dotnet-new/CSharp_MSBuild/Program.cs
Normal file
12
src/dotnet/commands/dotnet-new/CSharp_MSBuild/Program.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace ConsoleApplication
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"dependencies": {},
|
||||
"frameworks": {
|
||||
"netcoreapp1.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"runtimes": {
|
||||
"$currentruntime$": {}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,15 @@
|
|||
// 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 Microsoft.DotNet.Cli.CommandLine;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.IO.Compression;
|
||||
using Microsoft.DotNet.Cli.CommandLine;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.InternalAbstractions;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.New
|
||||
{
|
||||
|
@ -64,11 +65,37 @@ namespace Microsoft.DotNet.Tools.New
|
|||
}
|
||||
}
|
||||
|
||||
archive.ExtractToDirectory(Directory.GetCurrentDirectory());
|
||||
string projectDirectory = Directory.GetCurrentDirectory();
|
||||
|
||||
archive.ExtractToDirectory(projectDirectory);
|
||||
|
||||
string projectJsonFile = Path.Combine(projectDirectory, "project.json");
|
||||
|
||||
File.Move(
|
||||
Path.Combine(Directory.GetCurrentDirectory(), "project.json.template"),
|
||||
Path.Combine(Directory.GetCurrentDirectory(), "project.json"));
|
||||
Path.Combine(projectDirectory, "project.json.template"),
|
||||
projectJsonFile);
|
||||
|
||||
string originalProjectJsonText = File.ReadAllText(projectJsonFile);
|
||||
string replacedProjectJsonText = originalProjectJsonText
|
||||
.Replace("$currentruntime$", RuntimeEnvironment.GetRuntimeIdentifier());
|
||||
|
||||
if (replacedProjectJsonText != originalProjectJsonText)
|
||||
{
|
||||
File.WriteAllText(projectJsonFile, replacedProjectJsonText);
|
||||
}
|
||||
|
||||
string projectName = new DirectoryInfo(projectDirectory).Name;
|
||||
foreach (string file in Directory.GetFiles(projectDirectory, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
if (Path.GetFileNameWithoutExtension(file) == "$projectName$")
|
||||
{
|
||||
string extension = Path.GetExtension(file);
|
||||
|
||||
File.Move(
|
||||
file,
|
||||
Path.Combine(Path.GetDirectoryName(file), $"{projectName}{extension}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
|
@ -99,7 +126,7 @@ namespace Microsoft.DotNet.Tools.New
|
|||
app.Description = "Initializes empty project for .NET Platform";
|
||||
app.HelpOption("-h|--help");
|
||||
|
||||
var csharp = new { Name = "C#", Alias = new[] { "c#", "cs", "csharp" }, TemplatePrefix = "CSharp", Templates = new[] { "Console", "Web", "Lib", "xunittest" } };
|
||||
var csharp = new { Name = "C#", Alias = new[] { "c#", "cs", "csharp" }, TemplatePrefix = "CSharp", Templates = new[] { "Console", "Web", "Lib", "xunittest", "MSBuild" } };
|
||||
var fsharp = new { Name = "F#", Alias = new[] { "f#", "fs", "fsharp" }, TemplatePrefix = "FSharp", Templates = new[] { "Console", "Lib" } };
|
||||
|
||||
var languages = new[] { csharp, fsharp };
|
||||
|
|
60
src/dotnet/commands/dotnet-run3/Program.cs
Normal file
60
src/dotnet/commands/dotnet-run3/Program.cs
Normal file
|
@ -0,0 +1,60 @@
|
|||
// 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 Microsoft.DotNet.Cli.CommandLine;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Run
|
||||
{
|
||||
public partial class Run3Command
|
||||
{
|
||||
public static int Run(string[] args)
|
||||
{
|
||||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false);
|
||||
app.Name = "dotnet run3";
|
||||
app.FullName = ".NET Run3 Command";
|
||||
app.Description = "Command used to run .NET apps";
|
||||
app.HandleResponseFiles = true;
|
||||
app.AllowArgumentSeparator = true;
|
||||
app.HelpOption("-h|--help");
|
||||
|
||||
CommandOption configuration = app.Option("-c|--configuration", "Configuration under which to build", CommandOptionType.SingleValue);
|
||||
CommandOption project = app.Option("-p|--project", "The path to the project file to run (defaults to the current directory if there is only one project).", CommandOptionType.SingleValue);
|
||||
|
||||
app.OnExecute(() =>
|
||||
{
|
||||
Run3Command runCmd = new Run3Command();
|
||||
|
||||
runCmd.Configuration = configuration.Value();
|
||||
runCmd.Project = project.Value();
|
||||
runCmd.Args = app.RemainingArguments;
|
||||
|
||||
return runCmd.Start();
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
return app.Execute(args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
#if DEBUG
|
||||
Reporter.Error.WriteLine(ex.ToString());
|
||||
#else
|
||||
if (Reporter.IsVerbose)
|
||||
{
|
||||
Reporter.Error.WriteLine(ex.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
Reporter.Error.WriteLine(ex.Message);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
132
src/dotnet/commands/dotnet-run3/Run3Command.cs
Normal file
132
src/dotnet/commands/dotnet-run3/Run3Command.cs
Normal file
|
@ -0,0 +1,132 @@
|
|||
// 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 Microsoft.Build.Execution;
|
||||
using Microsoft.Build.Framework;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using NuGet.Frameworks;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.Run
|
||||
{
|
||||
public partial class Run3Command
|
||||
{
|
||||
private const string GetRunInformationTaskName = "GetRunInformation";
|
||||
|
||||
public string Configuration { get; set; }
|
||||
public string Project { get; set; }
|
||||
public IReadOnlyList<string> Args { get; set; }
|
||||
|
||||
private readonly ICommandFactory _commandFactory;
|
||||
private List<string> _args;
|
||||
|
||||
public Run3Command()
|
||||
: this(new RunCommandFactory())
|
||||
{
|
||||
}
|
||||
|
||||
public Run3Command(ICommandFactory commandFactory)
|
||||
{
|
||||
_commandFactory = commandFactory;
|
||||
}
|
||||
|
||||
public int Start()
|
||||
{
|
||||
Initialize();
|
||||
|
||||
ITaskItem runInfoItem = GetRunInformation();
|
||||
|
||||
string commandName = runInfoItem.GetMetadata("CommandName");
|
||||
string[] args = runInfoItem.GetMetadata("Args").Split(';');
|
||||
|
||||
ICommand command = _commandFactory.Create(commandName, Enumerable.Concat(args, _args));
|
||||
|
||||
return command
|
||||
.Execute()
|
||||
.ExitCode;
|
||||
}
|
||||
|
||||
private ITaskItem GetRunInformation()
|
||||
{
|
||||
Dictionary<string, string> globalProperties = new Dictionary<string, string>()
|
||||
{
|
||||
{ "MSBuildExtensionsPath", AppContext.BaseDirectory }
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Configuration))
|
||||
{
|
||||
globalProperties.Add("Configuration", Configuration);
|
||||
}
|
||||
|
||||
ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null);
|
||||
|
||||
BuildRequestData buildRequestData = new BuildRequestData(projectInstance, new string[] { GetRunInformationTaskName });
|
||||
BuildParameters buildParameters = new BuildParameters();
|
||||
|
||||
BuildResult result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequestData);
|
||||
|
||||
TargetResult runInfoResult;
|
||||
if (!result.ResultsByTarget.TryGetValue(GetRunInformationTaskName, out runInfoResult))
|
||||
{
|
||||
throw new InvalidOperationException($"Could not find a target named '{GetRunInformationTaskName}' in your project. Please ensure 'dotnet run' supports this project.");
|
||||
}
|
||||
|
||||
if (runInfoResult.ResultCode != TargetResultCode.Success)
|
||||
{
|
||||
throw new InvalidOperationException($"Could not get the run information for project {Project}. An internal MSBuild error has occured" + Environment.NewLine +
|
||||
runInfoResult.Exception?.ToString());
|
||||
}
|
||||
|
||||
ITaskItem runInfoItem = runInfoResult.Items.FirstOrDefault(i => i.ItemSpec == projectInstance.FullPath);
|
||||
if (runInfoItem == null)
|
||||
{
|
||||
throw new InvalidOperationException($"'{GetRunInformationTaskName}' did not return an ITaskItem with the project's FullPath as the ItemSpec.");
|
||||
}
|
||||
|
||||
return runInfoItem;
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Project))
|
||||
{
|
||||
string directory = Directory.GetCurrentDirectory();
|
||||
string[] projectFiles = Directory.GetFiles(directory, "*.*proj");
|
||||
|
||||
if (projectFiles.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Couldn't find a project to run. Ensure a project exists in {directory}." + Environment.NewLine +
|
||||
"Or pass the path to the project using --project");
|
||||
}
|
||||
else if (projectFiles.Length > 1)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Specify which project file to use because this '{directory}' contains more than one project file.");
|
||||
}
|
||||
|
||||
Project = projectFiles[0];
|
||||
}
|
||||
|
||||
if (Args == null)
|
||||
{
|
||||
_args = new List<string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_args = new List<string>(Args);
|
||||
}
|
||||
}
|
||||
|
||||
private class RunCommandFactory : ICommandFactory
|
||||
{
|
||||
public ICommand Create(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration = Constants.DefaultConfiguration)
|
||||
{
|
||||
return Command.Create(commandName, args, framework, configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue