Exemplo n.º 1
0
        public CommandFactory(IScriptServicesBuilder scriptServicesBuilder)
        {
            if (scriptServicesBuilder == null)
            {
                throw new ArgumentNullException(nameof(scriptServicesBuilder));
            }

            _scriptServicesBuilder  = scriptServicesBuilder;
            _initializationServices = _scriptServicesBuilder.InitializationServices;
            _fileSystem             = _initializationServices.GetFileSystem();

            if (_fileSystem.PackagesFile == null)
            {
                throw new ArgumentException(
                          "The file system provided by the initialization services provided by the script services builder has a null packages file.",
                          "scriptServicesBuilder");
            }

            if (_fileSystem.PackagesFolder == null)
            {
                throw new ArgumentException(
                          "The file system provided by the initialization services provided by the script services builder has a null package folder.",
                          "scriptServicesBuilder");
            }
        }
Exemplo n.º 2
0
        private Repl GetRepl(string[] args, out MemoryBufferConsole memoryBufferConsole)
        {
            SetProfile();
            var arguments             = ParseArguments(args);
            var scriptServicesBuilder = ScriptServicesBuilderFactory.Create(arguments.CommandArguments, arguments.ScriptArguments);
            IInitializationServices _initializationServices = scriptServicesBuilder.InitializationServices;
            IFileSystem             _fileSystem             = _initializationServices.GetFileSystem();

            if (_fileSystem.PackagesFile == null)
            {
                throw new ArgumentException("The file system provided by the initialization services provided by the script services builder has a null packages file.");
            }

            if (_fileSystem.PackagesFolder == null)
            {
                throw new ArgumentException("The file system provided by the initialization services provided by the script services builder has a null package folder.");
            }

            ScriptServices scriptServices = scriptServicesBuilder.Build();

            memoryBufferConsole = new MemoryBufferConsole();
            Repl repl = new Repl(arguments.ScriptArguments, _fileSystem, scriptServices.Engine,
                                 scriptServices.ObjectSerializer, scriptServices.Logger, memoryBufferConsole,
                                 scriptServices.FilePreProcessor, scriptServices.ReplCommands);

            var workingDirectory = _fileSystem.CurrentDirectory;
            var assemblies       = scriptServices.AssemblyResolver.GetAssemblyPaths(workingDirectory);
            var scriptPacks      = scriptServices.ScriptPackResolver.GetPacks();

            repl.Initialize(assemblies, scriptPacks, null);

            return(repl);
        }
Exemplo n.º 3
0
        public IScriptServicesBuilder LoadModules(string extension, params string[] moduleNames)
        {
            var config = new ModuleConfiguration(_debug, _scriptName, _repl, _logLevel, Overrides);
            var loader = _initializationServices.GetModuleLoader();

            loader.Load(config, _initializationServices.GetFileSystem().ModulesFolder, extension, moduleNames);
            return(this);
        }
        public IScriptServicesBuilder LoadModules(string extension, params string[] moduleNames)
        {
            var config = new ModuleConfiguration(_cache, _scriptName, _repl, _logLevel, Overrides);
            var loader = _initializationServices.GetModuleLoader();

            var fs      = _initializationServices.GetFileSystem();
            var folders = _debug ? new[] { fs.ModulesFolder, fs.CurrentDirectory } : new[] { fs.ModulesFolder };

            loader.Load(config, folders, extension, moduleNames);
            return(this);
        }
Exemplo n.º 5
0
        public CommandFactory(IScriptServicesBuilder scriptServicesBuilder)
        {
            Guard.AgainstNullArgument("scriptServicesBuilder", scriptServicesBuilder);

            _scriptServicesBuilder = scriptServicesBuilder;
            _initializationServices = _scriptServicesBuilder.InitializationServices;
            _fileSystem = _initializationServices.GetFileSystem();

            if (_fileSystem.PackagesFile == null)
            {
                throw new ArgumentException("The file system provided by the initialization services provided by the script services builder has a null packages file.");
            }

            if (_fileSystem.PackagesFolder == null)
            {
                throw new ArgumentException("The file system provided by the initialization services provided by the script services builder has a null package folder.");
            }
        }
Exemplo n.º 6
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());
        }