/plugins/devkit/src/run/PluginRunConfiguration.java

https://bitbucket.org/nbargnesi/idea
Java | 299 lines | 242 code | 41 blank | 16 comment | 59 complexity | 46c4a9916eeb0c9e99f320fb0603dc76 MD5 | raw file
  1. /*
  2. * Copyright 2000-2009 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 org.jetbrains.idea.devkit.run;
  17. import com.intellij.execution.CantRunException;
  18. import com.intellij.execution.ExecutionException;
  19. import com.intellij.execution.Executor;
  20. import com.intellij.execution.configurations.*;
  21. import com.intellij.execution.filters.TextConsoleBuilderFactory;
  22. import com.intellij.execution.runners.ExecutionEnvironment;
  23. import com.intellij.execution.runners.ProgramRunner;
  24. import com.intellij.openapi.application.ApplicationManager;
  25. import com.intellij.openapi.module.Module;
  26. import com.intellij.openapi.module.ModuleManager;
  27. import com.intellij.openapi.options.SettingsEditor;
  28. import com.intellij.openapi.project.Project;
  29. import com.intellij.openapi.projectRoots.JavaSdkType;
  30. import com.intellij.openapi.projectRoots.Sdk;
  31. import com.intellij.openapi.projectRoots.SdkModificator;
  32. import com.intellij.openapi.roots.ModuleRootManager;
  33. import com.intellij.openapi.util.*;
  34. import com.intellij.openapi.util.text.StringUtil;
  35. import org.jdom.Element;
  36. import org.jetbrains.annotations.NonNls;
  37. import org.jetbrains.annotations.NotNull;
  38. import org.jetbrains.annotations.Nullable;
  39. import org.jetbrains.idea.devkit.DevKitBundle;
  40. import org.jetbrains.idea.devkit.projectRoots.IdeaJdk;
  41. import org.jetbrains.idea.devkit.projectRoots.Sandbox;
  42. import java.io.File;
  43. import java.io.IOException;
  44. public class PluginRunConfiguration extends RunConfigurationBase implements ModuleRunConfiguration {
  45. private Module myModule;
  46. private String myModuleName;
  47. public String VM_PARAMETERS;
  48. public String PROGRAM_PARAMETERS;
  49. @NonNls private static final String NAME = "name";
  50. @NonNls private static final String MODULE = "module";
  51. @NonNls private static final String ALTERNATIVE_PATH_ELEMENT = "alternative-path";
  52. @NonNls private static final String PATH = "path";
  53. @NonNls private static final String ALTERNATIVE_PATH_ENABLED_ATTR = "alternative-path-enabled";
  54. private String ALTERNATIVE_JRE_PATH = null;
  55. private boolean ALTERNATIVE_JRE_PATH_ENABLED = false;
  56. public PluginRunConfiguration(final Project project, final ConfigurationFactory factory, final String name) {
  57. super(project, factory, name);
  58. }
  59. public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() {
  60. return new PluginRunConfigurationEditor(this);
  61. }
  62. public JDOMExternalizable createRunnerSettings(ConfigurationInfoProvider provider) {
  63. return null;
  64. }
  65. public SettingsEditor<JDOMExternalizable> getRunnerSettingsEditor(ProgramRunner runner) {
  66. return null;
  67. }
  68. public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException {
  69. if (getModule() == null){
  70. throw new ExecutionException(DevKitBundle.message("run.configuration.no.module.specified"));
  71. }
  72. final ModuleRootManager rootManager = ModuleRootManager.getInstance(getModule());
  73. final Sdk jdk = rootManager.getSdk();
  74. if (jdk == null) {
  75. throw CantRunException.noJdkForModule(getModule());
  76. }
  77. final Sdk ideaJdk = IdeaJdk.findIdeaJdk(jdk);
  78. if (ideaJdk == null) {
  79. throw new ExecutionException(DevKitBundle.message("jdk.type.incorrect.common"));
  80. }
  81. String sandboxHome = ((Sandbox)ideaJdk.getSdkAdditionalData()).getSandboxHome();
  82. if (sandboxHome == null){
  83. throw new ExecutionException(DevKitBundle.message("sandbox.no.configured"));
  84. }
  85. try {
  86. sandboxHome = new File(sandboxHome).getCanonicalPath();
  87. }
  88. catch (IOException e) {
  89. throw new ExecutionException(DevKitBundle.message("sandbox.no.configured"));
  90. }
  91. final String canonicalSandbox = sandboxHome;
  92. //copy license from running instance of idea
  93. IdeaLicenseHelper.copyIDEALicense(sandboxHome, ideaJdk);
  94. final JavaCommandLineState state = new JavaCommandLineState(env) {
  95. protected JavaParameters createJavaParameters() throws ExecutionException {
  96. final JavaParameters params = new JavaParameters();
  97. ParametersList vm = params.getVMParametersList();
  98. fillParameterList(vm, VM_PARAMETERS);
  99. fillParameterList(params.getProgramParametersList(), PROGRAM_PARAMETERS);
  100. Sdk usedIdeaJdk = ideaJdk;
  101. if (isAlternativeJreEnabled() && !StringUtil.isEmptyOrSpaces(getAlternativeJrePath())) {
  102. try {
  103. usedIdeaJdk = (Sdk)usedIdeaJdk.clone();
  104. }
  105. catch (CloneNotSupportedException e) {
  106. throw new ExecutionException(e.getMessage());
  107. }
  108. final SdkModificator sdkToSetUp = usedIdeaJdk.getSdkModificator();
  109. sdkToSetUp.setHomePath(getAlternativeJrePath());
  110. sdkToSetUp.commitChanges();
  111. }
  112. @NonNls String libPath = usedIdeaJdk.getHomePath() + File.separator + "lib";
  113. vm.add("-Xbootclasspath/a:" + libPath + File.separator + "boot.jar");
  114. vm.defineProperty("idea.config.path", canonicalSandbox + File.separator + "config");
  115. vm.defineProperty("idea.system.path", canonicalSandbox + File.separator + "system");
  116. vm.defineProperty("idea.plugins.path", canonicalSandbox + File.separator + "plugins");
  117. if (SystemInfo.isMac) {
  118. vm.defineProperty("idea.smooth.progress", "false");
  119. vm.defineProperty("apple.laf.useScreenMenuBar", "true");
  120. }
  121. if (SystemInfo.isLinux) {
  122. if (VM_PARAMETERS == null || !VM_PARAMETERS.contains("-Dsun.awt.disablegrab")) {
  123. vm.defineProperty("sun.awt.disablegrab", "true"); // See http://devnet.jetbrains.net/docs/DOC-1142
  124. }
  125. }
  126. String buildNumber = IdeaJdk.getBuildNumber(usedIdeaJdk.getHomePath());
  127. if (buildNumber != null) {
  128. if (buildNumber.startsWith("IC")) {
  129. vm.defineProperty("idea.platform.prefix", "Idea");
  130. }
  131. else if (buildNumber.startsWith("PY")) {
  132. vm.defineProperty("idea.platform.prefix", "Python");
  133. }
  134. else if (buildNumber.startsWith("RM")) {
  135. vm.defineProperty("idea.platform.prefix", "Ruby");
  136. }
  137. else if (buildNumber.startsWith("PS")) {
  138. vm.defineProperty("idea.platform.prefix", "PhpStorm");
  139. }
  140. else if (buildNumber.startsWith("WS")) {
  141. vm.defineProperty("idea.platform.prefix", "WebStorm");
  142. }
  143. else if (buildNumber.startsWith("OC")) {
  144. vm.defineProperty("idea.platform.prefix", "CIDR");
  145. }
  146. }
  147. params.setWorkingDirectory(usedIdeaJdk.getHomePath() + File.separator + "bin" + File.separator);
  148. params.setJdk(usedIdeaJdk);
  149. params.getClassPath().addFirst(libPath + File.separator + "log4j.jar");
  150. params.getClassPath().addFirst(libPath + File.separator + "jdom.jar");
  151. params.getClassPath().addFirst(libPath + File.separator + "trove4j.jar");
  152. params.getClassPath().addFirst(libPath + File.separator + "openapi.jar");
  153. params.getClassPath().addFirst(libPath + File.separator + "util.jar");
  154. params.getClassPath().addFirst(libPath + File.separator + "extensions.jar");
  155. params.getClassPath().addFirst(libPath + File.separator + "bootstrap.jar");
  156. params.getClassPath().addFirst(libPath + File.separator + "idea.jar");
  157. params.getClassPath().addFirst(libPath + File.separator + "idea_rt.jar");
  158. params.getClassPath().addFirst(((JavaSdkType)usedIdeaJdk.getSdkType()).getToolsPath(usedIdeaJdk));
  159. params.setMainClass("com.intellij.idea.Main");
  160. return params;
  161. }
  162. };
  163. state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject()));
  164. return state;
  165. }
  166. public String getAlternativeJrePath() {
  167. return ALTERNATIVE_JRE_PATH;
  168. }
  169. public void setAlternativeJrePath(String ALTERNATIVE_JRE_PATH) {
  170. this.ALTERNATIVE_JRE_PATH = ALTERNATIVE_JRE_PATH;
  171. }
  172. public boolean isAlternativeJreEnabled() {
  173. return ALTERNATIVE_JRE_PATH_ENABLED;
  174. }
  175. public void setAlternativeJreEnabled(boolean ALTERNATIVE_JRE_PATH_ENABLED) {
  176. this.ALTERNATIVE_JRE_PATH_ENABLED = ALTERNATIVE_JRE_PATH_ENABLED;
  177. }
  178. private static void fillParameterList(ParametersList list, @Nullable String value) {
  179. if (value == null) return;
  180. for (String parameter : value.split(" ")) {
  181. if (parameter != null && parameter.length() > 0) {
  182. list.add(parameter);
  183. }
  184. }
  185. }
  186. public void checkConfiguration() throws RuntimeConfigurationException {
  187. if (getModule() == null) {
  188. throw new RuntimeConfigurationException(DevKitBundle.message("run.configuration.no.module.specified"));
  189. }
  190. String moduleName = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
  191. public String compute() {
  192. return getModule().getName();
  193. }
  194. });
  195. if (ModuleManager.getInstance(getProject()).findModuleByName(moduleName) == null){
  196. throw new RuntimeConfigurationException(DevKitBundle.message("run.configuration.no.module.specified"));
  197. }
  198. final ModuleRootManager rootManager = ModuleRootManager.getInstance(getModule());
  199. final Sdk jdk = rootManager.getSdk();
  200. if (jdk == null) {
  201. throw new RuntimeConfigurationException(DevKitBundle.message("jdk.no.specified", moduleName));
  202. }
  203. if (IdeaJdk.findIdeaJdk(jdk) == null) {
  204. throw new RuntimeConfigurationException(DevKitBundle.message("jdk.type.incorrect", moduleName));
  205. }
  206. }
  207. @NotNull
  208. public Module[] getModules() {
  209. final Module module = getModule();
  210. return module != null ? new Module[]{module} : Module.EMPTY_ARRAY;
  211. }
  212. public void readExternal(Element element) throws InvalidDataException {
  213. Element module = element.getChild(MODULE);
  214. if (module != null) {
  215. myModuleName = module.getAttributeValue(NAME);
  216. }
  217. DefaultJDOMExternalizer.readExternal(this, element);
  218. final Element altElement = element.getChild(ALTERNATIVE_PATH_ELEMENT);
  219. if (altElement != null) {
  220. ALTERNATIVE_JRE_PATH = altElement.getAttributeValue(PATH);
  221. final String enabledAttr = altElement.getAttributeValue(ALTERNATIVE_PATH_ENABLED_ATTR);
  222. ALTERNATIVE_JRE_PATH_ENABLED = enabledAttr != null && Boolean.parseBoolean(enabledAttr);
  223. }
  224. super.readExternal(element);
  225. }
  226. public void writeExternal(Element element) throws WriteExternalException {
  227. Element moduleElement = new Element(MODULE);
  228. moduleElement.setAttribute(NAME, ApplicationManager.getApplication().runReadAction(new Computable<String>() {
  229. public String compute() {
  230. final Module module = getModule();
  231. return module != null ? module.getName()
  232. : myModuleName != null ? myModuleName : "";
  233. }
  234. }));
  235. element.addContent(moduleElement);
  236. DefaultJDOMExternalizer.writeExternal(this, element);
  237. if (!StringUtil.isEmptyOrSpaces(ALTERNATIVE_JRE_PATH)) {
  238. Element altElement = new Element(ALTERNATIVE_PATH_ELEMENT);
  239. altElement.setAttribute(PATH, ALTERNATIVE_JRE_PATH);
  240. altElement.setAttribute(ALTERNATIVE_PATH_ENABLED_ATTR, String.valueOf(ALTERNATIVE_JRE_PATH_ENABLED));
  241. element.addContent(altElement);
  242. }
  243. super.writeExternal(element);
  244. }
  245. @Nullable
  246. public Module getModule() {
  247. if (myModule == null && myModuleName != null){
  248. myModule = ModuleManager.getInstance(getProject()).findModuleByName(myModuleName);
  249. }
  250. if (myModule != null && myModule.isDisposed()) {
  251. myModule = null;
  252. }
  253. return myModule;
  254. }
  255. public void setModule(Module module) {
  256. myModule = module;
  257. }
  258. }