From 379195a82a6352a1c3d5bd5ff45d0aed7b531a4c Mon Sep 17 00:00:00 2001 From: piotrp Date: Wed, 9 Dec 2015 17:48:40 -0800 Subject: [PATCH] Initial commit of dotnet-test-xunit prototype --- CONTRIBUTING.md | 12 + NuGet.Config | 13 + README.md | 36 + build.ps1 | 40 + build.sh | 60 + dnx.xunit.proj | 90 + dnx.xunit.sln | 41 + dnx.xunit.v2.ncrunchsolution | Bin 0 -> 1684 bytes global.json | 4 + src/dotnet-test-xunit/CommandLine.cs | 299 +++ .../Common/AssemblyExtensions.cs | 26 + .../Common/DictionaryExtensions.cs | 40 + src/dotnet-test-xunit/Common/Guard.cs | 40 + .../Common/TestDiscoveryVisitor.cs | 26 + .../Common/XmlTestExecutionVisitor.cs | 275 +++ src/dotnet-test-xunit/ConsoleRunnerLogger.cs | 80 + .../DesignTime/DesignTimeExecutionVisitor.cs | 103 + .../DesignTime/DesignTimeTestConverter.cs | 112 + .../DesignTime/SourceInformationProvider.cs | 56 + src/dotnet-test-xunit/ParallelismOption.cs | 10 + src/dotnet-test-xunit/Program.cs | 464 ++++ .../Properties/AssemblyInfo.cs | 3 + .../Properties/GlobalAssemblyInfo.cs | 13 + .../Utility/IExecutionVisitor.cs | 11 + .../Utility/StackFrameTransformer.cs | 54 + src/dotnet-test-xunit/Utility/Transform.cs | 12 + .../Utility/TransformFactory.cs | 41 + .../Utility/XmlAggregateVisitor.cs | 54 + .../Visitors/DiagnosticMessageVisitor.cs | 38 + src/dotnet-test-xunit/dotnet-test-xunit.xproj | 16 + src/dotnet-test-xunit/project.json | 43 + .../test.xunit.runner.dnx/CommandLineTests.cs | 716 ++++++ test/test.xunit.runner.dnx/project.json | 10 + .../test.xunit.runner.dnx.xproj | 19 + test/test.xunit.runner.dnx/xunit.runner.json | 3 + tools/dnvm.ps1 | 1911 +++++++++++++++++ tools/dnvm.sh | 1054 +++++++++ tools/dnx-build.ps1 | 24 + tools/dnx-tests.ps1 | 21 + tools/packages.proj | 55 + 40 files changed, 5925 insertions(+) create mode 100644 CONTRIBUTING.md create mode 100644 NuGet.Config create mode 100644 README.md create mode 100644 build.ps1 create mode 100644 build.sh create mode 100644 dnx.xunit.proj create mode 100644 dnx.xunit.sln create mode 100644 dnx.xunit.v2.ncrunchsolution create mode 100644 global.json create mode 100644 src/dotnet-test-xunit/CommandLine.cs create mode 100644 src/dotnet-test-xunit/Common/AssemblyExtensions.cs create mode 100644 src/dotnet-test-xunit/Common/DictionaryExtensions.cs create mode 100644 src/dotnet-test-xunit/Common/Guard.cs create mode 100644 src/dotnet-test-xunit/Common/TestDiscoveryVisitor.cs create mode 100644 src/dotnet-test-xunit/Common/XmlTestExecutionVisitor.cs create mode 100644 src/dotnet-test-xunit/ConsoleRunnerLogger.cs create mode 100644 src/dotnet-test-xunit/DesignTime/DesignTimeExecutionVisitor.cs create mode 100644 src/dotnet-test-xunit/DesignTime/DesignTimeTestConverter.cs create mode 100644 src/dotnet-test-xunit/DesignTime/SourceInformationProvider.cs create mode 100644 src/dotnet-test-xunit/ParallelismOption.cs create mode 100644 src/dotnet-test-xunit/Program.cs create mode 100644 src/dotnet-test-xunit/Properties/AssemblyInfo.cs create mode 100644 src/dotnet-test-xunit/Properties/GlobalAssemblyInfo.cs create mode 100644 src/dotnet-test-xunit/Utility/IExecutionVisitor.cs create mode 100644 src/dotnet-test-xunit/Utility/StackFrameTransformer.cs create mode 100644 src/dotnet-test-xunit/Utility/Transform.cs create mode 100644 src/dotnet-test-xunit/Utility/TransformFactory.cs create mode 100644 src/dotnet-test-xunit/Utility/XmlAggregateVisitor.cs create mode 100644 src/dotnet-test-xunit/Visitors/DiagnosticMessageVisitor.cs create mode 100644 src/dotnet-test-xunit/dotnet-test-xunit.xproj create mode 100644 src/dotnet-test-xunit/project.json create mode 100644 test/test.xunit.runner.dnx/CommandLineTests.cs create mode 100644 test/test.xunit.runner.dnx/project.json create mode 100644 test/test.xunit.runner.dnx/test.xunit.runner.dnx.xproj create mode 100644 test/test.xunit.runner.dnx/xunit.runner.json create mode 100644 tools/dnvm.ps1 create mode 100644 tools/dnvm.sh create mode 100644 tools/dnx-build.ps1 create mode 100644 tools/dnx-tests.ps1 create mode 100644 tools/packages.proj diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..325f1a3 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,12 @@ +# Before you file a bug... + +* Did you [read the documentation](https://xunit.github.io/)? +* Did you search the issues list to see if someone already reported it? +* Did you create a simple repro for the problem? + +# Before you submit a PR... + +* Did you ensure this is an [accepted up-for-grabs issue](https://github.com/xunit/xunit/issues?q=is%3Aopen+is%3Aissue+label%3A%22%5Bs%5D+Up+For+Grabs%22)? (If not, open one to start the discussion) +* Did you read the [project governance](https://xunit.github.io/governance.html)? +* Does the code follow existing coding styles? (spaces, comments, no regions, etc.)? +* Did you write unit tests? diff --git a/NuGet.Config b/NuGet.Config new file mode 100644 index 0000000..f95b8ae --- /dev/null +++ b/NuGet.Config @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9efd408 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +## + +This runner supports [xUnit.net](https://github.com/xunit/xunit) tests for [DNX 4.5.1+, and DNX Core 5+](https://github.com/aspnet/dnx) (this includes [ASP.NET 5+](https://github.com/aspnet)). + +### Usage + +To install this package, ensure your project.json contains the following lines: + +```JSON +{ + "dependencies": { + "xunit": "2.1.0-*", + "xunit.runner.dnx": "2.1.0-*" + }, + "commands": { + "test": "xunit.runner.dnx" + } +} +``` + +To run tests from the command line, use the following. + +```Shell +# Restore NuGet packages +dnu restore + +# Run tests in current directory +dnx test + +# Run tests if tests are not in the current directory +dnx -p path/to/project test +``` + +### More Information + +For more complete example usage, please see [Getting Started with xUnit.net and DNX / ASP.NET 5](http://xunit.github.io/docs/getting-started-dnx.html). diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..f2e45af --- /dev/null +++ b/build.ps1 @@ -0,0 +1,40 @@ +param( + [string]$target = "Test", + [string]$verbosity = "minimal", + [int]$maxCpuCount = 0 +) + +# Kill all MSBUILD.EXE processes because they could very likely have a lock against our +# MSBuild runner from when we last ran unit tests. +get-process -name "msbuild" -ea SilentlyContinue | %{ stop-process $_.ID -force } + +$msbuilds = @(get-command msbuild -ea SilentlyContinue) +if ($msbuilds.Count -gt 0) { + $msbuild = $msbuilds[0].Definition +} +else { + if (test-path "env:\ProgramFiles(x86)") { + $path = join-path ${env:ProgramFiles(x86)} "MSBuild\14.0\bin\MSBuild.exe" + if (test-path $path) { + $msbuild = $path + } + } + if ($msbuild -eq $null) { + $path = join-path $env:ProgramFiles "MSBuild\14.0\bin\MSBuild.exe" + if (test-path $path) { + $msbuild = $path + } + } + if ($msbuild -eq $null) { + throw "MSBuild could not be found in the path. Please ensure MSBuild v14 (from Visual Studio 2015) is in the path." + } +} + +if ($maxCpuCount -lt 1) { + $maxCpuCountText = $Env:MSBuildProcessorCount +} else { + $maxCpuCountText = ":$maxCpuCount" +} + +$allArgs = @("dnx.xunit.proj", "/m$maxCpuCountText", "/nologo", "/verbosity:$verbosity", "/t:$target", $args) +& $msbuild $allArgs diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..5f9f9b2 --- /dev/null +++ b/build.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +if ! [ -x "$(command -v mono)" ]; then + echo >&2 "Could not find 'mono' on the path." + exit 1 +fi + +echo "" +echo "Installing .NET Execution Environment..." +echo "" + +. tools/dnvm.sh +dnvm install 1.0.0-rc2-16177 -r coreclr -u +if [ $? -ne 0 ]; then + echo >&2 ".NET Execution Environment installation has failed." + exit 1 +fi + +dnvm install 1.0.0-rc2-16177 -u +if [ $? -ne 0 ]; then + echo >&2 ".NET Execution Environment installation has failed." + exit 1 +fi + +echo "" +echo "Restoring packages..." +echo "" + +dnvm use 1.0.0-rc2-16177 +dnu restore +if [ $? -ne 0 ]; then + echo >&2 "Package restore has failed." + exit 1 +fi + +echo "" +echo "Building packages..." +echo "" + +dnu pack src/xunit.runner.dnx +if [ $? -ne 0 ]; then + echo >&2 "Build packages has failed." + exit 1 +fi + +echo "" +echo "Running tests..." +echo "" +dnx -p test/test.xunit.runner.dnx test -parallel none +if [ $? -ne 0 ]; then + echo >&2 "Running tests on Mono has failed." + exit 1 +fi + +dnvm use 1.0.0-rc2-16177 -r coreclr +dnx -p test/test.xunit.runner.dnx test -parallel none +if [ $? -ne 0 ]; then + echo >&2 "Running tests on CoreCLR has failed." + exit 1 +fi diff --git a/dnx.xunit.proj b/dnx.xunit.proj new file mode 100644 index 0000000..71670e6 --- /dev/null +++ b/dnx.xunit.proj @@ -0,0 +1,90 @@ + + + + + + + + Release + false + $(MSBuildProjectDirectory) + $(SolutionDir)\.nuget\nuget.exe + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dnx.xunit.sln b/dnx.xunit.sln new file mode 100644 index 0000000..88206a3 --- /dev/null +++ b/dnx.xunit.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 14.0.22823.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8EE7A674-CE00-403B-BBDE-1C250C7A1F52}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{8B1FE5A0-EA59-456D-B293-82B806DDBEFD}" +EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "xunit.runner.dnx", "src\xunit.runner.dnx\xunit.runner.dnx.xproj", "{002D321E-170E-4E55-BDD1-77C6353A6EB5}" +EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "test.xunit.runner.dnx", "test\test.xunit.runner.dnx\test.xunit.runner.dnx.xproj", "{838070AB-04F2-4A37-9C86-91669E633F70}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{02FA41F5-A5D0-457A-96E7-724756124491}" + ProjectSection(SolutionItems) = preProject + global.json = global.json + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {002D321E-170E-4E55-BDD1-77C6353A6EB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {002D321E-170E-4E55-BDD1-77C6353A6EB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {002D321E-170E-4E55-BDD1-77C6353A6EB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {002D321E-170E-4E55-BDD1-77C6353A6EB5}.Release|Any CPU.Build.0 = Release|Any CPU + {838070AB-04F2-4A37-9C86-91669E633F70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {838070AB-04F2-4A37-9C86-91669E633F70}.Debug|Any CPU.Build.0 = Debug|Any CPU + {838070AB-04F2-4A37-9C86-91669E633F70}.Release|Any CPU.ActiveCfg = Release|Any CPU + {838070AB-04F2-4A37-9C86-91669E633F70}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {002D321E-170E-4E55-BDD1-77C6353A6EB5} = {8EE7A674-CE00-403B-BBDE-1C250C7A1F52} + {838070AB-04F2-4A37-9C86-91669E633F70} = {8B1FE5A0-EA59-456D-B293-82B806DDBEFD} + EndGlobalSection +EndGlobal diff --git a/dnx.xunit.v2.ncrunchsolution b/dnx.xunit.v2.ncrunchsolution new file mode 100644 index 0000000000000000000000000000000000000000..a89c0f3997829e23e6b94b845a67eb5bccb5ba1a GIT binary patch literal 1684 zcmcJQ+fKqj5QhJ26W_r%FnUdz5EWx0ga{sPu_>bVVA>-3^6EDOr9kuo3n5FlJ2U%# zJ3G7m{%UEUOtGda^-M3-ng5S@dtx-#XlV&_r~@7GYpH`hR-`*xu3`}z=mgvGpVm75 z3Z=2f_KDB*LO(?5hUa`d!XxCZ(5=s(!qcW#FqY^M^-uMnOWY?QYQ&gXjbQil*X(U{ zVwh*@qiGqk_lP!hmQok`pck%Wb(8OZbNt3oX%?tw2CfECVHxl8JGrKa3nZUoDHxM%8Bon0_j zwYuM28+EPJl>1>>*{92F@2j@gwQBM%du^xRPEz4FSIAkK`dPB?!L^^M5 _arguments = new Stack(); + readonly IReadOnlyList _reporters; + + protected CommandLine(IReadOnlyList reporters, string[] args, Predicate fileExists = null) + { + _reporters = reporters; + + if (fileExists == null) + fileExists = fileName => File.Exists(fileName); + + for (var i = args.Length - 1; i >= 0; i--) + _arguments.Push(args[i]); + + DesignTimeTestUniqueNames = new List(); + Project = Parse(fileExists); + Reporter = reporters.FirstOrDefault(r => r.IsEnvironmentallyEnabled) ?? Reporter ?? new DefaultRunnerReporter(); + } + + public bool DiagnosticMessages { get; set; } + + public bool Debug { get; set; } + + public bool DesignTime { get; set; } + + // Used with --designtime - to specify specific tests by uniqueId. + public List DesignTimeTestUniqueNames { get; private set; } + + public bool List { get; set; } + + public int? MaxParallelThreads { get; set; } + + public bool NoColor { get; set; } + + public bool NoLogo { get; set; } + + public XunitProject Project { get; protected set; } + + public bool? ParallelizeAssemblies { get; set; } + + public bool? ParallelizeTestCollections { get; set; } + + public IRunnerReporter Reporter { get; protected set; } + + public bool Wait { get; protected set; } + + static XunitProject GetProjectFile(List> assemblies) + { + var result = new XunitProject(); + + foreach (var assembly in assemblies) + result.Add(new XunitProjectAssembly + { + AssemblyFilename = Path.GetFullPath(assembly.Item1), + ConfigFilename = assembly.Item2 != null ? Path.GetFullPath(assembly.Item2) : null, + }); + + return result; + } + + static void GuardNoOptionValue(KeyValuePair option) + { + if (option.Value != null) + throw new ArgumentException(string.Format("error: unknown command line option: {0}", option.Value)); + } + + public static CommandLine Parse(IReadOnlyList reporters, params string[] args) + { + return new CommandLine(reporters, args); + } + + protected XunitProject Parse(Predicate fileExists) + { + if (_arguments.Count == 0) + throw new ArgumentException("must specify at least one assembly"); + + var assemblyFile = _arguments.Pop(); + string configFile = null; + if (_arguments.Count > 0) + { + var value = _arguments.Peek(); + if (!value.StartsWith("-") && value.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) + { + configFile = _arguments.Pop(); + if (!fileExists(configFile)) + throw new ArgumentException(string.Format("config file not found: {0}", configFile)); + } + } + + var assemblies = new List> { Tuple.Create(assemblyFile, configFile) }; + var project = GetProjectFile(assemblies); + + while (_arguments.Count > 0) + { + var option = PopOption(_arguments); + var optionName = option.Key.ToLowerInvariant(); + + if (!optionName.StartsWith("-")) + throw new ArgumentException(string.Format("unknown command line option: {0}", option.Key)); + + optionName = optionName.Substring(1); + + if (optionName == "nologo") + { + GuardNoOptionValue(option); + NoLogo = true; + } + else if (optionName == "nocolor") + { + GuardNoOptionValue(option); + NoColor = true; + } + else if (optionName == "debug") + { + GuardNoOptionValue(option); + Debug = true; + } + else if (optionName == "wait") + { + GuardNoOptionValue(option); + Wait = true; + } + else if (optionName == "diagnostics") + { + GuardNoOptionValue(option); + DiagnosticMessages = true; + } + else if (optionName == "maxthreads") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -maxthreads"); + + switch (option.Value) + { + case "default": + MaxParallelThreads = 0; + break; + + case "unlimited": + MaxParallelThreads = -1; + break; + + default: + int threadValue; + if (!int.TryParse(option.Value, out threadValue) || threadValue < 1) + throw new ArgumentException("incorrect argument value for -maxthreads (must be 'default', 'unlimited', or a positive number)"); + + MaxParallelThreads = threadValue; + break; + } + } + else if (optionName == "parallel") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -parallel"); + + ParallelismOption parallelismOption; + if (!Enum.TryParse(option.Value, out parallelismOption)) + throw new ArgumentException("incorrect argument value for -parallel"); + + switch (parallelismOption) + { + case ParallelismOption.All: + ParallelizeAssemblies = true; + ParallelizeTestCollections = true; + break; + + case ParallelismOption.Assemblies: + ParallelizeAssemblies = true; + ParallelizeTestCollections = false; + break; + + case ParallelismOption.Collections: + ParallelizeAssemblies = false; + ParallelizeTestCollections = true; + break; + + case ParallelismOption.None: + default: + ParallelizeAssemblies = false; + ParallelizeTestCollections = false; + break; + } + } + else if (optionName == "trait") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -trait"); + + var pieces = option.Value.Split('='); + if (pieces.Length != 2 || string.IsNullOrEmpty(pieces[0]) || string.IsNullOrEmpty(pieces[1])) + throw new ArgumentException("incorrect argument format for -trait (should be \"name=value\")"); + + var name = pieces[0]; + var value = pieces[1]; + project.Filters.IncludedTraits.Add(name, value); + } + else if (optionName == "notrait") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -notrait"); + + var pieces = option.Value.Split('='); + if (pieces.Length != 2 || string.IsNullOrEmpty(pieces[0]) || string.IsNullOrEmpty(pieces[1])) + throw new ArgumentException("incorrect argument format for -notrait (should be \"name=value\")"); + + var name = pieces[0]; + var value = pieces[1]; + project.Filters.ExcludedTraits.Add(name, value); + } + else if (optionName == "class") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -class"); + + project.Filters.IncludedClasses.Add(option.Value); + } + else if (optionName == "method") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -method"); + + project.Filters.IncludedMethods.Add(option.Value); + } + else if (optionName == "namespace") + { + if (option.Value == null) + throw new ArgumentException("missing argument for -namespace"); + + project.Filters.IncludedNameSpaces.Add(option.Value); + } + // BEGIN: Special command line switches for DNX <=> Visual Studio integration + else if (optionName == "test" || optionName == "-test") + { + if (option.Value == null) + throw new ArgumentException("missing argument for --test"); + + DesignTimeTestUniqueNames.Add(option.Value); + } + else if (optionName == "list" || optionName == "-list") + { + GuardNoOptionValue(option); + List = true; + } + else if (optionName == "designtime" || optionName == "-designtime") + { + GuardNoOptionValue(option); + DesignTime = true; + } + // END: Special command line switches for DNX <=> Visual Studio integration + else + { + // Might be a reporter... + var reporter = _reporters.FirstOrDefault(r => string.Equals(r.RunnerSwitch, optionName, StringComparison.OrdinalIgnoreCase)); + if (reporter != null) + { + GuardNoOptionValue(option); + if (Reporter != null) + throw new ArgumentException("only one reporter is allowed"); + + Reporter = reporter; + } + // ...or an result output file + else + { + if (!TransformFactory.AvailableTransforms.Any(t => t.CommandLine.Equals(optionName, StringComparison.OrdinalIgnoreCase))) + throw new ArgumentException($"unknown option: {option.Key}"); + + if (option.Value == null) + throw new ArgumentException(string.Format("missing filename for {0}", option.Key)); + + project.Output.Add(optionName, option.Value); + } + } + } + + return project; + } + + static KeyValuePair PopOption(Stack arguments) + { + var option = arguments.Pop(); + string value = null; + + if (arguments.Count > 0 && !arguments.Peek().StartsWith("-")) + value = arguments.Pop(); + + return new KeyValuePair(option, value); + } + } +} diff --git a/src/dotnet-test-xunit/Common/AssemblyExtensions.cs b/src/dotnet-test-xunit/Common/AssemblyExtensions.cs new file mode 100644 index 0000000..6a57f08 --- /dev/null +++ b/src/dotnet-test-xunit/Common/AssemblyExtensions.cs @@ -0,0 +1,26 @@ +#if !DNXCORE50 + +using System; +using System.IO; +using System.Reflection; + +internal static class AssemblyExtensions +{ + public static string GetLocalCodeBase(this Assembly assembly) + { + string codeBase = assembly.CodeBase; + if (codeBase == null) + return null; + + if (!codeBase.StartsWith("file:///")) + throw new ArgumentException(string.Format("Code base {0} in wrong format; must start with file:///", codeBase), "assembly"); + + codeBase = codeBase.Substring(8); + if (Path.DirectorySeparatorChar == '/') + return "/" + codeBase; + + return codeBase.Replace('/', Path.DirectorySeparatorChar); + } +} + +#endif diff --git a/src/dotnet-test-xunit/Common/DictionaryExtensions.cs b/src/dotnet-test-xunit/Common/DictionaryExtensions.cs new file mode 100644 index 0000000..7e65323 --- /dev/null +++ b/src/dotnet-test-xunit/Common/DictionaryExtensions.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +internal static class DictionaryExtensions +{ + public static void Add(this IDictionary> dictionary, TKey key, TValue value) + { + dictionary.GetOrAdd(key).Add(value); + } + + public static bool Contains(this IDictionary> dictionary, TKey key, TValue value, IEqualityComparer valueComparer) + { + List values; + + if (!dictionary.TryGetValue(key, out values)) + return false; + + return values.Contains(value, valueComparer); + } + + public static TValue GetOrAdd(this IDictionary dictionary, TKey key) + where TValue : new() + { + return dictionary.GetOrAdd(key, () => new TValue()); + } + + public static TValue GetOrAdd(this IDictionary dictionary, TKey key, Func newValue) + { + TValue result; + + if (!dictionary.TryGetValue(key, out result)) + { + result = newValue(); + dictionary[key] = result; + } + + return result; + } +} diff --git a/src/dotnet-test-xunit/Common/Guard.cs b/src/dotnet-test-xunit/Common/Guard.cs new file mode 100644 index 0000000..2e285be --- /dev/null +++ b/src/dotnet-test-xunit/Common/Guard.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections; +using System.Diagnostics.CodeAnalysis; + +/// +/// Guard class, used for guard clauses and argument validation +/// +internal static class Guard +{ + /// + public static void ArgumentNotNull(string argName, object argValue) + { + if (argValue == null) + throw new ArgumentNullException(argName); + } + + /// + [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This method may not be called by all users of Guard.")] + public static void ArgumentNotNullOrEmpty(string argName, IEnumerable argValue) + { + ArgumentNotNull(argName, argValue); + + if (!argValue.GetEnumerator().MoveNext()) + throw new ArgumentException("Argument was empty", argName); + } + + /// + [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This method may not be called by all users of Guard.")] + public static void ArgumentValid(string argName, string message, bool test) + { + if (!test) + throw new ArgumentException(message, argName); + } + + /// + [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "This method may not be called by all users of Guard.")] + public static void FileExists(string argName, string fileName) + { + } +} diff --git a/src/dotnet-test-xunit/Common/TestDiscoveryVisitor.cs b/src/dotnet-test-xunit/Common/TestDiscoveryVisitor.cs new file mode 100644 index 0000000..54089b6 --- /dev/null +++ b/src/dotnet-test-xunit/Common/TestDiscoveryVisitor.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using Xunit.Abstractions; + +#if XUNIT_CORE_DLL +namespace Xunit.Sdk +#else +namespace Xunit +#endif +{ + internal class TestDiscoveryVisitor : TestMessageVisitor + { + public TestDiscoveryVisitor() + { + TestCases = new List(); + } + + public List TestCases { get; private set; } + + protected override bool Visit(ITestCaseDiscoveryMessage discovery) + { + TestCases.Add(discovery.TestCase); + + return true; + } + } +} diff --git a/src/dotnet-test-xunit/Common/XmlTestExecutionVisitor.cs b/src/dotnet-test-xunit/Common/XmlTestExecutionVisitor.cs new file mode 100644 index 0000000..5038ab0 --- /dev/null +++ b/src/dotnet-test-xunit/Common/XmlTestExecutionVisitor.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Concurrent; +using System.Globalization; +using System.Xml.Linq; +using Xunit.Abstractions; + +namespace Xunit +{ + public class XmlTestExecutionVisitor : TestMessageVisitor + { + readonly XElement _assemblyElement; + readonly XElement _errorsElement; + readonly ConcurrentDictionary _testCollectionElements = new ConcurrentDictionary(); + + public XmlTestExecutionVisitor(XElement assemblyElement, Func cancelThunk) + { + CancelThunk = cancelThunk ?? (() => false); + + _assemblyElement = assemblyElement; + + if (_assemblyElement != null) + { + _errorsElement = new XElement("errors"); + _assemblyElement.Add(_errorsElement); + } + } + + public readonly Func CancelThunk; + public int Errors; + public int Failed; + public int Skipped; + public decimal Time; + public int Total; + + XElement CreateTestResultElement(ITestResultMessage testResult, string resultText) + { + var collectionElement = GetTestCollectionElement(testResult.TestCase.TestMethod.TestClass.TestCollection); + var testResultElement = + new XElement("test", + new XAttribute("name", XmlEscape(testResult.Test.DisplayName)), + new XAttribute("type", testResult.TestCase.TestMethod.TestClass.Class.Name), + new XAttribute("method", testResult.TestCase.TestMethod.Method.Name), + new XAttribute("time", testResult.ExecutionTime.ToString(CultureInfo.InvariantCulture)), + new XAttribute("result", resultText) + ); + + if (!string.IsNullOrWhiteSpace(testResult.Output)) + { + testResultElement.Add(new XElement("output", new XCData(testResult.Output))); + } + + if (testResult.TestCase.SourceInformation != null) + { + if (testResult.TestCase.SourceInformation.FileName != null) + testResultElement.Add(new XAttribute("source-file", testResult.TestCase.SourceInformation.FileName)); + if (testResult.TestCase.SourceInformation.LineNumber != null) + testResultElement.Add(new XAttribute("source-line", testResult.TestCase.SourceInformation.LineNumber.GetValueOrDefault())); + } + + if (testResult.TestCase.Traits != null && testResult.TestCase.Traits.Count > 0) + { + var traitsElement = new XElement("traits"); + + foreach (var key in testResult.TestCase.Traits.Keys) + foreach (var value in testResult.TestCase.Traits[key]) + traitsElement.Add( + new XElement("trait", + new XAttribute("name", XmlEscape(key)), + new XAttribute("value", XmlEscape(value)) + ) + ); + + testResultElement.Add(traitsElement); + } + + collectionElement.Add(testResultElement); + + return testResultElement; + } + + XElement GetTestCollectionElement(ITestCollection testCollection) + { + return _testCollectionElements.GetOrAdd(testCollection, tc => new XElement("collection")); + } + + public override bool OnMessage(IMessageSinkMessage message) + { + var result = base.OnMessage(message); + if (result) + result = !CancelThunk(); + + return result; + } + + protected override bool Visit(ITestAssemblyFinished assemblyFinished) + { + Total += assemblyFinished.TestsRun; + Failed += assemblyFinished.TestsFailed; + Skipped += assemblyFinished.TestsSkipped; + Time += assemblyFinished.ExecutionTime; + + if (_assemblyElement != null) + { + _assemblyElement.Add( + new XAttribute("total", Total), + new XAttribute("passed", Total - Failed - Skipped), + new XAttribute("failed", Failed), + new XAttribute("skipped", Skipped), + new XAttribute("time", Time.ToString("0.000", CultureInfo.InvariantCulture)), + new XAttribute("errors", Errors) + ); + + foreach (var element in _testCollectionElements.Values) + _assemblyElement.Add(element); + } + + return base.Visit(assemblyFinished); + } + + protected override bool Visit(ITestAssemblyStarting assemblyStarting) + { + if (_assemblyElement != null) + { + _assemblyElement.Add( + new XAttribute("name", assemblyStarting.TestAssembly.Assembly.AssemblyPath), + new XAttribute("environment", assemblyStarting.TestEnvironment), + new XAttribute("test-framework", assemblyStarting.TestFrameworkDisplayName), + new XAttribute("run-date", assemblyStarting.StartTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)), + new XAttribute("run-time", assemblyStarting.StartTime.ToString("HH:mm:ss", CultureInfo.InvariantCulture)) + ); + + if (assemblyStarting.TestAssembly.ConfigFileName != null) + _assemblyElement.Add(new XAttribute("config-file", assemblyStarting.TestAssembly.ConfigFileName)); + } + + return base.Visit(assemblyStarting); + } + + protected override bool Visit(ITestCollectionFinished testCollectionFinished) + { + if (_assemblyElement != null) + { + var collectionElement = GetTestCollectionElement(testCollectionFinished.TestCollection); + collectionElement.Add( + new XAttribute("total", testCollectionFinished.TestsRun), + new XAttribute("passed", testCollectionFinished.TestsRun - testCollectionFinished.TestsFailed - testCollectionFinished.TestsSkipped), + new XAttribute("failed", testCollectionFinished.TestsFailed), + new XAttribute("skipped", testCollectionFinished.TestsSkipped), + new XAttribute("name", XmlEscape(testCollectionFinished.TestCollection.DisplayName)), + new XAttribute("time", testCollectionFinished.ExecutionTime.ToString("0.000", CultureInfo.InvariantCulture)) + ); + } + + return base.Visit(testCollectionFinished); + } + + protected override bool Visit(ITestFailed testFailed) + { + if (_assemblyElement != null) + { + var testElement = CreateTestResultElement(testFailed, "Fail"); + testElement.Add(CreateFailureElement(testFailed)); + } + + return base.Visit(testFailed); + } + + protected override bool Visit(ITestPassed testPassed) + { + if (_assemblyElement != null) + CreateTestResultElement(testPassed, "Pass"); + + return base.Visit(testPassed); + } + + protected override bool Visit(ITestSkipped testSkipped) + { + if (_assemblyElement != null) + { + var testElement = CreateTestResultElement(testSkipped, "Skip"); + testElement.Add(new XElement("reason", new XCData(XmlEscape(testSkipped.Reason)))); + } + + return base.Visit(testSkipped); + } + + protected override bool Visit(IErrorMessage error) + { + AddError("fatal", null, error); + + return base.Visit(error); + } + + protected override bool Visit(ITestAssemblyCleanupFailure cleanupFailure) + { + AddError("assembly-cleanup", cleanupFailure.TestAssembly.Assembly.AssemblyPath, cleanupFailure); + + return base.Visit(cleanupFailure); + } + + protected override bool Visit(ITestCaseCleanupFailure cleanupFailure) + { + AddError("test-case-cleanup", cleanupFailure.TestCase.DisplayName, cleanupFailure); + + return base.Visit(cleanupFailure); + } + + protected override bool Visit(ITestClassCleanupFailure cleanupFailure) + { + AddError("test-class-cleanup", cleanupFailure.TestClass.Class.Name, cleanupFailure); + + return base.Visit(cleanupFailure); + } + + protected override bool Visit(ITestCollectionCleanupFailure cleanupFailure) + { + AddError("test-collection-cleanup", cleanupFailure.TestCollection.DisplayName, cleanupFailure); + + return base.Visit(cleanupFailure); + } + + protected override bool Visit(ITestCleanupFailure cleanupFailure) + { + AddError("test-cleanup", cleanupFailure.Test.DisplayName, cleanupFailure); + + return base.Visit(cleanupFailure); + } + + protected override bool Visit(ITestMethodCleanupFailure cleanupFailure) + { + AddError("test-method-cleanup", cleanupFailure.TestMethod.Method.Name, cleanupFailure); + + return base.Visit(cleanupFailure); + } + + void AddError(string type, string name, IFailureInformation failureInfo) + { + Errors++; + + if (_errorsElement == null) + return; + + var errorElement = new XElement("error", new XAttribute("type", type), CreateFailureElement(failureInfo)); + if (name != null) + errorElement.Add(new XAttribute("name", name)); + + _errorsElement.Add(errorElement); + } + + static XElement CreateFailureElement(IFailureInformation failureInfo) + { + return new XElement("failure", + new XAttribute("exception-type", failureInfo.ExceptionTypes[0]), + new XElement("message", new XCData(XmlEscape(ExceptionUtility.CombineMessages(failureInfo)))), + new XElement("stack-trace", new XCData(ExceptionUtility.CombineStackTraces(failureInfo) ?? string.Empty)) + ); + } + + protected static string Escape(string value) + { + if (value == null) + return string.Empty; + + return value.Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t").Replace("\0", "\\0"); + } + + protected static string XmlEscape(string value) + { + if (value == null) + return string.Empty; + + return value.Replace("\0", "\\0"); + } + } +} diff --git a/src/dotnet-test-xunit/ConsoleRunnerLogger.cs b/src/dotnet-test-xunit/ConsoleRunnerLogger.cs new file mode 100644 index 0000000..647b812 --- /dev/null +++ b/src/dotnet-test-xunit/ConsoleRunnerLogger.cs @@ -0,0 +1,80 @@ +using System; + +namespace Xunit +{ + /// + /// An implementation of which logs messages + /// to and . + /// + public class ConsoleRunnerLogger : IRunnerLogger + { + readonly object _lockObject = new object(); + readonly bool _useColors; + + /// + /// Initializes a new instance of the class. + /// + /// A flag to indicate whether colors should be used when + /// logging messages. + public ConsoleRunnerLogger(bool useColors) + { + _useColors = useColors; + } + + /// + public object LockObject + { + get { return _lockObject; } + } + + /// + public void LogError(StackFrameInfo stackFrame, string message) + { + using (SetColor(ConsoleColor.Red)) + lock (LockObject) + Console.Error.WriteLine(message); + } + + /// + public void LogImportantMessage(StackFrameInfo stackFrame, string message) + { + using (SetColor(ConsoleColor.Gray)) + lock (LockObject) + Console.WriteLine(message); + } + + /// + public void LogMessage(StackFrameInfo stackFrame, string message) + { + using (SetColor(ConsoleColor.DarkGray)) + lock (LockObject) + Console.WriteLine(message); + } + + /// + public void LogWarning(StackFrameInfo stackFrame, string message) + { + using (SetColor(ConsoleColor.Yellow)) + lock (LockObject) + Console.WriteLine(message); + } + + IDisposable SetColor(ConsoleColor color) + { + return _useColors ? new ColorRestorer(color) : null; + } + + class ColorRestorer : IDisposable + { + public ColorRestorer(ConsoleColor color) + { + Console.ForegroundColor = color; + } + + public void Dispose() + { + Console.ResetColor(); + } + } + } +} diff --git a/src/dotnet-test-xunit/DesignTime/DesignTimeExecutionVisitor.cs b/src/dotnet-test-xunit/DesignTime/DesignTimeExecutionVisitor.cs new file mode 100644 index 0000000..63c0007 --- /dev/null +++ b/src/dotnet-test-xunit/DesignTime/DesignTimeExecutionVisitor.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using Microsoft.Extensions.Testing.Abstractions; +using Xunit.Abstractions; +using VsTestCase = Microsoft.Extensions.Testing.Abstractions.Test; + +namespace Xunit.Runner.DotNet +{ + public class DesignTimeExecutionVisitor : TestMessageVisitor, IExecutionVisitor + { + private readonly ITestExecutionSink _sink; + private readonly IDictionary _conversions; + private readonly IMessageSink _next; + + public DesignTimeExecutionVisitor(ITestExecutionSink sink, IDictionary conversions, IMessageSink next) + { + _sink = sink; + _conversions = conversions; + _next = next; + + ExecutionSummary = new ExecutionSummary(); + } + + public ExecutionSummary ExecutionSummary { get; private set; } + + protected override bool Visit(ITestStarting testStarting) + { + var test = _conversions[testStarting.TestCase]; + + if (_sink != null) + _sink.RecordStart(test); + + return true; + } + + protected override bool Visit(ITestSkipped testSkipped) + { + var test = _conversions[testSkipped.TestCase]; + + if (_sink != null) + _sink.RecordResult(new TestResult(test) { Outcome = TestOutcome.Skipped }); + + return true; + } + + protected override bool Visit(ITestFailed testFailed) + { + var test = _conversions[testFailed.TestCase]; + var result = new TestResult(test) + { + Outcome = TestOutcome.Failed, + Duration = TimeSpan.FromSeconds((double)testFailed.ExecutionTime), + ErrorMessage = string.Join(Environment.NewLine, testFailed.Messages), + ErrorStackTrace = string.Join(Environment.NewLine, testFailed.StackTraces), + }; + + result.Messages.Add(testFailed.Output); + + if (_sink != null) + _sink.RecordResult(result); + + return true; + } + + protected override bool Visit(ITestPassed testPassed) + { + var test = _conversions[testPassed.TestCase]; + + if (_sink != null) + { + _sink.RecordResult(new TestResult(test) + { + Outcome = TestOutcome.Passed, + Duration = TimeSpan.FromSeconds((double)testPassed.ExecutionTime), + }); + } + + return true; + } + + protected override bool Visit(ITestAssemblyFinished assemblyFinished) + { + var result = base.Visit(assemblyFinished); + + ExecutionSummary = new ExecutionSummary + { + Failed = assemblyFinished.TestsFailed, + Skipped = assemblyFinished.TestsSkipped, + Time = assemblyFinished.ExecutionTime, + Total = assemblyFinished.TestsRun + }; + + return result; + } + + public override bool OnMessage(IMessageSinkMessage message) + { + return + base.OnMessage(message) && + _next.OnMessage(message); + } + } +} diff --git a/src/dotnet-test-xunit/DesignTime/DesignTimeTestConverter.cs b/src/dotnet-test-xunit/DesignTime/DesignTimeTestConverter.cs new file mode 100644 index 0000000..a9f0ffb --- /dev/null +++ b/src/dotnet-test-xunit/DesignTime/DesignTimeTestConverter.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using Xunit.Abstractions; +using VsTestCase = Microsoft.Extensions.Testing.Abstractions.Test; + +namespace Xunit.Runner.DotNet +{ + public static class DesignTimeTestConverter + { +#if DNXCORE50 + private readonly static HashAlgorithm Hash = SHA1.Create(); +#else + private readonly static HashAlgorithm _hash = new SHA1Managed(); +#endif + public static IDictionary Convert(IEnumerable testcases) + { + // When tests have the same class name and method name, generate unique names for display + var groups = testcases + .Select(tc => new + { + testcase = tc, + shortName = GetShortName(tc), + fullyQualifiedName = string.Format("{0}.{1}", tc.TestMethod.TestClass.Class.Name, tc.TestMethod.Method.Name) + }) + .GroupBy(tc => tc.fullyQualifiedName); + + var results = new Dictionary(); + foreach (var group in groups) + { + var uniquifyNames = group.Count() > 1; + foreach (var testcase in group) + { + results.Add( + testcase.testcase, + Convert( + testcase.testcase, + testcase.shortName, + testcase.fullyQualifiedName, + uniquifyNames)); + } + } + + return results; + } + + private static string GetShortName(ITestCase tc) + { + var shortName = new StringBuilder(); + + var classFullName = tc.TestMethod.TestClass.Class.Name; + var dotIndex = classFullName.LastIndexOf('.'); + if (dotIndex >= 0) + shortName.Append(classFullName.Substring(dotIndex + 1)); + else + shortName.Append(classFullName); + + shortName.Append("."); + shortName.Append(tc.TestMethod.Method.Name); + + // We need to shorten the arguments list if it's long. Let's arbitrarily pick 50 characters. + var argumentsIndex = tc.DisplayName.IndexOf('('); + if (argumentsIndex >= 0 && tc.DisplayName.Length - argumentsIndex > 50) + { + shortName.Append(tc.DisplayName.Substring(argumentsIndex, 46)); + shortName.Append("..."); + shortName.Append(")"); + } + else if (argumentsIndex >= 0) + shortName.Append(tc.DisplayName.Substring(argumentsIndex)); + + return shortName.ToString(); + } + + private static VsTestCase Convert( + ITestCase testcase, + string shortName, + string fullyQualifiedName, + bool uniquifyNames) + { + string uniqueName; + if (uniquifyNames) + uniqueName = string.Format("{0}({1})", fullyQualifiedName, testcase.UniqueID); + else + uniqueName = fullyQualifiedName; + + var result = new VsTestCase(); + result.DisplayName = shortName; + result.FullyQualifiedName = uniqueName; + + result.Id = GuidFromString(testcase.UniqueID); + + if (testcase.SourceInformation != null) + { + result.CodeFilePath = testcase.SourceInformation.FileName; + result.LineNumber = testcase.SourceInformation.LineNumber; + } + + return result; + } + + private static Guid GuidFromString(string data) + { + var hash = Hash.ComputeHash(Encoding.Unicode.GetBytes(data)); + var b = new byte[16]; + Array.Copy((Array)hash, (Array)b, 16); + return new Guid(b); + } + } +} \ No newline at end of file diff --git a/src/dotnet-test-xunit/DesignTime/SourceInformationProvider.cs b/src/dotnet-test-xunit/DesignTime/SourceInformationProvider.cs new file mode 100644 index 0000000..0758139 --- /dev/null +++ b/src/dotnet-test-xunit/DesignTime/SourceInformationProvider.cs @@ -0,0 +1,56 @@ +using Xunit.Abstractions; +using TestHostSourceInformationProvider = Microsoft.Extensions.Testing.Abstractions.ISourceInformationProvider; + +namespace Xunit.Runner.DotNet +{ + public class SourceInformationProviderAdapater : ISourceInformationProvider + { + private readonly TestHostSourceInformationProvider _provider; + + public SourceInformationProviderAdapater(TestHostSourceInformationProvider provider) + { + _provider = provider; + } + + public void Dispose() { } + + public ISourceInformation GetSourceInformation(ITestCase testCase) + { + if (_provider == null) + return null; + + var reflectionMethodInfo = testCase.TestMethod.Method as IReflectionMethodInfo; + if (reflectionMethodInfo == null) + return null; + + var innerInformation = _provider.GetSourceInformation(reflectionMethodInfo.MethodInfo); + if (innerInformation == null) + return null; + + return new SourceInformation + { + FileName = innerInformation.Filename, + LineNumber = innerInformation.LineNumber, + }; + } + + private class SourceInformation : ISourceInformation + { + public string FileName { get; set; } + + public int? LineNumber { get; set; } + + public void Deserialize(IXunitSerializationInfo info) + { + FileName = info.GetValue("FileName"); + LineNumber = info.GetValue("LineNumber"); + } + + public void Serialize(IXunitSerializationInfo info) + { + info.AddValue("FileName", FileName, typeof(string)); + info.AddValue("LineNumber", LineNumber, typeof(int?)); + } + } + } +} diff --git a/src/dotnet-test-xunit/ParallelismOption.cs b/src/dotnet-test-xunit/ParallelismOption.cs new file mode 100644 index 0000000..a09ba6a --- /dev/null +++ b/src/dotnet-test-xunit/ParallelismOption.cs @@ -0,0 +1,10 @@ +namespace Xunit.Runner.DotNet +{ + public enum ParallelismOption + { + None, + Collections, + Assemblies, + All + } +} diff --git a/src/dotnet-test-xunit/Program.cs b/src/dotnet-test-xunit/Program.cs new file mode 100644 index 0000000..6527ecd --- /dev/null +++ b/src/dotnet-test-xunit/Program.cs @@ -0,0 +1,464 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Loader; +using System.Threading; +using System.Threading.Tasks; +using System.Xml.Linq; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.Extensions.Testing.Abstractions; +using Microsoft.DotNet.ProjectModel; +using Microsoft.DotNet.ProjectModel.Loader; +using NuGet.Frameworks; +using Xunit.Abstractions; +using ISourceInformationProvider = Xunit.Abstractions.ISourceInformationProvider; +using VsTestCase = Microsoft.Extensions.Testing.Abstractions.Test; + +namespace Xunit.Runner.DotNet +{ + public class Program + { +#pragma warning disable 0649 + volatile bool _cancel; +#pragma warning restore 0649 + readonly ConcurrentDictionary _completionMessages = new ConcurrentDictionary(); + bool _failed; + IRunnerLogger _logger; + IMessageSink _reporterMessageHandler; + readonly ITestDiscoverySink _testDiscoverySink; + readonly ITestExecutionSink _testExecutionSink; + + public static int Main(string[] args) + { + DebugHelper.HandleDebugSwitch(ref args); + + var dllPath = args[0]; + var projectPath = GetProjectPathFromDllPath(dllPath); + AssemblyLoadContext.InitializeDefaultContext(ProjectContext.Create(projectPath, FrameworkConstants.CommonFrameworks.DnxCore50, new[] { RuntimeIdentifier.Current }).CreateLoadContext()); + + return new Program().Run(args); + } + + // This is a temporary workaround. + private static string GetProjectPathFromDllPath(string dllPath) + { + var directory = new DirectoryInfo(Path.GetDirectoryName(dllPath)); + while (directory != directory.Root && directory.EnumerateFiles().All(f => f.Name != "project.json")) + { + directory = directory.Parent; + } + + var projectFile = directory.EnumerateFiles().FirstOrDefault(f => f.Name == "project.json"); + + return projectFile?.FullName; + } + + public Program() + { + _testDiscoverySink = new StreamingTestDiscoverySink(Console.OpenStandardOutput()); + + _testExecutionSink = new StreamingTestExecutionSink(Console.OpenStandardOutput()); + } + + public int Run(string[] args) + { + try + { + var reporters = GetAvailableRunnerReporters(); + + if (args.Length == 0 || args.Any(arg => arg == "-?")) + { + PrintHeader(); + PrintUsage(reporters); + return 2; + } + +#if !DNXCORE50 + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; +#endif + + var commandLine = CommandLine.Parse(reporters, args); + +#if !DNXCORE50 + if (commandLine.Debug) + Debugger.Launch(); +#else + if (commandLine.Debug) + { + Console.WriteLine("Debug support is not available in DNX Core."); + return -1; + } +#endif + + _logger = new ConsoleRunnerLogger(!commandLine.NoColor); + _reporterMessageHandler = commandLine.Reporter.CreateMessageHandler(_logger); + + if (!commandLine.NoLogo) + PrintHeader(); + + var failCount = RunProject(commandLine.Project, commandLine.ParallelizeAssemblies, commandLine.ParallelizeTestCollections, + commandLine.MaxParallelThreads, commandLine.DiagnosticMessages, commandLine.NoColor, + commandLine.DesignTime, commandLine.List, commandLine.DesignTimeTestUniqueNames); + + if (commandLine.Wait) + { + Console.WriteLine(); + + Console.Write("Press ENTER to continue..."); + Console.ReadLine(); + + Console.WriteLine(); + } + + return failCount > 0 ? 1 : 0; + } + catch (ArgumentException ex) + { + Console.WriteLine("error: {0}", ex.Message); + return 3; + } + catch (BadImageFormatException ex) + { + Console.WriteLine("{0}", ex.Message); + return 4; + } + finally + { + Console.ResetColor(); + } + } + +#if !DNXCORE50 + static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) + { + var ex = e.ExceptionObject as Exception; + + if (ex != null) + Console.WriteLine(ex.ToString()); + else + Console.WriteLine("Error of unknown type thrown in application domain"); + + Environment.Exit(1); + } +#endif + static List GetAvailableRunnerReporters() + { + var result = new List(); + var runnerPath = Path.GetDirectoryName(AppContext.BaseDirectory); + + foreach (var dllFile in Directory.GetFiles(runnerPath, "*.dll").Select(f => Path.Combine(runnerPath, f))) + { + TypeInfo[] types; + + var dllName = Path.GetFileNameWithoutExtension(dllFile); + + try + { + var assembly = Assembly.Load(new AssemblyName(dllName)); + types = assembly.DefinedTypes.ToArray(); + } + catch + { + continue; + } + + foreach (var type in types) + { + if (type == null || type.IsAbstract || type == typeof(DefaultRunnerReporter).GetTypeInfo() || type.ImplementedInterfaces.All(i => i != typeof (IRunnerReporter))) + continue; + + var ctor = type.DeclaredConstructors.FirstOrDefault(c => c.GetParameters().Length == 0); + if (ctor == null) + { + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine($"Type {type.FullName} in assembly {dllFile} appears to be a runner reporter, but does not have an empty constructor."); + Console.ResetColor(); + continue; + } + + result.Add((IRunnerReporter)ctor.Invoke(new object[0])); + } + } + + return result; + } + + void PrintHeader() + { + Console.WriteLine("xUnit.net DNX Runner ({0}-bit {1})", IntPtr.Size * 8, RuntimeIdentifier.Current); + } + + static void PrintUsage(IReadOnlyList reporters) + { + Console.WriteLine("Copyright (C) 2015 Outercurve Foundation."); + Console.WriteLine(); + Console.WriteLine("usage: xunit.runner.dnx [configFile.json] [options] [reporter] [resultFormat filename [...]]"); + Console.WriteLine(); + Console.WriteLine("Valid options:"); + Console.WriteLine(" -nologo : do not show the copyright message"); + Console.WriteLine(" -nocolor : do not output results with colors"); + Console.WriteLine(" -parallel option : set parallelization based on option"); + Console.WriteLine(" : none - turn off all parallelization"); + Console.WriteLine(" : collections - only parallelize collections"); + Console.WriteLine(" : assemblies - only parallelize assemblies"); + Console.WriteLine(" : all - parallelize collections and assemblies"); + Console.WriteLine(" -maxthreads count : maximum thread count for collection parallelization"); + Console.WriteLine(" : default - run with default (1 thread per CPU thread)"); + Console.WriteLine(" : unlimited - run with unbounded thread count"); + Console.WriteLine(" : (number) - limit task thread pool size to 'count'"); + Console.WriteLine(" -wait : wait for input after completion"); + Console.WriteLine(" -diagnostics : enable diagnostics messages for all test assemblies"); +#if !DNXCORE50 + Console.WriteLine(" -debug : launch the debugger to debug the tests"); +#endif + Console.WriteLine(" -trait \"name=value\" : only run tests with matching name/value traits"); + Console.WriteLine(" : if specified more than once, acts as an OR operation"); + Console.WriteLine(" -notrait \"name=value\" : do not run tests with matching name/value traits"); + Console.WriteLine(" : if specified more than once, acts as an AND operation"); + Console.WriteLine(" -method \"name\" : run a given test method (should be fully specified;"); + Console.WriteLine(" : i.e., 'MyNamespace.MyClass.MyTestMethod')"); + Console.WriteLine(" : if specified more than once, acts as an OR operation"); + Console.WriteLine(" -class \"name\" : run all methods in a given test class (should be fully"); + Console.WriteLine(" : specified; i.e., 'MyNamespace.MyClass')"); + Console.WriteLine(" : if specified more than once, acts as an OR operation"); + Console.WriteLine(" -namespace \"name\" : run all methods in a given namespace (i.e.,"); + Console.WriteLine(" : 'MyNamespace.MySubNamespace')"); + Console.WriteLine(" : if specified more than once, acts as an OR operation"); + Console.WriteLine(); + + var switchableReporters = reporters.Where(r => !string.IsNullOrWhiteSpace(r.RunnerSwitch)).ToList(); + if (switchableReporters.Count > 0) + { + Console.WriteLine("Reporters: (optional, choose only one)"); + + foreach (var reporter in switchableReporters.OrderBy(r => r.RunnerSwitch)) + Console.WriteLine(" -{0} : {1}", reporter.RunnerSwitch.ToLowerInvariant().PadRight(21), reporter.Description); + + Console.WriteLine(); + } + + Console.WriteLine("Result formats: (optional, choose one or more)"); + + foreach (var transform in TransformFactory.AvailableTransforms) + Console.WriteLine(" {0} : {1}", + string.Format("-{0} ", transform.CommandLine).PadRight(22).Substring(0, 22), + transform.Description); + } + + int RunProject(XunitProject project, + bool? parallelizeAssemblies, + bool? parallelizeTestCollections, + int? maxThreadCount, + bool diagnosticMessages, + bool noColor, + bool designTime, + bool list, + IReadOnlyList designTimeFullyQualifiedNames) + { + XElement assembliesElement = null; + var xmlTransformers = TransformFactory.GetXmlTransformers(project); + var needsXml = xmlTransformers.Count > 0; + var consoleLock = new object(); + + if (!parallelizeAssemblies.HasValue) + parallelizeAssemblies = project.All(assembly => assembly.Configuration.ParallelizeAssemblyOrDefault); + + if (needsXml) + assembliesElement = new XElement("assemblies"); + + var originalWorkingFolder = Directory.GetCurrentDirectory(); + + using (AssemblyHelper.SubscribeResolve()) + { + var clockTime = Stopwatch.StartNew(); + + if (parallelizeAssemblies.GetValueOrDefault()) + { + var tasks = project.Assemblies.Select(assembly => TaskRun(() => ExecuteAssembly(consoleLock, assembly, needsXml, parallelizeTestCollections, maxThreadCount, diagnosticMessages, noColor, project.Filters, designTime, list, designTimeFullyQualifiedNames))); + var results = Task.WhenAll(tasks).GetAwaiter().GetResult(); + foreach (var assemblyElement in results.Where(result => result != null)) + assembliesElement.Add(assemblyElement); + } + else + { + foreach (var assembly in project.Assemblies) + { + var assemblyElement = ExecuteAssembly(consoleLock, assembly, needsXml, parallelizeTestCollections, maxThreadCount, diagnosticMessages, noColor, project.Filters, designTime, list, designTimeFullyQualifiedNames); + if (assemblyElement != null) + assembliesElement.Add(assemblyElement); + } + } + + clockTime.Stop(); + + if (_completionMessages.Count > 0) + _reporterMessageHandler.OnMessage(new TestExecutionSummary(clockTime.Elapsed, _completionMessages.OrderBy(kvp => kvp.Key).ToList())); + } + + Directory.SetCurrentDirectory(originalWorkingFolder); + + foreach (var transformer in xmlTransformers) + transformer(assembliesElement); + + return _failed ? 1 : _completionMessages.Values.Sum(summary => summary.Failed); + } + + XElement ExecuteAssembly(object consoleLock, + XunitProjectAssembly assembly, + bool needsXml, + bool? parallelizeTestCollections, + int? maxThreadCount, + bool diagnosticMessages, + bool noColor, + XunitFilters filters, + bool designTime, + bool listTestCases, + IReadOnlyList designTimeFullyQualifiedNames) + { + if (_cancel) + return null; + + var assemblyElement = needsXml ? new XElement("assembly") : null; + + try + { + // Turn off pre-enumeration of theories when we're not running in Visual Studio + if (!designTime) + assembly.Configuration.PreEnumerateTheories = false; + + if (diagnosticMessages) + assembly.Configuration.DiagnosticMessages = true; + + var discoveryOptions = TestFrameworkOptions.ForDiscovery(assembly.Configuration); + var executionOptions = TestFrameworkOptions.ForExecution(assembly.Configuration); + if (maxThreadCount.HasValue) + executionOptions.SetMaxParallelThreads(maxThreadCount); + if (parallelizeTestCollections.HasValue) + executionOptions.SetDisableParallelization(!parallelizeTestCollections.GetValueOrDefault()); + + var assemblyDisplayName = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename); + var diagnosticMessageVisitor = new DiagnosticMessageVisitor(consoleLock, assemblyDisplayName, assembly.Configuration.DiagnosticMessagesOrDefault, noColor); + + var sourceInformationProvider = GetSourceInformationProviderAdapater(assembly); + + + using (var controller = new XunitFrontController(AppDomainSupport.Denied, assembly.AssemblyFilename, assembly.ConfigFilename, false, diagnosticMessageSink: diagnosticMessageVisitor, sourceInformationProvider: sourceInformationProvider)) + using (var discoveryVisitor = new TestDiscoveryVisitor()) + { + var includeSourceInformation = designTime && listTestCases; + + // Discover & filter the tests + _reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, false, false, discoveryOptions)); + + controller.Find(includeSourceInformation: includeSourceInformation, messageSink: discoveryVisitor, discoveryOptions: discoveryOptions); + discoveryVisitor.Finished.WaitOne(); + + IDictionary vsTestCases = null; + if (designTime) + vsTestCases = DesignTimeTestConverter.Convert(discoveryVisitor.TestCases); + + if (listTestCases) + { + lock (consoleLock) + { + if (designTime) + { + foreach (var testcase in vsTestCases.Values) + { + _testDiscoverySink?.SendTest(testcase); + + Console.WriteLine(testcase.FullyQualifiedName); + } + } + else + { + foreach (var testcase in discoveryVisitor.TestCases) + Console.WriteLine(testcase.DisplayName); + } + } + + return assemblyElement; + } + + IExecutionVisitor resultsVisitor; + + if (designTime) + { + resultsVisitor = new DesignTimeExecutionVisitor(_testExecutionSink, vsTestCases, _reporterMessageHandler); + } + else + resultsVisitor = new XmlAggregateVisitor(_reporterMessageHandler, _completionMessages, assemblyElement, () => _cancel); + + IList filteredTestCases; + var testCasesDiscovered = discoveryVisitor.TestCases.Count; + if (!designTime || designTimeFullyQualifiedNames.Count == 0) + filteredTestCases = discoveryVisitor.TestCases.Where(filters.Filter).ToList(); + else + filteredTestCases = vsTestCases.Where(t => designTimeFullyQualifiedNames.Contains(t.Value.FullyQualifiedName)).Select(t => t.Key).ToList(); + var testCasesToRun = filteredTestCases.Count; + + _reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryFinished(assembly, discoveryOptions, testCasesDiscovered, testCasesToRun)); + + if (filteredTestCases.Count == 0) + _completionMessages.TryAdd(Path.GetFileName(assembly.AssemblyFilename), new ExecutionSummary()); + else + { + _reporterMessageHandler.OnMessage(new TestAssemblyExecutionStarting(assembly, executionOptions)); + + controller.RunTests(filteredTestCases, resultsVisitor, executionOptions); + resultsVisitor.Finished.WaitOne(); + + _reporterMessageHandler.OnMessage(new TestAssemblyExecutionFinished(assembly, executionOptions, resultsVisitor.ExecutionSummary)); + } + } + } + catch (Exception ex) + { + _failed = true; + + var e = ex; + while (e != null) + { + Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message); + e = e.InnerException; + } + } + + return assemblyElement; + } + + private static ISourceInformationProvider GetSourceInformationProviderAdapater(XunitProjectAssembly assembly) + { + var directoryPath = Path.GetDirectoryName(assembly.AssemblyFilename); + var assemblyName = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename); + var pdbPath = Path.Combine(directoryPath, assemblyName + FileNameSuffixes.DotNet.ProgramDatabase); + + return File.Exists(pdbPath) + ? new SourceInformationProviderAdapater(new SourceInformationProvider(pdbPath, null)) + : null; + } + + static Task TaskRun(Func function) + { + var tcs = new TaskCompletionSource(); + + ThreadPool.QueueUserWorkItem(_ => + { + try + { + tcs.SetResult(function()); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + }); + + return tcs.Task; + } + } +} diff --git a/src/dotnet-test-xunit/Properties/AssemblyInfo.cs b/src/dotnet-test-xunit/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f5f3a39 --- /dev/null +++ b/src/dotnet-test-xunit/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Reflection; + +[assembly : AssemblyTitle("xUnit.net Test Runner (DNX / DNX Core)")] diff --git a/src/dotnet-test-xunit/Properties/GlobalAssemblyInfo.cs b/src/dotnet-test-xunit/Properties/GlobalAssemblyInfo.cs new file mode 100644 index 0000000..4798270 --- /dev/null +++ b/src/dotnet-test-xunit/Properties/GlobalAssemblyInfo.cs @@ -0,0 +1,13 @@ +using System.Diagnostics.CodeAnalysis; +using System.Reflection; + +[assembly: AssemblyCompany("Outercurve Foundation")] +[assembly: AssemblyProduct("xUnit.net Testing Framework")] +[assembly: AssemblyCopyright("Copyright (C) Outercurve Foundation")] +[assembly: AssemblyVersion("99.99.99.0")] + +[assembly: SuppressMessage("Microsoft.Design", "CA1020:AvoidNamespacesWithFewTypes", Scope = "namespace", Target = "Xunit.Sdk")] +[assembly: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "xunit")] +[assembly: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "extensions")] +[assembly: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "utility")] +[assembly: SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "runner")] \ No newline at end of file diff --git a/src/dotnet-test-xunit/Utility/IExecutionVisitor.cs b/src/dotnet-test-xunit/Utility/IExecutionVisitor.cs new file mode 100644 index 0000000..10b92b6 --- /dev/null +++ b/src/dotnet-test-xunit/Utility/IExecutionVisitor.cs @@ -0,0 +1,11 @@ +using System.Threading; +using Xunit.Abstractions; + +namespace Xunit +{ + public interface IExecutionVisitor : IMessageSink + { + ExecutionSummary ExecutionSummary { get; } + ManualResetEvent Finished { get; } + } +} diff --git a/src/dotnet-test-xunit/Utility/StackFrameTransformer.cs b/src/dotnet-test-xunit/Utility/StackFrameTransformer.cs new file mode 100644 index 0000000..89dd364 --- /dev/null +++ b/src/dotnet-test-xunit/Utility/StackFrameTransformer.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace Xunit.Runner.DotNet +{ + /// + /// Transforms stack frames and stack traces into compiler-like output + /// so they can be double-clicked in Visual Studio. + /// + public static class StackFrameTransformer + { + static Regex _regex; + + static StackFrameTransformer() + { + _regex = new Regex(@"^\s*at (?.*) in (?.*):(line )?(?\d+)$"); + } + + /// + public static string TransformFrame(string stackFrame, string defaultDirectory) + { + if (stackFrame == null) + return null; + + var match = _regex.Match(stackFrame); + if (match == Match.Empty) + return stackFrame; + + var file = match.Groups["file"].Value; + if (file.StartsWith(defaultDirectory, StringComparison.OrdinalIgnoreCase)) + file = file.Substring(defaultDirectory.Length); + + return string.Format("{0}({1},0): at {2}", + file, + match.Groups["line"].Value, + match.Groups["method"].Value); + } + + /// + public static string TransformStack(string stack, string defaultDirectory) + { + if (stack == null) + return null; + + List results = new List(); + + foreach (string frame in stack.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) + results.Add(TransformFrame(frame, defaultDirectory)); + + return string.Join(Environment.NewLine, results.ToArray()); + } + } +} diff --git a/src/dotnet-test-xunit/Utility/Transform.cs b/src/dotnet-test-xunit/Utility/Transform.cs new file mode 100644 index 0000000..320999a --- /dev/null +++ b/src/dotnet-test-xunit/Utility/Transform.cs @@ -0,0 +1,12 @@ +using System; +using System.Xml.Linq; + +namespace Xunit.Runner.DotNet +{ + public class Transform + { + public string CommandLine; + public string Description; + public Action OutputHandler; + } +} diff --git a/src/dotnet-test-xunit/Utility/TransformFactory.cs b/src/dotnet-test-xunit/Utility/TransformFactory.cs new file mode 100644 index 0000000..250aaa6 --- /dev/null +++ b/src/dotnet-test-xunit/Utility/TransformFactory.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; + +namespace Xunit.Runner.DotNet +{ + public class TransformFactory + { + static readonly TransformFactory Instance = new TransformFactory(); + + readonly Dictionary _availableTransforms = new Dictionary(StringComparer.OrdinalIgnoreCase); + + protected TransformFactory() + { + _availableTransforms.Add("xml", new Transform { CommandLine = "xml", Description = "output results to xUnit.net v2 style XML file", OutputHandler = Handler_DirectWrite }); + } + + public static List AvailableTransforms + { + get { return Instance._availableTransforms.Values.ToList(); } + } + + public static List> GetXmlTransformers(XunitProject project) + { + return project.Output.Select(output => new Action(xml => Instance._availableTransforms[output.Key].OutputHandler(xml, output.Value))).ToList(); + } + + static void Handler_DirectWrite(XElement xml, string outputFileName) + { + // Create the output parent directories if they don't exist. + int lastSlashIdx = outputFileName.LastIndexOf('/'); + if (lastSlashIdx != -1) + Directory.CreateDirectory(outputFileName.Substring(0, lastSlashIdx)); + + using (var stream = File.Open(outputFileName, FileMode.Create)) + xml.Save(stream); + } + } +} diff --git a/src/dotnet-test-xunit/Utility/XmlAggregateVisitor.cs b/src/dotnet-test-xunit/Utility/XmlAggregateVisitor.cs new file mode 100644 index 0000000..7009bf2 --- /dev/null +++ b/src/dotnet-test-xunit/Utility/XmlAggregateVisitor.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using System.Xml.Linq; +using Xunit.Abstractions; + +namespace Xunit +{ + public class XmlAggregateVisitor : XmlTestExecutionVisitor, IExecutionVisitor + { + readonly ConcurrentDictionary _completionMessages; + readonly IMessageSink _innerMessageSink; + + public XmlAggregateVisitor(IMessageSink innerMessageSink, + ConcurrentDictionary completionMessages, + XElement assemblyElement, + Func cancelThunk) + : base(assemblyElement, cancelThunk) + { + _innerMessageSink = innerMessageSink; + _completionMessages = completionMessages; + + ExecutionSummary = new ExecutionSummary(); + } + + public ExecutionSummary ExecutionSummary { get; private set; } + + protected override bool Visit(ITestAssemblyFinished assemblyFinished) + { + var result = base.Visit(assemblyFinished); + + ExecutionSummary = new ExecutionSummary + { + Total = assemblyFinished.TestsRun, + Failed = assemblyFinished.TestsFailed, + Skipped = assemblyFinished.TestsSkipped, + Time = assemblyFinished.ExecutionTime, + Errors = Errors + }; + + if (_completionMessages != null) + _completionMessages.TryAdd(Path.GetFileNameWithoutExtension(assemblyFinished.TestAssembly.Assembly.AssemblyPath), ExecutionSummary); + + return result; + } + + public override bool OnMessage(IMessageSinkMessage message) + { + var result = base.OnMessage(message); + result = _innerMessageSink.OnMessage(message) || result; + return result; + } + } +} diff --git a/src/dotnet-test-xunit/Visitors/DiagnosticMessageVisitor.cs b/src/dotnet-test-xunit/Visitors/DiagnosticMessageVisitor.cs new file mode 100644 index 0000000..99aff3d --- /dev/null +++ b/src/dotnet-test-xunit/Visitors/DiagnosticMessageVisitor.cs @@ -0,0 +1,38 @@ +using System; +using Xunit.Abstractions; + +namespace Xunit.Runner.DotNet +{ + public class DiagnosticMessageVisitor : TestMessageVisitor + { + readonly string _assemblyDisplayName; + readonly object _consoleLock; + readonly bool _noColor; + readonly bool _showDiagnostics; + + public DiagnosticMessageVisitor(object consoleLock, string assemblyDisplayName, bool showDiagnostics, bool noColor) + { + _noColor = noColor; + _consoleLock = consoleLock; + _assemblyDisplayName = assemblyDisplayName; + _showDiagnostics = showDiagnostics; + } + + protected override bool Visit(IDiagnosticMessage diagnosticMessage) + { + if (_showDiagnostics) + lock (_consoleLock) + { + if (!_noColor) + Console.ForegroundColor = ConsoleColor.Yellow; + + Console.WriteLine(" {0}: {1}", _assemblyDisplayName, diagnosticMessage.Message); + + if (!_noColor) + Console.ForegroundColor = ConsoleColor.Gray; + } + + return base.Visit(diagnosticMessage); + } + } +} diff --git a/src/dotnet-test-xunit/dotnet-test-xunit.xproj b/src/dotnet-test-xunit/dotnet-test-xunit.xproj new file mode 100644 index 0000000..f9b4548 --- /dev/null +++ b/src/dotnet-test-xunit/dotnet-test-xunit.xproj @@ -0,0 +1,16 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 002d321e-170e-4e55-bdd1-77c6353a6eb5 + Xunit.Runner.Dnx + + + 2.0 + + + \ No newline at end of file diff --git a/src/dotnet-test-xunit/project.json b/src/dotnet-test-xunit/project.json new file mode 100644 index 0000000..e65d5d2 --- /dev/null +++ b/src/dotnet-test-xunit/project.json @@ -0,0 +1,43 @@ +{ + "authors": [ "James Newkirk", "Brad Wilson" ], + "title": "xUnit.net [Runner: dotnet]", + "description": "Console and Visual Studio runner for xUnit.net.\r\n\r\nSupported platforms:\r\n- dotnet 5.4+", + "name": "dotnet-test-xunit", + "projectUrl": "https://github.com/xunit/dnx.xunit", + "iconUrl": "https://raw.githubusercontent.com/xunit/media/master/logo-512-transparent.png", + "licenseUrl": "https://raw.githubusercontent.com/xunit/xunit/master/license.txt", + "requireLicenseAcceptance": false, + "developmentDependency": true, + "version": "99.99.99-dev", + "compilationOptions": { + "warningsAsErrors": true, + "emitEntryPoint": true + }, + "frameworks": { + "dnxcore50": { + "dependencies": { + "System.Linq.Expressions": "4.0.11-rc2-*", + "System.Security.Cryptography.Algorithms": "4.0.0-beta-*", + "System.Threading.ThreadPool": "4.0.10-beta-*", + "System.Xml.XDocument": "4.0.11-beta-*" + } + } + }, + "dependencies": { + "Microsoft.NETCore.Runtime": "1.0.1-beta-*", + "Microsoft.Extensions.Testing.Abstractions": "1.0.0-*", + "Microsoft.DotNet.ProjectModel": "1.0.0-*", + "Microsoft.DotNet.ProjectModel.Loader": "1.0.0-*", + "Microsoft.DotNet.Cli.Utils": "1.0.0-*", + "Microsoft.Extensions.CommandLineUtils.Sources": "1.0.0-*", + "NuGet.Packaging.Core": "3.3.0-rtm-*", + "xunit.runner.reporters": "2.1.0-rc1-*" + }, + "repository": { + "type": "git", + "url": "https://github.com/xunit/dnx.xunit" + }, + "packInclude": { + "lib/dnxcore50/": [ "bin/Debug/dnxcore50/dotnet-test-xunit.deps", "bin/Debug/dnxcore50/dotnet-test-xunit.exe" ] + } +} \ No newline at end of file diff --git a/test/test.xunit.runner.dnx/CommandLineTests.cs b/test/test.xunit.runner.dnx/CommandLineTests.cs new file mode 100644 index 0000000..cf66008 --- /dev/null +++ b/test/test.xunit.runner.dnx/CommandLineTests.cs @@ -0,0 +1,716 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; +using Xunit.Abstractions; +using Xunit.Runner.DotNet; + +public class CommandLineTests +{ + public class UnknownSwitch + { + [Fact] + public static void UnknownSwitchThrows() + { + var exception = Record.Exception(() => TestableCommandLine.Parse(new[] { "assemblyName.dll", "-unknown" })); + + Assert.IsType(exception); + Assert.Equal("unknown option: -unknown", exception.Message); + } + } + + public class Filename + { + [Fact] + public static void MissingAssemblyFileNameThrows() + { + var exception = Record.Exception(() => TestableCommandLine.Parse()); + + Assert.IsType(exception); + Assert.Equal("must specify at least one assembly", exception.Message); + } + + [Fact] + public static void ConfigFileDoesNotExist_Throws() + { + var arguments = new[] { "assemblyName.dll", "badConfig.json" }; + + var exception = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(exception); + Assert.Equal("config file not found: badConfig.json", exception.Message); + } + } + + public class DiagnosticsOption + { + [Fact] + public static void DiagnosticsNotSetDebugIsFalse() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.False(commandLine.DiagnosticMessages); + } + + [Fact] + public static void DiagnosticsSetDebugIsTrue() + { + var arguments = new[] { "assemblyName.dll", "-diagnostics" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.DiagnosticMessages); + } + } + + public class DebugOption + { + [Fact] + public static void DebugNotSetDebugIsFalse() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.False(commandLine.Debug); + } + + [Fact] + public static void DebugSetDebugIsTrue() + { + var arguments = new[] { "assemblyName.dll", "-debug" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.Debug); + } + } + + public class MaxThreadsOption + { + [Fact] + public static void DefaultValueIsNull() + { + var commandLine = TestableCommandLine.Parse("assemblyName.dll"); + + Assert.Null(commandLine.MaxParallelThreads); + } + + [Fact] + public static void MissingValue() + { + var ex = Assert.Throws(() => TestableCommandLine.Parse("assemblyName.dll", "-maxthreads")); + + Assert.Equal("missing argument for -maxthreads", ex.Message); + } + + [Theory] + [InlineData("0")] + [InlineData("abc")] + public static void InvalidValues(string value) + { + var ex = Assert.Throws(() => TestableCommandLine.Parse("assemblyName.dll", "-maxthreads", value)); + + Assert.Equal("incorrect argument value for -maxthreads (must be 'default', 'unlimited', or a positive number)", ex.Message); + } + + [Theory] + [InlineData("default", 0)] + [InlineData("unlimited", -1)] + [InlineData("16", 16)] + public static void ValidValues(string value, int expected) + { + var commandLine = TestableCommandLine.Parse("assemblyName.dll", "-maxthreads", value); + + Assert.Equal(expected, commandLine.MaxParallelThreads); + } + } + + public class NoLogoOption + { + [Fact] + public static void NoLogoNotSetNoLogoIsFalse() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.False(commandLine.NoLogo); + } + + [Fact] + public static void NoLogoSetNoLogoIsTrue() + { + var arguments = new[] { "assemblyName.dll", "-nologo" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.NoLogo); + } + } + + public class WaitOption + { + [Fact] + public static void WaitOptionNotPassedWaitFalse() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.False(commandLine.Wait); + } + + [Fact] + public static void WaitOptionWaitIsTrue() + { + var arguments = new[] { "assemblyName.dll", "-wait" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.Wait); + } + + [Fact] + public static void WaitOptionIgnoreCaseWaitIsTrue() + { + var arguments = new[] { "assemblyName.dll", "-wAiT" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.Wait); + } + } + + public class TraitArgument + { + [Fact] + public static void TraitArgumentNotPassed() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(0, commandLine.Project.Filters.IncludedTraits.Count); + } + + [Fact] + public static void SingleValidTraitArgument() + { + var arguments = new[] { "assemblyName.dll", "-trait", "foo=bar" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(1, commandLine.Project.Filters.IncludedTraits.Count); + Assert.Equal(1, commandLine.Project.Filters.IncludedTraits["foo"].Count()); + Assert.Contains("bar", commandLine.Project.Filters.IncludedTraits["foo"]); + } + + [Fact] + public static void MultipleValidTraitArguments_SameName() + { + var arguments = new[] { "assemblyName.dll", "-trait", "foo=bar", "-trait", "foo=baz" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(1, commandLine.Project.Filters.IncludedTraits.Count); + Assert.Equal(2, commandLine.Project.Filters.IncludedTraits["foo"].Count()); + Assert.Contains("bar", commandLine.Project.Filters.IncludedTraits["foo"]); + Assert.Contains("baz", commandLine.Project.Filters.IncludedTraits["foo"]); + } + + [Fact] + public static void MultipleValidTraitArguments_DifferentName() + { + var arguments = new[] { "assemblyName.dll", "-trait", "foo=bar", "-trait", "baz=biff" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(2, commandLine.Project.Filters.IncludedTraits.Count); + Assert.Equal(1, commandLine.Project.Filters.IncludedTraits["foo"].Count()); + Assert.Contains("bar", commandLine.Project.Filters.IncludedTraits["foo"]); + Assert.Equal(1, commandLine.Project.Filters.IncludedTraits["baz"].Count()); + Assert.Contains("biff", commandLine.Project.Filters.IncludedTraits["baz"]); + } + + [Fact] + public static void MissingOptionValue() + { + var arguments = new[] { "assemblyName.dll", "-trait" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("missing argument for -trait", ex.Message); + } + + [Fact] + public static void OptionValueMissingEquals() + { + var arguments = new[] { "assemblyName.dll", "-trait", "foobar" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -trait (should be \"name=value\")", ex.Message); + } + + [Fact] + public static void OptionValueMissingName() + { + var arguments = new[] { "assemblyName.dll", "-trait", "=bar" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -trait (should be \"name=value\")", ex.Message); + } + + [Fact] + public static void OptionNameMissingValue() + { + var arguments = new[] { "assemblyName.dll", "-trait", "foo=" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -trait (should be \"name=value\")", ex.Message); + } + + [Fact] + public static void TooManyEqualsSigns() + { + var arguments = new[] { "assemblyName.dll", "-trait", "foo=bar=baz" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -trait (should be \"name=value\")", ex.Message); + } + } + + public class MinusTraitArgument + { + [Fact] + public static void TraitArgumentNotPassed() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(0, commandLine.Project.Filters.ExcludedTraits.Count); + } + + [Fact] + public static void SingleValidTraitArgument() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "foo=bar" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(1, commandLine.Project.Filters.ExcludedTraits.Count); + Assert.Equal(1, commandLine.Project.Filters.ExcludedTraits["foo"].Count()); + Assert.Contains("bar", commandLine.Project.Filters.ExcludedTraits["foo"]); + } + + [Fact] + public static void MultipleValidTraitArguments_SameName() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "foo=bar", "-notrait", "foo=baz" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(1, commandLine.Project.Filters.ExcludedTraits.Count); + Assert.Equal(2, commandLine.Project.Filters.ExcludedTraits["foo"].Count()); + Assert.Contains("bar", commandLine.Project.Filters.ExcludedTraits["foo"]); + Assert.Contains("baz", commandLine.Project.Filters.ExcludedTraits["foo"]); + } + + [Fact] + public static void MultipleValidTraitArguments_DifferentName() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "foo=bar", "-notrait", "baz=biff" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(2, commandLine.Project.Filters.ExcludedTraits.Count); + Assert.Equal(1, commandLine.Project.Filters.ExcludedTraits["foo"].Count()); + Assert.Contains("bar", commandLine.Project.Filters.ExcludedTraits["foo"]); + Assert.Equal(1, commandLine.Project.Filters.ExcludedTraits["baz"].Count()); + Assert.Contains("biff", commandLine.Project.Filters.ExcludedTraits["baz"]); + } + + [Fact] + public static void MissingOptionValue() + { + var arguments = new[] { "assemblyName.dll", "-notrait" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("missing argument for -notrait", ex.Message); + } + + [Fact] + public static void OptionValueMissingEquals() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "foobar" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -notrait (should be \"name=value\")", ex.Message); + } + + [Fact] + public static void OptionValueMissingName() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "=bar" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -notrait (should be \"name=value\")", ex.Message); + } + + [Fact] + public static void OptionNameMissingValue() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "foo=" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -notrait (should be \"name=value\")", ex.Message); + } + + [Fact] + public static void TooManyEqualsSigns() + { + var arguments = new[] { "assemblyName.dll", "-notrait", "foo=bar=baz" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("incorrect argument format for -notrait (should be \"name=value\")", ex.Message); + } + } + + public class MethodArgument + { + [Fact] + public static void MethodArgumentNotPassed() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(0, commandLine.Project.Filters.IncludedMethods.Count); + } + + [Fact] + public static void SingleValidMethodArgument() + { + const string name = "Namespace.Class.Method1"; + + var arguments = new[] { "assemblyName.dll", "-method", name }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(1, commandLine.Project.Filters.IncludedMethods.Count); + Assert.True(commandLine.Project.Filters.IncludedMethods.Contains(name)); + } + + [Fact] + public static void MultipleValidMethodArguments() + { + const string name1 = "Namespace.Class.Method1"; + const string name2 = "Namespace.Class.Method2"; + + var arguments = new[] { "assemblyName.dll", "-method", name1, "-method", name2 }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(2, commandLine.Project.Filters.IncludedMethods.Count); + Assert.True(commandLine.Project.Filters.IncludedMethods.Contains(name1)); + Assert.True(commandLine.Project.Filters.IncludedMethods.Contains(name2)); + } + + [Fact] + public static void MissingOptionValue() + { + var arguments = new[] { "assemblyName.dll", "-method" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("missing argument for -method", ex.Message); + } + } + + public class ClassArgument + { + [Fact] + public static void ClassArgumentNotPassed() + { + var arguments = new[] { "assemblyName.dll" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(0, commandLine.Project.Filters.IncludedMethods.Count); + } + + [Fact] + public static void SingleValidClassArgument() + { + const string name = "Namespace.Class"; + + var arguments = new[] { "assemblyName.dll", "-class", name }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(1, commandLine.Project.Filters.IncludedClasses.Count); + Assert.True(commandLine.Project.Filters.IncludedClasses.Contains(name)); + } + + [Fact] + public static void MultipleValidClassArguments() + { + const string name1 = "Namespace.Class1"; + const string name2 = "Namespace.Class2"; + + var arguments = new[] { "assemblyName.dll", "-class", name1, "-class", name2 }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(2, commandLine.Project.Filters.IncludedClasses.Count); + Assert.True(commandLine.Project.Filters.IncludedClasses.Contains(name1)); + Assert.True(commandLine.Project.Filters.IncludedClasses.Contains(name2)); + } + + [Fact] + public static void MissingOptionValue() + { + var arguments = new[] { "assemblyName.dll", "-class" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("missing argument for -class", ex.Message); + } + } + + public class ParallelizationOptions + { + [Fact] + public static void ParallelizationOptionsAreNullByDefault() + { + var project = TestableCommandLine.Parse("assemblyName.dll"); + + Assert.Null(project.ParallelizeAssemblies); + Assert.Null(project.ParallelizeTestCollections); + } + + [Fact] + public static void FailsWithoutOptionOrWithIncorrectOptions() + { + var aex1 = Assert.Throws(() => TestableCommandLine.Parse("assemblyName.dll", "-parallel")); + Assert.Equal("missing argument for -parallel", aex1.Message); + + var aex2 = Assert.Throws(() => TestableCommandLine.Parse("assemblyName.dll", "-parallel", "nonsense")); + Assert.Equal("incorrect argument value for -parallel", aex2.Message); + } + + [Theory] + [InlineData("None", false, false)] + [InlineData("Assemblies", true, false)] + [InlineData("Collections", false, true)] + [InlineData("All", true, true)] + public static void ParallelCanBeTurnedOn(string parallelOption, bool expectedAssemblyParallelization, bool expectedCollectionsParallelization) + { + + var project = TestableCommandLine.Parse("assemblyName.dll", "-parallel", parallelOption); + + Assert.Equal(expectedAssemblyParallelization, project.ParallelizeAssemblies); + Assert.Equal(expectedCollectionsParallelization, project.ParallelizeTestCollections); + } + } + + public class Reporters + { + [Fact] + public void NoReporters_UsesDefaultReporter() + { + var commandLine = TestableCommandLine.Parse("assemblyName.dll"); + + Assert.IsType(commandLine.Reporter); + } + + [Fact] + public void NoExplicitReporter_NoEnvironmentallyEnabledReporters_UsesDefaultReporter() + { + var implicitReporter = new MockRunnerReporter(isEnvironmentallyEnabled: false); + + var commandLine = TestableCommandLine.Parse(new[] { implicitReporter }, "assemblyName.dll"); + + Assert.IsType(commandLine.Reporter); + } + + [Fact] + public void ExplicitReporter_NoEnvironmentalOverride_UsesExplicitReporter() + { + var explicitReporter = new MockRunnerReporter("switch"); + + var commandLine = TestableCommandLine.Parse(new[] { explicitReporter }, "assemblyName.dll", "-switch"); + + Assert.Same(explicitReporter, commandLine.Reporter); + } + + [Fact] + public void ExplicitReporter_WithEnvironmentalOverride_UsesEnvironmentalOverride() + { + var explicitReporter = new MockRunnerReporter("switch"); + var implicitReporter = new MockRunnerReporter(isEnvironmentallyEnabled: true); + + var commandLine = TestableCommandLine.Parse(new[] { explicitReporter, implicitReporter }, "assemblyName.dll", "-switch"); + + Assert.Same(implicitReporter, commandLine.Reporter); + } + + [Fact] + public void NoExplicitReporter_SelectsFirstEnvironmentallyEnabledReporter() + { + var explicitReporter = new MockRunnerReporter("switch"); + var implicitReporter1 = new MockRunnerReporter(isEnvironmentallyEnabled: true); + var implicitReporter2 = new MockRunnerReporter(isEnvironmentallyEnabled: true); + + var commandLine = TestableCommandLine.Parse(new[] { explicitReporter, implicitReporter1, implicitReporter2 }, "assemblyName.dll"); + + Assert.Same(implicitReporter1, commandLine.Reporter); + } + } + + public class Transform + { + [Fact] + public static void OutputMissingFilename() + { + var arguments = new[] { "assemblyName.dll", "-xml" }; + + var ex = Record.Exception(() => TestableCommandLine.Parse(arguments)); + + Assert.IsType(ex); + Assert.Equal("missing filename for -xml", ex.Message); + } + + [Fact] + public static void Output() + { + var arguments = new[] { "assemblyName.dll", "-xml", "foo.xml" }; + + var commandLine = TestableCommandLine.Parse(arguments); + + var output = Assert.Single(commandLine.Project.Output); + Assert.Equal("xml", output.Key); + Assert.Equal("foo.xml", output.Value); + } + } + + public class DesignTimeSwitch + { + [Theory] + [InlineData("-designtime")] + [InlineData("--designtime")] + public static void DesignTime(string arg) + { + var arguments = new[] { "assemblyName.dll", arg }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.DesignTime); + } + } + + public class ListSwitch + { + [Theory] + [InlineData("-list")] + [InlineData("--list")] + public static void List(string arg) + { + var arguments = new[] { "assemblyName.dll", arg }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.True(commandLine.List); + } + } + + public class TestArgument + { + [Fact] + public static void TestUniqueNames() + { + var arguments = new[] + { + "assemblyName.dll", + "-test", + "foo", + "--test", + "bar", + "--test", + "baz", + }; + + var commandLine = TestableCommandLine.Parse(arguments); + + Assert.Equal(3, commandLine.DesignTimeTestUniqueNames.Count); + Assert.Contains("foo", commandLine.DesignTimeTestUniqueNames); + Assert.Contains("bar", commandLine.DesignTimeTestUniqueNames); + Assert.Contains("baz", commandLine.DesignTimeTestUniqueNames); + } + } + + class MockRunnerReporter : IRunnerReporter + { + // Need this here so the runner doesn't complain that this isn't a legal runner reporter. :-p + public MockRunnerReporter() { } + + public MockRunnerReporter(string runnerSwitch = null, bool isEnvironmentallyEnabled = false) + { + RunnerSwitch = runnerSwitch; + IsEnvironmentallyEnabled = isEnvironmentallyEnabled; + } + + public string Description { get { return "The description"; } } + + public bool IsEnvironmentallyEnabled { get; private set; } + + public string RunnerSwitch { get; private set; } + + public IMessageSink CreateMessageHandler(IRunnerLogger logger) + { + throw new NotImplementedException(); + } + } + + class TestableCommandLine : CommandLine + { + private TestableCommandLine(IReadOnlyList reporters, params string[] arguments) + : base(reporters, arguments, filename => filename != "badConfig.json") + { + } + + public static TestableCommandLine Parse(params string[] arguments) + { + return new TestableCommandLine(new IRunnerReporter[0], arguments); + } + + public new static TestableCommandLine Parse(IReadOnlyList reporters, params string[] arguments) + { + return new TestableCommandLine(reporters, arguments); + } + } +} diff --git a/test/test.xunit.runner.dnx/project.json b/test/test.xunit.runner.dnx/project.json new file mode 100644 index 0000000..84ec9a1 --- /dev/null +++ b/test/test.xunit.runner.dnx/project.json @@ -0,0 +1,10 @@ +{ + "frameworks": { + "dnxcore50": { } + }, + "testRunner":"xunit", + "dependencies": { + "xunit": "2.1.0", + "dotnet-test-xunit": "99.99.99-dev" + } +} diff --git a/test/test.xunit.runner.dnx/test.xunit.runner.dnx.xproj b/test/test.xunit.runner.dnx/test.xunit.runner.dnx.xproj new file mode 100644 index 0000000..d70e818 --- /dev/null +++ b/test/test.xunit.runner.dnx/test.xunit.runner.dnx.xproj @@ -0,0 +1,19 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 838070ab-04f2-4a37-9c86-91669e633f70 + Xunit.Runner.Dnx + + + 2.0 + + + + + + \ No newline at end of file diff --git a/test/test.xunit.runner.dnx/xunit.runner.json b/test/test.xunit.runner.dnx/xunit.runner.json new file mode 100644 index 0000000..0630182 --- /dev/null +++ b/test/test.xunit.runner.dnx/xunit.runner.json @@ -0,0 +1,3 @@ +{ + "diagnosticMessages": true +} diff --git a/tools/dnvm.ps1 b/tools/dnvm.ps1 new file mode 100644 index 0000000..e42f5ea --- /dev/null +++ b/tools/dnvm.ps1 @@ -0,0 +1,1911 @@ +#Requires -Version 2 + +if (Test-Path env:WEBSITE_SITE_NAME) +{ + # This script is run in Azure Web Sites + # Disable progress indicator + $ProgressPreference = "SilentlyContinue" +} + +$ScriptPath = $MyInvocation.MyCommand.Definition + +$Script:UseWriteHost = $true +function _WriteDebug($msg) { + if($Script:UseWriteHost) { + try { + Write-Debug $msg + } catch { + $Script:UseWriteHost = $false + _WriteDebug $msg + } + } +} + +function _WriteOut { + param( + [Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true)][string]$msg, + [Parameter(Mandatory=$false)][ConsoleColor]$ForegroundColor, + [Parameter(Mandatory=$false)][ConsoleColor]$BackgroundColor, + [Parameter(Mandatory=$false)][switch]$NoNewLine) + + if($__TestWriteTo) { + $cur = Get-Variable -Name $__TestWriteTo -ValueOnly -Scope Global -ErrorAction SilentlyContinue + $val = $cur + "$msg" + if(!$NoNewLine) { + $val += [Environment]::NewLine + } + Set-Variable -Name $__TestWriteTo -Value $val -Scope Global -Force + return + } + + if(!$Script:UseWriteHost) { + if(!$msg) { + $msg = "" + } + if($NoNewLine) { + [Console]::Write($msg) + } else { + [Console]::WriteLine($msg) + } + } + else { + try { + if(!$ForegroundColor) { + $ForegroundColor = $host.UI.RawUI.ForegroundColor + } + if(!$BackgroundColor) { + $BackgroundColor = $host.UI.RawUI.BackgroundColor + } + + Write-Host $msg -ForegroundColor:$ForegroundColor -BackgroundColor:$BackgroundColor -NoNewLine:$NoNewLine + } catch { + $Script:UseWriteHost = $false + _WriteOut $msg + } + } +} + +### Constants +$ProductVersion="1.0.0" +$BuildVersion="rc1-15527" +$Authors="Microsoft Open Technologies, Inc." + +# If the Version hasn't been replaced... +# We can't compare directly with the build version token +# because it'll just get replaced here as well :) +if($BuildVersion.StartsWith("{{")) { + # We're being run from source code rather than the "compiled" artifact + $BuildVersion = "HEAD" +} +$FullVersion="$ProductVersion-$BuildVersion" + +Set-Variable -Option Constant "CommandName" ([IO.Path]::GetFileNameWithoutExtension($ScriptPath)) +Set-Variable -Option Constant "CommandFriendlyName" ".NET Version Manager" +Set-Variable -Option Constant "DefaultUserDirectoryName" ".dnx" +Set-Variable -Option Constant "DefaultGlobalDirectoryName" "Microsoft DNX" +Set-Variable -Option Constant "OldUserDirectoryNames" @(".kre", ".k") +Set-Variable -Option Constant "RuntimePackageName" "dnx" +Set-Variable -Option Constant "DefaultFeed" "https://www.nuget.org/api/v2" +Set-Variable -Option Constant "DefaultFeedKey" "DNX_FEED" +Set-Variable -Option Constant "DefaultUnstableFeed" "https://www.myget.org/F/aspnetvnext/api/v2" +Set-Variable -Option Constant "DefaultUnstableFeedKey" "DNX_UNSTABLE_FEED" +Set-Variable -Option Constant "CrossGenCommand" "dnx-crossgen" +Set-Variable -Option Constant "OldCrossGenCommand" "k-crossgen" +Set-Variable -Option Constant "CommandPrefix" "dnvm-" +Set-Variable -Option Constant "DefaultArchitecture" "x86" +Set-Variable -Option Constant "DefaultRuntime" "clr" +Set-Variable -Option Constant "AliasExtension" ".txt" +Set-Variable -Option Constant "DefaultOperatingSystem" "win" + +# These are intentionally using "%" syntax. The environment variables are expanded whenever the value is used. +Set-Variable -Option Constant "OldUserHomes" @("%USERPROFILE%\.kre", "%USERPROFILE%\.k") +Set-Variable -Option Constant "DefaultUserHome" "%USERPROFILE%\$DefaultUserDirectoryName" +Set-Variable -Option Constant "HomeEnvVar" "DNX_HOME" + +Set-Variable -Option Constant "RuntimeShortFriendlyName" "DNX" + +Set-Variable -Option Constant "DNVMUpgradeUrl" "https://raw.githubusercontent.com/aspnet/Home/dev/dnvm.ps1" + +Set-Variable -Option Constant "AsciiArt" @" + ___ _ ___ ____ ___ + / _ \/ |/ / | / / |/ / + / // / /| |/ / /|_/ / +/____/_/|_/ |___/_/ /_/ +"@ + +$ExitCodes = @{ + "Success" = 0 + "AliasDoesNotExist" = 1001 + "UnknownCommand" = 1002 + "InvalidArguments" = 1003 + "OtherError" = 1004 + "NoSuchPackage" = 1005 + "NoRuntimesOnFeed" = 1006 +} + +$ColorScheme = $DnvmColors +if(!$ColorScheme) { + $ColorScheme = @{ + "Banner"=[ConsoleColor]::Cyan + "RuntimeName"=[ConsoleColor]::Yellow + "Help_Header"=[ConsoleColor]::Yellow + "Help_Switch"=[ConsoleColor]::Green + "Help_Argument"=[ConsoleColor]::Cyan + "Help_Optional"=[ConsoleColor]::Gray + "Help_Command"=[ConsoleColor]::DarkYellow + "Help_Executable"=[ConsoleColor]::DarkYellow + "Feed_Name"=[ConsoleColor]::Cyan + "Warning" = [ConsoleColor]::Yellow + "Error" = [ConsoleColor]::Red + "ActiveRuntime" = [ConsoleColor]::Cyan + } +} + +Set-Variable -Option Constant "OptionPadding" 20 +Set-Variable -Option Constant "CommandPadding" 15 + +# Test Control Variables +if($__TeeTo) { + _WriteDebug "Saving output to '$__TeeTo' variable" + Set-Variable -Name $__TeeTo -Value "" -Scope Global -Force +} + +# Commands that have been deprecated but do still work. +$DeprecatedCommands = @("unalias") + +# Load Environment variables +$RuntimeHomes = $(if (Test-Path "env:\$HomeEnvVar") {Get-Content "env:\$HomeEnvVar"}) +$UserHome = $env:DNX_USER_HOME +$GlobalHome = $env:DNX_GLOBAL_HOME +$ActiveFeed = $(if (Test-Path "env:\$DefaultFeedKey") {Get-Content "env:\$DefaultFeedKey"}) +$ActiveUnstableFeed = $(if (Test-Path "env:\$DefaultUnstableFeedKey") {Get-Content "env:\$DefaultUnstableFeedKey"}) + +# Default Exit Code +$Script:ExitCode = $ExitCodes.Success + +############################################################ +### Below this point, the terms "DNVM", "DNX", etc. ### +### should never be used. Instead, use the Constants ### +### defined above ### +############################################################ +# An exception to the above: The commands are defined by functions +# named "dnvm-[command name]" so that extension functions can be added + +$StartPath = $env:PATH + +if($CmdPathFile) { + if(Test-Path $CmdPathFile) { + _WriteDebug "Cleaning old CMD PATH file: $CmdPathFile" + Remove-Item $CmdPathFile -Force + } + _WriteDebug "Using CMD PATH file: $CmdPathFile" +} + +# Determine the default installation directory (UserHome) +if(!$UserHome) { + if ($RuntimeHomes) { + _WriteDebug "Detecting User Home..." + $pf = $env:ProgramFiles + if(Test-Path "env:\ProgramFiles(x86)") { + $pf32 = Get-Content "env:\ProgramFiles(x86)" + } + + # Canonicalize so we can do StartsWith tests + if(!$pf.EndsWith("\")) { $pf += "\" } + if($pf32 -and !$pf32.EndsWith("\")) { $pf32 += "\" } + + $UserHome = $RuntimeHomes.Split(";") | Where-Object { + # Take the first path that isn't under program files + !($_.StartsWith($pf) -or $_.StartsWith($pf32)) + } | Select-Object -First 1 + + _WriteDebug "Found: $UserHome" + } + + if(!$UserHome) { + $UserHome = "$DefaultUserHome" + } +} +$UserHome = [Environment]::ExpandEnvironmentVariables($UserHome) + +# Determine the default global installation directory (GlobalHome) +if(!$GlobalHome) { + if($env:ProgramData) { + $GlobalHome = "$env:ProgramData\$DefaultGlobalDirectoryName" + } else { + $GlobalHome = "$env:AllUsersProfile\$DefaultGlobalDirectoryName" + } +} +$GlobalHome = [Environment]::ExpandEnvironmentVariables($GlobalHome) + +# Determine where runtimes can exist (RuntimeHomes) +if(!$RuntimeHomes) { + # Set up a default value for the runtime home + $UnencodedHomes = "$UserHome;$GlobalHome" +} elseif ($RuntimeHomes.StartsWith(';')) { + _WriteOut "Ignoring invalid $HomeEnvVar; value was '$RuntimeHomes'" -ForegroundColor $ColorScheme.Warning + Clean-HomeEnv($true) + + # Use default instead. + $UnencodedHomes = "$UserHome;$GlobalHome" +} else { + $UnencodedHomes = $RuntimeHomes +} + +$UnencodedHomes = $UnencodedHomes.Split(";") +$RuntimeHomes = $UnencodedHomes | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_) } +$RuntimeDirs = $RuntimeHomes | ForEach-Object { Join-Path $_ "runtimes" } + +_WriteDebug "" +_WriteDebug "=== Running $CommandName ===" +_WriteDebug "Runtime Homes: $RuntimeHomes" +_WriteDebug "User Home: $UserHome" +$AliasesDir = Join-Path $UserHome "alias" +$RuntimesDir = Join-Path $UserHome "runtimes" +$GlobalRuntimesDir = Join-Path $GlobalHome "runtimes" +$Aliases = $null + +### Helper Functions +# Remove $HomeEnv from process and user environment. +# Called when current value is invalid or after installing files to default location. +function Clean-HomeEnv { + param([switch]$SkipUserEnvironment) + + if (Test-Path "env:\$HomeEnvVar") { + _WriteOut "Removing Process $HomeEnvVar" + Set-Content "env:\$HomeEnvVar" $null + } + + if (!$SkipUserEnvironment -and [Environment]::GetEnvironmentVariable($HomeEnvVar, "User")) { + _WriteOut "Removing User $HomeEnvVar" + [Environment]::SetEnvironmentVariable($HomeEnvVar, $null, "User") + } +} + +# Checks if a specified file exists in the destination folder and if not, copies the file +# to the destination folder. +function Safe-Filecopy { + param( + [Parameter(Mandatory=$true, Position=0)] $Filename, + [Parameter(Mandatory=$true, Position=1)] $SourceFolder, + [Parameter(Mandatory=$true, Position=2)] $DestinationFolder) + + # Make sure the destination folder is created if it doesn't already exist. + if(!(Test-Path $DestinationFolder)) { + _WriteOut "Creating destination folder '$DestinationFolder' ... " + + New-Item -Type Directory $Destination | Out-Null + } + + $sourceFilePath = Join-Path $SourceFolder $Filename + $destFilePath = Join-Path $DestinationFolder $Filename + + if(Test-Path $sourceFilePath) { + _WriteOut "Installing '$Filename' to '$DestinationFolder' ... " + + if (Test-Path $destFilePath) { + _WriteOut " Skipping: file already exists" -ForegroundColor Yellow + } + else { + Copy-Item $sourceFilePath $destFilePath -Force + } + } + else { + _WriteOut "WARNING: Unable to install: Could not find '$Filename' in '$SourceFolder'. " + } +} + +$OSRuntimeDefaults = @{ + "win"="clr"; + "linux"="mono"; + "darwin"="mono"; +} + +$RuntimeBitnessDefaults = @{ + "clr"="x86"; + "coreclr"="x64"; +} + +function GetRuntimeInfo($Architecture, $Runtime, $OS, $Version) { + $runtimeInfo = @{ + "Architecture"="$Architecture"; + "Runtime"="$Runtime"; + "OS"="$OS"; + "Version"="$Version"; + } + + if([String]::IsNullOrEmpty($runtimeInfo.OS)) { + if($runtimeInfo.Runtime -eq "mono"){ + #If OS is empty and you are asking for mono, i.e `dnvm install latest -os mono` then we don't know what OS to pick. It could be Linux or Darwin. + #we could just arbitrarily pick one but it will probably be wrong as often as not. + #If Mono can run on Windows then this error doesn't make sense anymore. + throw "Unable to determine an operating system for a $($runtimeInfo.Runtime) runtime. You must specify which OS to use with the OS parameter." + } + $runtimeInfo.OS = $DefaultOperatingSystem + } + + if($runtimeInfo.OS -eq "osx") { + $runtimeInfo.OS = "darwin" + } + + if([String]::IsNullOrEmpty($runtimeInfo.Runtime)) { + $runtimeInfo.Runtime = $OSRuntimeDefaults.Get_Item($runtimeInfo.OS) + } + + if([String]::IsNullOrEmpty($runtimeInfo.Architecture)) { + $runtimeInfo.Architecture = $RuntimeBitnessDefaults.Get_Item($RuntimeInfo.Runtime) + } + + $runtimeObject = New-Object PSObject -Property $runtimeInfo + + $runtimeObject | Add-Member -MemberType ScriptProperty -Name RuntimeId -Value { + if($this.Runtime -eq "mono") { + "$RuntimePackageName-$($this.Runtime)".ToLowerInvariant() + } else { + "$RuntimePackageName-$($this.Runtime)-$($this.OS)-$($this.Architecture)".ToLowerInvariant() + } + } + + $runtimeObject | Add-Member -MemberType ScriptProperty -Name RuntimeName -Value { + "$($this.RuntimeId).$($this.Version)" + } + + $runtimeObject +} + +function Write-Usage { + _WriteOut -ForegroundColor $ColorScheme.Banner $AsciiArt + _WriteOut "$CommandFriendlyName v$FullVersion" + if(!$Authors.StartsWith("{{")) { + _WriteOut "By $Authors" + } + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Header "usage:" + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Executable " $CommandName" + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command " " + _WriteOut -ForegroundColor $ColorScheme.Help_Argument " []" +} + +function Write-Feeds { + _WriteOut + _WriteOut -ForegroundColor $ColorScheme.Help_Header "Current feed settings:" + _WriteOut -NoNewline -ForegroundColor $ColorScheme.Feed_Name "Default Stable: " + _WriteOut "$DefaultFeed" + _WriteOut -NoNewline -ForegroundColor $ColorScheme.Feed_Name "Default Unstable: " + _WriteOut "$DefaultUnstableFeed" + _WriteOut -NoNewline -ForegroundColor $ColorScheme.Feed_Name "Current Stable Override: " + if($ActiveFeed) { + _WriteOut "$ActiveFeed" + } else { + _WriteOut "" + } + _WriteOut -NoNewline -ForegroundColor $ColorScheme.Feed_Name "Current Unstable Override: " + if($ActiveUnstableFeed) { + _WriteOut "$ActiveUnstableFeed" + } else { + _WriteOut "" + } + _WriteOut + _WriteOut -NoNewline " To use override feeds, set " + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Executable "$DefaultFeedKey" + _WriteOut -NoNewline " and " + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Executable "$DefaultUnstableFeedKey" + _WriteOut -NoNewline " environment keys respectively" + _WriteOut +} + +function Get-RuntimeAlias { + if($Aliases -eq $null) { + _WriteDebug "Scanning for aliases in $AliasesDir" + if(Test-Path $AliasesDir) { + $Aliases = @(Get-ChildItem ($UserHome + "\alias\") | Select-Object @{label='Alias';expression={$_.BaseName}}, @{label='Name';expression={Get-Content $_.FullName }}, @{label='Orphan';expression={-Not (Test-Path ($RuntimesDir + "\" + (Get-Content $_.FullName)))}}) + } else { + $Aliases = @() + } + } + $Aliases +} + +function IsOnPath { + param($dir) + + $env:Path.Split(';') -icontains $dir +} + +function Get-RuntimeAliasOrRuntimeInfo( + [Parameter(Mandatory=$true)][string]$Version, + [Parameter()][string]$Architecture, + [Parameter()][string]$Runtime, + [Parameter()][string]$OS) { + + $aliasPath = Join-Path $AliasesDir "$Version$AliasExtension" + + if(Test-Path $aliasPath) { + $BaseName = Get-Content $aliasPath + + if(!$Architecture) { + $Architecture = Get-PackageArch $BaseName + } + if(!$Runtime) { + $Runtime = Get-PackageRuntime $BaseName + } + $Version = Get-PackageVersion $BaseName + $OS = Get-PackageOS $BaseName + } + + GetRuntimeInfo $Architecture $Runtime $OS $Version +} + +filter List-Parts { + param($aliases, $items) + + $location = "" + + $binDir = Join-Path $_.FullName "bin" + if ((Test-Path $binDir)) { + $location = $_.Parent.FullName + } + $active = IsOnPath $binDir + + $fullAlias="" + $delim="" + + foreach($alias in $aliases) { + if($_.Name.Split('\', 2) -contains $alias.Name) { + $fullAlias += $delim + $alias.Alias + (&{if($alias.Orphan){" (missing)"}}) + $delim = ", " + } + } + + $parts1 = $_.Name.Split('.', 2) + $parts2 = $parts1[0].Split('-', 4) + + if($parts1[0] -eq "$RuntimePackageName-mono") { + $parts2 += "linux/osx" + $parts2 += "x86/x64" + } + + $aliasUsed = "" + if($items) { + $aliasUsed = $items | ForEach-Object { + if($_.Architecture -eq $parts2[3] -and $_.Runtime -eq $parts2[1] -and $_.OperatingSystem -eq $parts2[2] -and $_.Version -eq $parts1[1]) { + return $true; + } + return $false; + } + } + + if($aliasUsed -eq $true) { + $fullAlias = "" + } + + return New-Object PSObject -Property @{ + Active = $active + Version = $parts1[1] + Runtime = $parts2[1] + OperatingSystem = $parts2[2] + Architecture = $parts2[3] + Location = $location + Alias = $fullAlias + } +} + +function Read-Alias($Name) { + _WriteDebug "Listing aliases matching '$Name'" + + $aliases = Get-RuntimeAlias + + $result = @($aliases | Where-Object { !$Name -or ($_.Alias.Contains($Name)) }) + if($Name -and ($result.Length -eq 1)) { + _WriteOut "Alias '$Name' is set to '$($result[0].Name)'" + } elseif($Name -and ($result.Length -eq 0)) { + _WriteOut "Alias does not exist: '$Name'" + $Script:ExitCode = $ExitCodes.AliasDoesNotExist + } else { + $result + } +} + +function Write-Alias { + param( + [Parameter(Mandatory=$true)][string]$Name, + [Parameter(Mandatory=$true)][string]$Version, + [Parameter(Mandatory=$false)][string]$Architecture, + [Parameter(Mandatory=$false)][string]$Runtime, + [Parameter(Mandatory=$false)][string]$OS) + + # If the first character is non-numeric, it's a full runtime name + if(![Char]::IsDigit($Version[0])) { + $runtimeInfo = GetRuntimeInfo $(Get-PackageArch $Version) $(Get-PackageRuntime $Version) $(Get-PackageOS $Version) $(Get-PackageVersion $Version) + } else { + $runtimeInfo = GetRuntimeInfo $Architecture $Runtime $OS $Version + } + + $aliasFilePath = Join-Path $AliasesDir "$Name.txt" + $action = if (Test-Path $aliasFilePath) { "Updating" } else { "Setting" } + + if(!(Test-Path $AliasesDir)) { + _WriteDebug "Creating alias directory: $AliasesDir" + New-Item -Type Directory $AliasesDir | Out-Null + } + _WriteOut "$action alias '$Name' to '$($runtimeInfo.RuntimeName)'" + $runtimeInfo.RuntimeName | Out-File $aliasFilePath ascii +} + +function Delete-Alias { + param( + [Parameter(Mandatory=$true)][string]$Name) + + $aliasPath = Join-Path $AliasesDir "$Name.txt" + if (Test-Path -literalPath "$aliasPath") { + _WriteOut "Removing alias $Name" + + # Delete with "-Force" because we already confirmed above + Remove-Item -literalPath $aliasPath -Force + } else { + _WriteOut "Cannot remove alias '$Name'. It does not exist." + $Script:ExitCode = $ExitCodes.AliasDoesNotExist # Return non-zero exit code for scripting + } +} + +function Apply-Proxy { +param( + [System.Net.WebClient] $wc, + [string]$Proxy +) + if (!$Proxy) { + $Proxy = $env:http_proxy + } + if ($Proxy) { + $wp = New-Object System.Net.WebProxy($Proxy) + $pb = New-Object UriBuilder($Proxy) + if (!$pb.UserName) { + $wp.Credentials = [System.Net.CredentialCache]::DefaultCredentials + } else { + $wp.Credentials = New-Object System.Net.NetworkCredential($pb.UserName, $pb.Password) + } + $wc.Proxy = $wp + } +} + +function Find-Package { + param( + $runtimeInfo, + [string]$Feed, + [string]$Proxy + ) + $url = "$Feed/Packages()?`$filter=Id eq '$($runtimeInfo.RuntimeId)' and Version eq '$($runtimeInfo.Version)'" + Invoke-NuGetWebRequest $runtimeInfo.RuntimeId $url $Proxy +} + +function Find-Latest { + param( + $runtimeInfo, + [Parameter(Mandatory=$true)] + [string]$Feed, + [string]$Proxy + ) + + _WriteOut "Determining latest version" + $RuntimeId = $runtimeInfo.RuntimeId + _WriteDebug "Latest RuntimeId: $RuntimeId" + $url = "$Feed/GetUpdates()?packageIds=%27$RuntimeId%27&versions=%270.0%27&includePrerelease=true&includeAllVersions=false" + Invoke-NuGetWebRequest $RuntimeId $url $Proxy +} + +function Invoke-NuGetWebRequest { + param ( + [string]$RuntimeId, + [string]$Url, + [string]$Proxy + ) + # NOTE: DO NOT use Invoke-WebRequest. It requires PowerShell 4.0! + + $wc = New-Object System.Net.WebClient + Apply-Proxy $wc -Proxy:$Proxy + _WriteDebug "Downloading $Url ..." + try { + [xml]$xml = $wc.DownloadString($Url) + } catch { + $Script:ExitCode = $ExitCodes.NoRuntimesOnFeed + throw "Unable to find any runtime packages on the feed!" + } + + $version = Select-Xml "//d:Version" -Namespace @{d='http://schemas.microsoft.com/ado/2007/08/dataservices'} $xml + if($version) { + $downloadUrl = (Select-Xml "//d:content/@src" -Namespace @{d='http://www.w3.org/2005/Atom'} $xml).Node.value + _WriteDebug "Found $version at $downloadUrl" + @{ Version = $version; DownloadUrl = $downloadUrl } + } else { + throw "There are no runtimes matching the name $RuntimeId on feed $feed." + } +} + +function Get-PackageVersion() { + param( + [string] $runtimeFullName + ) + return $runtimeFullName -replace '[^.]*.(.*)', '$1' +} + +function Get-PackageRuntime() { + param( + [string] $runtimeFullName + ) + return $runtimeFullName -replace "$RuntimePackageName-([^-]*).*", '$1' +} + +function Get-PackageArch() { + param( + [string] $runtimeFullName + ) + return $runtimeFullName -replace "$RuntimePackageName-[^-]*-[^-]*-([^.]*).*", '$1' +} + +function Get-PackageOS() { + param( + [string] $runtimeFullName + ) + $runtimeFullName -replace "$RuntimePackageName-[^-]*-([^-]*)-[^.]*.*", '$1' +} + +function Download-Package() { + param( + $runtimeInfo, + [Parameter(Mandatory=$true)] + [string]$DownloadUrl, + [string]$DestinationFile, + [Parameter(Mandatory=$true)] + [string]$Feed, + [string]$Proxy + ) + + _WriteOut "Downloading $($runtimeInfo.RuntimeName) from $feed" + $wc = New-Object System.Net.WebClient + try { + Apply-Proxy $wc -Proxy:$Proxy + _WriteDebug "Downloading $DownloadUrl ..." + + Register-ObjectEvent $wc DownloadProgressChanged -SourceIdentifier WebClient.ProgressChanged -action { + $Global:downloadData = $eventArgs + } | Out-Null + + Register-ObjectEvent $wc DownloadFileCompleted -SourceIdentifier WebClient.ProgressComplete -action { + $Global:downloadData = $eventArgs + $Global:downloadCompleted = $true + } | Out-Null + + $wc.DownloadFileAsync($DownloadUrl, $DestinationFile) + + while(-not $Global:downloadCompleted){ + $percent = $Global:downloadData.ProgressPercentage + $totalBytes = $Global:downloadData.TotalBytesToReceive + $receivedBytes = $Global:downloadData.BytesReceived + If ($percent -ne $null) { + Write-Progress -Activity ("Downloading $RuntimeShortFriendlyName from $DownloadUrl") ` + -Status ("Downloaded $($Global:downloadData.BytesReceived) of $($Global:downloadData.TotalBytesToReceive) bytes") ` + -PercentComplete $percent -Id 2 -ParentId 1 + } + } + + if($Global:downloadData.Error) { + if($Global:downloadData.Error.Response.StatusCode -eq [System.Net.HttpStatusCode]::NotFound){ + throw "The server returned a 404 (NotFound). This is most likely caused by the feed not having the version that you typed. Check that you typed the right version and try again. Other possible causes are the feed doesn't have a $RuntimeShortFriendlyName of the right name format or some other error caused a 404 on the server." + } else { + throw "Unable to download package: {0}" -f $Global:downloadData.Error.Message + } + } + + Write-Progress -Status "Done" -Activity ("Downloading $RuntimeShortFriendlyName from $DownloadUrl") -Id 2 -ParentId 1 -Completed + } + finally { + Remove-Variable downloadData -Scope "Global" + Remove-Variable downloadCompleted -Scope "Global" + Unregister-Event -SourceIdentifier WebClient.ProgressChanged + Unregister-Event -SourceIdentifier WebClient.ProgressComplete + $wc.Dispose() + } +} + +function Unpack-Package([string]$DownloadFile, [string]$UnpackFolder) { + _WriteDebug "Unpacking $DownloadFile to $UnpackFolder" + + $compressionLib = [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') + + if($compressionLib -eq $null) { + try { + # Shell will not recognize nupkg as a zip and throw, so rename it to zip + $runtimeZip = [System.IO.Path]::ChangeExtension($DownloadFile, "zip") + Rename-Item $DownloadFile $runtimeZip + # Use the shell to uncompress the nupkg + $shell_app=new-object -com shell.application + $zip_file = $shell_app.namespace($runtimeZip) + $destination = $shell_app.namespace($UnpackFolder) + $destination.Copyhere($zip_file.items(), 0x14) #0x4 = don't show UI, 0x10 = overwrite files + } + finally { + # Clean up the package file itself. + Remove-Item $runtimeZip -Force + } + } else { + [System.IO.Compression.ZipFile]::ExtractToDirectory($DownloadFile, $UnpackFolder) + + # Clean up the package file itself. + Remove-Item $DownloadFile -Force + } + + If (Test-Path -LiteralPath ($UnpackFolder + "\[Content_Types].xml")) { + Remove-Item -LiteralPath ($UnpackFolder + "\[Content_Types].xml") + } + If (Test-Path ($UnpackFolder + "\_rels\")) { + Remove-Item -LiteralPath ($UnpackFolder + "\_rels\") -Force -Recurse + } + If (Test-Path ($UnpackFolder + "\package\")) { + Remove-Item -LiteralPath ($UnpackFolder + "\package\") -Force -Recurse + } +} + +function Get-RuntimePath($runtimeFullName) { + _WriteDebug "Resolving $runtimeFullName" + foreach($RuntimeHome in $RuntimeHomes) { + $runtimeBin = "$RuntimeHome\runtimes\$runtimeFullName\bin" + _WriteDebug " Candidate $runtimeBin" + if (Test-Path $runtimeBin) { + _WriteDebug " Found in $runtimeBin" + return $runtimeBin + } + } + return $null +} + +function Change-Path() { + param( + [string] $existingPaths, + [string] $prependPath, + [string[]] $removePaths + ) + _WriteDebug "Updating value to prepend '$prependPath' and remove '$removePaths'" + + $newPath = $prependPath + foreach($portion in $existingPaths.Split(';')) { + if(![string]::IsNullOrEmpty($portion)) { + $skip = $portion -eq "" + foreach($removePath in $removePaths) { + if(![string]::IsNullOrEmpty($removePath)) { + $removePrefix = if($removePath.EndsWith("\")) { $removePath } else { "$removePath\" } + + if ($removePath -and (($portion -eq $removePath) -or ($portion.StartsWith($removePrefix)))) { + _WriteDebug " Removing '$portion' because it matches '$removePath'" + $skip = $true + } + } + } + if (!$skip) { + if(![String]::IsNullOrEmpty($newPath)) { + $newPath += ";" + } + $newPath += $portion + } + } + } + return $newPath +} + +function Set-Path() { + param( + [string] $newPath + ) + + $env:PATH = $newPath + + if($CmdPathFile) { + $Parent = Split-Path -Parent $CmdPathFile + if(!(Test-Path $Parent)) { + New-Item -Type Directory $Parent -Force | Out-Null + } + _WriteDebug " Writing PATH file for CMD script" + @" +SET "PATH=$newPath" +"@ | Out-File $CmdPathFile ascii + } +} + +function Ngen-Library( + [Parameter(Mandatory=$true)] + [string]$runtimeBin, + + [ValidateSet("x86", "x64")] + [Parameter(Mandatory=$true)] + [string]$architecture) { + + if ($architecture -eq 'x64') { + $regView = [Microsoft.Win32.RegistryView]::Registry64 + } + elseif ($architecture -eq 'x86') { + $regView = [Microsoft.Win32.RegistryView]::Registry32 + } + else { + _WriteOut "Installation does not understand architecture $architecture, skipping ngen..." + return + } + + $regHive = [Microsoft.Win32.RegistryHive]::LocalMachine + $regKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey($regHive, $regView) + $frameworkPath = $regKey.OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").GetValue("InstallPath") + $ngenExe = Join-Path $frameworkPath 'ngen.exe' + + $ngenCmds = "" + foreach ($bin in Get-ChildItem $runtimeBin -Filter "Microsoft.CodeAnalysis.CSharp.dll") { + $ngenCmds += "$ngenExe install $($bin.FullName);" + } + + $ngenProc = Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList "-ExecutionPolicy unrestricted & $ngenCmds" -Wait -PassThru -WindowStyle Hidden +} + +function Is-Elevated() { + $user = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() + return $user.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") +} + +function Get-ScriptRoot() { + if ($PSVersionTable.PSVersion.Major -ge 3) { + return $PSScriptRoot + } + + return Split-Path $script:MyInvocation.MyCommand.Path -Parent +} + +### Commands + +<# +.SYNOPSIS + Updates DNVM to the latest version. +.PARAMETER Proxy + Use the given address as a proxy when accessing remote server +#> +function dnvm-update-self { + param( + [Parameter(Mandatory=$false)] + [string]$Proxy) + + _WriteOut "Updating $CommandName from $DNVMUpgradeUrl" + $wc = New-Object System.Net.WebClient + Apply-Proxy $wc -Proxy:$Proxy + + $CurrentScriptRoot = Get-ScriptRoot + $dnvmFile = Join-Path $CurrentScriptRoot "dnvm.ps1" + $tempDnvmFile = Join-Path $CurrentScriptRoot "temp" + $backupFilePath = Join-Path $CurrentScriptRoot "dnvm.ps1.bak" + + $wc.DownloadFile($DNVMUpgradeUrl, $tempDnvmFile) + + if(Test-Path $backupFilePath) { + Remove-Item $backupFilePath -Force + } + + Rename-Item $dnvmFile $backupFilePath + Rename-Item $tempDnvmFile $dnvmFile +} + +<# +.SYNOPSIS + Displays a list of commands, and help for specific commands +.PARAMETER Command + A specific command to get help for +#> +function dnvm-help { + [CmdletBinding(DefaultParameterSetName="GeneralHelp")] + param( + [Parameter(Mandatory=$true,Position=0,ParameterSetName="SpecificCommand")][string]$Command, + [switch]$PassThru) + + if($Command) { + $cmd = Get-Command "dnvm-$Command" -ErrorAction SilentlyContinue + if(!$cmd) { + _WriteOut "No such command: $Command" + dnvm-help + $Script:ExitCodes = $ExitCodes.UnknownCommand + return + } + if($Host.Version.Major -lt 3) { + $help = Get-Help "dnvm-$Command" + } else { + $help = Get-Help "dnvm-$Command" -ShowWindow:$false + } + if($PassThru -Or $Host.Version.Major -lt 3) { + $help + } else { + _WriteOut -ForegroundColor $ColorScheme.Help_Header "$CommandName $Command" + _WriteOut " $($help.Synopsis.Trim())" + _WriteOut + _WriteOut -ForegroundColor $ColorScheme.Help_Header "usage:" + $help.Syntax.syntaxItem | ForEach-Object { + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Executable " $CommandName " + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command "$Command" + if($_.parameter) { + $_.parameter | ForEach-Object { + $cmdParam = $cmd.Parameters[$_.name] + $name = $_.name + if($cmdParam.Aliases.Length -gt 0) { + $name = $cmdParam.Aliases | Sort-Object | Select-Object -First 1 + } + + _WriteOut -NoNewLine " " + + if($_.required -ne "true") { + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Optional "[" + } + + if($_.position -eq "Named") { + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Switch "-$name" + } + if($_.parameterValue) { + if($_.position -eq "Named") { + _WriteOut -NoNewLine " " + } + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Argument "<$($_.name)>" + } + + if($_.required -ne "true") { + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Optional "]" + } + } + } + _WriteOut + } + + if($help.parameters -and $help.parameters.parameter) { + _WriteOut + _WriteOut -ForegroundColor $ColorScheme.Help_Header "options:" + $help.parameters.parameter | ForEach-Object { + $cmdParam = $cmd.Parameters[$_.name] + $name = $_.name + if($cmdParam.Aliases.Length -gt 0) { + $name = $cmdParam.Aliases | Sort-Object | Select-Object -First 1 + } + + _WriteOut -NoNewLine " " + + if($_.position -eq "Named") { + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Switch "-$name".PadRight($OptionPadding) + } else { + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Argument "<$($_.name)>".PadRight($OptionPadding) + } + _WriteOut " $($_.description.Text)" + } + } + + if($help.description) { + _WriteOut + _WriteOut -ForegroundColor $ColorScheme.Help_Header "remarks:" + $help.description.Text.Split(@("`r", "`n"), "RemoveEmptyEntries") | + ForEach-Object { _WriteOut " $_" } + } + + if($DeprecatedCommands -contains $Command) { + _WriteOut "This command has been deprecated and should not longer be used" + } + } + } else { + Write-Usage + Write-Feeds + _WriteOut + _WriteOut -ForegroundColor $ColorScheme.Help_Header "commands: " + Get-Command "$CommandPrefix*" | + ForEach-Object { + if($Host.Version.Major -lt 3) { + $h = Get-Help $_.Name + } else { + $h = Get-Help $_.Name -ShowWindow:$false + } + $name = $_.Name.Substring($CommandPrefix.Length) + if($DeprecatedCommands -notcontains $name) { + _WriteOut -NoNewLine " " + _WriteOut -NoNewLine -ForegroundColor $ColorScheme.Help_Command $name.PadRight($CommandPadding) + _WriteOut " $($h.Synopsis.Trim())" + } + } + } +} + +filter ColorActive { + param([string] $color) + $lines = $_.Split("`n") + foreach($line in $lines) { + if($line.Contains("*")){ + _WriteOut -ForegroundColor $ColorScheme.ActiveRuntime $line + } else { + _WriteOut $line + } + } +} + +<# +.SYNOPSIS + Displays the DNVM version. +#> +function dnvm-version { + _WriteOut "$FullVersion" +} + +<# +.SYNOPSIS + Lists available runtimes +.PARAMETER Detailed + Display more detailed information on each runtime +.PARAMETER PassThru + Set this switch to return unformatted powershell objects for use in scripting +#> +function dnvm-list { + param( + [Parameter(Mandatory=$false)][switch]$PassThru, + [Parameter(Mandatory=$false)][switch]$Detailed) + $aliases = Get-RuntimeAlias + + if(-not $PassThru) { + Check-Runtimes + } + + $items = @() + $RuntimeHomes | ForEach-Object { + _WriteDebug "Scanning $_ for runtimes..." + if (Test-Path "$_\runtimes") { + $items += Get-ChildItem "$_\runtimes\$RuntimePackageName-*" | List-Parts $aliases $items + } + } + + $aliases | Where-Object {$_.Orphan} | ForEach-Object { + $items += $_ | Select-Object @{label='Name';expression={$_.Name}}, @{label='FullName';expression={Join-Path $RuntimesDir $_.Name}} | List-Parts $aliases + } + + if($PassThru) { + $items + } else { + if($items) { + #TODO: Probably a better way to do this. + if($Detailed) { + $items | + Sort-Object Version, Runtime, Architecture, OperatingSystem, Alias | + Format-Table -AutoSize -Property @{name="Active";expression={if($_.Active) { "*" } else { "" }};alignment="center"}, "Version", "Runtime", "Architecture", "OperatingSystem", "Alias", "Location" | Out-String| ColorActive + } else { + $items | + Sort-Object Version, Runtime, Architecture, OperatingSystem, Alias | + Format-Table -AutoSize -Property @{name="Active";expression={if($_.Active) { "*" } else { "" }};alignment="center"}, "Version", "Runtime", "Architecture", "OperatingSystem", "Alias" | Out-String | ColorActive + } + } else { + _WriteOut "No runtimes installed. You can run `dnvm install latest` or `dnvm upgrade` to install a runtime." + } + } +} + +<# +.SYNOPSIS + Lists and manages aliases +.PARAMETER Name + The name of the alias to read/create/delete +.PARAMETER Version + The version to assign to the new alias +.PARAMETER Architecture + The architecture of the runtime to assign to this alias +.PARAMETER Runtime + The flavor of the runtime to assign to this alias +.PARAMETER OS + The operating system that the runtime targets +.PARAMETER Delete + Set this switch to delete the alias with the specified name +.DESCRIPTION + If no arguments are provided, this command lists all aliases. If is provided, + the value of that alias, if present, is displayed. If and are + provided, the alias is set to the runtime defined by , + (defaults to 'x86') and (defaults to 'clr'). + + Finally, if the '-d' switch is provided, the alias is deleted, if it exists. + + NOTE: You cannot create an alias for a non-windows runtime. The intended use case for + an alias to help make it easier to switch the runtime, and you cannot use a non-windows + runtime on a windows machine. +#> +function dnvm-alias { + param( + [Alias("d")] + [switch]$Delete, + + [Parameter(Position=0)] + [string]$Name, + + [Parameter(Position=1)] + [string]$Version, + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr", "mono")] + [Parameter(ParameterSetName="Write")] + [string]$Runtime = "", + + [ValidateSet("win", "osx", "darwin", "linux")] + [Parameter(Mandatory=$false,ParameterSetName="Write")] + [string]$OS = "") + + if($Name -like "help" -or $Name -like "/?") { + #It is unlikely that the user is trying to read an alias called help, so lets just help them out by displaying help text. + #If people need an alias called help or one that contains a `?` then we can change this to a prompt. + dnvm help alias + return + } + + if($Version) { + Write-Alias $Name $Version -Architecture $Architecture -Runtime $Runtime -OS:$OS + } elseif ($Delete) { + Delete-Alias $Name + } else { + Read-Alias $Name + } +} + +<# +.SYNOPSIS + [DEPRECATED] Removes an alias +.PARAMETER Name + The name of the alias to remove +#> +function dnvm-unalias { + param( + [Parameter(Mandatory=$true,Position=0)][string]$Name) + _WriteOut "This command has been deprecated. Use '$CommandName alias -d' instead" + dnvm-alias -Delete -Name $Name +} + +<# +.SYNOPSIS + Installs the latest version of the runtime and reassigns the specified alias to point at it +.PARAMETER Alias + The alias to upgrade (default: 'default') +.PARAMETER Architecture + The processor architecture of the runtime to install (default: x86) +.PARAMETER Runtime + The runtime flavor to install (default: clr) +.PARAMETER OS + The operating system that the runtime targets (default: win) +.PARAMETER Force + Overwrite an existing runtime if it already exists +.PARAMETER Proxy + Use the given address as a proxy when accessing remote server +.PARAMETER NoNative + Skip generation of native images +.PARAMETER Ngen + For CLR flavor only. Generate native images for runtime libraries on Desktop CLR to improve startup time. This option requires elevated privilege and will be automatically turned on if the script is running in administrative mode. To opt-out in administrative mode, use -NoNative switch. +.PARAMETER Unstable + Upgrade from the unstable dev feed. This will give you the latest development version of the runtime. +.PARAMETER Global + Installs to configured global dnx file location (default: C:\ProgramData) +#> +function dnvm-upgrade { + param( + [Parameter(Mandatory=$false, Position=0)] + [string]$Alias = "default", + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [Parameter(Mandatory=$false)] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr", "mono")] + [Parameter(Mandatory=$false)] + [string]$Runtime = "", + + [ValidateSet("", "win", "osx", "darwin", "linux")] + [Parameter(Mandatory=$false)] + [string]$OS = "", + + [Alias("f")] + [Parameter(Mandatory=$false)] + [switch]$Force, + + [Parameter(Mandatory=$false)] + [string]$Proxy, + + [Parameter(Mandatory=$false)] + [switch]$NoNative, + + [Parameter(Mandatory=$false)] + [switch]$Ngen, + + [Alias("u")] + [Parameter(Mandatory=$false)] + [switch]$Unstable, + + [Alias("g")] + [Parameter(Mandatory=$false)] + [switch]$Global) + + if($OS -ne "win" -and ![String]::IsNullOrEmpty($OS)) { + #We could remove OS as an option from upgrade, but I want to take this opporunty to educate users about the difference between install and upgrade + #It's possible we should just do install here instead. + _WriteOut -ForegroundColor $ColorScheme.Error "You cannot upgrade to a non-windows runtime. Upgrade will download the latest version of the $RuntimeShortFriendlyName and also set it as your machines default. You cannot set the default $RuntimeShortFriendlyName to a non-windows version because you cannot use it to run an application. If you want to install a non-windows $RuntimeShortFriendlyName to package with your application then use 'dnvm install latest -OS:$OS' instead. Install will download the package but not set it as your default." + $Script:ExitCode = $ExitCodes.OtherError + return + } + + dnvm-install "latest" -Alias:$Alias -Architecture:$Architecture -Runtime:$Runtime -OS:$OS -Force:$Force -Proxy:$Proxy -NoNative:$NoNative -Ngen:$Ngen -Unstable:$Unstable -Persistent:$true -Global:$Global +} + +<# +.SYNOPSIS + Installs a version of the runtime +.PARAMETER VersionNuPkgOrAlias + The version to install from the current channel, the path to a '.nupkg' file to install, 'latest' to + install the latest available version from the current channel, or an alias value to install an alternate + runtime or architecture flavor of the specified alias. +.PARAMETER Architecture + The processor architecture of the runtime to install (default: x86) +.PARAMETER Runtime + The runtime flavor to install (default: clr) +.PARAMETER OS + The operating system that the runtime targets (default: win) +.PARAMETER Alias + Set alias to the installed runtime +.PARAMETER Force + Overwrite an existing runtime if it already exists +.PARAMETER Proxy + Use the given address as a proxy when accessing remote server +.PARAMETER NoNative + Skip generation of native images +.PARAMETER Ngen + For CLR flavor only. Generate native images for runtime libraries on Desktop CLR to improve startup time. This option requires elevated privilege and will be automatically turned on if the script is running in administrative mode. To opt-out in administrative mode, use -NoNative switch. +.PARAMETER Persistent + Make the installed runtime useable across all processes run by the current user +.PARAMETER Unstable + Upgrade from the unstable dev feed. This will give you the latest development version of the runtime. +.PARAMETER Global + Installs to configured global dnx file location (default: C:\ProgramData) +.DESCRIPTION + A proxy can also be specified by using the 'http_proxy' environment variable +#> +function dnvm-install { + param( + [Parameter(Mandatory=$false, Position=0)] + [string]$VersionNuPkgOrAlias, + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [Parameter(Mandatory=$false)] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr", "mono")] + [Parameter(Mandatory=$false)] + [string]$Runtime = "", + + [ValidateSet("", "win", "osx", "darwin", "linux")] + [Parameter(Mandatory=$false)] + [string]$OS = "", + + [Parameter(Mandatory=$false)] + [string]$Alias, + + [Alias("f")] + [Parameter(Mandatory=$false)] + [switch]$Force, + + [Parameter(Mandatory=$false)] + [string]$Proxy, + + [Parameter(Mandatory=$false)] + [switch]$NoNative, + + [Parameter(Mandatory=$false)] + [switch]$Ngen, + + [Alias("p")] + [Parameter(Mandatory=$false)] + [switch]$Persistent, + + [Alias("u")] + [Parameter(Mandatory=$false)] + [switch]$Unstable, + + [Alias("g")] + [Parameter(Mandatory=$false)] + [switch]$Global) + + $selectedFeed = "" + + if($Unstable) { + $selectedFeed = $ActiveUnstableFeed + if(!$selectedFeed) { + $selectedFeed = $DefaultUnstableFeed + } else { + _WriteOut -ForegroundColor $ColorScheme.Warning "Default unstable feed ($DefaultUnstableFeed) is being overridden by the value of the $DefaultUnstableFeedKey environment variable ($ActiveUnstableFeed)" + } + } else { + $selectedFeed = $ActiveFeed + if(!$selectedFeed) { + $selectedFeed = $DefaultFeed + } else { + _WriteOut -ForegroundColor $ColorScheme.Warning "Default stable feed ($DefaultFeed) is being overridden by the value of the $DefaultFeedKey environment variable ($ActiveFeed)" + } + } + + if(!$VersionNuPkgOrAlias) { + _WriteOut "A version, nupkg path, or the string 'latest' must be provided." + dnvm-help install + $Script:ExitCode = $ExitCodes.InvalidArguments + return + } + + $IsNuPkg = $VersionNuPkgOrAlias.EndsWith(".nupkg") + + if ($IsNuPkg) { + if(!(Test-Path $VersionNuPkgOrAlias)) { + throw "Unable to locate package file: '$VersionNuPkgOrAlias'" + } + Write-Progress -Activity "Installing runtime" -Status "Parsing package file name" -Id 1 + $runtimeFullName = [System.IO.Path]::GetFileNameWithoutExtension($VersionNuPkgOrAlias) + $Architecture = Get-PackageArch $runtimeFullName + $Runtime = Get-PackageRuntime $runtimeFullName + $OS = Get-PackageOS $runtimeFullName + $Version = Get-PackageVersion $runtimeFullName + } else { + $aliasPath = Join-Path $AliasesDir "$VersionNuPkgOrAlias$AliasExtension" + if(Test-Path $aliasPath) { + $BaseName = Get-Content $aliasPath + #Check empty checks let us override a given alias property when installing the same again. e.g. `dnvm install default -x64` + if([String]::IsNullOrEmpty($Architecture)) { + $Architecture = Get-PackageArch $BaseName + } + + if([String]::IsNullOrEmpty($Runtime)) { + $Runtime = Get-PackageRuntime $BaseName + } + + if([String]::IsNullOrEmpty($Version)) { + $Version = Get-PackageVersion $BaseName + } + + if([String]::IsNullOrEmpty($OS)) { + $OS = Get-PackageOS $BaseName + } + } else { + $Version = $VersionNuPkgOrAlias + } + } + + $runtimeInfo = GetRuntimeInfo $Architecture $Runtime $OS $Version + + if (!$IsNuPkg) { + if ($VersionNuPkgOrAlias -eq "latest") { + Write-Progress -Activity "Installing runtime" -Status "Determining latest runtime" -Id 1 + $findPackageResult = Find-Latest -runtimeInfo:$runtimeInfo -Feed:$selectedFeed + } + else { + $findPackageResult = Find-Package -runtimeInfo:$runtimeInfo -Feed:$selectedFeed + } + $Version = $findPackageResult.Version + } + + #If the version is still empty at this point then VersionOrNupkgOrAlias is an actual version. + if([String]::IsNullOrEmpty($Version)) { + $Version = $VersionNuPkgOrAlias + } + + $runtimeInfo.Version = $Version + + _WriteDebug "Preparing to install runtime '$($runtimeInfo.RuntimeName)'" + _WriteDebug "Architecture: $($runtimeInfo.Architecture)" + _WriteDebug "Runtime: $($runtimeInfo.Runtime)" + _WriteDebug "Version: $($runtimeInfo.Version)" + _WriteDebug "OS: $($runtimeInfo.OS)" + + $installDir = $RuntimesDir + if (!$Global) { + $RuntimeFolder = Join-Path $RuntimesDir $($runtimeInfo.RuntimeName) + } + else { + $installDir = $GlobalRuntimesDir + $RuntimeFolder = Join-Path $GlobalRuntimesDir $($runtimeInfo.RuntimeName) + } + + _WriteDebug "Destination: $RuntimeFolder" + + if((Test-Path $RuntimeFolder) -and $Force) { + _WriteOut "Cleaning existing installation..." + Remove-Item $RuntimeFolder -Recurse -Force + } + + $installed="" + if(Test-Path (Join-Path $RuntimesDir $($runtimeInfo.RuntimeName))) { + $installed = Join-Path $RuntimesDir $($runtimeInfo.RuntimeName) + } + if(Test-Path (Join-Path $GlobalRuntimesDir $($runtimeInfo.RuntimeName))) { + $installed = Join-Path $GlobalRuntimesDir $($runtimeInfo.RuntimeName) + } + if($installed -ne "") { + _WriteOut "'$($runtimeInfo.RuntimeName)' is already installed in $installed." + if($runtimeInfo.OS -eq "win") { + dnvm-use $runtimeInfo.Version -Architecture:$runtimeInfo.Architecture -Runtime:$runtimeInfo.Runtime -Persistent:$Persistent -OS:$runtimeInfo.OS + } + } + else { + + $Architecture = $runtimeInfo.Architecture + $Runtime = $runtimeInfo.Runtime + $OS = $runtimeInfo.OS + + $TempFolder = Join-Path $installDir "temp" + $UnpackFolder = Join-Path $TempFolder $runtimeFullName + $DownloadFile = Join-Path $UnpackFolder "$runtimeFullName.nupkg" + + if(Test-Path $UnpackFolder) { + _WriteDebug "Cleaning temporary directory $UnpackFolder" + Remove-Item $UnpackFolder -Recurse -Force + } + New-Item -Type Directory $UnpackFolder | Out-Null + + if($IsNuPkg) { + Write-Progress -Activity "Installing runtime" -Status "Copying package" -Id 1 + _WriteDebug "Copying local nupkg $VersionNuPkgOrAlias to $DownloadFile" + Copy-Item $VersionNuPkgOrAlias $DownloadFile + } else { + # Download the package + Write-Progress -Activity "Installing runtime" -Status "Downloading runtime" -Id 1 + _WriteDebug "Downloading version $($runtimeInfo.Version) to $DownloadFile" + + Download-Package -RuntimeInfo:$runtimeInfo -DownloadUrl:$findPackageResult.DownloadUrl -DestinationFile:$DownloadFile -Proxy:$Proxy -Feed:$selectedFeed + } + + Write-Progress -Activity "Installing runtime" -Status "Unpacking runtime" -Id 1 + Unpack-Package $DownloadFile $UnpackFolder + + if(Test-Path $RuntimeFolder) { + # Ensure the runtime hasn't been installed in the time it took to download the package. + _WriteOut "'$($runtimeInfo.RuntimeName)' is already installed." + } + else { + _WriteOut "Installing to $RuntimeFolder" + _WriteDebug "Moving package contents to $RuntimeFolder" + try { + Move-Item $UnpackFolder $RuntimeFolder + } catch { + if(Test-Path $RuntimeFolder) { + #Attempt to cleanup the runtime folder if it is there after a fail. + Remove-Item $RuntimeFolder -Recurse -Force + throw + } + } + #If there is nothing left in the temp folder remove it. There could be other installs happening at the same time as this. + if(Test-Path $(Join-Path $TempFolder "*")) { + Remove-Item $TempFolder -Recurse + } + } + + if($runtimeInfo.OS -eq "win") { + dnvm-use $runtimeInfo.Version -Architecture:$runtimeInfo.Architecture -Runtime:$runtimeInfo.Runtime -Persistent:$Persistent -OS:$runtimeInfo.OS + } + + if ($runtimeInfo.Runtime -eq "clr") { + if (-not $NoNative) { + if ((Is-Elevated) -or $Ngen) { + $runtimeBin = Get-RuntimePath $runtimeInfo.RuntimeName + Write-Progress -Activity "Installing runtime" -Status "Generating runtime native images" -Id 1 + Ngen-Library $runtimeBin $runtimeInfo.Architecture + } + else { + _WriteOut "Native image generation (ngen) is skipped. Include -Ngen switch to turn on native image generation to improve application startup time." + } + } + } + elseif ($runtimeInfo.Runtime -eq "coreclr") { + if ($NoNative -or $runtimeInfo.OS -ne "win") { + _WriteOut "Skipping native image compilation." + } + else { + _WriteOut "Compiling native images for $($runtimeInfo.RuntimeName) to improve startup performance..." + Write-Progress -Activity "Installing runtime" -Status "Generating runtime native images" -Id 1 + + if(Get-Command $CrossGenCommand -ErrorAction SilentlyContinue) { + $crossGenCommand = $CrossGenCommand + } else { + $crossGenCommand = $OldCrossGenCommand + } + + if ($DebugPreference -eq 'SilentlyContinue') { + Start-Process $crossGenCommand -Wait -WindowStyle Hidden + } + else { + Start-Process $crossGenCommand -Wait -NoNewWindow + } + _WriteOut "Finished native image compilation." + } + } + else { + _WriteOut "Unexpected platform: $($runtimeInfo.Runtime). No optimization would be performed on the package installed." + } + } + + if($Alias) { + if($runtimeInfo.OS -eq "win") { + _WriteDebug "Aliasing installed runtime to '$Alias'" + dnvm-alias $Alias $runtimeInfo.Version -Architecture:$RuntimeInfo.Architecture -Runtime:$RuntimeInfo.Runtime -OS:$RuntimeInfo.OS + } else { + _WriteOut "Unable to set an alias for a non-windows runtime. Installing non-windows runtimes on Windows are meant only for publishing, not running." + } + } + + Write-Progress -Status "Done" -Activity "Install complete" -Id 1 -Complete +} + +<# +.SYNOPSIS + Uninstalls a version of the runtime +.PARAMETER VersionOrAlias + The version to uninstall from the current channel or an alias value to uninstall an alternate + runtime or architecture flavor of the specified alias. +.PARAMETER Architecture + The processor architecture of the runtime to uninstall (default: x86) +.PARAMETER Runtime + The runtime flavor to uninstall (default: clr) +.PARAMETER OS + The operating system that the runtime targets (default: win) +#> +function dnvm-uninstall { + param( + [Parameter(Mandatory=$true, Position=0)] + [string]$VersionOrAlias, + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [Parameter(Mandatory=$false)] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr", "mono")] + [Parameter(Mandatory=$false)] + [string]$Runtime = "", + + [ValidateSet("", "win", "osx", "darwin", "linux")] + [Parameter(Mandatory=$false)] + [string]$OS = "") + + $aliasPath = Join-Path $AliasesDir "$VersionOrAlias$AliasExtension" + + if(Test-Path $aliasPath) { + $BaseName = Get-Content $aliasPath + } else { + $Version = $VersionOrAlias + $runtimeInfo = GetRuntimeInfo $Architecture $Runtime $OS $Version + $BaseName = $runtimeInfo.RuntimeName + } + + $runtimeFolder="" + if(Test-Path (Join-Path $RuntimesDir $BaseName)) { + $runtimeFolder = Join-Path $RuntimesDir $BaseName + } + if(Test-Path (Join-Path $GlobalRuntimesDir $BaseName)) { + $runtimeFolder = Join-Path $GlobalRuntimesDir $BaseName + } + + if($runtimeFolder -ne "") { + Remove-Item -literalPath $runtimeFolder -Force -Recurse + _WriteOut "Removed '$($runtimeFolder)'" + } else { + _WriteOut "'$($BaseName)' is not installed" + } + + $aliases = Get-RuntimeAlias + + $result = @($aliases | Where-Object { $_.Name.EndsWith($BaseName) }) + foreach($alias in $result) { + dnvm-alias -Delete -Name $alias.Alias + } +} + +<# +.SYNOPSIS + Adds a runtime to the PATH environment variable for your current shell +.PARAMETER VersionOrAlias + The version or alias of the runtime to place on the PATH +.PARAMETER Architecture + The processor architecture of the runtime to place on the PATH (default: x86, or whatever the alias specifies in the case of use-ing an alias) +.PARAMETER Runtime + The runtime flavor of the runtime to place on the PATH (default: clr, or whatever the alias specifies in the case of use-ing an alias) +.PARAMETER OS + The operating system that the runtime targets (default: win) +.PARAMETER Persistent + Make the change persistent across all processes run by the current user +#> +function dnvm-use { + param( + [Parameter(Mandatory=$true, Position=0)] + [string]$VersionOrAlias, + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [Parameter(Mandatory=$false)] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr")] + [Parameter(Mandatory=$false)] + [string]$Runtime = "", + + [ValidateSet("", "win", "osx", "darwin", "linux")] + [Parameter(Mandatory=$false)] + [string]$OS = "", + + [Alias("p")] + [Parameter(Mandatory=$false)] + [switch]$Persistent) + + if ($versionOrAlias -eq "none") { + _WriteOut "Removing all runtimes from process PATH" + Set-Path (Change-Path $env:Path "" $RuntimeDirs) + + if ($Persistent) { + _WriteOut "Removing all runtimes from user PATH" + $userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) + $userPath = Change-Path $userPath "" $RuntimeDirs + [Environment]::SetEnvironmentVariable("Path", $userPath, [System.EnvironmentVariableTarget]::User) + } + return; + } + + $runtimeInfo = Get-RuntimeAliasOrRuntimeInfo -Version:$VersionOrAlias -Architecture:$Architecture -Runtime:$Runtime -OS:$OS + $runtimeFullName = $runtimeInfo.RuntimeName + $runtimeBin = Get-RuntimePath $runtimeFullName + if ($runtimeBin -eq $null) { + throw "Cannot find $runtimeFullName, do you need to run '$CommandName install $versionOrAlias'?" + } + + _WriteOut "Adding $runtimeBin to process PATH" + Set-Path (Change-Path $env:Path $runtimeBin $RuntimeDirs) + + if ($Persistent) { + _WriteOut "Adding $runtimeBin to user PATH" + $userPath = [Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User) + $userPath = Change-Path $userPath $runtimeBin $RuntimeDirs + [Environment]::SetEnvironmentVariable("Path", $userPath, [System.EnvironmentVariableTarget]::User) + } +} + +<# +.SYNOPSIS + Locates the dnx.exe for the specified version or alias and executes it, providing the remaining arguments to dnx.exe +.PARAMETER VersionOrAlias + The version of alias of the runtime to execute +.PARAMETER Architecture + The processor architecture of the runtime to use (default: x86, or whatever the alias specifies in the case of running an alias) +.PARAMETER Runtime + The runtime flavor of the runtime to use (default: clr, or whatever the alias specifies in the case of running an alias) +.PARAMETER DnxArguments + The arguments to pass to dnx.exe +#> +function dnvm-run { + param( + [Parameter(Mandatory=$true, Position=0)] + [string]$VersionOrAlias, + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [Parameter(Mandatory=$false)] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr")] + [Parameter(Mandatory=$false)] + [string]$Runtime = "", + + [Parameter(Mandatory=$false, Position=1, ValueFromRemainingArguments=$true)] + [object[]]$DnxArguments) + + $runtimeInfo = Get-RuntimeAliasOrRuntimeInfo -Version:$VersionOrAlias -Runtime:$Runtime -Architecture:$Architecture + + $runtimeBin = Get-RuntimePath $runtimeInfo.RuntimeName + if ($runtimeBin -eq $null) { + throw "Cannot find $($runtimeInfo.Name), do you need to run '$CommandName install $versionOrAlias'?" + } + $dnxExe = Join-Path $runtimeBin "dnx.exe" + if(!(Test-Path $dnxExe)) { + throw "Cannot find a dnx.exe in $runtimeBin, the installation may be corrupt. Try running 'dnvm install $VersionOrAlias -f' to reinstall it" + } + _WriteDebug "> $dnxExe $DnxArguments" + & $dnxExe @DnxArguments + $Script:ExitCode = $LASTEXITCODE +} + +<# +.SYNOPSIS + Executes the specified command in a sub-shell where the PATH has been augmented to include the specified DNX +.PARAMETER VersionOrAlias + The version of alias of the runtime to make active in the sub-shell +.PARAMETER Architecture + The processor architecture of the runtime to use (default: x86, or whatever the alias specifies in the case of exec-ing an alias) +.PARAMETER Runtime + The runtime flavor of the runtime to use (default: clr, or whatever the alias specifies in the case of exec-ing an alias) +.PARAMETER Command + The command to execute in the sub-shell +#> +function dnvm-exec { + param( + [Parameter(Mandatory=$true, Position=0)] + [string]$VersionOrAlias, + [Parameter(Mandatory=$false, Position=1)] + [string]$Command, + + [Alias("arch", "a")] + [ValidateSet("", "x86", "x64", "arm")] + [Parameter(Mandatory=$false)] + [string]$Architecture = "", + + [Alias("r")] + [ValidateSet("", "clr", "coreclr")] + [Parameter(Mandatory=$false)] + [string]$Runtime = "", + [Parameter(Mandatory=$false, Position=2, ValueFromRemainingArguments=$true)] + [object[]]$Arguments) + + $runtimeInfo = Get-RuntimeAliasOrRuntimeInfo -Version:$VersionOrAlias -Runtime:$Runtime -Architecture:$Architecture + $runtimeBin = Get-RuntimePath $runtimeInfo.RuntimeName + + if ($runtimeBin -eq $null) { + throw "Cannot find $($runtimeInfo.RuntimeName), do you need to run '$CommandName install $versionOrAlias'?" + } + + $oldPath = $env:PATH + try { + $env:PATH = "$runtimeBin;$($env:PATH)" + & $Command @Arguments + } finally { + $Script:ExitCode = $LASTEXITCODE + $env:PATH = $oldPath + } +} + +<# +.SYNOPSIS + Installs the version manager into your User profile directory +.PARAMETER SkipUserEnvironmentInstall + Set this switch to skip configuring the user-level DNX_HOME and PATH environment variables +#> +function dnvm-setup { + param( + [switch]$SkipUserEnvironmentInstall) + + $DestinationHome = [Environment]::ExpandEnvironmentVariables("$DefaultUserHome") + + # Install scripts + $Destination = "$DestinationHome\bin" + _WriteOut "Installing $CommandFriendlyName to $Destination" + + $ScriptFolder = Split-Path -Parent $ScriptPath + + # Copy script files (if necessary): + Safe-Filecopy "$CommandName.ps1" $ScriptFolder $Destination + Safe-Filecopy "$CommandName.cmd" $ScriptFolder $Destination + + # Configure Environment Variables + # Also, clean old user home values if present + # We'll be removing any existing homes, both + $PathsToRemove = @( + "$DefaultUserHome", + [Environment]::ExpandEnvironmentVariables($OldUserHome), + $DestinationHome, + $OldUserHome) + + # First: PATH + _WriteOut "Adding $Destination to Process PATH" + Set-Path (Change-Path $env:PATH $Destination $PathsToRemove) + + if(!$SkipUserEnvironmentInstall) { + _WriteOut "Adding $Destination to User PATH" + $userPath = [Environment]::GetEnvironmentVariable("PATH", "User") + $userPath = Change-Path $userPath $Destination $PathsToRemove + [Environment]::SetEnvironmentVariable("PATH", $userPath, "User") + } + + # Now clean up the HomeEnvVar if currently set; script installed to default location. + Clean-HomeEnv($SkipUserEnvironmentInstall) +} + +function Check-Runtimes(){ + $runtimesInstall = $false; + foreach($runtimeHomeDir in $RuntimeHomes) { + if (Test-Path "$runtimeHomeDir\runtimes") { + if(Test-Path "$runtimeHomeDir\runtimes\$RuntimePackageName-*"){ + $runtimesInstall = $true; + break; + } + } + } + + if (-not $runtimesInstall){ + $title = "Getting started" + $message = "It looks like you don't have any runtimes installed. Do you want us to install a $RuntimeShortFriendlyName to get you started?" + + $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Install the latest runtime for you" + + $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not install the latest runtime and continue" + + $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) + + $result = $host.ui.PromptForChoice($title, $message, $options, 0) + + if($result -eq 0){ + dnvm-upgrade + } + } +} + +### The main "entry point" + +# Check for old DNX_HOME values +if($UnencodedHomes -contains $OldUserHome) { + _WriteOut -ForegroundColor Yellow "WARNING: Found '$OldUserHome' in your $HomeEnvVar value. This folder has been deprecated." + if($UnencodedHomes -notcontains $DefaultUserHome) { + _WriteOut -ForegroundColor Yellow "WARNING: Didn't find '$DefaultUserHome' in your $HomeEnvVar value. You should run '$CommandName setup' to upgrade." + } +} + +# Check for old KRE_HOME variable +if(Test-Path env:\KRE_HOME) { + _WriteOut -ForegroundColor Yellow "WARNING: Found a KRE_HOME environment variable. This variable has been deprecated and should be removed, or it may interfere with DNVM and the .NET Execution environment" +} + +# Read arguments + +$cmd = $args[0] + +$cmdargs = @() +if($args.Length -gt 1) { + # Combine arguments, ensuring any containing whitespace or parenthesis are correctly quoted + ForEach ($arg In $args[1..($args.Length-1)]) { + if ($arg -match "[\s\(\)]") { + $cmdargs += """$arg""" + } else { + $cmdargs += $arg + } + $cmdargs += " " + } +} + +# Can't add this as script-level arguments because they mask '-a' arguments in subcommands! +# So we manually parse them :) +if($cmdargs -icontains "-amd64") { + $CompatArch = "x64" + _WriteOut "The -amd64 switch has been deprecated. Use the '-arch x64' parameter instead" +} elseif($cmdargs -icontains "-x86") { + $CompatArch = "x86" + _WriteOut "The -x86 switch has been deprecated. Use the '-arch x86' parameter instead" +} elseif($cmdargs -icontains "-x64") { + $CompatArch = "x64" + _WriteOut "The -x64 switch has been deprecated. Use the '-arch x64' parameter instead" +} +$cmdargs = @($cmdargs | Where-Object { @("-amd64", "-x86", "-x64") -notcontains $_ }) + +if(!$cmd) { + Check-Runtimes + $cmd = "help" + $Script:ExitCode = $ExitCodes.InvalidArguments +} + +# Check for the command and run it +try { + if(Get-Command -Name "$CommandPrefix$cmd" -ErrorAction SilentlyContinue) { + _WriteDebug "& dnvm-$cmd $cmdargs" + Invoke-Command ([ScriptBlock]::Create("dnvm-$cmd $cmdargs")) + } + else { + _WriteOut "Unknown command: '$cmd'" + dnvm-help + $Script:ExitCode = $ExitCodes.UnknownCommand + } +} catch { + throw + if(!$Script:ExitCode) { $Script:ExitCode = $ExitCodes.OtherError } +} + +_WriteDebug "=== End $CommandName (Exit Code $Script:ExitCode) ===" +_WriteDebug "" +exit $Script:ExitCode diff --git a/tools/dnvm.sh b/tools/dnvm.sh new file mode 100644 index 0000000..d67036a --- /dev/null +++ b/tools/dnvm.sh @@ -0,0 +1,1054 @@ +# dnvm.sh +# Source this file from your .bash-profile or script to use + +# "Constants" +_DNVM_BUILDNUMBER="rc1-15527" +_DNVM_AUTHORS="Microsoft Open Technologies, Inc." +_DNVM_RUNTIME_PACKAGE_NAME="dnx" +_DNVM_RUNTIME_FRIENDLY_NAME=".NET Execution Environment" +_DNVM_RUNTIME_SHORT_NAME="DNX" +_DNVM_RUNTIME_FOLDER_NAME=".dnx" +_DNVM_COMMAND_NAME="dnvm" +_DNVM_PACKAGE_MANAGER_NAME="dnu" +_DNVM_VERSION_MANAGER_NAME=".NET Version Manager" +_DNVM_DEFAULT_FEED="https://www.nuget.org/api/v2" +_DNVM_DEFAULT_UNSTABLE_FEED="https://www.myget.org/F/aspnetvnext/api/v2" +_DNVM_UPDATE_LOCATION="https://raw.githubusercontent.com/aspnet/Home/dev/dnvm.sh" + +if [ "$NO_COLOR" != "1" ]; then + # ANSI Colors + RCol='\e[0m' # Text Reset + + # Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds + Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m'; + Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m'; + Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m'; + Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m'; + Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m'; + Pur='\e[0;35m'; BPur='\e[1;35m'; UPur='\e[4;35m'; IPur='\e[0;95m'; BIPur='\e[1;95m'; On_Pur='\e[45m'; On_IPur='\e[0;105m'; + Cya='\e[0;36m'; BCya='\e[1;36m'; UCya='\e[4;36m'; ICya='\e[0;96m'; BICya='\e[1;96m'; On_Cya='\e[46m'; On_ICya='\e[0;106m'; + Whi='\e[0;37m'; BWhi='\e[1;37m'; UWhi='\e[4;37m'; IWhi='\e[0;97m'; BIWhi='\e[1;97m'; On_Whi='\e[47m'; On_IWhi='\e[0;107m'; +fi + + +[[ "$_DNVM_BUILDNUMBER" = {{* ]] && _DNVM_BUILDNUMBER="HEAD" + +__dnvm_has() { + type "$1" > /dev/null 2>&1 + return $? +} + +if __dnvm_has "unsetopt"; then + unsetopt nomatch 2>/dev/null +fi + +if [ -z "$DNX_USER_HOME" ]; then + eval DNX_USER_HOME="~/$_DNVM_RUNTIME_FOLDER_NAME" +fi + +if [ -z "$DNX_GLOBAL_HOME" ]; then + eval DNX_GLOBAL_HOME="/usr/local/lib/dnx" +fi + +if [ -z "$DNX_HOME" ]; then + # Set to the user home value + eval DNX_HOME="$DNX_USER_HOME:$DNX_GLOBAL_HOME" +elif [[ $DNX_HOME != *"$DNX_GLOBAL_HOME"* ]]; then + eval DNX_HOME="$DNX_HOME:$DNX_GLOBAL_HOME" +fi + +_DNVM_USER_PACKAGES="$DNX_USER_HOME/runtimes" +_DNVM_GLOBAL_PACKAGES="$DNX_GLOBAL_HOME/runtimes" +_DNVM_ALIAS_DIR="$DNX_USER_HOME/alias" +_DNVM_DNVM_DIR="$DNX_USER_HOME/dnvm" + +DNX_ACTIVE_FEED="" + +__dnvm_current_os() +{ + local uname=$(uname) + if [[ $uname == "Darwin" ]]; then + echo "darwin" + else + echo "linux" + fi +} + +__dnvm_os_runtime_defaults() +{ + local os=$1 + + if [[ $os == "win" ]]; then + echo "clr" + elif [[ $os == "linux" ]]; then + echo "mono" + elif [[ $os == "darwin" ]]; then + echo "mono" + else + echo "unknown os" + fi +} + +__dnvm_runtime_bitness_defaults() +{ + local runtime=$1 + if [[ $runtime == "clr" ]]; then + echo "x86" + elif [[ $runtime == "coreclr" ]]; then + echo "x64" + else + echo "unknown runtime" + fi +} + +__dnvm_query_feed() { + local url=$1 + xml="$(curl $url 2>/dev/null)" + echo $xml | grep \<[a-zA-Z]:Version\>* >> /dev/null || return 1 + version="$(echo $xml | sed 's/.*<[a-zA-Z]:Version>\([^<]*\).*/\1/')" + downloadUrl="$(echo $xml | sed 's/.*&2; + return 1 + fi + + if [[ $platform == "mono" ]]; then + #dnx-mono + local packageId="$_DNVM_RUNTIME_PACKAGE_NAME-$platform" + else + #dnx-coreclr-linux-x64 + local packageId="$_DNVM_RUNTIME_PACKAGE_NAME-$platform-$os-$arch" + fi + + local url="$DNX_ACTIVE_FEED/GetUpdates()?packageIds=%27$packageId%27&versions=%270.0%27&includePrerelease=true&includeAllVersions=false" + __dnvm_query_feed $url + return $? +} + +__dnvm_find_package() { + local platform=$1 + local arch=$2 + local os=$3 + local version=$4 + + if [[ $platform == "mono" ]]; then + #dnx-mono + local packageId="$_DNVM_RUNTIME_PACKAGE_NAME-$platform" + else + #dnx-coreclr-linux-x64 + local packageId="$_DNVM_RUNTIME_PACKAGE_NAME-$platform-$os-$arch" + fi + + local url="$DNX_ACTIVE_FEED/Packages()?\$filter=Id%20eq%27$packageId%27%20and%20Version%20eq%20%27$version%27" + __dnvm_query_feed $url + return $? +} + +__dnvm_strip_path() { + echo "$1" | sed -e "s#$_DNVM_USER_PACKAGES/[^/]*$2[^:]*:##g" -e "s#:$_DNVM_USER_PACKAGES/[^/]*$2[^:]*##g" -e "s#$_DNVM_USER_PACKAGES/[^/]*$2[^:]*##g" | sed -e "s#$_DNVM_GLOBAL_PACKAGES/[^/]*$2[^:]*:##g" -e "s#:$_DNVM_GLOBAL_PACKAGES/[^/]*$2[^:]*##g" -e "s#$_DNVM_GLOBAL_PACKAGES/[^/]*$2[^:]*##g" +} + +__dnvm_prepend_path() { + if [ -z "$1" ]; then + echo "$2" + else + echo "$2:$1" + fi +} + +__dnvm_package_version() { + local runtimeFullName="$1" + echo "$runtimeFullName" | sed "s/[^.]*.\(.*\)/\1/" +} + +__dnvm_package_name() { + local runtimeFullName="$1" + echo "$runtimeFullName" | sed "s/\([^.]*\).*/\1/" +} + +__dnvm_package_runtime() { + local runtimeFullName="$1" + echo "$runtimeFullName" | sed "s/$_DNVM_RUNTIME_PACKAGE_NAME-\([^.-]*\).*/\1/" +} + +__dnvm_package_arch() { + local runtimeFullName="$1" + if [[ "$runtimeFullName" =~ $_DNVM_RUNTIME_PACKAGE_NAME-[^-.]*-[^-.]*-[^-.]*\..* ]]; + then + echo "$runtimeFullName" | sed "s/$_DNVM_RUNTIME_PACKAGE_NAME-[^-.]*-[^-.]*-\([^-.]*\)\..*/\1/" + fi +} + +__dnvm_package_os() { + local runtimeFullName="$1" + if [[ "$runtimeFullName" =~ "mono" ]]; then + echo "linux/osx" + else + echo "$runtimeFullName" | sed "s/$_DNVM_RUNTIME_PACKAGE_NAME-[^-.]*-\([^.-]*\).*/\1/" + fi +} + +__dnvm_update_self() { + local dnvmFileLocation="$_DNVM_DNVM_DIR/dnvm.sh" + if [ ! -e $dnvmFileLocation ]; then + local formattedDnvmFileLocation=`(echo $dnvmFileLocation | sed s=$HOME=~=g)` + local formattedDnvmHome=`(echo $_DNVM_DNVM_DIR | sed s=$HOME=~=g)` + local bashSourceLocation=${BASH_SOURCE} + local scriptLocation=$bashSourceLocation + if [ -z "${bashSourceLocation}" ]; then + local scriptLocation=${(%):-%x} + fi + printf "%b\n" "${Red}$formattedDnvmFileLocation doesn't exist. This command assumes you have installed dnvm in the usual location and are trying to update it. If you want to use update-self then dnvm.sh should be sourced from $formattedDnvmHome. dnvm is currently sourced from $scriptLocation ${RCol}" + return 1 + fi + printf "%b\n" "${Cya}Downloading dnvm.sh from $_DNVM_UPDATE_LOCATION ${RCol}" + local httpResult=$(curl -L -D - "$_DNVM_UPDATE_LOCATION" -o "$dnvmFileLocation" -# | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/") + + [[ $httpResult == "404" ]] &&printf "%b\n" "${Red}404. Unable to download DNVM from $_DNVM_UPDATE_LOCATION ${RCol}" && return 1 + [[ $httpResult != "302" && $httpResult != "200" ]] && echo "${Red}HTTP Error $httpResult fetching DNVM from $_DNVM_UPDATE_LOCATION ${RCol}" && return 1 + + source "$dnvmFileLocation" +} + +__dnvm_promptSudo() { + local acceptSudo="$1" + local sudoMsg="$2" + + local answer= + if [ "$acceptSudo" == "0" ]; then + echo $2 + read -p "You may be prompted for your password via 'sudo' during this process. Is this Ok? (y/N) " answer + else + answer="y" + fi + if echo $answer | grep -iq "^y" ; then + return 1 + else + return 0 + fi +} + +__dnvm_download() { + local runtimeFullName="$1" + local downloadUrl="$2" + local runtimeFolder="$3" + local force="$4" + local acceptSudo="$5" + + local pkgName=$(__dnvm_package_name "$runtimeFullName") + local pkgVersion=$(__dnvm_package_version "$runtimeFullName") + local runtimeFile="$runtimeFolder/$runtimeFullName.nupkg" + + if [ -n "$force" ]; then + printf "%b\n" "${Yel}Forcing download by deleting $runtimeFolder directory ${RCol}" + rm -rf "$runtimeFolder" + fi + + if [ -e "$runtimeFolder" ]; then + printf "%b\n" "${Gre}$runtimeFullName already installed. ${RCol}" + return 0 + fi + + if ! __dnvm_has "curl"; then + printf "%b\n" "${Red}$_DNVM_COMMAND_NAME needs curl to proceed. ${RCol}" >&2; + return 1 + fi + + local useSudo= + mkdir -p "$runtimeFolder" > /dev/null 2>&1 + if [ ! -d $runtimeFolder ]; then + if ! __dnvm_promptSudo $acceptSudo "In order to install dnx globally, dnvm will have to temporarily run as root." ; then + useSudo=sudo + sudo mkdir -p "$runtimeFolder" > /dev/null 2>&1 || return 1 + else + return 1 + fi + fi + echo "Downloading $runtimeFullName from $DNX_ACTIVE_FEED" + echo "Download: $downloadUrl" + + local httpResult=$($useSudo curl -L -D - "$downloadUrl" -o "$runtimeFile" -# | grep "^HTTP/1.1" | head -n 1 | sed "s/HTTP.1.1 \([0-9]*\).*/\1/") + + if [[ $httpResult == "404" ]]; then + printf "%b\n" "${Red}$runtimeFullName was not found in repository $DNX_ACTIVE_FEED ${RCol}" + printf "%b\n" "${Cya}This is most likely caused by the feed not having the version that you typed. Check that you typed the right version and try again. Other possible causes are the feed doesn't have a $_DNVM_RUNTIME_SHORT_NAME of the right name format or some other error caused a 404 on the server.${RCol}" + return 1 + fi + [[ $httpResult != "302" && $httpResult != "200" ]] && echo "${Red}HTTP Error $httpResult fetching $runtimeFullName from $DNX_ACTIVE_FEED ${RCol}" && return 1 + + __dnvm_unpack $runtimeFile $runtimeFolder $useSudo + return $? +} + +__dnvm_unpack() { + local runtimeFile="$1" + local runtimeFolder="$2" + local useSudo=$3 + + echo "Installing to $runtimeFolder" + + if ! __dnvm_has "unzip"; then + echo "$_DNVM_COMMAND_NAME needs unzip to proceed." >&2; + return 1 + fi + + $useSudo unzip $runtimeFile -d $runtimeFolder > /dev/null 2>&1 + + [ -e "$runtimeFolder/[Content_Types].xml" ] && $useSudo rm "$runtimeFolder/[Content_Types].xml" + + [ -e "$runtimeFolder/_rels/" ] && $useSudo rm -rf "$runtimeFolder/_rels/" + + [ -e "$runtimeFolder/package/" ] && $useSudo rm -rf "$runtimeFolder/_package/" + + [ -e "$runtimeFile" ] && $useSudo rm -f "$runtimeFile" + + #Set dnx to be executable + if [[ -s "$runtimeFolder/bin/dnx" ]]; then + $useSudo chmod 775 "$runtimeFolder/bin/dnx" + fi + + #Set dnu to be executable + if [[ -s "$runtimeFolder/bin/dnu" ]]; then + $useSudo chmod 775 "$runtimeFolder/bin/dnu" + fi +} + +__dnvm_requested_version_or_alias() { + local versionOrAlias="$1" + local runtime="$2" + local arch="$3" + local os="$4" + local runtimeBin=$(__dnvm_locate_runtime_bin_from_full_name "$versionOrAlias") + + # If the name specified is an existing package, just use it as is + if [ -n "$runtimeBin" ]; then + echo "$versionOrAlias" + else + if [ -e "$_DNVM_ALIAS_DIR/$versionOrAlias.alias" ]; then + local runtimeFullName=$(cat "$_DNVM_ALIAS_DIR/$versionOrAlias.alias") + if [[ ! -n "$runtime" && ! -n "$arch" ]]; then + echo "$runtimeFullName" + return + fi + local pkgVersion=$(__dnvm_package_version "$runtimeFullName") + fi + + if [[ ! -n "$pkgVersion" ]]; then + local pkgVersion=$versionOrAlias + fi + local pkgArchitecture="x64" + local pkgSystem=$os + + if [[ -z $runtime || "$runtime" == "mono" ]]; then + echo "$_DNVM_RUNTIME_PACKAGE_NAME-mono.$pkgVersion" + else + if [ "$arch" != "" ]; then + local pkgArchitecture="$arch" + fi + if [ "$os" == "" ]; then + local pkgSystem=$(__dnvm_current_os) + fi + + echo "$_DNVM_RUNTIME_PACKAGE_NAME-$runtime-$pkgSystem-$pkgArchitecture.$pkgVersion" + fi + fi +} + +# This will be more relevant if we support global installs +__dnvm_locate_runtime_bin_from_full_name() { + local runtimeFullName=$1 + for v in `echo $DNX_HOME | tr ":" "\n"`; do + if [ -e "$v/runtimes/$runtimeFullName/bin" ]; then + echo "$v/runtimes/$runtimeFullName/bin" && return + fi + done +} + +__echo_art() { + printf "%b" "${Cya}" + echo " ___ _ ___ ____ ___" + echo " / _ \/ |/ / | / / |/ /" + echo " / // / /| |/ / /|_/ / " + echo " /____/_/|_/ |___/_/ /_/ " + printf "%b" "${RCol}" +} + +__dnvm_description() { + __echo_art + echo "" + echo "$_DNVM_VERSION_MANAGER_NAME - Version 1.0.0-$_DNVM_BUILDNUMBER" + [[ "$_DNVM_AUTHORS" != {{* ]] && echo "By $_DNVM_AUTHORS" + echo "" + echo "DNVM can be used to download versions of the $_DNVM_RUNTIME_FRIENDLY_NAME and manage which version you are using." + echo "You can control the URL of the stable and unstable channel by setting the DNX_FEED and DNX_UNSTABLE_FEED variables." + echo "" + printf "%b\n" "${Yel}Current feed settings:${RCol}" + printf "%b\n" "${Cya}Default Stable:${Yel} $_DNVM_DEFAULT_FEED" + printf "%b\n" "${Cya}Default Unstable:${Yel} $_DNVM_DEFAULT_UNSTABLE_FEED" + + local dnxStableOverride="" + [[ -n $DNX_FEED ]] && dnxStableOverride="$DNX_FEED" + + printf "%b\n" "${Cya}Current Stable Override:${Yel} $dnxStableOverride" + + local dnxUnstableOverride="" + [[ -n $DNX_UNSTABLE_FEED ]] && dnxUnstableOverride="$DNX_UNSTABLE_FEED" + + printf "%b\n" "${Cya}Current Unstable Override:${Yel} $dnxUnstableOverride${RCol}" + echo "" + +} + +__dnvm_version() { + echo "1.0.0-$_DNVM_BUILDNUMBER" +} + +__dnvm_help() { + __dnvm_description + printf "%b\n" "${Cya}USAGE:${Yel} $_DNVM_COMMAND_NAME [options] ${RCol}" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME upgrade [-f|-force] [-u|-unstable] [-g|-global] [-y]${RCol}" + echo " install latest $_DNVM_RUNTIME_SHORT_NAME from feed" + echo " adds $_DNVM_RUNTIME_SHORT_NAME bin to path of current command line" + echo " set installed version as default" + echo " -f|-force force upgrade. Overwrite existing version of $_DNVM_RUNTIME_SHORT_NAME if already installed" + echo " -u|-unstable use unstable feed. Installs the $_DNVM_RUNTIME_SHORT_NAME from the unstable feed" + echo " -r|-runtime runtime flavor to install [mono or coreclr] (default: mono)" + echo " -g|-global Installs the latest $_DNVM_RUNTIME_SHORT_NAME in the configured global $_DNVM_RUNTIME_SHORT_NAME file location (default: /usr/local/lib/dnx current: $DNX_GLOBAL_HOME)" + echo " -y Assume Yes to all queries and do not prompt" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME install |||latest [-r ] [-OS ] [-alias ] [-a|-arch ] [-p|-persistent] [-f|-force] [-u|-unstable] [-g|-global] [-y]${RCol}" + echo " | install requested $_DNVM_RUNTIME_SHORT_NAME from feed" + echo " install requested $_DNVM_RUNTIME_SHORT_NAME from local package on filesystem" + echo " latest install latest version of $_DNVM_RUNTIME_SHORT_NAME from feed" + echo " -OS the operating system that the runtime targets (default:$(__dnvm_current_os))" + echo " -alias set alias for requested $_DNVM_RUNTIME_SHORT_NAME on install" + echo " -a|-arch architecture to use (x64)" + echo " -p|-persistent set installed version as default" + echo " -f|-force force install. Overwrite existing version of $_DNVM_RUNTIME_SHORT_NAME if already installed" + echo " -u|-unstable use unstable feed. Installs the $_DNVM_RUNTIME_SHORT_NAME from the unstable feed" + echo " -r|-runtime runtime flavor to install [mono or coreclr] (default: mono)" + echo " -g|-global Installs to the configured global $_DNVM_RUNTIME_SHORT_NAME file location (default: /usr/local/lib/dnx current: $DNX_GLOBAL_HOME)" + echo " -y Assume Yes to all queries and do not prompt" + echo "" + echo " adds $_DNVM_RUNTIME_SHORT_NAME bin to path of current command line" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME uninstall [-r|-runtime ] [-a|-arch ] [-OS ]${RCol}" + echo " the version to uninstall" + echo " -r|-runtime runtime flavor to uninstall [mono or coreclr] (default: mono)" + echo " -a|-arch architecture to use (x64)" + echo " -OS the operating system that the runtime targets (default:$(__dnvm_current_os))" + echo " -y Assume Yes to all queries and do not prompt" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME use |||none [-p|-persistent] [-r|-runtime ] [-a|-arch ] ${RCol}" + echo " || add $_DNVM_RUNTIME_SHORT_NAME bin to path of current command line " + echo " none remove $_DNVM_RUNTIME_SHORT_NAME bin from path of current command line" + echo " -p|-persistent set selected version as default" + echo " -r|-runtime runtime flavor to use [mono or coreclr] (default: mono)" + echo " -a|-arch architecture to use (x64)" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME run | ${RCol}" + echo " | the version or alias to run" + echo " arguments to be passed to $_DNVM_RUNTIME_SHORT_NAME" + echo "" + echo " runs the $_DNVM_RUNTIME_SHORT_NAME command from the specified version of the runtime without affecting the current PATH" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME exec | ${RCol}" + echo " | the version or alias to execute in" + echo " the command to run" + echo " arguments to be passed to the command" + echo "" + echo " runs the specified command in the context of the specified version of the runtime without affecting the current PATH" + echo " example: $_DNVM_COMMAND_NAME exec 1.0.0-beta4 $_DNVM_PACKAGE_MANAGER_NAME build" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME list [-detailed]${RCol}" + echo " -detailed display more detailed information on each runtime" + echo "" + echo " list $_DNVM_RUNTIME_SHORT_NAME versions installed " + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME alias ${RCol}" + echo " list $_DNVM_RUNTIME_SHORT_NAME aliases which have been defined" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME alias ${RCol}" + echo " display value of the specified alias" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME alias || ${RCol}" + echo " the name of the alias to set" + echo " || the $_DNVM_RUNTIME_SHORT_NAME version to set the alias to. Alternatively use the version of the specified alias" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME alias [-d|-delete] ${RCol}" + echo " remove the specified alias" + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME [help|-h|-help|--help] ${RCol}" + echo " displays this help text." + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME [version|-v|-version|--version] ${RCol}" + echo " print the dnvm version." + echo "" + printf "%b\n" "${Yel}$_DNVM_COMMAND_NAME update-self ${RCol}" + echo " updates dnvm itself." +} + +dnvm() +{ + if [ $# -lt 1 ]; then + __dnvm_description + + printf "%b\n" "Use ${Yel}$_DNVM_COMMAND_NAME [help|-h|-help|--help] ${RCol} to display help text." + echo "" + return + fi + + case $1 in + "help"|"-h"|"-help"|"--help" ) + __dnvm_help + ;; + + "version"|"-v"|"-version"|"--version" ) + __dnvm_version + ;; + + "update-self" ) + __dnvm_update_self + ;; + + "upgrade" ) + shift + $_DNVM_COMMAND_NAME install latest -p $@ + ;; + + "install" ) + [ $# -lt 2 ] && __dnvm_help && return + shift + local persistent= + local versionOrAlias= + local alias= + local force= + local unstable= + local os= + local runtime= + local arch= + local global=0 + local acceptSudo=0 + while [ $# -ne 0 ] + do + if [[ $1 == "-p" || $1 == "-persistent" ]]; then + local persistent="-p" + elif [[ $1 == "-alias" ]]; then + local alias=$2 + shift + elif [[ $1 == "-f" || $1 == "-force" ]]; then + local force="-f" + elif [[ $1 == "-u" || $1 == "-unstable" ]]; then + local unstable="-u" + elif [[ $1 == "-r" || $1 == "-runtime" ]]; then + local runtime=$2 + shift + elif [[ $1 == "-OS" ]]; then + local os=$2 + shift + elif [[ $1 == "-y" ]]; then + local acceptSudo=1 + elif [[ $1 == "-a" || $1 == "-arch" ]]; then + local arch=$2 + shift + + if [[ $arch != "x86" && $arch != "x64" ]]; then + printf "%b\n" "${Red}Architecture must be x86 or x64.${RCol}" + return 1 + fi + elif [[ $1 == "-g" || $1 == "-global" ]]; then + local global=1 + elif [[ -n $1 ]]; then + [[ -n $versionOrAlias ]] && echo "Invalid option $1" && __dnvm_help && return 1 + local versionOrAlias=$1 + fi + shift + done + + if [[ $arch == "x86" && $runtime == "coreclr" && $os != "win" ]]; then + printf "%b\n" "${Red}Core CLR doesn't currently have a 32 bit build. You must use x64.${RCol}" + return 1 + fi + + if [ -z $unstable ]; then + DNX_ACTIVE_FEED="$DNX_FEED" + if [ -z "$DNX_ACTIVE_FEED" ]; then + DNX_ACTIVE_FEED="$_DNVM_DEFAULT_FEED" + else + printf "%b\n" "${Yel}Default stable feed ($_DNVM_DEFAULT_FEED) is being overridden by the value of the DNX_FEED variable ($DNX_FEED). ${RCol}" + fi + else + DNX_ACTIVE_FEED="$DNX_UNSTABLE_FEED" + if [ -z "$DNX_ACTIVE_FEED" ]; then + DNX_ACTIVE_FEED="$_DNVM_DEFAULT_UNSTABLE_FEED" + else + printf "%b\n" "${Yel}Default unstable feed ($_DNVM_DEFAULT_UNSTABLE_FEED) is being overridden by the value of the DNX_UNSTABLE_FEED variable ($DNX_UNSTABLE_FEED). ${RCol}" + fi + fi + + if [[ -z $os ]]; then + os=$(__dnvm_current_os) + fi + if [[ $os == "osx" ]]; then + os="darwin" + fi + + if [[ -z $runtime ]]; then + runtime=$(__dnvm_os_runtime_defaults "$os") + fi + + if [[ -z $arch ]]; then + arch=$(__dnvm_runtime_bitness_defaults "$runtime") + fi + + if [[ $runtime == "mono" ]] && ! __dnvm_has "mono"; then + printf "%b\n" "${Yel}It appears you don't have Mono available. Remember to get Mono before trying to run $DNVM_RUNTIME_SHORT_NAME application. ${RCol}" >&2; + fi + + local runtimeDir=$_DNVM_USER_PACKAGES + if [ $global == 1 ]; then + runtimeDir=$_DNVM_GLOBAL_PACKAGES + fi + + if [[ "$versionOrAlias" != *.nupkg ]]; then + if [[ "$versionOrAlias" == "latest" ]]; then + echo "Determining latest version" + read versionOrAlias downloadUrl < <(__dnvm_find_latest "$runtime" "$arch" "$os") + [[ $? == 1 ]] && echo "Error: Could not find latest version from feed $DNX_ACTIVE_FEED" && return 1 + printf "%b\n" "Latest version is ${Cya}$versionOrAlias ${RCol}" + else + local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$os") + local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") + + read versionOrAlias downloadUrl < <(__dnvm_find_package "$runtime" "$arch" "$os" "$runtimeVersion") + [[ $? == 1 ]] && echo "Error: Could not find version $runtimeVersion in feed $DNX_ACTIVE_FEED" && return 1 + fi + local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$os") + local runtimeFolder="$runtimeDir/$runtimeFullName" + + local exist=0 + for folder in `echo $DNX_HOME | tr ":" "\n"`; do + if [ -e "$folder/runtimes/$runtimeFullName" ]; then + echo "$runtimeFullName already installed in $folder" + exist=1 + fi + done + + if [[ $exist != 1 ]]; then + __dnvm_download "$runtimeFullName" "$downloadUrl" "$runtimeFolder" "$force" "$acceptSudo" + fi + [[ $? == 1 ]] && return 1 + if [[ "$os" == $(__dnvm_current_os) ]]; then + $_DNVM_COMMAND_NAME use "$versionOrAlias" "$persistent" "-runtime" "$runtime" "-arch" "$arch" + [[ -n $alias ]] && $_DNVM_COMMAND_NAME alias "$alias" "$versionOrAlias" + fi + else + local runtimeFullName=$(basename $versionOrAlias | sed "s/\(.*\)\.nupkg/\1/") + local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") + local runtimeFolder="$runtimeDir/$runtimeFullName" + local runtimeFile="$runtimeFolder/$runtimeFullName.nupkg" + local runtimeClr=$(__dnvm_package_runtime "$runtimeFullName") + + if [ -n "$force" ]; then + printf "%b\n" "${Yel}Forcing download by deleting $runtimeFolder directory ${RCol}" + rm -rf "$runtimeFolder" + fi + + if [ -e "$runtimeFolder" ]; then + echo "$runtimeFullName already installed" + else + local useSudo= + mkdir -p "$runtimeFolder" > /dev/null 2>&1 + if [ ! -d $runtimeFolder ]; then + if ! __dnvm_promptSudo $acceptSudo "In order to install dnx globally, dnvm will have to temporarily run as root." ; then + useSudo=sudo + sudo mkdir -p "$runtimeFolder" > /dev/null 2>&1 || return 1 + else + return 1 + fi + fi + cp -a "$versionOrAlias" "$runtimeFile" + __dnvm_unpack "$runtimeFile" "$runtimeFolder" $useSudo + [[ $? == 1 ]] && return 1 + fi + $_DNVM_COMMAND_NAME use "$runtimeVersion" "$persistent" -r "$runtimeClr" + [[ -n $alias ]] && $_DNVM_COMMAND_NAME alias "$alias" "$runtimeVersion" + fi + ;; + + "uninstall" ) + [[ $# -lt 2 ]] && __dnvm_help && return + shift + + local versionOrAlias= + local runtime= + local architecture= + local os= + local acceptSudo=0 + while [ $# -ne 0 ] + do + if [[ $1 == "-r" || $1 == "-runtime" ]]; then + local runtime=$2 + shift + elif [[ $1 == "-a" || $1 == "-arch" ]]; then + local architecture=$2 + shift + elif [[ $1 == "-OS" ]]; then + local os=$2 + shift + elif [[ $1 == "-y" ]]; then + local acceptSudo=1 + elif [[ -n $1 ]]; then + local versionOrAlias=$1 + fi + + shift + done + + if [[ -z $os ]]; then + os=$(__dnvm_current_os) + elif [[ $os == "osx" ]]; then + os="darwin" + fi + + if [[ -z $runtime ]]; then + runtime=$(__dnvm_os_runtime_defaults "$os") + fi + + if [[ -z $architecture ]]; then + architecture=$(__dnvm_runtime_bitness_defaults "$runtime") + fi + + # dnx-coreclr-linux-x64.1.0.0-beta7-12290 + local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$architecture" "$os") + + for folder in `echo $DNX_HOME | tr ":" "\n"`; do + if [ -e "$folder/runtimes/$runtimeFullName" ]; then + local runtimeFolder="$folder/runtimes/$runtimeFullName" + fi + done + + if [[ -e $runtimeFolder ]]; then + if [[ $runtimeFolder == *"$DNX_GLOBAL_HOME"* ]] ; then + if ! __dnvm_promptSudo $acceptSudo "In order to uninstall a global dnx, dnvm will have to temporarily run as root." ; then + local useSudo=sudo + fi + fi + $useSudo rm -r $runtimeFolder + echo "Removed $runtimeFolder" + else + echo "$runtimeFolder is not installed" + fi + + if [ -d "$_DNVM_ALIAS_DIR" ]; then + for __dnvm_file in $(find "$_DNVM_ALIAS_DIR" -name *.alias); do + if [ $(cat $__dnvm_file) == "$runtimeFullName" ]; then + rm $__dnvm_file + fi + done + fi + ;; + + "use"|"run"|"exec" ) + [[ $1 == "use" && $# -lt 2 ]] && __dnvm_help && return + + local cmd=$1 + local persistent= + local arch= + local runtime= + + local versionOrAlias= + shift + if [ $cmd == "use" ]; then + while [ $# -ne 0 ] + do + if [[ $1 == "-p" || $1 == "-persistent" ]]; then + local persistent="true" + elif [[ $1 == "-a" || $1 == "-arch" ]]; then + local arch=$2 + shift + elif [[ $1 == "-r" || $1 == "-runtime" ]]; then + local runtime=$2 + shift + elif [[ $1 == -* ]]; then + echo "Invalid option $1" && __dnvm_help && return 1 + elif [[ -n $1 ]]; then + [[ -n $versionOrAlias ]] && echo "Invalid option $1" && __dnvm_help && return 1 + local versionOrAlias=$1 + fi + shift + done + else + while [ $# -ne 0 ] + do + if [[ $1 == "-a" || $1 == "-arch" ]]; then + local arch=$2 + shift + elif [[ $1 == "-r" || $1 == "-runtime" ]]; then + local runtime=$2 + shift + elif [[ -n $1 ]]; then + [[ -n $versionOrAlias ]] && break + local versionOrAlias=$1 + fi + shift + done + fi + + if [[ $cmd == "use" && $versionOrAlias == "none" ]]; then + echo "Removing $_DNVM_RUNTIME_SHORT_NAME from process PATH" + # Strip other version from PATH + PATH=$(__dnvm_strip_path "$PATH" "/bin") + + if [[ -n $persistent && -e "$_DNVM_ALIAS_DIR/default.alias" ]]; then + echo "Setting default $_DNVM_RUNTIME_SHORT_NAME to none" + rm "$_DNVM_ALIAS_DIR/default.alias" + fi + return 0 + fi + + local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$(__dnvm_current_os)") + local runtimeBin=$(__dnvm_locate_runtime_bin_from_full_name "$runtimeFullName") + + if [[ -z $runtimeBin ]]; then + echo "Cannot find $runtimeFullName, do you need to run '$_DNVM_COMMAND_NAME install $versionOrAlias'?" + return 1 + fi + + case $cmd in + "run") + local hostpath="$runtimeBin/dnx" + if [[ -e $hostpath ]]; then + $hostpath $@ + return $? + else + echo "Cannot find $_DNVM_RUNTIME_SHORT_NAME in $runtimeBin. It may have been corrupted. Use '$_DNVM_COMMAND_NAME install $versionOrAlias -f' to attempt to reinstall it" + fi + ;; + "exec") + ( + PATH=$(__dnvm_strip_path "$PATH" "/bin") + PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin") + $@ + ) + return $? + ;; + "use") + echo "Adding" $runtimeBin "to process PATH" + + PATH=$(__dnvm_strip_path "$PATH" "/bin") + PATH=$(__dnvm_prepend_path "$PATH" "$runtimeBin") + + if [[ -n $persistent ]]; then + local runtimeVersion=$(__dnvm_package_version "$runtimeFullName") + $_DNVM_COMMAND_NAME alias default "$runtimeVersion" + fi + ;; + esac + ;; + + "alias" ) + [[ $# -gt 7 ]] && __dnvm_help && return + + [[ ! -e "$_DNVM_ALIAS_DIR/" ]] && mkdir "$_DNVM_ALIAS_DIR/" > /dev/null + + if [[ $# == 1 ]]; then + echo "" + local format="%-25s %s\n" + printf "$format" "Alias" "Name" + printf "$format" "-----" "----" + if [ -d "$_DNVM_ALIAS_DIR" ]; then + for __dnvm_file in $(find "$_DNVM_ALIAS_DIR" -name *.alias); do + local alias="$(basename $__dnvm_file | sed 's/\.alias//')" + local name="$(cat $__dnvm_file)" + printf "$format" "$alias" "$name" + done + fi + echo "" + return + fi + shift + + if [[ $1 == "-d" || $1 == "-delete" ]]; then + local name=$2 + local aliasPath="$_DNVM_ALIAS_DIR/$name.alias" + [[ ! -e "$aliasPath" ]] && echo "Cannot remove alias, '$name' is not a valid alias name" && return 1 + echo "Removing alias $name" + rm "$aliasPath" >> /dev/null 2>&1 + return + fi + + local name="$1" + + if [[ $# == 1 ]]; then + [[ ! -e "$_DNVM_ALIAS_DIR/$name.alias" ]] && echo "There is no alias called '$name'" && return 1 + cat "$_DNVM_ALIAS_DIR/$name.alias" + echo "" + return + fi + + shift + local versionOrAlias="$1" + shift + while [ $# -ne 0 ] + do + if [[ $1 == "-a" || $1 == "-arch" ]]; then + local arch=$2 + shift + elif [[ $1 == "-r" || $1 == "-runtime" ]]; then + local runtime=$2 + shift + elif [[ $1 == "-OS" ]]; then + local os=$2 + shift + fi + shift + done + + local runtimeFullName=$(__dnvm_requested_version_or_alias "$versionOrAlias" "$runtime" "$arch" "$os") + + ([[ ! -d "$_DNVM_USER_PACKAGES/$runtimeFullName" ]] && [[ ! -d "$_DNVM_GLOBAL_PACKAGES/$runtimeFullName" ]]) && echo "$runtimeFullName is not an installed $_DNVM_RUNTIME_SHORT_NAME version" && return 1 + + local action="Setting" + [[ -e "$_DNVM_ALIAS_DIR/$name.alias" ]] && action="Updating" + echo "$action alias '$name' to '$runtimeFullName'" + echo "$runtimeFullName" >| "$_DNVM_ALIAS_DIR/$name.alias" + ;; + + "unalias" ) + [[ $# -ne 2 ]] && __dnvm_help && return + + local name=$2 + echo "This command has been deprecated. Use '$_DNVM_COMMAND_NAME alias -d' instead" + $_DNVM_COMMAND_NAME alias -d $name + return $? + ;; + + "list" ) + [[ $# -gt 2 ]] && __dnvm_help && return + + [[ ! -d $_DNVM_USER_PACKAGES ]] && echo "$_DNVM_RUNTIME_FRIENDLY_NAME is not installed." && return 1 + + local searchGlob="$_DNVM_RUNTIME_PACKAGE_NAME-*" + + local runtimes="" + for location in `echo $DNX_HOME | tr ":" "\n"`; do + location+="/runtimes" + if [ -d "$location" ]; then + local oruntimes="$(find $location -name "$searchGlob" \( -type d -or -type l \) -prune -exec basename {} \;)" + for v in `echo $oruntimes | tr "\n" " "`; do + runtimes+="$v:$location"$'\n' + done + fi + done + + [[ -z $runtimes ]] && echo 'No runtimes installed. You can run `dnvm install latest` or `dnvm upgrade` to install a runtime.' && return + + echo "" + + # Separate empty array declaration from initialization + # to avoid potential ZSH error: local:217: maximum nested function level reached + local arr + arr=() + + # Z shell array-index starts at one. + local i=1 + if [ -d "$_DNVM_ALIAS_DIR" ]; then + for __dnvm_file in $(find "$_DNVM_ALIAS_DIR" -name *.alias); do + if [ ! -d "$_DNVM_USER_PACKAGES/$(cat $__dnvm_file)" ] && [ ! -d "$_DNVM_GLOBAL_PACKAGES/$(cat $__dnvm_file)" ]; then + arr[$i]="$(basename $__dnvm_file | sed 's/\.alias//')/missing/$(cat $__dnvm_file)" + runtimes="$runtimes $(cat $__dnvm_file)" + else + arr[$i]="$(basename $__dnvm_file | sed 's/\.alias//')/$(cat $__dnvm_file)" + fi + let i+=1 + done + fi + + if [[ $2 == "-detailed" ]]; then + # Calculate widest alias + local widestAlias=5 + for f in `echo $runtimes`; do + local pkgName=$(__dnvm_package_name "$f") + local pkgVersion=$(__dnvm_package_version "$f") + local alias="" + local delim="" + for i in "${arr[@]}"; do + if [[ ${i##*/} == "$pkgName.$pkgVersion" ]]; then + alias+="$delim${i%%/*}" + delim=", " + if [[ "${i%/*}" =~ \/missing$ ]]; then + alias+=" (missing)" + fi + fi + done + if [ "${#alias}" -gt "$widestAlias" ]; then + widestAlias=${#alias} + fi + done + local formatString="%-6s %-20s %-7s %-12s %-15s %-${widestAlias}s %s\n" + printf "$formatString" "Active" "Version" "Runtime" "Architecture" "OperatingSystem" "Alias" "Location" + printf "$formatString" "------" "-------" "-------" "------------" "---------------" "-----" "--------" + else + local formatString="%-6s %-20s %-7s %-12s %-15s %s\n" + printf "$formatString" "Active" "Version" "Runtime" "Architecture" "OperatingSystem" "Alias" + printf "$formatString" "------" "-------" "-------" "------------" "---------------" "-----" + fi + + for f in `echo -e "$runtimes" | sort -t. -k2 -k3 -k4 -k1`; do + local location=`echo $f | sed 's/.*\([:]\)//'` + f=`echo $f | sed 's/\([:]\).*//'` + local formattedHome=`(echo $location | sed s=$HOME=~=g)` + local active="" + [[ $PATH == *"$location/$f/bin"* ]] && local active=" *" + local pkgRuntime=$(__dnvm_package_runtime "$f") + local pkgName=$(__dnvm_package_name "$f") + local pkgVersion=$(__dnvm_package_version "$f") + local pkgArch=$(__dnvm_package_arch "$f") + local pkgOs=$(__dnvm_package_os "$f") + + local alias="" + local delim="" + for i in "${arr[@]}"; do + if [[ ${i##*/} == "$pkgName.$pkgVersion" ]]; then + alias+="$delim${i%%/*}" + delim=", " + if [[ "${i%/*}" =~ \/missing$ ]]; then + alias+=" (missing)" + formattedHome="" + fi + fi + done + + if [[ $2 == "-detailed" ]]; then + printf "$formatString" "$active" "$pkgVersion" "$pkgRuntime" "$pkgArch" "$pkgOs" "$alias" "$formattedHome" + else + printf "$formatString" "$active" "$pkgVersion" "$pkgRuntime" "$pkgArch" "$pkgOs" "$alias" + fi + done + + echo "" + ;; + + *) + echo "Unknown command $1" + return 1 + esac + + return 0 +} + +# Add the home location's bin directory to the path if it doesn't exist +[[ ":$PATH:" != *":$DNX_USER_HOME/bin:"* ]] && export PATH="$DNX_USER_HOME/bin:$PATH" + +# Generate the command function using the constant defined above. +$_DNVM_COMMAND_NAME alias default >/dev/null && $_DNVM_COMMAND_NAME use default >/dev/null || true diff --git a/tools/dnx-build.ps1 b/tools/dnx-build.ps1 new file mode 100644 index 0000000..27dd505 --- /dev/null +++ b/tools/dnx-build.ps1 @@ -0,0 +1,24 @@ +$toolsPath = split-path $MyInvocation.MyCommand.Definition +$dnvm = join-path $toolsPath "dnvm.ps1" +$solutionPath = [System.IO.Path]::GetFullPath($(join-path $toolsPath "..")) +$globalJson = join-path $solutionPath "global.json" +$dnxVersion = (ConvertFrom-JSON ([System.IO.File]::ReadAllText($globalJson))).sdk.version + +& $dnvm install $dnxVersion -runtime CoreCLR -arch x86 -u +& $dnvm install $dnxVersion -runtime CLR -arch x86 -u +& $dnvm install $dnxVersion -runtime CoreCLR -arch x64 -u +& $dnvm install $dnxVersion -runtime CLR -arch x64 -u + +& $dnvm use $dnxVersion -runtime CLR -arch x86 + +# Update build number during CI +if ($env:BuildSemanticVersion -ne $null) { + $projectJson = join-path $solutionPath "src\xunit.runner.dnx\project.json" + $content = get-content $projectJson + $content = $content.Replace("99.99.99-dev", $env:BuildSemanticVersion) + set-content $projectJson $content -encoding UTF8 +} + +# Restore packages and build +& dnu restore $solutionPath +& dnu pack $(join-path $solutionPath "src\xunit.runner.dnx") --configuration Release diff --git a/tools/dnx-tests.ps1 b/tools/dnx-tests.ps1 new file mode 100644 index 0000000..cb4a439 --- /dev/null +++ b/tools/dnx-tests.ps1 @@ -0,0 +1,21 @@ +$toolsPath = split-path $MyInvocation.MyCommand.Definition +$dnvm = join-path $toolsPath "dnvm.ps1" +$solutionPath = [System.IO.Path]::GetFullPath($(join-path $toolsPath "..")) +$globalJson = join-path $solutionPath "global.json" +$dnxVersion = (ConvertFrom-JSON ([System.IO.File]::ReadAllText($globalJson))).sdk.version + +& $dnvm use $dnxVersion -runtime CLR -arch x86 +& dnx -p $(join-path $solutionPath "test\test.xunit.runner.dnx") test +if ($LastExitCode -ne 0) { exit 1 } + +& $dnvm use $dnxVersion -runtime CoreCLR -arch x86 +& dnx -p $(join-path $solutionPath "test\test.xunit.runner.dnx") test +if ($LastExitCode -ne 0) { exit 1 } + +& $dnvm use $dnxVersion -runtime CLR -arch x64 +& dnx -p $(join-path $solutionPath "test\test.xunit.runner.dnx") test +if ($LastExitCode -ne 0) { exit 1 } + +& $dnvm use $dnxVersion -runtime CoreCLR -arch x64 +& dnx -p $(join-path $solutionPath "test\test.xunit.runner.dnx") test +if ($LastExitCode -ne 0) { exit 1 } diff --git a/tools/packages.proj b/tools/packages.proj new file mode 100644 index 0000000..f8c163a --- /dev/null +++ b/tools/packages.proj @@ -0,0 +1,55 @@ + + + $(MSBuildProjectDirectory)\..\ + $(SolutionDir).nuget\nuget.exe + https://www.myget.org/F/xunit/api/v2/package + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +