Beispiel #1
0
        private ScanResult ScanMountPointForTemplatesAndLangpacks(MountPointScanSource source)
        {
            _ = source ?? throw new ArgumentNullException(nameof(source));
            // look for things to install

            var templates            = new List <ITemplate>();
            var localizationLocators = new List <ILocalizationLocator>();

            foreach (IGenerator generator in _environmentSettings.Components.OfType <IGenerator>())
            {
                IList <ITemplate> templateList = generator.GetTemplatesAndLangpacksFromDir(source.MountPoint, out IList <ILocalizationLocator> localizationInfo);

                foreach (ILocalizationLocator locator in localizationInfo)
                {
                    localizationLocators.Add(locator);
                }

                foreach (ITemplate template in templateList)
                {
                    templates.Add(template);
                }

                source.FoundTemplates |= templateList.Count > 0 || localizationInfo.Count > 0;
            }

            return(new ScanResult(source.MountPoint, templates, localizationLocators, Array.Empty <(string, Type, IIdentifiedComponent)>()));
        }
Beispiel #2
0
        public ScanResult Scan(string mountPointUri, bool scanForComponents)
        {
            if (string.IsNullOrWhiteSpace(mountPointUri))
            {
                throw new ArgumentException($"{nameof(mountPointUri)} should not be null or empty");
            }
            MountPointScanSource source = GetOrCreateMountPointScanInfoForInstallSource(mountPointUri);

            if (scanForComponents)
            {
                ScanForComponents(source);
            }
            return(ScanMountPointForTemplatesAndLangpacks(source));
        }
Beispiel #3
0
        private IReadOnlyList <MountPointScanSource> SetupMountPointScanInfoForInstallSourceList(IReadOnlyList <string> directoriesToScan)
        {
            List <MountPointScanSource> locationList = new List <MountPointScanSource>();

            foreach (string directory in directoriesToScan)
            {
                MountPointScanSource locationInfo = GetOrCreateMountPointScanInfoForInstallSource(directory);
                if (locationInfo != null)
                {
                    locationList.Add(locationInfo);
                }
            }

            return(locationList);
        }
        public ScanResult Scan(string sourceLocation)
        {
            if (string.IsNullOrWhiteSpace(sourceLocation))
            {
                throw new ArgumentException($"{nameof(sourceLocation)} should not be null or empty");
            }
            MountPointScanSource source = GetOrCreateMountPointScanInfoForInstallSource(sourceLocation);

            ScanResult scanResult = new ScanResult();

            ScanForComponents(source);
            ScanMountPointForTemplatesAndLangpacks(source, scanResult);

            source.MountPoint.Dispose();

            return(scanResult);
        }
        private void ScanMountPointForTemplatesAndLangpacks(MountPointScanSource source, ScanResult scanResult)
        {
            _ = source ?? throw new ArgumentNullException(nameof(source));
            // look for things to install
            foreach (IGenerator generator in _environmentSettings.SettingsLoader.Components.OfType <IGenerator>())
            {
                IList <ITemplate> templateList = generator.GetTemplatesAndLangpacksFromDir(source.MountPoint, out IList <ILocalizationLocator> localizationInfo);

                foreach (ILocalizationLocator locator in localizationInfo)
                {
                    scanResult.AddLocalization(locator);
                }

                foreach (ITemplate template in templateList)
                {
                    scanResult.AddTemplate(template);
                }

                source.FoundTemplates |= templateList.Count > 0 || localizationInfo.Count > 0;
            }
        }
Beispiel #6
0
        private void ScanMountPointForTemplatesAndLangpacks(MountPointScanSource source, ScanResult scanResult)
        {
            bool        isCopiedIntoPackagesDirectory;
            string      actualScanPath;
            IMountPoint scanMountPoint = null;

            if (!source.ShouldStayInOriginalLocation)
            {
                if (!TryCopyForNonFileSystemBasedMountPoints(source.MountPoint, source.Location, _paths.User.Packages, false, out actualScanPath))
                {
                    return;
                }

                foreach (IMountPointFactory factory in _environmentSettings.SettingsLoader.Components.OfType <IMountPointFactory>())
                {
                    if (factory.TryMount(_environmentSettings, null, actualScanPath, out scanMountPoint))
                    {
                        break;
                    }
                }

                if (scanMountPoint == null)
                {
                    _environmentSettings.Host.FileSystem.FileDelete(actualScanPath);
                    return;
                }

                isCopiedIntoPackagesDirectory = true;
            }
            else
            {
                actualScanPath = source.Location;
                scanMountPoint = source.MountPoint;
                isCopiedIntoPackagesDirectory = false;
            }

            // look for things to install
            foreach (IGenerator generator in _environmentSettings.SettingsLoader.Components.OfType <IGenerator>())
            {
                IList <ITemplate> templateList = generator.GetTemplatesAndLangpacksFromDir(scanMountPoint, out IList <ILocalizationLocator> localizationInfo);

                foreach (ILocalizationLocator locator in localizationInfo)
                {
                    scanResult.AddLocalization(locator);
                }

                foreach (ITemplate template in templateList)
                {
                    scanResult.AddTemplate(template);
                }

                source.FoundTemplates |= templateList.Count > 0 || localizationInfo.Count > 0;
            }

            // finalize the result
            if (source.FoundTemplates)
            {
                // add the MP
                _environmentSettings.SettingsLoader.AddMountPoint(scanMountPoint);
                // add the MP to the scan result
                scanResult.AddInstalledMountPointId(scanMountPoint.Info.MountPointId);
            }
            else
            {
                // delete the copy
                if (!source.FoundTemplates && isCopiedIntoPackagesDirectory)
                {
                    try
                    {
                        // The source was copied to packages and then scanned for templates.
                        // Nothing was found, and this is a copy that now has no use, so delete it.
                        _environmentSettings.SettingsLoader.ReleaseMountPoint(scanMountPoint);

                        // It's always copied as an archive, so it's a file delete, not a directory delete
                        _environmentSettings.Host.FileSystem.FileDelete(actualScanPath);
                    }
                    catch (Exception ex)
                    {
                        _environmentSettings.Host.LogDiagnosticMessage($"During ScanMountPointForTemplatesAndLangpacks() cleanup, couldn't delete source copied into the packages dir: {actualScanPath}", "Install");
                        _environmentSettings.Host.LogDiagnosticMessage($"\tError: {ex.Message}", "Install");
                    }
                }
            }
        }
Beispiel #7
0
        private void ScanForComponents(MountPointScanSource source, bool allowDevInstall)
        {
            bool isCopiedIntoContentDirectory;

            if (!source.MountPoint.Root.EnumerateFiles("*.dll", SearchOption.AllDirectories).Any())
            {
                return;
            }

            string actualScanPath;

            if (!source.ShouldStayInOriginalLocation)
            {
                if (!TryCopyForNonFileSystemBasedMountPoints(source.MountPoint, source.Location, _paths.User.Content, true, out actualScanPath))
                {
                    return;
                }

                isCopiedIntoContentDirectory = true;
            }
            else
            {
                if (!allowDevInstall)
                {
                    // TODO: localize this message
                    _environmentSettings.Host.LogDiagnosticMessage("Installing local .dll files is not a standard supported scenario. Either package it in a .zip or .nupkg, or install it using '--dev:install'.", "Install");
                    // dont allow .dlls to be installed from a file unless the debugging flag is set.
                    return;
                }

                actualScanPath = source.Location;
                isCopiedIntoContentDirectory = false;
            }

            foreach (KeyValuePair <string, Assembly> asm in AssemblyLoader.LoadAllFromPath(_paths, out IEnumerable <string> failures, actualScanPath))
            {
                try
                {
                    IReadOnlyList <Type> typeList = asm.Value.GetTypes();

                    if (typeList.Count > 0)
                    {
                        // TODO: figure out what to do with probing path registration when components are not found.
                        // They need to be registered for dependent assemblies, not just when an assembly can be loaded.
                        // We'll need to figure out how to know when that is.
                        _environmentSettings.SettingsLoader.Components.RegisterMany(typeList);
                        _environmentSettings.SettingsLoader.AddProbingPath(Path.GetDirectoryName(asm.Key));
                        source.FoundComponents = true;
                    }
                }
                catch
                {
                    // exceptions here are ok, due to dependency errors, etc.
                }
            }

            if (!source.FoundComponents && isCopiedIntoContentDirectory)
            {
                try
                {
                    // The source was copied to content and then scanned for components.
                    // Nothing was found, and this is a copy that now has no use, so delete it.
                    // Note: no mount point was created for this copy, so no need to release it.
                    _environmentSettings.Host.FileSystem.DirectoryDelete(actualScanPath, true);
                }
                catch (Exception ex)
                {
                    _environmentSettings.Host.LogDiagnosticMessage($"During ScanForComponents() cleanup, couldn't delete source copied into the content dir: {actualScanPath}", "Install");
                    _environmentSettings.Host.LogDiagnosticMessage($"\tError: {ex.Message}", "Install");
                }
            }
        }
Beispiel #8
0
        private void ScanForComponents(MountPointScanSource source)
        {
            _ = source ?? throw new ArgumentNullException(nameof(source));

            bool isCopiedIntoContentDirectory;

            if (!source.MountPoint.Root.EnumerateFiles("*.dll", SearchOption.AllDirectories).Any())
            {
                return;
            }

            string?actualScanPath;

            if (!source.ShouldStayInOriginalLocation)
            {
                if (!TryCopyForNonFileSystemBasedMountPoints(source.MountPoint, source.Location, _paths.Content, true, out actualScanPath) || actualScanPath == null)
                {
                    return;
                }

                isCopiedIntoContentDirectory = true;
            }
            else
            {
                actualScanPath = source.Location;
                isCopiedIntoContentDirectory = false;
            }

            foreach (KeyValuePair <string, Assembly> asm in LoadAllFromPath(out IEnumerable <string> failures, actualScanPath))
            {
                try
                {
                    IReadOnlyList <Type> typeList = asm.Value.GetTypes();

                    if (typeList.Count > 0)
                    {
                        // TODO: figure out what to do with probing path registration when components are not found.
                        // They need to be registered for dependent assemblies, not just when an assembly can be loaded.
                        // We'll need to figure out how to know when that is.
#pragma warning disable CS0618 // Type or member is obsolete
                        _environmentSettings.Components.RegisterMany(typeList);
#pragma warning restore CS0618 // Type or member is obsolete
                        source.FoundComponents = true;
                    }
                }
                catch
                {
                    // exceptions here are ok, due to dependency errors, etc.
                }
            }

            if (!source.FoundComponents && isCopiedIntoContentDirectory)
            {
                try
                {
                    // The source was copied to content and then scanned for components.
                    // Nothing was found, and this is a copy that now has no use, so delete it.
                    // Note: no mount point was created for this copy, so no need to release it.
                    _environmentSettings.Host.FileSystem.DirectoryDelete(actualScanPath, true);
                }
                catch (Exception ex)
                {
                    _logger.LogDebug($"During ScanForComponents() cleanup, couldn't delete source copied into the content dir: {actualScanPath}. Details: {ex}.");
                }
            }
        }