2015-11-21 03:00:56 +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.Runtime.InteropServices;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2015-11-28 00:19:54 +00:00
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
2016-02-03 18:57:25 +00:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2015-11-21 03:00:56 +00:00
|
|
|
|
using NuGet.Frameworks;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Run
|
|
|
|
|
{
|
2016-01-31 05:47:50 +00:00
|
|
|
|
public partial class RunCommand
|
2015-11-21 03:00:56 +00:00
|
|
|
|
{
|
|
|
|
|
public string Framework = null;
|
|
|
|
|
public string Configuration = null;
|
|
|
|
|
public string Project = null;
|
|
|
|
|
public IReadOnlyList<string> Args = null;
|
|
|
|
|
|
|
|
|
|
ProjectContext _context;
|
|
|
|
|
List<string> _args;
|
|
|
|
|
|
|
|
|
|
public int Start()
|
|
|
|
|
{
|
2015-11-24 00:31:27 +00:00
|
|
|
|
if (IsInteractive())
|
2015-11-21 03:00:56 +00:00
|
|
|
|
{
|
|
|
|
|
return RunInteractive(Project);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return RunExecutable();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-24 00:31:27 +00:00
|
|
|
|
private bool IsInteractive()
|
2015-11-21 03:00:56 +00:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(Project))
|
|
|
|
|
{
|
2015-11-24 00:31:27 +00:00
|
|
|
|
if (File.Exists(Project) && (Path.GetExtension(Project).ToLowerInvariant() == ".csx"))
|
2015-11-21 03:00:56 +00:00
|
|
|
|
{
|
2015-11-24 00:31:27 +00:00
|
|
|
|
return true;
|
2015-11-21 03:00:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-24 00:31:27 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CalculateDefaultsForNonAssigned()
|
|
|
|
|
{
|
2015-11-24 03:58:11 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(Project))
|
|
|
|
|
{
|
|
|
|
|
Project = Directory.GetCurrentDirectory();
|
|
|
|
|
}
|
2015-11-21 03:00:56 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Configuration))
|
|
|
|
|
{
|
|
|
|
|
Configuration = Constants.DefaultConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-03 18:57:25 +00:00
|
|
|
|
var rids = PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers();
|
2016-02-19 23:12:04 +00:00
|
|
|
|
|
2015-11-21 03:00:56 +00:00
|
|
|
|
if (Framework == null)
|
|
|
|
|
{
|
2016-02-03 18:57:25 +00:00
|
|
|
|
var defaultFrameworks = new[]
|
|
|
|
|
{
|
|
|
|
|
FrameworkConstants.FrameworkIdentifiers.DnxCore,
|
|
|
|
|
FrameworkConstants.FrameworkIdentifiers.NetStandardApp,
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-19 23:12:04 +00:00
|
|
|
|
var contexts = ProjectContext.CreateContextForEachFramework(Project, null);
|
2016-02-03 18:57:25 +00:00
|
|
|
|
|
2016-02-19 23:12:04 +00:00
|
|
|
|
ProjectContext context;
|
|
|
|
|
if (contexts.Count() == 1)
|
|
|
|
|
{
|
|
|
|
|
context = contexts.Single();
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-02-03 18:57:25 +00:00
|
|
|
|
{
|
2016-02-19 23:12:04 +00:00
|
|
|
|
context = contexts.FirstOrDefault(c => defaultFrameworks.Contains(c.TargetFramework.Framework));
|
|
|
|
|
if (context == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Couldn't find target to run. Defaults: {string.Join(", ", defaultFrameworks)}");
|
|
|
|
|
}
|
2016-02-03 18:57:25 +00:00
|
|
|
|
}
|
2016-02-19 23:12:04 +00:00
|
|
|
|
|
|
|
|
|
_context = context.CreateRuntimeContext(rids);
|
2015-11-21 03:00:56 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-03 18:57:25 +00:00
|
|
|
|
_context = ProjectContext.Create(Project, NuGetFramework.Parse(Framework), rids);
|
2015-11-21 03:00:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Args == null)
|
|
|
|
|
{
|
|
|
|
|
_args = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_args = new List<string>(Args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int RunExecutable()
|
|
|
|
|
{
|
2015-11-24 03:58:11 +00:00
|
|
|
|
CalculateDefaultsForNonAssigned();
|
|
|
|
|
|
2015-11-21 03:00:56 +00:00
|
|
|
|
// Compile to that directory
|
2016-02-03 20:04:09 +00:00
|
|
|
|
var result = Build.BuildCommand.Run(new[]
|
2016-02-05 03:12:02 +00:00
|
|
|
|
{
|
|
|
|
|
$"--framework",
|
|
|
|
|
$"{_context.TargetFramework}",
|
|
|
|
|
$"--configuration",
|
2016-02-03 18:57:25 +00:00
|
|
|
|
Configuration,
|
2016-02-05 03:12:02 +00:00
|
|
|
|
$"{_context.ProjectFile.ProjectDirectory}"
|
|
|
|
|
});
|
2015-11-21 03:00:56 +00:00
|
|
|
|
|
2016-02-03 20:04:09 +00:00
|
|
|
|
if (result != 0)
|
2015-11-21 03:00:56 +00:00
|
|
|
|
{
|
2016-02-03 20:04:09 +00:00
|
|
|
|
return result;
|
2015-11-21 03:00:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Now launch the output and give it the results
|
2016-02-03 18:57:25 +00:00
|
|
|
|
var outputName = _context.GetOutputPaths(Configuration).RuntimeFiles.Executable;
|
2016-01-26 14:39:13 +00:00
|
|
|
|
|
2015-11-21 03:00:56 +00:00
|
|
|
|
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
if (_context.TargetFramework.IsDesktop())
|
|
|
|
|
{
|
|
|
|
|
// Run mono if we're running a desktop target on non windows
|
2016-01-26 14:39:13 +00:00
|
|
|
|
_args.Insert(0, outputName);
|
2015-11-21 03:00:56 +00:00
|
|
|
|
|
|
|
|
|
if (string.Equals(Configuration, "Debug", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// If we're compiling for the debug configuration then add the --debug flag
|
|
|
|
|
// other options may be passed using the MONO_OPTIONS env var
|
|
|
|
|
_args.Insert(0, "--debug");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outputName = "mono";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 22:04:04 +00:00
|
|
|
|
result = Command.Create(outputName, _args)
|
2015-11-21 03:00:56 +00:00
|
|
|
|
.ForwardStdOut()
|
|
|
|
|
.ForwardStdErr()
|
2016-02-03 20:04:09 +00:00
|
|
|
|
.Execute()
|
|
|
|
|
.ExitCode;
|
2015-12-14 06:40:11 +00:00
|
|
|
|
|
2016-02-03 20:04:09 +00:00
|
|
|
|
return result;
|
2015-11-21 03:00:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int RunInteractive(string scriptName)
|
|
|
|
|
{
|
2016-01-31 05:47:50 +00:00
|
|
|
|
var command = Command.CreateDotNet($"repl-csi", new [] {scriptName})
|
2015-11-21 03:00:56 +00:00
|
|
|
|
.ForwardStdOut()
|
|
|
|
|
.ForwardStdErr();
|
|
|
|
|
var result = command.Execute();
|
|
|
|
|
return result.ExitCode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|