From 92f20068e71ccc6284ede520940c0b6facfb22ff Mon Sep 17 00:00:00 2001 From: David Fowler Date: Tue, 6 Oct 2015 02:19:27 -0700 Subject: [PATCH] Execute the known commands - Added dotnet.sh and dotnet.cmd for testing - Still uses dnx and dnu for bootstrapping --- scripts/dotnet.cmd | 8 +++ scripts/dotnet.sh | 14 +++++ src/Microsoft.DotNet.Cli/Program.cs | 83 ++++++++++++++++++++--------- 3 files changed, 80 insertions(+), 25 deletions(-) create mode 100644 scripts/dotnet.cmd create mode 100644 scripts/dotnet.sh diff --git a/scripts/dotnet.cmd b/scripts/dotnet.cmd new file mode 100644 index 000000000..ce90beb34 --- /dev/null +++ b/scripts/dotnet.cmd @@ -0,0 +1,8 @@ +@Echo OFF +SETLOCAL +SET ERRORLEVEL= + +dnx %DOTNET_OPTIONS% -p %~dp0..\src\Microsoft.DotNet.Cli run %* + +exit /b %ERRORLEVEL% +ENDLOCAL \ No newline at end of file diff --git a/scripts/dotnet.sh b/scripts/dotnet.sh new file mode 100644 index 000000000..f98fdd7a3 --- /dev/null +++ b/scripts/dotnet.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +# work around restore timeouts on Mono +[ -z "$MONO_THREADS_PER_CPU" ] && export MONO_THREADS_PER_CPU=50 + +exec "dnx" "$DIR/../src/Microsoft.DotNet.Cli" run "$@" \ No newline at end of file diff --git a/src/Microsoft.DotNet.Cli/Program.cs b/src/Microsoft.DotNet.Cli/Program.cs index 6aeb5924f..637edd266 100644 --- a/src/Microsoft.DotNet.Cli/Program.cs +++ b/src/Microsoft.DotNet.Cli/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; -using System.Threading.Tasks; +using System.Diagnostics; +using System.Linq; using Microsoft.Dnx.Runtime.Common.CommandLine; namespace Microsoft.DotNet.Cli @@ -9,62 +10,49 @@ namespace Microsoft.DotNet.Cli { public static int Main(string[] args) { - // TODO: Use System.CommandLine var app = new CommandLineApplication(); app.Name = "dotnet"; app.Description = "The .NET CLI"; - app.HelpOption("-?|-h|--help"); - + // Most commonly used commands app.Command("init", c => { c.Description = "Scaffold a basic .NET application"; - c.HelpOption("-?|-h|--help"); + c.OnExecute(() => Exec("dotnet-init", c.RemainingArguments)); }); app.Command("compile", c => { c.Description = "Produce assemblies for the project in given directory"; - var optionFramework = c.Option("--framework ", "A list of target frameworks to build.", CommandOptionType.MultipleValue); - var optionOut = c.Option("--out ", "Output directory", CommandOptionType.SingleValue); - var optionQuiet = c.Option("--quiet", "Do not show output such as dependencies in use", - CommandOptionType.NoValue); - var argProjectDir = c.Argument( - "[projects]", - "One or more projects build. If not specified, the project in the current directory will be used.", - multipleValues: true); - c.HelpOption("-?|-h|--help"); + c.OnExecute(() => + { + // Temporary! + return Exec("dnu", new[] { "build" }); + // Exec("dotnet-compile", c.RemainingArguments); + }); }); app.Command("restore", c => { c.Description = "Restore packages"; - var argRoot = c.Argument("[project]", - "List of projects and project folders to restore. Each value can be: a path to a project.json or global.json file, or a folder to recursively search for project.json files.", - multipleValues: true); - - var optRuntimes = c.Option("--runtime ", - "List of runtime identifiers to restore for", - CommandOptionType.MultipleValue); - - c.HelpOption("-?|-h|--help"); + c.OnExecute(() => Exec("dotnet-restore", c.RemainingArguments)); }); app.Command("pack", c => { c.Description = "Produce a NuGet package"; - c.HelpOption("-?|-h|--help"); + c.OnExecute(() => Exec("dotnet-pack", c.RemainingArguments)); }); app.Command("publish", c => { c.Description = "Produce deployable assets"; - c.HelpOption("-?|-h|--help"); + c.OnExecute(() => Exec("dotnet-publish", c.RemainingArguments)); }); app.OnExecute(() => @@ -76,5 +64,50 @@ namespace Microsoft.DotNet.Cli return app.Execute(args); } + + private static int Exec(string executable, IList remainingArguments) + { + var comSpec = Environment.GetEnvironmentVariable("ComSpec"); + if (!string.IsNullOrEmpty(comSpec)) + { + remainingArguments = + new[] { "/C", "\"", executable } + .Concat(remainingArguments) + .Concat(new[] { "\"" }) + .ToArray(); + executable = comSpec; + } + + var psi = new ProcessStartInfo + { + FileName = executable, + Arguments = string.Join(" ", remainingArguments), + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardError = true, + RedirectStandardOutput = true + }; + + var process = Process.Start(psi); + process.ErrorDataReceived += OnProcessErrorDataReceived; + process.OutputDataReceived += OnProcessOutputDataReceived; + + process.BeginErrorReadLine(); + process.BeginOutputReadLine(); + + process.WaitForExit(); + + return process.ExitCode; + } + + private static void OnProcessOutputDataReceived(object sender, DataReceivedEventArgs e) + { + Console.WriteLine(e.Data); + } + + private static void OnProcessErrorDataReceived(object sender, DataReceivedEventArgs e) + { + Console.Error.WriteLine(e.Data); + } } }