2 * Copyright 2000-2010 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.execution.util;
18 import com.intellij.execution.CommonProgramRunConfigurationParameters;
19 import com.intellij.execution.configurations.ModuleBasedConfiguration;
20 import com.intellij.execution.configurations.SimpleProgramParameters;
21 import com.intellij.openapi.components.PathMacroManager;
22 import com.intellij.openapi.module.Module;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.util.io.FileUtil;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import com.intellij.util.PathUtil;
27 import org.jetbrains.annotations.Nullable;
29 import java.util.HashMap;
32 public class ProgramParametersUtil {
33 public static void configureConfiguration(SimpleProgramParameters parameters, CommonProgramRunConfigurationParameters configuration) {
34 Project project = configuration.getProject();
35 Module module = getModule(configuration);
37 parameters.getProgramParametersList().addParametersString(configuration.getProgramParameters());
39 String workingDirectory = configuration.getWorkingDirectory();
40 VirtualFile baseDir = project.getBaseDir();
42 if (workingDirectory == null || workingDirectory.trim().length() == 0) {
43 workingDirectory = PathUtil.getLocalPath(baseDir);
45 workingDirectory = expandPath(workingDirectory, module, project);
46 if (!FileUtil.isAbsolute(workingDirectory) && baseDir != null) {
47 workingDirectory = baseDir.getPath() + "/" + workingDirectory;
49 parameters.setWorkingDirectory(workingDirectory);
51 parameters.setupEnvs(configuration.getEnvs(), configuration.isPassParentEnvs());
52 if (parameters.getEnv() != null) {
53 Map<String, String> expanded = new HashMap<String, String>();
54 for (Map.Entry<String, String> each : parameters.getEnv().entrySet()) {
55 expanded.put(each.getKey(), expandPath(each.getValue(), module, project));
57 parameters.setEnv(expanded);
61 protected static String expandPath(String path, Module module, Project project) {
62 path = PathMacroManager.getInstance(project).expandPath(path);
64 path = PathMacroManager.getInstance(module).expandPath(path);
70 protected static Module getModule(CommonProgramRunConfigurationParameters configuration) {
71 if (configuration instanceof ModuleBasedConfiguration) {
72 return ((ModuleBasedConfiguration)configuration).getConfigurationModule().getModule();