From 0546b02e1ca0d22972a3f607cbc9155530517576 Mon Sep 17 00:00:00 2001 From: Peter Honeder Date: Tue, 7 Jul 2015 18:02:04 -0700 Subject: [PATCH] added --file parameter to run a script --- scripts/Makefile | 5 ++++ scripts/runps.sh | 2 +- src/powershell-run/main.cs | 49 +++++++++++++------------------------- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/scripts/Makefile b/scripts/Makefile index f81bee4f2..e1c29196e 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -159,6 +159,11 @@ run-interactive: $(POWERSHELL_RUN_TARGETS) dotnetlibs/corerun dotnetlibs/Microso if ! diff dotnetlibs/corerun $(MONAD_EXT)/coreclr/Release/corerun; then cp -r $(MONAD_EXT)/coreclr/Release/* dotnetlibs; fi cd exec_env/app_base && ./runps.sh +run-file: $(POWERSHELL_RUN_TARGETS) dotnetlibs/corerun dotnetlibs/Microsoft.PowerShell.Commands.Management.dll dotnetlibs/Microsoft.PowerShell.Commands.Utility.dll dotnetlibs/api-ms-win-core-registry-l1-1-0.dll internal-prepare-exec_env + # check if corerun is the right one (could be the debug version) + if ! diff dotnetlibs/corerun $(MONAD_EXT)/coreclr/Release/corerun; then cp -r $(MONAD_EXT)/coreclr/Release/* dotnetlibs; fi + cd exec_env/app_base && ./runps.sh --file $(PSSCRIPT) + test: dotnetlibs/ps_test_runner.exe dotnetlibs/corerun dotnetlibs/api-ms-win-core-registry-l1-1-0.dll cd dotnetlibs && LD_LIBRARY_PATH=. ./corerun ps_test_runner.exe diff --git a/scripts/runps.sh b/scripts/runps.sh index 6845d1795..b781fe46d 100755 --- a/scripts/runps.sh +++ b/scripts/runps.sh @@ -4,5 +4,5 @@ SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) cd $SCRIPTDIR echo "launching PS now" -PSMODULEPATH=$SCRIPTDIR/Modules LD_LIBRARY_PATH=. ./corerun powershell-run.exe +PSMODULEPATH=$SCRIPTDIR/Modules LD_LIBRARY_PATH=. ./corerun powershell-run.exe "$@" diff --git a/src/powershell-run/main.cs b/src/powershell-run/main.cs index 9b16bdbe9..b305bda50 100644 --- a/src/powershell-run/main.cs +++ b/src/powershell-run/main.cs @@ -7,6 +7,7 @@ namespace Microsoft.Samples.PowerShell.Host using System.Management.Automation.Host; using System.Management.Automation.Runspaces; using System.Text; + using System.IO; using PowerShell = System.Management.Automation.PowerShell; /// @@ -95,17 +96,25 @@ namespace Microsoft.Samples.PowerShell.Host Console.WriteLine("- Type 'exit' to exit"); Console.WriteLine("- Utility and Management cmdlet modules are loadable"); Console.WriteLine(); - //Console.ForegroundColor = oldFg; + + String initialScript = null; + if (args.Length > 0 && args[0] == "--file") + { + initialScript = File.ReadAllText(args[1]); + } // Create the listener and run it. This method never returns. - PSListenerConsoleSample listener = new PSListenerConsoleSample(); - listener.Run(); + PSListenerConsoleSample listener = new PSListenerConsoleSample(initialScript); + + // only run if there was no script file passed in + if (initialScript == null) + listener.Run(); } /// /// Initializes a new instance of the PSListenerConsoleSample class. /// - public PSListenerConsoleSample() + public PSListenerConsoleSample(string initialScript) { // Create the host and runspace instances for this interpreter. // Note that this application does not support console files so @@ -115,35 +124,9 @@ namespace Microsoft.Samples.PowerShell.Host this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost,iss); this.myRunSpace.Open(); - // Create a PowerShell object to run the commands used to create - // $profile and load the profiles. - lock (this.instanceLock) - { - this.currentPowerShell = PowerShell.Create(); - } - - try - { - this.currentPowerShell.Runspace = this.myRunSpace; - -/* PSCommand[] profileCommands = Microsoft.Samples.PowerShell.Host.HostUtilities.GetProfileCommands("SampleHost06"); - foreach (PSCommand command in profileCommands) - { - this.currentPowerShell.Commands = command; - this.currentPowerShell.Invoke(); - }*/ - } - finally - { - // Dispose the PowerShell object and set currentPowerShell - // to null. It is locked because currentPowerShell may be - // accessed by the ctrl-C handler. - lock (this.instanceLock) - { - this.currentPowerShell.Dispose(); - this.currentPowerShell = null; - } - } + // run the initial script + if (initialScript != null) + executeHelper(initialScript,null); } ///