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

Commit

Permalink
Removing passing args using environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzyk committed Sep 22, 2015
1 parent 7168520 commit f4ec758
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 223 deletions.
6 changes: 0 additions & 6 deletions src/Microsoft.Dnx.Host.Clr/DomainManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public override void InitializeNewDomain(AppDomainSetup appDomainInfo)

_info.Main = Main;
BindApplicationMain(ref _info);

if (!string.IsNullOrEmpty(_info.ApplicationBase))
{
Environment.SetEnvironmentVariable(EnvironmentNames.AppBase, _info.ApplicationBase);
}

appDomainInfo.ApplicationBase = _info.RuntimeDirectory;
appDomainInfo.TargetFrameworkName = DetermineAppDomainTargetFramework();
appDomainInfo.ConfigurationFile = Path.Combine(_info.ApplicationBase, Constants.AppConfigurationFileName);
Expand Down
20 changes: 5 additions & 15 deletions src/Microsoft.Dnx.Host.Mono/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,15 @@ public static int Main(string[] arguments)
}
}

// Set the default lib path to be next to the entry point location
Environment.SetEnvironmentVariable(EnvironmentNames.DefaultLib, Path.GetDirectoryName(typeof(EntryPoint).Assembly.Location));
Environment.SetEnvironmentVariable(EnvironmentNames.ConsoleHost, "1");

var p = Environment.OSVersion.Platform;
if (p != PlatformID.MacOSX && p != PlatformID.Unix)
{
Environment.SetEnvironmentVariable(EnvironmentNames.DnxIsWindows, "1");
}

arguments = ExpandCommandLineArguments(arguments);

// Set application base dir
var appbaseIndex = arguments.ToList().FindIndex(arg =>
string.Equals(arg, "--appbase", StringComparison.OrdinalIgnoreCase));
if (appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1))
{
Environment.SetEnvironmentVariable(EnvironmentNames.AppBase, arguments[appbaseIndex + 1]);
}

var appBase = appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1)
? arguments[appbaseIndex + 1]
: Directory.GetCurrentDirectory();

string operatingSystem, osVersion, architecture;
GetOsDetails(out operatingSystem, out osVersion, out architecture);
Expand All @@ -84,7 +74,7 @@ public static int Main(string[] arguments)
OsVersion = osVersion,
Architecture = architecture,
RuntimeDirectory = Path.GetDirectoryName(typeof(EntryPoint).Assembly.Location),
ApplicationBase = appbaseIndex >= 0 && (appbaseIndex < arguments.Length - 1) ? arguments[appbaseIndex + 1] : Directory.GetCurrentDirectory(),
ApplicationBase = appBase,
// NOTE(anurse): Mono is always "dnx451" (for now).
TargetFramework = new FrameworkName("DNX", new Version(4, 5, 1)),
HandleExceptions = true
Expand Down
14 changes: 3 additions & 11 deletions src/Microsoft.Dnx.Host/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Microsoft.Dnx.Runtime;
using Microsoft.Dnx.Runtime.Common;
using Microsoft.Dnx.Runtime.Common.DependencyInjection;
using Microsoft.Dnx.Runtime.Common.Impl;
using Microsoft.Dnx.Runtime.Infrastructure;
using Microsoft.Dnx.Runtime.Loader;

Expand All @@ -26,7 +23,7 @@ public Bootstrapper(IEnumerable<string> searchPaths)
_searchPaths = searchPaths;
}

public Task<int> RunAsync(List<string> args, IRuntimeEnvironment env, FrameworkName targetFramework)
public Task<int> RunAsync(List<string> args, IRuntimeEnvironment env, string appBase, FrameworkName targetFramework)
{
var accessor = LoadContextAccessor.Instance;
var container = new LoaderContainer();
Expand All @@ -48,17 +45,12 @@ public Task<int> RunAsync(List<string> args, IRuntimeEnvironment env, FrameworkN
}

#if DNX451
string applicationBaseDirectory = Environment.GetEnvironmentVariable(EnvironmentNames.AppBase);

if (string.IsNullOrEmpty(applicationBaseDirectory))
{
applicationBaseDirectory = Directory.GetCurrentDirectory();
}
string applicationBaseDirectory = appBase;

// Set the app domain variable so that AppContext.BaseDirectory works on .NET Framework (and hopefully mono)
AppDomain.CurrentDomain.SetData("APP_CONTEXT_BASE_DIRECTORY", applicationBaseDirectory);
#else
string applicationBaseDirectory = AppContext.BaseDirectory;
var applicationBaseDirectory = AppContext.BaseDirectory;
#endif

var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug";
Expand Down
76 changes: 0 additions & 76 deletions src/Microsoft.Dnx.Host/NativeMethods.cs

This file was deleted.

18 changes: 8 additions & 10 deletions src/Microsoft.Dnx.Host/RuntimeBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ public static class RuntimeBootstrapper

public static int Execute(string[] args, BootstrapperContext bootstrapperContext)
{
// If we're a console host then print exceptions to stderr
var printExceptionsToStdError = Environment.GetEnvironmentVariable(EnvironmentNames.ConsoleHost) == "1";

try
{
return ExecuteAsync(args, bootstrapperContext).GetAwaiter().GetResult();
}
catch (Exception ex)
{
if (printExceptionsToStdError)
// If we're a console host then print exceptions to stderr
if (bootstrapperContext.HandleExceptions)
{
PrintErrors(ex);
return 1;
Expand Down Expand Up @@ -87,7 +85,7 @@ public static Task<int> ExecuteAsync(string[] args, BootstrapperContext bootstra
var optionFramework = app.Option("--framework <FRAMEWORK_ID>", "Set the framework version to use when running (i.e. dnx451, dnx452, dnx46, ...)", CommandOptionType.SingleValue);
#endif

var env = new RuntimeEnvironment();
var env = new RuntimeEnvironment(bootstrapperContext);

app.HelpOption("-?|-h|--help");
app.VersionOption("--version",
Expand Down Expand Up @@ -154,18 +152,18 @@ public static Task<int> ExecuteAsync(string[] args, BootstrapperContext bootstra
}

// Resolve the lib paths
IEnumerable<string> searchPaths = ResolveSearchPaths(optionLib.Values, app.RemainingArguments);
IEnumerable<string> searchPaths =
ResolveSearchPaths(bootstrapperContext.RuntimeDirectory, optionLib.Values, app.RemainingArguments);

var bootstrapper = new Bootstrapper(searchPaths);
return bootstrapper.RunAsync(app.RemainingArguments, env, bootstrapperContext.TargetFramework);

return bootstrapper.RunAsync(app.RemainingArguments, env, bootstrapperContext.ApplicationBase, bootstrapperContext.TargetFramework);
}

private static IEnumerable<string> ResolveSearchPaths(List<string> libPaths, List<string> remainingArgs)
private static IEnumerable<string> ResolveSearchPaths(string defaultLibPath, List<string> libPaths, List<string> remainingArgs)
{
var searchPaths = new List<string>();

var defaultLibPath = Environment.GetEnvironmentVariable(EnvironmentNames.DefaultLib);

if (!string.IsNullOrEmpty(defaultLibPath))
{
// Add the default lib folder if specified
Expand Down
34 changes: 5 additions & 29 deletions src/Microsoft.Dnx.Host/RuntimeEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Reflection;
using Microsoft.Dnx.Host;

namespace Microsoft.Dnx.Runtime
{
Expand All @@ -14,33 +15,22 @@ public class RuntimeEnvironment : IRuntimeEnvironment

private string _runtimeVersion;

public RuntimeEnvironment()
public RuntimeEnvironment(BootstrapperContext bootstrapperContext)
{
#if DNXCORE50
RuntimeType = RuntimeTypes.CoreCLR;
RuntimeArchitecture = IntPtr.Size == 8 ? RuntimeArchitectures.X64 : RuntimeArchitectures.X86;
#else
RuntimeType = Type.GetType("Mono.Runtime") == null ? RuntimeTypes.CLR : RuntimeTypes.Mono;
RuntimeArchitecture = Environment.Is64BitProcess ? RuntimeArchitectures.X64 : RuntimeArchitectures.X86;
#endif

// This is a temporary workaround until we pass a struct with OS information from native code
if (Environment.GetEnvironmentVariable(EnvironmentNames.DnxIsWindows) == "1")
{
_osName = RuntimeOperatingSystems.Windows;
}
RuntimeArchitecture = bootstrapperContext.Architecture;
_osName = bootstrapperContext.OperatingSystem;
_osVersion = bootstrapperContext.OsVersion;
}

public string OperatingSystem
{
get
{
if (_osName == null)
{
string uname = NativeMethods.Uname();
_osName = string.IsNullOrEmpty(uname) ? RuntimeOperatingSystems.Windows : uname;
}

return _osName;
}
}
Expand All @@ -49,20 +39,6 @@ public string OperatingSystemVersion
{
get
{
if (OperatingSystem != RuntimeOperatingSystems.Windows)
{
return null;
}

if (_osVersion == null)
{
#if DNXCORE50
_osVersion = NativeMethods.OSVersion.ToString();
#else
_osVersion = Environment.OSVersion.Version.ToString();
#endif
}

return _osVersion;
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/Microsoft.Dnx.Runtime.Internals/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ internal static class RuntimeTypes
public static readonly string Mono = nameof(Mono);
}

internal static class RuntimeArchitectures
{
public static readonly string X86 = "x86";
public static readonly string X64 = "x64";
}

internal static class RuntimeOperatingSystems
{
public static readonly string Windows = nameof(Windows);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ private static string GetRuntimeOsName(this IRuntimeEnvironment env)
{
ver = "10";
}

return os + ver;
}
else
{
os = os.ToLowerInvariant(); // Just use the lower-case full name of the OS as the RID OS
}
return os + ver;
return os;
}
}
}
3 changes: 0 additions & 3 deletions src/Microsoft.Dnx.Runtime.Sources/Impl/EnvironmentNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ internal static class EnvironmentNames
public const string AppBase = "DNX_APPBASE";
public const string Framework = "DNX_FRAMEWORK";
public const string Configuration = "DNX_CONFIGURATION";
public const string ConsoleHost = "DNX_CONSOLE_HOST";
public const string DefaultLib = "DNX_DEFAULT_LIB";
public const string BuildKeyFile = "DNX_BUILD_KEY_FILE";
public const string BuildDelaySign = "DNX_BUILD_DELAY_SIGN";
public const string PortablePdb = "DNX_BUILD_PORTABLE_PDB";
public const string DnxIsWindows = "DNX_IS_WINDOWS";
public const string AspNetLoaderPath = "DNX_ASPNET_LOADER_PATH";
public const string DnxDisableMinVersionCheck = "DNX_NO_MIN_VERSION_CHECK";
}
Expand Down
7 changes: 0 additions & 7 deletions src/dnx/dnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,8 @@ bool GetApplicationBase(const dnx::xstring_t& currentDirectory, int argc, dnx::c

int CallApplicationProcessMain(int argc, dnx::char_t* argv[], dnx::trace_writer& trace_writer)
{
// Set the DNX_CONOSLE_HOST flag which will print exceptions to stderr instead of throwing
SetConsoleHost();

const auto currentDirectory = GetNativeBootstrapperDirectory();

// Set the DEFAULT_LIB environment variable to be the same directory as the exe
SetEnvironmentVariable(_X("DNX_DEFAULT_LIB"), currentDirectory.c_str());

// Set the FRAMEWORK environment variable to the value provided on the command line
// (it needs to be available BEFORE the application main is called)
auto frameworkName = GetOptionValue(argc, argv, _X("--framework"));
Expand Down Expand Up @@ -247,7 +241,6 @@ int CallApplicationProcessMain(int argc, dnx::char_t* argv[], dnx::trace_writer&
_X("dnx.coreclr.so");
#else
_X("dnx.clr.dll");
SetEnvironmentVariable(_X("DNX_IS_WINDOWS"), _X("1"));
#endif

// Note: need to keep as ASCII as GetProcAddress function takes ASCII params
Expand Down
1 change: 0 additions & 1 deletion src/dnx/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
dnx::xstring_t GetNativeBootstrapperDirectory();
void WaitForDebuggerToAttach();
bool IsTracingEnabled();
void SetConsoleHost();
bool GetAppBasePathFromEnvironment(dnx::char_t* szPath);
bool GetFullPath(const dnx::char_t* szPath, dnx::char_t* szFullPath);
int CallApplicationMain(const dnx::char_t* moduleName, const char* functionName, CALL_APPLICATION_MAIN_DATA* data, dnx::trace_writer& trace_writer);
Expand Down
10 changes: 0 additions & 10 deletions src/dnx/pal.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ bool IsTracingEnabled()
return dnxTraceEnv != NULL && (strcmp(dnxTraceEnv, "1") == 0);
}

void SetConsoleHost()
{
char* dnxConsoleHostEnv = getenv("DNX_CONSOLE_HOST");

if (dnxConsoleHostEnv == NULL)
{
setenv("DNX_CONSOLE_HOST", "1", 1);
}
}

bool GetAppBasePathFromEnvironment(char* szPath)
{
char* appBaseEnv = getenv("DNX_APPBASE");
Expand Down
Loading

0 comments on commit f4ec758

Please sign in to comment.