-- get rid of inheritance from AbstractConsoleRunner with history. It adds no almost...
[idea/community.git] / python / src / com / jetbrains / python / run / PythonScriptCommandLineState.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.jetbrains.python.run;
17
18 import com.google.common.collect.Lists;
19 import com.intellij.execution.DefaultExecutionResult;
20 import com.intellij.execution.ExecutionException;
21 import com.intellij.execution.ExecutionResult;
22 import com.intellij.execution.Executor;
23 import com.intellij.execution.configurations.GeneralCommandLine;
24 import com.intellij.execution.configurations.ParametersList;
25 import com.intellij.execution.configurations.ParamsGroup;
26 import com.intellij.execution.console.ConsoleExecuteAction;
27 import com.intellij.execution.executors.DefaultDebugExecutor;
28 import com.intellij.execution.runners.ExecutionEnvironment;
29 import com.intellij.openapi.actionSystem.AnAction;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.projectRoots.Sdk;
32 import com.intellij.openapi.util.text.StringUtil;
33 import com.intellij.util.ArrayUtil;
34 import com.jetbrains.python.PythonHelper;
35 import com.jetbrains.python.console.PyConsoleOptions;
36 import com.jetbrains.python.console.PyConsoleType;
37 import com.jetbrains.python.console.PydevConsoleRunner;
38 import com.jetbrains.python.console.PydevConsoleRunnerImpl;
39 import com.jetbrains.python.sdk.PythonEnvUtil;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42
43 import java.util.List;
44 import java.util.Map;
45
46 import static com.intellij.execution.runners.AbstractConsoleRunnerWithHistory.registerActionShortcuts;
47
48 /**
49  * @author yole
50  */
51 public class PythonScriptCommandLineState extends PythonCommandLineState {
52   private final PythonRunConfiguration myConfig;
53
54   public PythonScriptCommandLineState(PythonRunConfiguration runConfiguration, ExecutionEnvironment env) {
55     super(runConfiguration, env);
56     myConfig = runConfiguration;
57   }
58
59   @Override
60   public ExecutionResult execute(Executor executor, final CommandLinePatcher... patchers) throws ExecutionException {
61     if (myConfig.showCommandLineAfterwards()) {
62       if (executor.getId() == DefaultDebugExecutor.EXECUTOR_ID) {
63         return super.execute(executor, ArrayUtil.append(patchers, new CommandLinePatcher() {
64           @Override
65           public void patchCommandLine(GeneralCommandLine commandLine) {
66             commandLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_DEBUGGER).addParameterAt(1, "--cmd-line");
67           }
68         }));
69       }
70
71       PydevConsoleRunner runner =
72         new PythonScriptWithConsoleRunner(myConfig.getProject(), myConfig.getSdk(), PyConsoleType.PYTHON, myConfig.getWorkingDirectory(),
73                                           myConfig.getEnvs(), patchers,
74                                           PyConsoleOptions.getInstance(myConfig.getProject()).getPythonConsoleSettings());
75
76       runner.runSync();
77       // runner.getProcessHandler() would be null if execution error occurred
78       if (runner.getProcessHandler() == null) {
79         return null;
80       }
81       List<AnAction> actions = Lists.newArrayList(createActions(runner.getConsoleView(), runner.getProcessHandler()));
82
83       return new DefaultExecutionResult(runner.getConsoleView(), runner.getProcessHandler(), actions.toArray(new AnAction[actions.size()]));
84     }
85     else {
86       return super.execute(executor, patchers);
87     }
88   }
89
90   @Override
91   protected void buildCommandLineParameters(GeneralCommandLine commandLine) {
92     ParametersList parametersList = commandLine.getParametersList();
93     ParamsGroup exe_options = parametersList.getParamsGroup(GROUP_EXE_OPTIONS);
94     assert exe_options != null;
95     exe_options.addParametersString(myConfig.getInterpreterOptions());
96
97     ParamsGroup script_parameters = parametersList.getParamsGroup(GROUP_SCRIPT);
98     assert script_parameters != null;
99     if (!StringUtil.isEmptyOrSpaces(myConfig.getScriptName())) {
100       script_parameters.addParameter(myConfig.getScriptName());
101     }
102
103     final String script_options_string = myConfig.getScriptParameters();
104     if (script_options_string != null) script_parameters.addParametersString(script_options_string);
105
106     if (!StringUtil.isEmptyOrSpaces(myConfig.getWorkingDirectory())) {
107       commandLine.setWorkDirectory(myConfig.getWorkingDirectory());
108     }
109   }
110
111   /**
112    * @author traff
113    */
114   public class PythonScriptWithConsoleRunner extends PydevConsoleRunnerImpl {
115
116     private CommandLinePatcher[] myPatchers;
117
118     public PythonScriptWithConsoleRunner(@NotNull Project project,
119                                          @NotNull Sdk sdk,
120                                          @NotNull PyConsoleType consoleType,
121                                          @Nullable String workingDir,
122                                          Map<String, String> environmentVariables,
123                                          CommandLinePatcher[] patchers,
124                                          PyConsoleOptions.PyConsoleSettings consoleSettings,
125                                          String... statementsToExecute) {
126       super(project, sdk, consoleType, workingDir, environmentVariables, consoleSettings, statementsToExecute);
127       myPatchers = patchers;
128     }
129
130     @Override
131     protected void createContentDescriptorAndActions() {
132       AnAction a = new ConsoleExecuteAction(super.getConsoleView(), myConsoleExecuteActionHandler,
133                                             myConsoleExecuteActionHandler.getEmptyExecuteAction(), myConsoleExecuteActionHandler);
134       registerActionShortcuts(Lists.newArrayList(a), getConsoleView().getConsoleEditor().getComponent());
135     }
136
137     @Override
138     protected GeneralCommandLine createCommandLine(@NotNull Sdk sdk,
139                                                    @NotNull Map<String, String> environmentVariables,
140                                                    String workingDir, int[] ports) {
141       GeneralCommandLine consoleCmdLine = doCreateConsoleCmdLine(sdk, environmentVariables, workingDir, ports, PythonHelper.RUN_IN_CONSOLE);
142
143       final GeneralCommandLine cmd = generateCommandLine(myPatchers);
144
145       ParamsGroup group = consoleCmdLine.getParametersList().getParamsGroup(PythonCommandLineState.GROUP_SCRIPT);
146       assert group != null;
147       group.addParameters(cmd.getParametersList().getList());
148
149       PythonEnvUtil.mergePythonPath(consoleCmdLine.getEnvironment(), cmd.getEnvironment());
150
151       consoleCmdLine.getEnvironment().putAll(cmd.getEnvironment());
152
153       return consoleCmdLine;
154     }
155   }
156 }