Inheritance: IExecuteReplCommand
Ejemplo n.º 1
0
            public void MigratesTheFileSystem(
                [Frozen] Mock<IFileSystem> fileSystem,
                [Frozen] Mock<IConsole> console,
                [Frozen] Mock<IFileSystemMigrator> fileSystemMigrator,
                IScriptLibraryComposer composer)
            {
                // arrange
                console.Setup(c => c.ReadLine()).Throws(new Exception());
                var sut = new ExecuteReplCommand(
                    null,
                    null,
                    fileSystem.Object,
                    new Mock<IScriptPackResolver>().Object,
                    new Mock<IRepl>().Object,
                    new TestLogProvider(),
                    console.Object,
                    new Mock<IAssemblyResolver>().Object,
                    fileSystemMigrator.Object,
                    composer
                    );

                // act
                sut.Execute();

                // assert
                fileSystemMigrator.Verify(m => m.Migrate(), Times.Once);
            }
Ejemplo n.º 2
0
        public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("args", args);

            var logger = _initializationServices.Logger;
            var packageAssemblyResolver = _initializationServices.GetPackageAssemblyResolver();

            if (args.Help)
            {
                return(new ShowUsageCommand(logger, true));
            }

            if (args.Global)
            {
                var currentDir = _fileSystem.GlobalFolder;
                if (!_fileSystem.DirectoryExists(currentDir))
                {
                    _fileSystem.CreateDirectory(currentDir);
                }

                _fileSystem.CurrentDirectory = currentDir;
            }

            _initializationServices.GetInstallationProvider().Initialize();

            if (args.Repl)
            {
                var scriptServices = _scriptServicesBuilder.Build();
                var replCommand    = new ExecuteReplCommand(
                    args.ScriptName,
                    scriptArgs,
                    scriptServices.FileSystem,
                    scriptServices.ScriptPackResolver,
                    scriptServices.Repl,
                    scriptServices.Logger,
                    scriptServices.Console,
                    scriptServices.AssemblyResolver);

                return(replCommand);
            }

            if (args.ScriptName != null)
            {
                var currentDirectory = _fileSystem.CurrentDirectory;
                var packageFile      = Path.Combine(currentDirectory, _fileSystem.PackagesFile);
                var packagesFolder   = Path.Combine(currentDirectory, _fileSystem.PackagesFolder);

                if (_fileSystem.FileExists(packageFile) && !_fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        null,
                        true,
                        _fileSystem,
                        packageAssemblyResolver,
                        _initializationServices.GetPackageInstaller(),
                        logger);

                    var executeCommand = new DeferredCreationCommand <IScriptCommand>(() =>
                                                                                      CreateScriptCommand(
                                                                                          args,
                                                                                          scriptArgs,
                                                                                          ScriptServicesBuilderFactory.Create(args, scriptArgs).Build()));

                    return(new CompositeCommand(installCommand, executeCommand));
                }

                return(CreateScriptCommand(args, scriptArgs, _scriptServicesBuilder.Build()));
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, logger);

                if (args.Global)
                {
                    var currentDirectory = _fileSystem.GlobalFolder;
                    _fileSystem.CurrentDirectory = currentDirectory;
                    if (!_fileSystem.DirectoryExists(currentDirectory))
                    {
                        _fileSystem.CreateDirectory(currentDirectory);
                    }
                }

                return(new CompositeCommand(saveCommand, new CleanCommand(args.ScriptName, _fileSystem, logger)));
            }

            if (args.Save)
            {
                return(new SaveCommand(packageAssemblyResolver, _fileSystem, logger));
            }

            if (args.Version)
            {
                return(new VersionCommand(_scriptServicesBuilder.ConsoleInstance));
            }

            if (args.Install != null)
            {
                var installCommand = new InstallCommand(
                    args.Install,
                    args.PackageVersion,
                    args.AllowPreRelease,
                    _fileSystem,
                    packageAssemblyResolver,
                    _initializationServices.GetPackageInstaller(),
                    logger);

                return(new CompositeCommand(installCommand, new SaveCommand(packageAssemblyResolver, _fileSystem, logger)));
            }

            return(new ShowUsageCommand(logger, false));
        }
Ejemplo n.º 3
0
        public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("args", args);

            if (args.Help)
            {
                return new ShowUsageCommand(_scriptServiceRoot.Logger, isValid: true);
            }

            if (args.Repl)
            {
                var replCommand = new ExecuteReplCommand(
                    _scriptServiceRoot.FileSystem, _scriptServiceRoot.ScriptPackResolver,
                    _scriptServiceRoot.Engine, _scriptServiceRoot.FilePreProcessor, _scriptServiceRoot.Logger, _scriptServiceRoot.Console,
                    _scriptServiceRoot.AssemblyName);
                return replCommand;
            }

            if (args.ScriptName != null)
            {
                var executeCommand = new ExecuteScriptCommand(
                    args.ScriptName,
                    scriptArgs,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.Executor,
                    _scriptServiceRoot.ScriptPackResolver,
                    _scriptServiceRoot.Logger,
                    _scriptServiceRoot.AssemblyName);

                var fileSystem = _scriptServiceRoot.FileSystem;
                var currentDirectory = fileSystem.CurrentDirectory;
                var packageFile = Path.Combine(currentDirectory, Constants.PackagesFile);
                var packagesFolder = Path.Combine(currentDirectory, Constants.PackagesFolder);

                if (fileSystem.FileExists(packageFile) && !fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        false,
                        fileSystem,
                        _scriptServiceRoot.PackageAssemblyResolver,
                        _scriptServiceRoot.PackageInstaller,
                        _scriptServiceRoot.Logger);

                    var restoreCommand = new RestoreCommand(
                        args.ScriptName,
                        _scriptServiceRoot.FileSystem,
                        _scriptServiceRoot.PackageAssemblyResolver,
                        _scriptServiceRoot.Logger);

                    return new CompositeCommand(installCommand, restoreCommand, executeCommand);
                }

                if (args.Restore)
                {
                    var restoreCommand = new RestoreCommand(
                        args.ScriptName,
                        _scriptServiceRoot.FileSystem,
                        _scriptServiceRoot.PackageAssemblyResolver,
                        _scriptServiceRoot.Logger);

                    return new CompositeCommand(restoreCommand, executeCommand);
                }

                return executeCommand;
            }

            if (args.Install != null)
            {
                var installCommand = new InstallCommand(
                    args.Install,
                    args.AllowPreRelease,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.PackageAssemblyResolver,
                    _scriptServiceRoot.PackageInstaller,
                    _scriptServiceRoot.Logger);

                var restoreCommand = new RestoreCommand(
                    args.Install,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.PackageAssemblyResolver,
                    _scriptServiceRoot.Logger);

                var currentDirectory = _scriptServiceRoot.FileSystem.CurrentDirectory;
                var packageFile = Path.Combine(currentDirectory, Constants.PackagesFile);

                if (!_scriptServiceRoot.FileSystem.FileExists(packageFile))
                {
                    var saveCommand = new SaveCommand(_scriptServiceRoot.PackageAssemblyResolver);
                    return new CompositeCommand(installCommand, restoreCommand, saveCommand);
                }

                return new CompositeCommand(installCommand, restoreCommand);
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(_scriptServiceRoot.PackageAssemblyResolver);

                var cleanCommand = new CleanCommand(
                    args.ScriptName,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.PackageAssemblyResolver,
                    _scriptServiceRoot.Logger);

                return new CompositeCommand(saveCommand, cleanCommand);
            }

            if (args.Save)
            {
                return new SaveCommand(_scriptServiceRoot.PackageAssemblyResolver);
            }

            if (args.Version)
            {
                return new VersionCommand();
            }

            return new ShowUsageCommand(_scriptServiceRoot.Logger, isValid: false);
        }
        public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("args", args);

            if (args.Help)
            {
                return(new ShowUsageCommand(_scriptServices.Logger, isValid: true));
            }

            if (args.Global)
            {
                var currentDir = _scriptServices.FileSystem.ModulesFolder;
                if (!_scriptServices.FileSystem.DirectoryExists(currentDir))
                {
                    _scriptServices.FileSystem.CreateDirectory(currentDir);
                }

                _scriptServices.FileSystem.CurrentDirectory = currentDir;
            }

            _scriptServices.InstallationProvider.Initialize();

            if (args.Repl)
            {
                var replCommand = new ExecuteReplCommand(
                    args.ScriptName,
                    scriptArgs,
                    _scriptServices.FileSystem,
                    _scriptServices.ScriptPackResolver,
                    _scriptServices.Engine,
                    _scriptServices.FilePreProcessor,
                    _scriptServices.ReplCommandService,
                    _scriptServices.Logger,
                    _scriptServices.Console,
                    _scriptServices.AssemblyResolver);

                return(replCommand);
            }

            if (args.ScriptName != null)
            {
                var executeCommand = new ExecuteScriptCommand(
                    args.ScriptName,
                    scriptArgs,
                    _scriptServices.FileSystem,
                    _scriptServices.Executor,
                    _scriptServices.ScriptPackResolver,
                    _scriptServices.Logger,
                    _scriptServices.AssemblyResolver);

                var fileSystem       = _scriptServices.FileSystem;
                var currentDirectory = fileSystem.CurrentDirectory;
                var packageFile      = Path.Combine(currentDirectory, Constants.PackagesFile);
                var packagesFolder   = Path.Combine(currentDirectory, Constants.PackagesFolder);

                if (fileSystem.FileExists(packageFile) && !fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        false,
                        fileSystem,
                        _scriptServices.PackageAssemblyResolver,
                        _scriptServices.PackageInstaller,
                        _scriptServices.Logger);

                    return(new CompositeCommand(installCommand, executeCommand));
                }

                return(executeCommand);
            }

            if (args.Install != null)
            {
                var installCommand = new InstallCommand(
                    args.Install,
                    args.AllowPreRelease,
                    _scriptServices.FileSystem,
                    _scriptServices.PackageAssemblyResolver,
                    _scriptServices.PackageInstaller,
                    _scriptServices.Logger);

                string currentDirectory = null;

                currentDirectory = _scriptServices.FileSystem.CurrentDirectory;

                var packageFile = Path.Combine(currentDirectory, Constants.PackagesFile);

                if (!_scriptServices.FileSystem.FileExists(packageFile))
                {
                    var saveCommand = new SaveCommand(_scriptServices.PackageAssemblyResolver);
                    return(new CompositeCommand(installCommand, saveCommand));
                }

                return(installCommand);
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(_scriptServices.PackageAssemblyResolver);

                if (args.Global)
                {
                    var currentDirectory = _scriptServices.FileSystem.ModulesFolder;
                    _scriptServices.FileSystem.CurrentDirectory = currentDirectory;
                    if (!_scriptServices.FileSystem.DirectoryExists(currentDirectory))
                    {
                        _scriptServices.FileSystem.CreateDirectory(currentDirectory);
                    }
                }

                var cleanCommand = new CleanCommand(
                    args.ScriptName,
                    _scriptServices.FileSystem,
                    _scriptServices.Logger);

                return(new CompositeCommand(saveCommand, cleanCommand));
            }

            if (args.Save)
            {
                return(new SaveCommand(_scriptServices.PackageAssemblyResolver));
            }

            if (args.Version)
            {
                return(new VersionCommand(_scriptServices.Console));
            }

            return(new ShowUsageCommand(_scriptServices.Logger, isValid: false));
        }
Ejemplo n.º 5
0
        public ICommand CreateCommand(Config config, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("config", config);

            var scriptServices = _scriptServicesBuilder.Build();

            if (config.Global)
            {
                var currentDir = _fileSystem.GlobalFolder;
                if (!_fileSystem.DirectoryExists(currentDir))
                {
                    _fileSystem.CreateDirectory(currentDir);
                }

                _fileSystem.CurrentDirectory = currentDir;
            }

            _initializationServices.GetInstallationProvider().Initialize();

            if (config.Repl)
            {
                var explicitReplCommand = new ExecuteReplCommand(
                    config.ScriptName,
                    scriptArgs,
                    scriptServices.FileSystem,
                    scriptServices.ScriptPackResolver,
                    scriptServices.Repl,
                    scriptServices.LogProvider,
                    scriptServices.Console,
                    scriptServices.AssemblyResolver,
                    scriptServices.ScriptLibraryComposer);

                return(explicitReplCommand);
            }

            if (config.Eval != null)
            {
                var executeLooseScriptCommand = new ExecuteLooseScriptCommand(
                    config.Eval,
                    scriptArgs,
                    scriptServices.FileSystem,
                    scriptServices.Executor,
                    scriptServices.ScriptPackResolver,
                    scriptServices.LogProvider,
                    scriptServices.AssemblyResolver,
                    scriptServices.ScriptLibraryComposer);

                return(executeLooseScriptCommand);
            }

            if (config.ScriptName != null)
            {
                var currentDirectory = _fileSystem.CurrentDirectory;
                var packageFile      = Path.Combine(currentDirectory, _fileSystem.PackagesFile);
                var packagesFolder   = Path.Combine(currentDirectory, _fileSystem.PackagesFolder);

                if (_fileSystem.FileExists(packageFile) && !_fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        null,
                        true,
                        _fileSystem,
                        _initializationServices.GetPackageAssemblyResolver(),
                        _initializationServices.GetPackageInstaller(),
                        scriptServices.ScriptLibraryComposer,
                        _initializationServices.LogProvider);

                    var executeCommand = new DeferredCreationCommand <IScriptCommand>(() =>
                                                                                      CreateScriptCommand(
                                                                                          config,
                                                                                          scriptArgs,
                                                                                          ScriptServicesBuilderFactory.Create(config, scriptArgs).Build()));

                    return(new CompositeCommand(installCommand, executeCommand));
                }

                return(CreateScriptCommand(config, scriptArgs, scriptServices));
            }

            if (config.Clean)
            {
                var saveCommand = new SaveCommand(
                    _initializationServices.GetPackageAssemblyResolver(),
                    _fileSystem,
                    _initializationServices.LogProvider);

                if (config.Global)
                {
                    var currentDirectory = _fileSystem.GlobalFolder;
                    _fileSystem.CurrentDirectory = currentDirectory;
                    if (!_fileSystem.DirectoryExists(currentDirectory))
                    {
                        _fileSystem.CreateDirectory(currentDirectory);
                    }
                }

                var cleanCommand = new CleanCommand(config.ScriptName, _fileSystem, _initializationServices.LogProvider);

                return(new CompositeCommand(saveCommand, cleanCommand));
            }

            if (config.Save)
            {
                return(new SaveCommand(
                           _initializationServices.GetPackageAssemblyResolver(),
                           _fileSystem,
                           _initializationServices.LogProvider));
            }

            if (config.PackageName != null)
            {
                var packageAssemblyResolver = _initializationServices.GetPackageAssemblyResolver();

                var installCommand = new InstallCommand(
                    config.PackageName,
                    config.PackageVersion,
                    config.AllowPreRelease,
                    _fileSystem,
                    packageAssemblyResolver,
                    _initializationServices.GetPackageInstaller(),
                    scriptServices.ScriptLibraryComposer,
                    _initializationServices.LogProvider);

                var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, _initializationServices.LogProvider);

                return(new CompositeCommand(installCommand, saveCommand));
            }

            // NOTE (adamralph): no script name or command so assume REPL
            var replCommand = new ExecuteReplCommand(
                config.ScriptName,
                scriptArgs,
                scriptServices.FileSystem,
                scriptServices.ScriptPackResolver,
                scriptServices.Repl,
                scriptServices.LogProvider,
                scriptServices.Console,
                scriptServices.AssemblyResolver,
                scriptServices.ScriptLibraryComposer);

            return(replCommand);
        }
Ejemplo n.º 6
0
        public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("args", args);

            if (args.Help)
            {
                return(new ShowUsageCommand(_initializationServices.Logger));
            }

            if (args.Version)
            {
                return(new VersionCommand(_scriptServicesBuilder.ConsoleInstance));
            }

            var scriptServices = _scriptServicesBuilder.Build();

            // HACK (Adam): This should not be the responsbility of the command factory
            // but now is not the time to fix this.
            // This should be addressed by a wider refactoring, i.e. https://github.com/scriptcs/scriptcs/issues/897
            scriptServices.FileSystemMigrator.Migrate();

            if (args.Global)
            {
                var currentDir = _fileSystem.GlobalFolder;
                if (!_fileSystem.DirectoryExists(currentDir))
                {
                    _fileSystem.CreateDirectory(currentDir);
                }

                _fileSystem.CurrentDirectory = currentDir;
            }

            _initializationServices.GetInstallationProvider().Initialize();

            if (args.Repl)
            {
                var explicitReplCommand = new ExecuteReplCommand(
                    args.ScriptName,
                    scriptArgs,
                    scriptServices.FileSystem,
                    scriptServices.ScriptPackResolver,
                    scriptServices.Repl,
                    scriptServices.Logger,
                    scriptServices.Console,
                    scriptServices.AssemblyResolver,
                    scriptServices.FileSystemMigrator,
                    scriptServices.ScriptLibraryComposer);

                return(explicitReplCommand);
            }

            if (args.ScriptName != null)
            {
                var currentDirectory = _fileSystem.CurrentDirectory;
                var packageFile      = Path.Combine(currentDirectory, _fileSystem.PackagesFile);
                var packagesFolder   = Path.Combine(currentDirectory, _fileSystem.PackagesFolder);

                if (_fileSystem.FileExists(packageFile) && !_fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        null,
                        true,
                        _fileSystem,
                        _initializationServices.GetPackageAssemblyResolver(),
                        _initializationServices.GetPackageInstaller(),
                        scriptServices.ScriptLibraryComposer,
                        _initializationServices.Logger);

                    var executeCommand = new DeferredCreationCommand <IScriptCommand>(() =>
                                                                                      CreateScriptCommand(
                                                                                          args,
                                                                                          scriptArgs,
                                                                                          ScriptServicesBuilderFactory.Create(args, scriptArgs).Build()));

                    return(new CompositeCommand(installCommand, executeCommand));
                }

                return(CreateScriptCommand(args, scriptArgs, scriptServices));
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(
                    _initializationServices.GetPackageAssemblyResolver(),
                    _fileSystem,
                    _initializationServices.Logger);

                if (args.Global)
                {
                    var currentDirectory = _fileSystem.GlobalFolder;
                    _fileSystem.CurrentDirectory = currentDirectory;
                    if (!_fileSystem.DirectoryExists(currentDirectory))
                    {
                        _fileSystem.CreateDirectory(currentDirectory);
                    }
                }

                var cleanCommand = new CleanCommand(args.ScriptName, _fileSystem, _initializationServices.Logger);

                return(new CompositeCommand(saveCommand, cleanCommand));
            }

            if (args.Save)
            {
                return(new SaveCommand(
                           _initializationServices.GetPackageAssemblyResolver(),
                           _fileSystem,
                           _initializationServices.Logger));
            }

            if (args.Install != null)
            {
                var packageAssemblyResolver = _initializationServices.GetPackageAssemblyResolver();

                var installCommand = new InstallCommand(
                    args.Install,
                    args.PackageVersion,
                    args.AllowPreRelease,
                    _fileSystem,
                    packageAssemblyResolver,
                    _initializationServices.GetPackageInstaller(),
                    scriptServices.ScriptLibraryComposer,
                    _initializationServices.Logger);

                var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, _initializationServices.Logger);

                return(new CompositeCommand(installCommand, saveCommand));
            }

            // NOTE (adamralph): no script name or command so assume REPL
            var replCommand = new ExecuteReplCommand(
                args.ScriptName,
                scriptArgs,
                scriptServices.FileSystem,
                scriptServices.ScriptPackResolver,
                scriptServices.Repl,
                scriptServices.Logger,
                scriptServices.Console,
                scriptServices.AssemblyResolver,
                scriptServices.FileSystemMigrator,
                scriptServices.ScriptLibraryComposer);

            return(replCommand);
        }
Ejemplo n.º 7
0
        public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("args", args);

            var logger = _initializationServices.Logger;
            var packageAssemblyResolver = _initializationServices.GetPackageAssemblyResolver();
            ScriptServices scriptServices = null;

            if (args.Help)
            {
                return new ShowUsageCommand(logger, isValid: true);
            }

            if (args.Global)
            {
                var currentDir = _fileSystem.ModulesFolder;
                if (!_fileSystem.DirectoryExists(currentDir))
                {
                    _fileSystem.CreateDirectory(currentDir);
                }

                _fileSystem.CurrentDirectory = currentDir;
            }

            _initializationServices.GetInstallationProvider().Initialize();

            if (args.Repl)
            {
                scriptServices = _scriptServicesBuilder.Build();
                var replCommand = new ExecuteReplCommand(
                    args.ScriptName,
                    scriptArgs,
                    scriptServices.FileSystem,
                    scriptServices.ScriptPackResolver,
                    scriptServices.Engine,
                    scriptServices.FilePreProcessor,
                    scriptServices.ObjectSerializer,
                    scriptServices.Logger,
                    scriptServices.Console,
                    scriptServices.AssemblyResolver,
                    scriptServices.ReplCommands);

                return replCommand;
            }

            if (args.ScriptName != null)
            {
                var currentDirectory = _fileSystem.CurrentDirectory;
                var packageFile = Path.Combine(currentDirectory, _fileSystem.PackagesFile);
                var packagesFolder = Path.Combine(currentDirectory, _fileSystem.PackagesFolder);

                if (_fileSystem.FileExists(packageFile) && !_fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        null,
                        true,
                        _fileSystem,
                        packageAssemblyResolver,
                        _initializationServices.GetPackageInstaller(),
                        logger);

                    var executeCommand = new DeferredCreationCommand<IScriptCommand>(() =>
                    {
                        scriptServices = ScriptServicesBuilderFactory.Create(args, scriptArgs).Build();
                        return CreateScriptCommand(args, scriptArgs, scriptServices);
                    });

                    return new CompositeCommand(installCommand, executeCommand);
                }

                return CreateScriptCommand(args, scriptArgs, _scriptServicesBuilder.Build());
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, logger);

                if (args.Global)
                {
                    var currentDirectory = _fileSystem.ModulesFolder;
                    _fileSystem.CurrentDirectory = currentDirectory;
                    if (!_fileSystem.DirectoryExists(currentDirectory))
                    {
                        _fileSystem.CreateDirectory(currentDirectory);
                    }
                }

                var cleanCommand = new CleanCommand(
                    args.ScriptName,
                    _fileSystem,
                    logger);

                return new CompositeCommand(saveCommand, cleanCommand);
            }

            if (args.Save)
            {
                return new SaveCommand(packageAssemblyResolver, _fileSystem, logger);
            }

            if (args.Version)
            {
                return new VersionCommand(_scriptServicesBuilder.ConsoleInstance);
            }

            if (args.Install != null)
            {
                var installCommand = new InstallCommand(
                    args.Install,
                    args.PackageVersion,
                    args.AllowPreRelease,
                    _fileSystem,
                    packageAssemblyResolver,
                    _initializationServices.GetPackageInstaller(),
                    logger);

                var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, logger);

                return new CompositeCommand(installCommand, saveCommand);
            }

            return new ShowUsageCommand(logger, isValid: false);
        }
Ejemplo n.º 8
0
        public ICommand CreateCommand(ScriptCsArgs args, string[] scriptArgs)
        {
            Guard.AgainstNullArgument("args", args);

            if (args.Help)
            {
                return new ShowUsageCommand(_initializationServices.Logger);
            }

            if (args.Version)
            {
                return new VersionCommand(_scriptServicesBuilder.ConsoleInstance);
            }

            var scriptServices = _scriptServicesBuilder.Build();

            // HACK (Adam): This should not be the responsbility of the command factory
            // but now is not the time to fix this.
            // This should be addressed by a wider refactoring, i.e. https://github.com/scriptcs/scriptcs/issues/897
            scriptServices.FileSystemMigrator.Migrate();

            if (args.Global)
            {
                var currentDir = _fileSystem.GlobalFolder;
                if (!_fileSystem.DirectoryExists(currentDir))
                {
                    _fileSystem.CreateDirectory(currentDir);
                }

                _fileSystem.CurrentDirectory = currentDir;
            }

            _initializationServices.GetInstallationProvider().Initialize();

            if (args.Repl)
            {
                var explicitReplCommand = new ExecuteReplCommand(
                    args.ScriptName,
                    scriptArgs,
                    scriptServices.FileSystem,
                    scriptServices.ScriptPackResolver,
                    scriptServices.Repl,
                    scriptServices.Logger,
                    scriptServices.Console,
                    scriptServices.AssemblyResolver,
                    scriptServices.FileSystemMigrator,
                    scriptServices.ScriptLibraryComposer);

                return explicitReplCommand;
            }

            if (args.ScriptName != null)
            {
                var currentDirectory = _fileSystem.CurrentDirectory;
                var packageFile = Path.Combine(currentDirectory, _fileSystem.PackagesFile);
                var packagesFolder = Path.Combine(currentDirectory, _fileSystem.PackagesFolder);

                if (_fileSystem.FileExists(packageFile) && !_fileSystem.DirectoryExists(packagesFolder))
                {
                    var installCommand = new InstallCommand(
                        null,
                        null,
                        true,
                        _fileSystem,
                        _initializationServices.GetPackageAssemblyResolver(),
                        _initializationServices.GetPackageInstaller(),
                        scriptServices.ScriptLibraryComposer,
                        _initializationServices.Logger);

                    var executeCommand = new DeferredCreationCommand<IScriptCommand>(() =>
                        CreateScriptCommand(
                            args,
                            scriptArgs,
                            ScriptServicesBuilderFactory.Create(args, scriptArgs).Build()));

                    return new CompositeCommand(installCommand, executeCommand);
                }

                return CreateScriptCommand(args, scriptArgs, scriptServices);
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(
                    _initializationServices.GetPackageAssemblyResolver(),
                    _fileSystem,
                    _initializationServices.Logger);

                if (args.Global)
                {
                    var currentDirectory = _fileSystem.GlobalFolder;
                    _fileSystem.CurrentDirectory = currentDirectory;
                    if (!_fileSystem.DirectoryExists(currentDirectory))
                    {
                        _fileSystem.CreateDirectory(currentDirectory);
                    }
                }

                var cleanCommand = new CleanCommand(args.ScriptName, _fileSystem, _initializationServices.Logger);

                return new CompositeCommand(saveCommand, cleanCommand);
            }

            if (args.Save)
            {
                return new SaveCommand(
                    _initializationServices.GetPackageAssemblyResolver(),
                    _fileSystem,
                    _initializationServices.Logger);
            }

            if (args.Install != null)
            {
                var packageAssemblyResolver = _initializationServices.GetPackageAssemblyResolver();

                var installCommand = new InstallCommand(
                    args.Install,
                    args.PackageVersion,
                    args.AllowPreRelease,
                    _fileSystem,
                    packageAssemblyResolver,
                    _initializationServices.GetPackageInstaller(),
                    scriptServices.ScriptLibraryComposer,
                    _initializationServices.Logger);

                var saveCommand = new SaveCommand(packageAssemblyResolver, _fileSystem, _initializationServices.Logger);

                return new CompositeCommand(installCommand, saveCommand);
            }

            // NOTE (adamralph): no script name or command so assume REPL
            var replCommand = new ExecuteReplCommand(
                args.ScriptName,
                scriptArgs,
                scriptServices.FileSystem,
                scriptServices.ScriptPackResolver,
                scriptServices.Repl,
                scriptServices.Logger,
                scriptServices.Console,
                scriptServices.AssemblyResolver,
                scriptServices.FileSystemMigrator,
                scriptServices.ScriptLibraryComposer);

            return replCommand;
        }
Ejemplo n.º 9
0
        public ICommand CreateCommand(ScriptCsArgs args)
        {
            if (args.Help)
            {
                return(new ShowUsageCommand(_scriptServiceRoot.Logger, isValid: true));
            }

            if (args.Repl)
            {
                var replCommand = new ExecuteReplCommand(
                    _scriptServiceRoot.FileSystem, _scriptServiceRoot.ScriptPackResolver,
                    _scriptServiceRoot.Engine, _scriptServiceRoot.FilePreProcessor, _scriptServiceRoot.Logger, _scriptServiceRoot.Console);
                return(replCommand);
            }

            if (args.ScriptName != null)
            {
                var executeCommand = new ExecuteScriptCommand(
                    args.ScriptName,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.Executor,
                    _scriptServiceRoot.ScriptPackResolver,
                    _scriptServiceRoot.Logger);

                if (args.Restore)
                {
                    var restoreCommand = new RestoreCommand(
                        args.ScriptName,
                        _scriptServiceRoot.FileSystem,
                        _scriptServiceRoot.PackageAssemblyResolver,
                        _scriptServiceRoot.Logger);

                    return(new CompositeCommand(restoreCommand, executeCommand));
                }

                return(executeCommand);
            }

            if (args.Install != null)
            {
                var installCommand = new InstallCommand(
                    args.Install,
                    args.AllowPreRelease,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.PackageAssemblyResolver,
                    _scriptServiceRoot.PackageInstaller,
                    _scriptServiceRoot.Logger);

                var restoreCommand = new RestoreCommand(
                    args.Install,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.PackageAssemblyResolver,
                    _scriptServiceRoot.Logger);

                var currentDirectory = _scriptServiceRoot.FileSystem.CurrentDirectory;
                var packageFile      = Path.Combine(currentDirectory, Constants.PackagesFile);

                if (!_scriptServiceRoot.FileSystem.FileExists(packageFile))
                {
                    var saveCommand = new SaveCommand(_scriptServiceRoot.PackageAssemblyResolver);
                    return(new CompositeCommand(installCommand, restoreCommand, saveCommand));
                }

                return(new CompositeCommand(installCommand, restoreCommand));
            }

            if (args.Clean)
            {
                var saveCommand = new SaveCommand(_scriptServiceRoot.PackageAssemblyResolver);

                var cleanCommand = new CleanCommand(
                    args.ScriptName,
                    _scriptServiceRoot.FileSystem,
                    _scriptServiceRoot.PackageAssemblyResolver,
                    _scriptServiceRoot.Logger);

                return(new CompositeCommand(saveCommand, cleanCommand));
            }

            if (args.Save)
            {
                return(new SaveCommand(_scriptServiceRoot.PackageAssemblyResolver));
            }

            if (args.Version)
            {
                return(new VersionCommand());
            }

            return(new ShowUsageCommand(_scriptServiceRoot.Logger, isValid: false));
        }