Marcin Gardias avatar Marcin Gardias committed 87be135

support for powershell scripts

Comments (0)

Files changed (4)

src/main/java/com/atlassian/utils/process/DefaultExternalProcessFactory.java

         if (settings.hasIdleTimeout()) {
             process.setIdleTimeout(settings.getIdleTimeout());
         }
+        process.setRunWithPowershell(settings.isRunWithPowershell());
     }
 }

src/main/java/com/atlassian/utils/process/ExternalProcessBuilder.java

         
         return this;
     }
+
+    /**
+     * Use powershell instead of cmd.exe to run windows processes. Has no effect if {@link #suppressSpecialWindowsBehaviour()} is used
+     */
+    public ExternalProcessBuilder runWithPowershell()
+    {
+        settings.setRunWithPowershell(true);
+        return this;
+    }
 }

src/main/java/com/atlassian/utils/process/ExternalProcessImpl.java

     private File workingDir;
     private boolean useWindowsEncodingWorkaround;
 
+    private boolean runWithPowershell;
+
     /**
      * Process an external command.
      *
         this.useWindowsEncodingWorkaround = useWindowsEncodingWorkaround;
     }
 
+    public void setRunWithPowershell(final boolean runWithPowershell)
+    {
+        this.runWithPowershell = runWithPowershell;
+    }
+
     private boolean areOutputPumpsRunning() {
         return (outputPump != null && outputPump.isRunning()) || (errorPump != null && errorPump.isRunning());
     }
     private Process createWinProcess(List<String> command, Map<String, String> environment, File workingDir)
             throws IOException {
         List<String> newCommand = new ArrayList<String>(command.size() + 3);
-        newCommand.add("cmd");
-        newCommand.add("/A"); // use ANSI encoding
-        newCommand.add("/C");
+
+        if (runWithPowershell)
+        {
+            newCommand.add("powershell");
+            newCommand.add("-Command");
+        }
+        else
+        {
+            newCommand.add("cmd");
+            newCommand.add("/A"); // use ANSI encoding
+            newCommand.add("/C");
+        }
 
         if (useWindowsEncodingWorkaround) {
             newCommand.add(command.get(0));
                 protected void doTask() {
                     try {
                         handler.provideInput(process.getOutputStream());
+                        if (runWithPowershell)
+                        {
+                            process.getOutputStream().close();
+                        }
                     } catch (Throwable t) {
                         // The StdInHandler IO pump is intentionally interrupted when the process terminates, the output and the
                         // error pumps have finished and the StdInHandler does not finish by itself. In this case we do not
                 }
             };
         }
+        else if (runWithPowershell)
+        {
+            try
+            {
+                process.getOutputStream().close();
+            }
+            catch (IOException ioe)
+            {
+                //hmm....
+            }
+        }
 
         errorPump = new LatchedRunnable("StdErrHandler " + process) {
 

src/main/java/com/atlassian/utils/process/ExternalProcessSettings.java

     private boolean useWindowsEncodingWorkaround;
     private File workingDirectory;
 
+    private boolean runWithPowershell;
+
     public ExternalProcessSettings() {
         environment = new HashMap<String, String>();
         monitors = new ArrayList<ProcessMonitor>();
             throw new IllegalStateException("A ProcessHandler is required");
         }
     }
+
+    public boolean isRunWithPowershell()
+    {
+        return runWithPowershell;
+    }
+
+    public void setRunWithPowershell(final boolean runWithPowershell)
+    {
+        this.runWithPowershell = runWithPowershell;
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.