protected override IContainer CreateContainer()
        {
            var builder = new ContainerBuilder();

            this.Logger.Debug("Registering runtime services");

            builder.RegisterInstance(Logger).Exported(x => x.As <ILog>());
            builder.RegisterType(_scriptEngineType).As <IScriptEngine>().SingleInstance();
            builder.RegisterType(_scriptExecutorType).As <IScriptExecutor>().SingleInstance();
            builder.RegisterType <ScriptServices>().SingleInstance();

            RegisterLineProcessors(builder);

            RegisterOverrideOrDefault <IFileSystem>(builder, b => b.RegisterType <FileSystem>().As <IFileSystem>().SingleInstance());
            RegisterOverrideOrDefault <IAssemblyUtility>(builder, b => b.RegisterType <AssemblyUtility>().As <IAssemblyUtility>().SingleInstance());
            RegisterOverrideOrDefault <IPackageContainer>(builder, b => b.RegisterType <PackageContainer>().As <IPackageContainer>().SingleInstance());
            RegisterOverrideOrDefault <IPackageAssemblyResolver>(builder, b => b.RegisterType <PackageAssemblyResolver>().As <IPackageAssemblyResolver>().SingleInstance());
            RegisterOverrideOrDefault <IAssemblyResolver>(builder, b => b.RegisterType <AssemblyResolver>().As <IAssemblyResolver>().SingleInstance());
            RegisterOverrideOrDefault <IScriptHostFactory>(builder, b => b.RegisterType <ScriptHostFactory>().As <IScriptHostFactory>().SingleInstance());
            RegisterOverrideOrDefault <IFilePreProcessor>(builder, b => b.RegisterType <FilePreProcessor>().As <IFilePreProcessor>().SingleInstance());
            RegisterOverrideOrDefault <IReplCommandService>(builder, b => b.RegisterType <ReplCommandService>().As <IReplCommandService>().SingleInstance());
            RegisterOverrideOrDefault <IScriptPackResolver>(builder, b => b.RegisterType <ScriptPackResolver>().As <IScriptPackResolver>().SingleInstance());
            RegisterOverrideOrDefault <IInstallationProvider>(builder, b => b.RegisterType <NugetInstallationProvider>().As <IInstallationProvider>().SingleInstance());
            RegisterOverrideOrDefault <IPackageInstaller>(builder, b => b.RegisterType <PackageInstaller>().As <IPackageInstaller>().SingleInstance());
            RegisterOverrideOrDefault <ScriptServices>(builder, b => b.RegisterType <ScriptServices>().SingleInstance());
            RegisterOverrideOrDefault <IConsole>(builder, b => b.RegisterInstance(_console));

            var assemblyResolver = _initializationServices.GetAssemblyResolver();

            if (_initDirectoryCatalog)
            {
                var currentDirectory = Environment.CurrentDirectory;
                var assemblies       = assemblyResolver.GetAssemblyPaths(currentDirectory, _scriptName);

                var aggregateCatalog = new AggregateCatalog();

                assemblies.Select(x => new AssemblyCatalog(x)).ToList()
                .ForEach(catalog => aggregateCatalog.Catalogs.Add(catalog));

                builder.RegisterComposablePartCatalog(aggregateCatalog);
            }

            return(builder.Build());
        }
Exemplo n.º 2
0
        protected override IContainer CreateContainer()
        {
            var builder = new ContainerBuilder();

            _log.Debug("Registering runtime services");

            builder.RegisterInstance(this.LogProvider).Exported(x => x.As <ILogProvider>());
            builder.RegisterType(_scriptEngineType).As <IScriptEngine>().SingleInstance();
            builder.RegisterType(_scriptExecutorType).As <IScriptExecutor>().SingleInstance();
            builder.RegisterType(_replType).As <IRepl>().SingleInstance();
            builder.RegisterType <ScriptServices>().SingleInstance();
            builder.RegisterType <Repl>().As <IRepl>().SingleInstance();
            builder.RegisterType <Printers>().SingleInstance();

            RegisterLineProcessors(builder);
            RegisterReplCommands(builder);

            RegisterOverrideOrDefault <IFileSystem>(
                builder, b => b.RegisterType <FileSystem>().As <IFileSystem>().SingleInstance());

            RegisterOverrideOrDefault <IAssemblyUtility>(
                builder, b => b.RegisterType <AssemblyUtility>().As <IAssemblyUtility>().SingleInstance());

            RegisterOverrideOrDefault <IPackageContainer>(
                builder, b => b.RegisterType <PackageContainer>().As <IPackageContainer>().SingleInstance());

            RegisterOverrideOrDefault <IPackageAssemblyResolver>(
                builder, b => b.RegisterType <PackageAssemblyResolver>().As <IPackageAssemblyResolver>().SingleInstance());

            RegisterOverrideOrDefault <IAssemblyResolver>(
                builder, b => b.RegisterType <AssemblyResolver>().As <IAssemblyResolver>().SingleInstance());

            RegisterOverrideOrDefault <IScriptHostFactory>(
                builder, b => b.RegisterType <ScriptHostFactory>().As <IScriptHostFactory>().SingleInstance());

            RegisterOverrideOrDefault <IFilePreProcessor>(
                builder, b => b.RegisterType <FilePreProcessor>().As <IFilePreProcessor>().SingleInstance());

            RegisterOverrideOrDefault <IScriptPackResolver>(
                builder, b => b.RegisterType <ScriptPackResolver>().As <IScriptPackResolver>().SingleInstance());

            RegisterOverrideOrDefault <IInstallationProvider>(
                builder, b => b.RegisterType <NugetInstallationProvider>().As <IInstallationProvider>().SingleInstance());

            RegisterOverrideOrDefault <IPackageInstaller>(
                builder, b => b.RegisterType <PackageInstaller>().As <IPackageInstaller>().SingleInstance());

            RegisterOverrideOrDefault <ScriptServices>(
                builder, b => b.RegisterType <ScriptServices>().SingleInstance());

            RegisterOverrideOrDefault <IObjectSerializer>(
                builder, b => b.RegisterType <ObjectSerializer>().As <IObjectSerializer>().SingleInstance());

            RegisterOverrideOrDefault <IConsole>(
                builder, b => b.RegisterInstance(_console));

            RegisterOverrideOrDefault <IFileSystemMigrator>(
                builder, b => b.RegisterType <FileSystemMigrator>().As <IFileSystemMigrator>().SingleInstance());

            RegisterOverrideOrDefault <IScriptLibraryComposer>(
                builder, b => b.RegisterType <ScriptLibraryComposer>().As <IScriptLibraryComposer>().SingleInstance());

            RegisterOverrideOrDefault <IVisualStudioSolutionWriter>(
                builder, b => b.RegisterType <VisualStudioSolutionWriter>().As <IVisualStudioSolutionWriter>().SingleInstance());

            if (_initDirectoryCatalog)
            {
                var fileSystem = _initializationServices.GetFileSystem();

                var assemblies = _initializationServices.GetAssemblyResolver()
                                 .GetAssemblyPaths(fileSystem.GetWorkingDirectory(_scriptName))
                                 .Where(assembly => ShouldLoadAssembly(fileSystem, _initializationServices.GetAssemblyUtility(), assembly));

                var aggregateCatalog     = new AggregateCatalog();
                var assemblyLoadFailures = false;

                foreach (var assemblyPath in assemblies)
                {
                    try
                    {
                        var catalog = new AssemblyCatalog(assemblyPath);
                        // force the parts to be queried to catch any errors that would otherwise show up later
                        catalog.Parts.ToList();
                        aggregateCatalog.Catalogs.Add(catalog);
                    }
                    catch (ReflectionTypeLoadException typeLoadEx)
                    {
                        assemblyLoadFailures = true;
                        if (typeLoadEx.LoaderExceptions != null && typeLoadEx.LoaderExceptions.Any())
                        {
                            foreach (var ex in typeLoadEx.LoaderExceptions.GroupBy(x => x.Message))
                            {
                                _log.DebugFormat(
                                    "Failure loading assembly: {0}. Exception: {1}", assemblyPath, ex.First().Message);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        assemblyLoadFailures = true;
                        _log.DebugFormat("Failure loading assembly: {0}. Exception: {1}", assemblyPath, ex.Message);
                    }
                }
                if (assemblyLoadFailures)
                {
                    _log.Warn(string.IsNullOrEmpty(_scriptName)
                        ? "Some assemblies failed to load. Launch with '-repl -loglevel debug' to see the details"
                        : "Some assemblies failed to load. Launch with '-loglevel debug' to see the details");
                }
                builder.RegisterComposablePartCatalog(aggregateCatalog);
            }

            return(builder.Build());
        }
        protected override IContainer CreateContainer()
        {
            var builder = new ContainerBuilder();

            this.Logger.Debug("Registering runtime services");

            builder.RegisterInstance <ILog>(this.Logger).Exported(x => x.As <ILog>());
            builder.RegisterType(_scriptEngineType).As <IScriptEngine>().SingleInstance();
            builder.RegisterType(_scriptExecutorType).As <IScriptExecutor>().SingleInstance();
            builder.RegisterType <ScriptServices>().SingleInstance();

            RegisterLineProcessors(builder);

            RegisterOverrideOrDefault <IFileSystem>(builder, b => b.RegisterType <FileSystem>().As <IFileSystem>().SingleInstance());
            RegisterOverrideOrDefault <IAssemblyUtility>(builder, b => b.RegisterType <AssemblyUtility>().As <IAssemblyUtility>().SingleInstance());
            RegisterOverrideOrDefault <IPackageContainer>(builder, b => b.RegisterType <PackageContainer>().As <IPackageContainer>().SingleInstance());
            RegisterOverrideOrDefault <IPackageAssemblyResolver>(builder, b => b.RegisterType <PackageAssemblyResolver>().As <IPackageAssemblyResolver>().SingleInstance());
            RegisterOverrideOrDefault <IAssemblyResolver>(builder, b => b.RegisterType <AssemblyResolver>().As <IAssemblyResolver>().SingleInstance());
            RegisterOverrideOrDefault <IScriptHostFactory>(builder, b => b.RegisterType <ScriptHostFactory>().As <IScriptHostFactory>().SingleInstance());
            RegisterOverrideOrDefault <IFilePreProcessor>(builder, b => b.RegisterType <FilePreProcessor>().As <IFilePreProcessor>().SingleInstance());
            RegisterOverrideOrDefault <IScriptPackResolver>(builder, b => b.RegisterType <ScriptPackResolver>().As <IScriptPackResolver>().SingleInstance());
            RegisterOverrideOrDefault <IInstallationProvider>(builder, b => b.RegisterType <NugetInstallationProvider>().As <IInstallationProvider>().SingleInstance());
            RegisterOverrideOrDefault <IPackageInstaller>(builder, b => b.RegisterType <PackageInstaller>().As <IPackageInstaller>().SingleInstance());
            RegisterOverrideOrDefault <ScriptServices>(builder, b => b.RegisterType <ScriptServices>().SingleInstance());
            RegisterOverrideOrDefault <IObjectSerializer>(builder, b => b.RegisterType <ObjectSerializer>().As <IObjectSerializer>().SingleInstance());
            RegisterOverrideOrDefault <IConsole>(builder, b => b.RegisterInstance(_console));

            var assemblyResolver = _initializationServices.GetAssemblyResolver();

            if (_initDirectoryCatalog)
            {
                var fileSystem       = _initializationServices.GetFileSystem();
                var currentDirectory = fileSystem.GetWorkingDirectory(_scriptName);
                var assemblies       = assemblyResolver.GetAssemblyPaths(currentDirectory);

                var  aggregateCatalog     = new AggregateCatalog();
                bool assemblyLoadFailures = false;

                foreach (var assembly in assemblies)
                {
                    try
                    {
                        var catalog = new AssemblyCatalog(assembly);
                        catalog.Parts.ToList(); //force the Parts to be read
                        aggregateCatalog.Catalogs.Add(catalog);
                    }
                    catch (Exception ex)
                    {
                        assemblyLoadFailures = true;
                        Logger.DebugFormat("Failure loading assembly: {0}. Exception: {1}", assembly, ex.Message);
                    }
                }
                if (assemblyLoadFailures)
                {
                    if (_scriptName == null || _scriptName.Length == 0)
                    {
                        Logger.Warn("Some assemblies failed to load. Launch with '-repl -loglevel debug' to see the details");
                    }
                    else
                    {
                        Logger.Warn("Some assemblies failed to load. Launch with '-loglevel debug' to see the details");
                    }
                }
                builder.RegisterComposablePartCatalog(aggregateCatalog);
            }

            return(builder.Build());
        }