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); }
public void Execute() { if (this.CommandArgs == null) { throw new InvalidOperationException("The command args are missing."); } var services = ScriptServicesBuilderFactory.Create(this.CommandArgs, this.ScriptArgs).Build(); var command = new ExecuteScriptCommand( this.CommandArgs.ScriptName, this.ScriptArgs, services.FileSystem, services.Executor, services.ScriptPackResolver, services.Logger, services.AssemblyResolver); this.Result = command.Execute(); }
public void Execute() { if (this.Config == null) { throw new InvalidOperationException("The config is missing."); } var services = ScriptServicesBuilderFactory.Create(this.Config, this.ScriptArgs).Build(); var command = new ExecuteScriptCommand( this.Config.ScriptName, this.ScriptArgs, services.FileSystem, services.Executor, services.ScriptPackResolver, services.LogProvider, services.AssemblyResolver, services.FileSystemMigrator, services.ScriptLibraryComposer); this.Result = command.Execute(); }
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(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); 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); }
public void Launch(string applicationPath, dynamic dynVault, dynamic dynListItems) { if (!Debugger.IsAttached) { Debugger.Launch(); } // TODO: heard this is bad, but don't have another way to do it currently // Need to set the app path because it is used later on to get the bin //AppDomain.CurrentDomain.SetData( "APPBASE", Path.Combine( applicationPath, "bin" ) ); // Sets AppDomain.CurrentDomain.BaseDirectory AppDomain.CurrentDomain.SetData("APPBASE", applicationPath); AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; Console.WriteLine("Launching"); this._console = new BufferedConsole(); try { var config = Config.Create(ScriptCsArgs.Parse(new string[] { })); var scriptArgs = new string[] { }; config.Console = this._console; var scriptServicesBuilder = (ScriptServicesBuilder)ScriptServicesBuilderFactory.Create(config, scriptArgs); var clientApp = new MFilesClientApplication(); var clientVault = clientApp.BindToVault((string)dynVault.Name, IntPtr.Zero, true, true); var props = clientVault.PropertyDefOperations.GetPropertyDefs(); var propsDictionary = new Dictionary <string, MFIdentifier>(); foreach (PropertyDef propertyDef in props) { // TODO: what if multiple have same name? propsDictionary.Add(propertyDef.Name, new MFIdentifier(propertyDef.GUID)); } var globs = new ScriptHostArguments { Vault = clientVault, // TODO: convert this list using clientVault to something useful // TODO: move the script dashboard to non popup (that way we can pass the current listing)? what if there are multiple? (home page) ListingItems = dynListItems, Properties = propsDictionary }; var myFactory = new MFilesScriptHostFactory(globs, this._console); scriptServicesBuilder.SetOverride <IScriptHostFactory, MFilesScriptHostFactory>(myFactory); scriptServicesBuilder.SetOverride <IRepl, MFilesRepl>(); var scriptServices = scriptServicesBuilder.Build(); var runtimeServices = (RuntimeServices)scriptServicesBuilder.RuntimeServices; runtimeServices.Container.Resolve <Printers>().AddCustomPrinter <Vault>(vault => vault.Name); runtimeServices.Container.Resolve <Printers>().AddCustomPrinter <IVault>(vault => vault.Name); runtimeServices.Container.Resolve <Printers>().AddCustomPrinter <VaultClass>(vault => vault.Name); runtimeServices.Container.Resolve <Printers>().AddCustomPrinter <MFSearchBuilder>(search => $"Search for {search.Vault.Name} ({search.Conditions.Count})"); this._command = new HtmlExecuteReplCommand(config.ScriptName, scriptArgs, scriptServices); this._command.Repl.Initialize(); this._command.Execute(); } catch (Exception e) { Console.WriteLine("Failed to set up script services"); Console.WriteLine(e); } }