Enable commands to be shipped in packages

This commit is contained in:
piotrp 2015-11-28 00:28:45 -08:00
parent bfc4d46bee
commit 976393ab0a
10 changed files with 139 additions and 27 deletions

View file

@ -8,6 +8,9 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.ProjectModel;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils namespace Microsoft.DotNet.Cli.Utils
{ {
@ -43,19 +46,26 @@ namespace Microsoft.DotNet.Cli.Utils
}; };
} }
public static Command Create(string executable, IEnumerable<string> args) public static Command Create(string executable, IEnumerable<string> args, NuGetFramework framework = null)
{ {
return Create(executable, string.Join(" ", args)); return Create(executable, string.Join(" ", args), framework);
} }
public static Command Create(string executable, string args) public static Command Create(string executable, string args, NuGetFramework framework = null)
{ {
ResolveExecutablePath(ref executable, ref args); ResolveExecutablePath(ref executable, ref args, framework);
return new Command(executable, args); return new Command(executable, args);
} }
private static void ResolveExecutablePath(ref string executable, ref string args) private static void ResolveExecutablePath(ref string executable, ref string args, NuGetFramework framework = null)
{
executable =
ResolveExecutablePathFromProject(executable, framework) ??
ResolveExecutableFromPath(executable, ref args);
}
private static string ResolveExecutableFromPath(string executable, ref string args)
{ {
foreach (string suffix in Constants.RunnableSuffixes) foreach (string suffix in Constants.RunnableSuffixes)
{ {
@ -88,6 +98,43 @@ namespace Microsoft.DotNet.Cli.Utils
args = $"/C \"{executable}\""; args = $"/C \"{executable}\"";
executable = comSpec; executable = comSpec;
} }
return executable;
}
private static string ResolveExecutablePathFromProject(string executable, NuGetFramework framework)
{
if (framework == null) return null;
var projectRootPath = PathUtility.GetProjectRootPath();
if (projectRootPath == null) return null;
var commandName = Path.GetFileNameWithoutExtension(executable);
var projectContext = ProjectContext.Create(projectRootPath, framework);
var commandPackage = projectContext.LibraryManager.GetLibraries()
.Where(l => l.GetType() == typeof (PackageDescription))
.Select(l => l as PackageDescription)
.FirstOrDefault(p =>
{
var fileNames = p.Library.Files
.Select(Path.GetFileName)
.Where(n => Path.GetFileNameWithoutExtension(n) == commandName)
.ToList();
return fileNames.Contains(commandName + FileNameSuffixes.ExeSuffix) &&
fileNames.Contains(commandName + FileNameSuffixes.DynamicLib) &&
fileNames.Contains(commandName + FileNameSuffixes.Deps);
});
if (commandPackage == null) return null;
var commandPath = commandPackage.Library.Files
.First(f => Path.GetFileName(f) == commandName + FileNameSuffixes.ExeSuffix);
return Path.Combine(projectContext.PackagesDirectory, commandPackage.Path, commandPath);
} }
private static bool ShouldUseCmd(string executable) private static bool ShouldUseCmd(string executable)

View file

@ -28,6 +28,9 @@ namespace Microsoft.DotNet.Cli.Utils
public static readonly string DynamicLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" : public static readonly string DynamicLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so"; RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so";
public static readonly string RuntimeIdentifier = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win7-x64" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "osx.10.10-x64" : "ubuntu.14.04-x64";
public static readonly string StaticLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".lib" : ".a" ; public static readonly string StaticLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".lib" : ".a" ;
} }
} }

View file

@ -2,10 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.DotNet.Cli.Utils namespace Microsoft.DotNet.Cli.Utils
{ {

View file

@ -3,6 +3,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Microsoft.DotNet.Tools.Common namespace Microsoft.DotNet.Tools.Common
@ -198,5 +199,43 @@ namespace Microsoft.DotNet.Tools.Common
return GetPathWithBackSlashes(path); return GetPathWithBackSlashes(path);
} }
} }
public static string GetProjectRootPath(string path = null)
{
var directoryPath = Directory.GetCurrentDirectory();
if (path != null)
{
try
{
directoryPath = Path.GetDirectoryName(directoryPath);
}
catch (ArgumentException)
{
throw new ArgumentException("Illegal Characters in Path", nameof(directoryPath));
}
if(!Directory.Exists(directoryPath))
{
throw new ArgumentException($"'{directoryPath}' does not exist.", nameof(directoryPath));
}
}
string projectRootPath = null;
directoryPath = directoryPath ?? Directory.GetCurrentDirectory();
var directory = new DirectoryInfo(directoryPath);
while (projectRootPath == null && directory != null)
{
if (directory.GetFiles("project.json").Any())
projectRootPath = directory.FullName;
directory = directory.GetFiles("global.json").Any() ? null : directory.Parent;
}
return projectRootPath;
}
} }
} }

View file

@ -4,14 +4,13 @@
"shared": "**/*.cs", "shared": "**/*.cs",
"dependencies": { "dependencies": {
"System.AppContext": "4.0.0", "System.AppContext": "4.0.0",
"System.Console": "4.0.0-beta-23504", "System.Console": "4.0.0-beta-23504",
"System.IO.FileSystem": "4.0.1-beta-23504", "System.IO.FileSystem": "4.0.1-beta-23504",
"System.Threading": "4.0.11-beta-23504", "System.Threading": "4.0.11-beta-23504",
"System.Diagnostics.Process": "4.1.0-beta-23504", "System.Diagnostics.Process": "4.1.0-beta-23504",
"System.Runtime.InteropServices.RuntimeInformation": "4.0.0-beta-23504", "System.Runtime.InteropServices.RuntimeInformation": "4.0.0-beta-23504",
"Microsoft.DotNet.ProjectModel": "1.0.0"
"Microsoft.DotNet.ProjectModel": { "target": "project" }
}, },
"frameworks": { "frameworks": {
"dnxcore50": { } "dnxcore50": { }

View file

@ -3,12 +3,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Dnx.Runtime.Common.CommandLine;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli namespace Microsoft.DotNet.Cli
{ {
@ -31,6 +28,8 @@ Common Commands:
public static int Main(string[] args) public static int Main(string[] args)
{ {
DebugHelper.HandleDebugSwitch(ref args);
// CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA. // CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.
var verbose = false; var verbose = false;
var success = true; var success = true;
@ -71,7 +70,7 @@ Common Commands:
return RunHelpCommand(appArgs); return RunHelpCommand(appArgs);
} }
return Command.Create("dotnet-" + command, appArgs) return Command.Create("dotnet-" + command, appArgs, new NuGetFramework("DNXCore", Version.Parse("5.0")))
.EnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString()) .EnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString())
.EnvironmentVariable(CommandContext.Variables.AnsiPassThru, bool.TrueString) .EnvironmentVariable(CommandContext.Variables.AnsiPassThru, bool.TrueString)
.ForwardStdErr() .ForwardStdErr()

View file

@ -0,0 +1,9 @@
namespace Microsoft.DotNet.ProjectModel
{
public static class DirectoryNames
{
public const string Bin = "bin";
public const string Obj = "obj";
}
}

View file

@ -0,0 +1,21 @@
using System.Runtime.InteropServices;
namespace Microsoft.DotNet.ProjectModel
{
public static class FileNameSuffixes
{
public static readonly string DynamicLib =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so";
public static readonly string ExeSuffix =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : string.Empty;
public const string ProgramDatabase = ".pdb";
public static readonly string StaticLib =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".lib" : ".a" ;
public static readonly string Deps = ".deps";
}
}

View file

@ -4,17 +4,15 @@
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" /> <Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>303677d5-7312-4c3f-baee-beb1a9bd9fe6</ProjectGuid> <ProjectGuid>303677d5-7312-4c3f-baee-beb1a9bd9fe6</ProjectGuid>
<RootNamespace>Microsoft.Extensions.ProjectModel</RootNamespace> <RootNamespace>Microsoft.DotNet.ProjectModel</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath> <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath> <OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" /> <Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project> </Project>

View file

@ -2,17 +2,16 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following // General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information // set of attributes. Change these attribute values to modify the information
// associated with an assembly. // associated with an assembly.
[assembly: AssemblyTitle("Microsoft.Extensions.ProjectModel")] [assembly: AssemblyTitle("Microsoft.DotNet.ProjectModel")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Microsoft.Extensions.ProjectModel")] [assembly: AssemblyProduct("Microsoft.DotNet.ProjectModel")]
[assembly: AssemblyCopyright("Copyright © 2015")] [assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]