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); }
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)); }
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); }