Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Enabling implicit appbase (dnx . run -> dnx run) on Mono
Browse files Browse the repository at this point in the history
The user no longer has to provide folder (or `.`) when running dnx commands.

Note that there might be an ambiguity between a folder and command which is resolved by checking if a folder with the provided name physically exists and if it does the parameter is treated as folder path otherwise it is treated as a command.

Fixes #1403
  • Loading branch information
moozzyk committed Jul 16, 2015
1 parent 677d6c3 commit 70e6b6b
Showing 1 changed file with 53 additions and 44 deletions.
97 changes: 53 additions & 44 deletions src/dnx.mono.managed/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading;
using dnx.host;
Expand Down Expand Up @@ -69,82 +68,92 @@ public static int Main(string[] arguments)
Environment.SetEnvironmentVariable(EnvironmentNames.AppBase, arguments[appbaseIndex + 1]);
}

return RuntimeBootstrapper.Execute(arguments,
return RuntimeBootstrapper.Execute(arguments,
// NOTE(anurse): Mono is always "dnx451" (for now).
new FrameworkName("DNX", new Version(4, 5, 1)));
}

private static string[] ExpandCommandLineArguments(string[] arguments)
{
// If '--appbase' is already given and it has a value, don't need to expand
var appbaseIndex = arguments.ToList().FindIndex(arg =>
string.Equals(arg, "--appbase", StringComparison.OrdinalIgnoreCase));
if (appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1))
var parameterIdx = FindAppBaseOrNonHostOption(arguments);

// no non-bootstrapper parameters found or --appbase was found
if (parameterIdx < 0 || string.Equals(arguments[parameterIdx], "--appbase", StringComparison.OrdinalIgnoreCase))
{
return arguments;
}

var expandedArgs = new List<string>();

// Copy all arguments (options & values) as is before the project.json/assembly path
var pathArgIndex = -1;
while (++pathArgIndex < arguments.Length)
for (var i = 0; i < arguments.Count(); i++)
{
var optionValNum = BootstrapperOptionValueNum(arguments[pathArgIndex]);

// It isn't a bootstrapper option, we treat it as the project.json/assembly path
if (optionValNum < 0)
if (i == parameterIdx)
{
break;
ExpandArgument(arguments[i], expandedArgs);
}

// Copy the option
expandedArgs.Add(arguments[pathArgIndex]);

// Copy the value if the option has one
if (optionValNum > 0 && (++pathArgIndex < arguments.Length))
else
{
expandedArgs.Add(arguments[pathArgIndex]);
expandedArgs.Add(arguments[i]);
}
}

// No path argument was found, no expansion is needed
if (pathArgIndex >= arguments.Length)
{
return arguments;
}
return expandedArgs.ToArray();
}

// Start to expand appbase option from path
private static void ExpandArgument(string argument, List<string> expandedArgs)
{
expandedArgs.Add("--appbase");

var pathArg = arguments[pathArgIndex];
if (pathArg.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) ||
pathArg.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
if (argument.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ||
argument.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
// "dnx /path/App.dll arg1" --> "dnx --appbase /path/ /path/App.dll arg1"
// "dnx /path/App.exe arg1" --> "dnx --appbase /path/ /path/App.exe arg1"
expandedArgs.Add(Path.GetDirectoryName(Path.GetFullPath(pathArg)));
expandedArgs.AddRange(arguments.Skip(pathArgIndex));
expandedArgs.Add(Path.GetDirectoryName(Path.GetFullPath(argument)));
expandedArgs.Add(argument);

return;
}
else

if (argument.Equals(".", StringComparison.Ordinal))
{
// "dnx . run" --> "dnx --appbase . Microsoft.Framework.ApplicationHost run"
expandedArgs.Add(argument);
expandedArgs.Add("Microsoft.Framework.ApplicationHost");
return;
}

if (string.Equals(Path.GetFileName(argument), "project.json", StringComparison.OrdinalIgnoreCase))
{
var fileName = Path.GetFileName(pathArg);
if (string.Equals(fileName, "project.json", StringComparison.OrdinalIgnoreCase))
expandedArgs.Add(Path.GetDirectoryName(Path.GetFullPath(argument)));
expandedArgs.Add("Microsoft.Framework.ApplicationHost");
return;
}

// "dnx run" --> "dnx --appbase . Microsoft.Framework.ApplicationHost run"
expandedArgs.Add(".");
expandedArgs.Add("Microsoft.Framework.ApplicationHost");
expandedArgs.Add(argument);
}

private static int FindAppBaseOrNonHostOption(string[] arguments)
{
for (var i = 0; i < arguments.Length; i++)
{
if (string.Equals(arguments[i], "--appbase", StringComparison.OrdinalIgnoreCase))
{
// "dnx /path/project.json run" --> "dnx --appbase /path/ Microsoft.Framework.ApplicationHost run"
expandedArgs.Add(Path.GetDirectoryName(Path.GetFullPath(pathArg)));
return i;
}
else

var option_arg_count = BootstrapperOptionValueNum(arguments[i]);
if (option_arg_count < 0)
{
// "dnx /path/ run" --> "dnx --appbase /path/ Microsoft.Framework.ApplicationHost run"
expandedArgs.Add(pathArg);
return i;
}

expandedArgs.Add("Microsoft.Framework.ApplicationHost");
expandedArgs.AddRange(arguments.Skip(pathArgIndex + 1));
i += option_arg_count;
}

return expandedArgs.ToArray();
return -1;
}

private static int BootstrapperOptionValueNum(string candidate)
Expand Down

0 comments on commit 70e6b6b

Please sign in to comment.