Esempio n. 1
0
        private static bool TryLocateNativeAssetFromDeps(string name, out string appLocalNativePath,
                                                         out string depsResolvedPath)
        {
            var defaultContext = DependencyContext.Default;

            if (defaultContext == null)
            {
                appLocalNativePath = null;
                depsResolvedPath   = null;
                return(false);
            }

            var currentRid = RuntimeEnvironment.GetRuntimeIdentifier();
            var allRiDs    = new List <string>();

            allRiDs.Add(currentRid);
            if (!AddFallbacks(allRiDs, currentRid, defaultContext.RuntimeGraph))
            {
                var guessedFallbackRid = GuessFallbackRid(currentRid);
                if (guessedFallbackRid != null)
                {
                    allRiDs.Add(guessedFallbackRid);
                    AddFallbacks(allRiDs, guessedFallbackRid, defaultContext.RuntimeGraph);
                }
            }

            foreach (var rid in allRiDs)
            {
                foreach (var runtimeLib in defaultContext.RuntimeLibraries)
                {
                    foreach (var nativeAsset in runtimeLib.GetRuntimeNativeAssets(defaultContext, rid))
                    {
                        if (Path.GetFileName(nativeAsset) == name || Path.GetFileNameWithoutExtension(nativeAsset) == name)
                        {
                            appLocalNativePath = Path.Combine(
                                AppContext.BaseDirectory,
                                nativeAsset);
                            appLocalNativePath = Path.GetFullPath(appLocalNativePath);

                            depsResolvedPath = Path.Combine(
                                GetNugetPackagesRootDirectory(),
                                runtimeLib.Name.ToLowerInvariant(),
                                runtimeLib.Version,
                                nativeAsset);
                            depsResolvedPath = Path.GetFullPath(depsResolvedPath);

                            return(true);
                        }
                    }
                }
            }

            appLocalNativePath = null;
            depsResolvedPath   = null;
            return(false);
        }
Esempio n. 2
0
        private void ConfigureServices(IServiceCollection serviceCollection)
        {
            _serviceCollection = serviceCollection;

            // WebServerOptions
            serviceCollection.Configure <WebServerOptions>(_configuration);

            // logging
            serviceCollection.AddLogging(loggingBuilder =>
            {
                WebClientLoggerProvider clientMessageLoggerProvider;

                // client message log
                clientMessageLoggerProvider = new WebClientLoggerProvider((category, logLevel) => category != "System" && logLevel >= LogLevel.Information);

                // add logger
                loggingBuilder
                .AddFilter((provider, source, logLevel) => !source.StartsWith("Microsoft."))
                .AddDebug()
                .AddProvider(clientMessageLoggerProvider);

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    EventLogSettings eventLogSettings;

                    if (BasicBootloader.IsUserInteractive)
                    {
                        eventLogSettings = new EventLogSettings();
                    }
                    else
                    {
                        eventLogSettings = new EventLogSettings()
                        {
                            LogName    = _webServerOptions.EventLogName,
                            SourceName = _webServerOptions.EventLogSourceName
                        };
                    }

                    eventLogSettings.Filter = (category, logLevel) => category == "System" && logLevel >= LogLevel.Information;

                    loggingBuilder.AddEventLog(eventLogSettings);
                }
            });

            // OneDasEngine
            serviceCollection.AddOneDas(options =>
            {
                options.RestoreRuntimeId = RuntimeEnvironment.GetRuntimeIdentifier();
            });

            // Misc
            serviceCollection.AddSingleton <OneDasConsole>();
            serviceCollection.AddSingleton <ExtensionLoader>();
            serviceCollection.AddSingleton <ClientPushService>();
        }
Esempio n. 3
0
 public Dictionary <string, string> GetTelemetryCommonProperties()
 {
     return(new Dictionary <string, string>
     {
         { OSVersion, RuntimeEnvironment.OperatingSystemVersion },
         { OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString() },
         { RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier() },
         { ProductVersion, _productVersion },
         { DockerContainer, IsDockerContainer() },
         { MachineId, GetMachineId() },
         { KernelVersion, GetKernelVersion() }
     });
 }
Esempio n. 4
0
        public static string GetNativeLibraryDirectoryName()
        {
            var runtimeIdentifier = RuntimeEnvironment.GetRuntimeIdentifier();
            int index             = Array.BinarySearch(s_rids, runtimeIdentifier);

            if (index < 0)
            {
                throw new PlatformNotSupportedException();
            }

            Debug.Assert(s_directories.Length == s_rids.Length);
            return(s_directories[index]);
        }
 public static string InferLegacyRestoreRuntimeIdentifier()
 {
     if (RuntimeEnvironment.OperatingSystemPlatform != Platform.Windows)
     {
         FrameworkDependencyFile fxDepsFile = new FrameworkDependencyFile();
         return(fxDepsFile.SupportsCurrentRuntime() ?
                RuntimeEnvironment.GetRuntimeIdentifier() :
                DotnetFiles.VersionFileObject.BuildRid);
     }
     else
     {
         var arch = RuntimeEnvironment.RuntimeArchitecture.ToLowerInvariant();
         return("win7-" + arch);
     }
 }
 public Dictionary <string, string> GetTelemetryCommonProperties()
 {
     return(new Dictionary <string, string>
     {
         { OSVersion, RuntimeEnvironment.OperatingSystemVersion },
         { OSPlatform, RuntimeEnvironment.OperatingSystemPlatform.ToString() },
         { OutputRedirected, Console.IsOutputRedirected.ToString() },
         { RuntimeId, RuntimeEnvironment.GetRuntimeIdentifier() },
         { ProductVersion, Constants.CliVersion },
         { TelemetryProfile, Environment.GetEnvironmentVariable(TelemetryProfileEnvironmentVariable) },
         { DockerContainer, IsDockerContainerCache() },
         { CurrentPathHash, _hasher(_getCurrentDirectory()) },
         { MachineId, MachineIdCache() },
         { KernelVersion, GetKernelVersion() },
         { InstallationType, ExternalTelemetryProperties.GetInstallationType() },
         { ProductType, ExternalTelemetryProperties.GetProductType() },
         { LibcRelease, ExternalTelemetryProperties.GetLibcRelease() },
         { LibcVersion, ExternalTelemetryProperties.GetLibcVersion() }
     });
 }
        private static void FindExtension()
        {
            if (_looked)
            {
                return;
            }

            bool hasDependencyContext;

            try
            {
                hasDependencyContext = DependencyContext.Default != null;
            }
            catch (Exception ex) // Work around dotnet/core-setup#4556
            {
                Debug.Fail(ex.ToString());
                hasDependencyContext = false;
            }

            if (hasDependencyContext)
            {
                var candidateAssets = new Dictionary <string, int>();
                var rid             = RuntimeEnvironment.GetRuntimeIdentifier();
                var rids            = DependencyContext.Default.RuntimeGraph.First(g => g.Runtime == rid).Fallbacks.ToList();
                rids.Insert(0, rid);

                foreach (var library in DependencyContext.Default.RuntimeLibraries)
                {
                    foreach (var group in library.NativeLibraryGroups)
                    {
                        foreach (var file in group.RuntimeFiles)
                        {
                            if (string.Equals(
                                    Path.GetFileName(file.Path),
                                    "mod_spatialite" + _sharedLibraryExtension,
                                    StringComparison.OrdinalIgnoreCase))
                            {
                                var fallbacks = rids.IndexOf(group.Runtime);
                                if (fallbacks != -1)
                                {
                                    candidateAssets.Add(library.Path + "/" + file.Path, fallbacks);
                                }
                            }
                        }
                    }
                }

                var assetPath = candidateAssets.OrderBy(p => p.Value)
                                .Select(p => p.Key.Replace('/', Path.DirectorySeparatorChar)).FirstOrDefault();
                if (assetPath != null)
                {
                    string assetFullPath      = null;
                    var    probingDirectories = ((string)AppDomain.CurrentDomain.GetData("PROBING_DIRECTORIES"))
                                                .Split(Path.PathSeparator);
                    foreach (var directory in probingDirectories)
                    {
                        var candidateFullPath = Path.Combine(directory, assetPath);
                        if (File.Exists(candidateFullPath))
                        {
                            assetFullPath = candidateFullPath;
                        }
                    }

                    Debug.Assert(assetFullPath != null);

                    var assetDirectory = Path.GetDirectoryName(assetFullPath);

                    var currentPath = Environment.GetEnvironmentVariable(_pathVariableName);
                    if (!currentPath.Split(Path.PathSeparator).Any(
                            p => string.Equals(
                                p.TrimEnd(Path.DirectorySeparatorChar),
                                assetDirectory,
                                StringComparison.OrdinalIgnoreCase)))
                    {
                        Environment.SetEnvironmentVariable(
                            _pathVariableName,
                            assetDirectory + Path.PathSeparator + currentPath);
                    }
                }
            }

            _looked = true;
        }
Esempio n. 8
0
        private static void FindExtension()
        {
            if (_looked)
            {
                return;
            }

            bool hasDependencyContext;

            try
            {
                hasDependencyContext = DependencyContext.Default != null;
            }
            catch (Exception ex) // Work around dotnet/core-setup#4556
            {
                Debug.Fail(ex.ToString());
                hasDependencyContext = false;
            }

            if (hasDependencyContext)
            {
                var candidateAssets = new Dictionary <(string, string), int>();
                var rid             = RuntimeEnvironment.GetRuntimeIdentifier();
                var rids            = DependencyContext.Default.RuntimeGraph.FirstOrDefault(g => g.Runtime == rid)?.Fallbacks.ToList()
                                      ?? new List <string>();
                rids.Insert(0, rid);

                foreach (var library in DependencyContext.Default.RuntimeLibraries)
                {
                    foreach (var group in library.NativeLibraryGroups)
                    {
                        foreach (var file in group.RuntimeFiles)
                        {
                            if (string.Equals(
                                    Path.GetFileName(file.Path),
                                    "mod_spatialite" + _sharedLibraryExtension,
                                    StringComparison.OrdinalIgnoreCase))
                            {
                                var fallbacks = rids.IndexOf(group.Runtime);
                                if (fallbacks != -1)
                                {
                                    candidateAssets.Add((library.Path, file.Path), fallbacks);
                                }
                            }
                        }
                    }
                }

                var assetPath = candidateAssets.OrderBy(p => p.Value)
                                .Select(p => p.Key).FirstOrDefault();
                if (assetPath != default)
                {
                    string assetDirectory = null;
                    if (File.Exists(Path.Combine(AppContext.BaseDirectory, assetPath.Item2)))
                    {
                        // NB: This enables framework-dependent deployments
                        assetDirectory = Path.Combine(
                            AppContext.BaseDirectory,
                            Path.GetDirectoryName(assetPath.Item2.Replace('/', Path.DirectorySeparatorChar)));
                    }
                    else
                    {
                        string assetFullPath      = null;
                        var    probingDirectories = ((string)AppDomain.CurrentDomain.GetData("PROBING_DIRECTORIES"))
                                                    .Split(Path.PathSeparator);
                        foreach (var directory in probingDirectories)
                        {
                            var candidateFullPath = Path.Combine(
                                directory,
                                (assetPath.Item1 + "/" + assetPath.Item2).Replace('/', Path.DirectorySeparatorChar));
                            if (File.Exists(candidateFullPath))
                            {
                                assetFullPath = candidateFullPath;
                            }
                        }

                        Check.DebugAssert(assetFullPath != null, "assetFullPath is null");

                        assetDirectory = Path.GetDirectoryName(assetFullPath);
                    }

                    Check.DebugAssert(assetDirectory != null, "assetDirectory is null");

                    // GetEnvironmentVariable can sometimes return null when there is a race condition
                    // with another thread setting it. Therefore we do a bit of back off and retry here.
                    // Note that the result can be null if no path is set on the system.
                    var delay       = 1;
                    var currentPath = Environment.GetEnvironmentVariable(_pathVariableName);
                    while (currentPath == null && delay < 1000)
                    {
                        Thread.Sleep(delay);
                        delay      *= 2;
                        currentPath = Environment.GetEnvironmentVariable(_pathVariableName);
                    }

                    if (currentPath == null ||
                        !currentPath.Split(Path.PathSeparator).Any(
                            p => string.Equals(
                                p.TrimEnd(Path.DirectorySeparatorChar),
                                assetDirectory,
                                StringComparison.OrdinalIgnoreCase)))
                    {
                        Environment.SetEnvironmentVariable(
                            _pathVariableName,
                            assetDirectory + Path.PathSeparator + currentPath);
                    }
                }
            }

            _looked = true;
        }
Esempio n. 9
0
        private bool TryLocateNativeAssetFromDeps
        (
            string name,
            out string appLocalNativePath,
            out string depsResolvedPath
        )
        {
            try
            {
                var defaultContext = DependencyContext.Default;
                var entAsm         = Assembly.GetEntryAssembly();
                if (defaultContext is null && !(entAsm is null))
                {
                    var json = new DependencyContextJsonReader();
                    defaultContext ??= json.Read
                    (
                        File.OpenRead
                        (
                            Path.Combine
                            (
                                Path.GetDirectoryName(entAsm.Location),
                                entAsm.GetName().Name + ".deps.json"
                            )
                        )
                    );
                    defaultContext ??=
                    json.Read
                    (
                        File.OpenRead
                        (
                            Path.Combine(AppContext.BaseDirectory, entAsm.GetName().Name + ".deps.json")
                        )
                    );
                }

                if (defaultContext == null)
                {
                    appLocalNativePath = null;
                    depsResolvedPath   = null;
                    return(false);
                }

                var currentRid = RuntimeEnvironment.GetRuntimeIdentifier();
                foreach (var rid in GetAllRuntimeIds(currentRid, defaultContext))
                {
                    foreach (var runtimeLib in defaultContext.RuntimeLibraries)
                    {
                        foreach (var nativeAsset in runtimeLib.GetRuntimeNativeAssets(defaultContext, rid))
                        {
                            if (Path.GetFileName(nativeAsset) == name || Path.GetFileNameWithoutExtension(nativeAsset) == name)
                            {
                                appLocalNativePath = Path.Combine
                                                     (
                                    AppContext.BaseDirectory,
                                    nativeAsset
                                                     );
                                appLocalNativePath = Path.GetFullPath(appLocalNativePath);

                                depsResolvedPath = Path.Combine
                                                   (
                                    GetNugetPackagesRootDirectory(),
                                    runtimeLib.Name.ToLowerInvariant(),
                                    runtimeLib.Version,
                                    nativeAsset
                                                   );
                                depsResolvedPath = Path.GetFullPath(depsResolvedPath);

                                return(true);
                            }
                        }
                    }
                }

                appLocalNativePath = null;
                depsResolvedPath   = null;
                return(false);
            }
            catch (Exception ex)
            {
                appLocalNativePath = null;
                depsResolvedPath   = null;
                return(false);
            }
        }
        internal static string GetNativeLibraryDirectory()
        {
            var dir = Path.GetDirectoryName(typeof(GitLoaderContext).Assembly.Location);

            return(Path.Combine(dir, RuntimePath, RuntimeIdMap.GetNativeLibraryDirectoryName(RuntimeEnvironment.GetRuntimeIdentifier()), "native"));
        }
Esempio n. 11
0
        static void EnsureLoadable(string package, string library)
        {
            var runtimeLibrary = DependencyContext.Default?.RuntimeLibraries.FirstOrDefault(l => l.Name == package);

            if (runtimeLibrary == null)
            {
                return;
            }

            string sharedLibraryExtension;
            string pathVariableName;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                sharedLibraryExtension = ".dll";
                pathVariableName       = "PATH";
            }
            else
            {
                // NB: Modifying the path at runtime only works on Windows. On Linux and Mac, set LD_LIBRARY_PATH or
                //     DYLD_LIBRARY_PATH before running the app
                return;
            }

            var candidateAssets = new Dictionary <(string Package, string Asset), int>();
            var rid             = RuntimeEnvironment.GetRuntimeIdentifier();
            var rids            = DependencyContext.Default.RuntimeGraph.First(g => g.Runtime == rid).Fallbacks.ToList();

            rids.Insert(0, rid);

            foreach (var group in runtimeLibrary.NativeLibraryGroups)
            {
                foreach (var file in group.RuntimeFiles)
                {
                    if (string.Equals(
                            Path.GetFileName(file.Path),
                            library + sharedLibraryExtension,
                            StringComparison.OrdinalIgnoreCase))
                    {
                        var fallbacks = rids.IndexOf(group.Runtime);
                        if (fallbacks != -1)
                        {
                            candidateAssets.Add((runtimeLibrary.Path, file.Path), fallbacks);
                        }
                    }
                }
            }

            var assetPath = candidateAssets
                            .OrderBy(p => p.Value)
                            .Select(p => p.Key)
                            .FirstOrDefault();

            if (assetPath != default)
            {
                string assetDirectory = null;
                if (File.Exists(Path.Combine(AppContext.BaseDirectory, assetPath.Asset)))
                {
                    // NB: Framework-dependent deployments copy assets to the application base directory
                    assetDirectory = Path.Combine(
                        AppContext.BaseDirectory,
                        Path.GetDirectoryName(assetPath.Asset.Replace('/', Path.DirectorySeparatorChar)));
                }
                else
                {
                    string assetFullPath      = null;
                    var    probingDirectories = ((string)AppDomain.CurrentDomain.GetData("PROBING_DIRECTORIES"))
                                                .Split(Path.PathSeparator);
                    foreach (var directory in probingDirectories)
                    {
                        var candidateFullPath = Path.Combine(
                            directory,
                            (assetPath.Package + "/" + assetPath.Asset).Replace('/', Path.DirectorySeparatorChar));
                        if (File.Exists(candidateFullPath))
                        {
                            assetFullPath = candidateFullPath;
                        }
                    }

                    Debug.Assert(assetFullPath != null);

                    assetDirectory = Path.GetDirectoryName(assetFullPath);
                }

                Debug.Assert(assetDirectory != null);

                var path = new HashSet <string>(Environment.GetEnvironmentVariable(pathVariableName).Split(Path.PathSeparator));

                if (path.Add(assetDirectory))
                {
                    Environment.SetEnvironmentVariable(pathVariableName, string.Join(Path.PathSeparator, path));
                }
            }
        }
        private static string FindExtension()
        {
            if (_extension != null)
            {
                return(_extension);
            }

            var candidateAssets = new Dictionary <string, int>();
            var rid             = RuntimeEnvironment.GetRuntimeIdentifier();
            var rids            = DependencyContext.Default.RuntimeGraph.First(g => g.Runtime == rid).Fallbacks.ToList();

            rids.Insert(0, rid);

            foreach (var library in DependencyContext.Default.RuntimeLibraries)
            {
                foreach (var group in library.NativeLibraryGroups)
                {
                    foreach (var file in group.RuntimeFiles)
                    {
                        if (string.Equals(
                                Path.GetFileName(file.Path),
                                "mod_spatialite" + _sharedLibraryExtension,
                                StringComparison.OrdinalIgnoreCase))
                        {
                            var fallbacks = rids.IndexOf(group.Runtime);
                            if (fallbacks != -1)
                            {
                                candidateAssets.Add(library.Path + "/" + file.Path, fallbacks);
                            }
                        }
                    }
                }
            }

            var assetPath = candidateAssets.OrderBy(p => p.Value)
                            .Select(p => p.Key.Replace('/', Path.DirectorySeparatorChar)).FirstOrDefault();

            if (assetPath != null)
            {
                string assetFullPath      = null;
                var    probingDirectories = ((string)AppDomain.CurrentDomain.GetData("PROBING_DIRECTORIES"))
                                            .Split(Path.PathSeparator);
                foreach (var directory in probingDirectories)
                {
                    var candidateFullPath = Path.Combine(directory, assetPath);
                    if (File.Exists(candidateFullPath))
                    {
                        assetFullPath = candidateFullPath;
                    }
                }
                Debug.Assert(assetFullPath != null);

                var assetDirectory = Path.GetDirectoryName(assetFullPath);

                var currentPath = Environment.GetEnvironmentVariable(_pathVariableName);
                if (!currentPath.Split(Path.PathSeparator).Any(
                        p => string.Equals(
                            p.TrimEnd(Path.DirectorySeparatorChar),
                            assetDirectory,
                            StringComparison.OrdinalIgnoreCase)))
                {
                    Environment.SetEnvironmentVariable(
                        _pathVariableName,
                        assetDirectory + Path.PathSeparator + currentPath);
                }
            }

            var extension = "mod_spatialite";

            // Workaround ericsink/SQLitePCL.raw#225
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                extension += _sharedLibraryExtension;
            }

            return(_extension = extension);
        }