dotnet-installer/src/dotnet/commands/dotnet-run/RunCommand.cs

193 lines
6.3 KiB
C#
Raw Normal View History

// 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;
using Microsoft.DotNet.ProjectModel;
2016-02-03 18:57:25 +00:00
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Run
{
public partial class RunCommand
{
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()
{
if (IsInteractive())
{
return RunInteractive(Project);
}
else
{
return RunExecutable();
}
}
private bool IsInteractive()
{
if (!string.IsNullOrEmpty(Project))
{
if (File.Exists(Project) && (Path.GetExtension(Project).ToLowerInvariant() == ".csx"))
{
return true;
}
}
return false;
}
private void CalculateDefaultsForNonAssigned()
{
if (string.IsNullOrWhiteSpace(Project))
{
Project = Directory.GetCurrentDirectory();
}
if (string.IsNullOrWhiteSpace(Configuration))
{
Configuration = Constants.DefaultConfiguration;
}
2016-02-03 18:57:25 +00:00
var rids = PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers();
if (Framework == null)
{
2016-02-03 18:57:25 +00:00
var defaultFrameworks = new[]
{
2016-04-07 18:51:41 +00:00
FrameworkConstants.FrameworkIdentifiers.NetCoreApp,
2016-02-03 18:57:25 +00:00
FrameworkConstants.FrameworkIdentifiers.NetStandardApp,
};
var contexts = ProjectContext.CreateContextForEachFramework(Project, null);
2016-02-03 18:57:25 +00:00
ProjectContext context;
if (contexts.Count() == 1)
{
context = contexts.Single();
}
else
2016-02-03 18:57:25 +00:00
{
context = contexts.FirstOrDefault(c => defaultFrameworks.Contains(c.TargetFramework.Framework));
if (context == null)
{
2016-02-24 18:49:00 +00:00
throw new InvalidOperationException($"Couldn't find target to run. Possible causes:" + Environment.NewLine +
"1. No project.lock.json file or restore failed - run `dotnet restore`" + Environment.NewLine +
$"2. project.lock.json has multiple targets none of which is in default list ({string.Join(", ", defaultFrameworks)})");
}
2016-02-03 18:57:25 +00:00
}
_context = context.CreateRuntimeContext(rids);
}
else
{
2016-02-03 18:57:25 +00:00
_context = ProjectContext.Create(Project, NuGetFramework.Parse(Framework), rids);
}
if (Args == null)
{
_args = new List<string>();
}
else
{
_args = new List<string>(Args);
}
}
private int RunExecutable()
{
CalculateDefaultsForNonAssigned();
// Compile to that directory
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}"
});
if (result != 0)
{
return result;
}
2016-04-08 03:50:44 +00:00
List<string> hostArgs = new List<string>();
if (!_context.TargetFramework.IsDesktop())
{
// Add Nuget Packages Probing Path
var nugetPackagesRoot = _context.PackagesDirectory;
var probingPathArg = "--additionalprobingpath";
2016-04-08 03:50:44 +00:00
hostArgs.Insert(0, nugetPackagesRoot);
hostArgs.Insert(0, probingPathArg);
}
// Now launch the output and give it the results
var outputPaths = _context.GetOutputPaths(Configuration);
var outputName = outputPaths.RuntimeFiles.Executable;
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (_context.TargetFramework.IsDesktop())
{
// Run mono if we're running a desktop target on non windows
_args.Insert(0, outputName);
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";
}
}
Command command;
if (outputName.EndsWith(FileNameSuffixes.DotNet.DynamicLib, StringComparison.OrdinalIgnoreCase))
{
// The executable is a ".dll", we need to call it through dotnet.exe
var muxer = new Muxer();
2016-04-08 03:50:44 +00:00
command = Command.Create(muxer.MuxerPath, Enumerable.Concat(
Enumerable.Concat(new string[] { "exec" }, hostArgs),
Enumerable.Concat(new string[] { outputName }, _args)));
}
else
{
2016-04-08 03:50:44 +00:00
command = Command.Create(outputName, Enumerable.Concat(hostArgs, _args));
}
result = command
.ForwardStdOut()
.ForwardStdErr()
.Execute()
.ExitCode;
2015-12-14 06:40:11 +00:00
return result;
}
private static int RunInteractive(string scriptName)
{
var command = Command.CreateDotNet($"repl-csi", new[] { scriptName })
.ForwardStdOut()
.ForwardStdErr();
var result = command.Execute();
return result.ExitCode;
}
}
}