/*
 * Copyright 2000-2007 JetBrains s.r.o.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.plugins.groovy.runner;

import com.intellij.execution.CantRunException;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.*;
import com.intellij.execution.filters.TextConsoleBuilderFactory;
import com.intellij.execution.runners.RunnerInfo;
import com.intellij.facet.FacetManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizer;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.io.FileUtil;
import org.jdom.Element;
import org.jetbrains.plugins.groovy.config.GroovyFacet;
import org.jetbrains.plugins.groovy.config.GroovyGrailsConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

class GroovyScriptRunConfiguration extends ModuleBasedConfiguration {
  private GroovyScriptConfigurationFactory factory;
  public String vmParams;
  public String scriptParams;
  public String scriptPath;
  public String workDir = ".";

  public GroovyScriptRunConfiguration(GroovyScriptConfigurationFactory factory, Project project, String name) {
    super(name, new RunConfigurationModule(project, true), factory);
    this.factory = factory;
  }

  public Collection<Module> getValidModules() {
    Module[] modules = ModuleManager.getInstance(getProject()).getModules();
    ArrayList<Module> res = new ArrayList<Module>();
    for (Module module : modules)
      if (FacetManager.getInstance(module).getFacetsByType(GroovyFacet.ID).size() > 0)
        res.add(module);
    return res;
  }

  public void setAbsoluteWorkDir(String dir) {
    workDir = FileUtil.getRelativePath(new File(getProject().getBaseDir().getPath()), new File(dir));
  }

  public String getAbsoluteWorkDir() {
    String path = getProject().getBaseDir().getPath();
    try {
      return new File(path, workDir).getCanonicalFile().getAbsolutePath();
    } catch (IOException e) {
      return path;
    }
  }

  public void readExternal(Element element) throws InvalidDataException {
    super.readExternal(element);
    readModule(element);
    scriptPath = JDOMExternalizer.readString(element, "path");
    vmParams = JDOMExternalizer.readString(element, "vmparams");
    scriptParams = JDOMExternalizer.readString(element, "params");
    workDir = JDOMExternalizer.readString(element, "workDir");
  }

  public void writeExternal(Element element) throws WriteExternalException {
    super.writeExternal(element);
    writeModule(element);
    JDOMExternalizer.write(element, "path", scriptPath);
    JDOMExternalizer.write(element, "vmparams", vmParams);
    JDOMExternalizer.write(element, "params", scriptParams);
    JDOMExternalizer.write(element, "workDir", workDir);
  }

  protected ModuleBasedConfiguration createInstance() {
    return new GroovyScriptRunConfiguration(factory, getConfigurationModule().getProject(), getName());
  }

  public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() {
    return new GroovyRunConfigurationEditor(getProject());
  }

  public RunProfileState getState(
          DataContext context,
          RunnerInfo runnerInfo,
          RunnerSettings runnerSettings,
          ConfigurationPerRunnerSettings configurationSettings) throws ExecutionException {
    GroovyGrailsConfiguration groovyConfig = GroovyGrailsConfiguration.getInstance();
    if (!groovyConfig.isGroovyConfigured(null)) {
      throw new ExecutionException("Groovy is not configured");
    }

    final Module module = getModule();
    if (module == null) {
      throw new ExecutionException("Module is not specified");
    }

    final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    final ProjectJdk jdk = rootManager.getJdk();
    if (jdk == null) {
      throw CantRunException.noJdkForModule(getModule());
    }

    final JavaCommandLineState state = new JavaCommandLineState(runnerSettings, configurationSettings) {
      protected JavaParameters createJavaParameters() throws ExecutionException {
        JavaParameters params = new JavaParameters();

        params.configureByModule(module, JavaParameters.JDK_AND_CLASSES);

        params.setWorkingDirectory(getAbsoluteWorkDir());
        params.getVMParametersList().addParametersString(vmParams);
        params.getProgramParametersList().add(scriptPath);
        params.getProgramParametersList().addParametersString(scriptParams);
        params.setMainClass("groovy.lang.GroovyShell");

        return params;
      }
    };

    state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject()));
    state.setModulesToCompile(getModules());
    return state;
  }

  public Module getModule() {
    return getConfigurationModule().getModule();
  }
}

@@ -2619,94 +2619,11 @@
 r = 
-FileUtil.getRelativePath(new File(getProject().getBaseDir().getPath()), new File(
 dir
-))
 ;%0A  
@@ -2668,75 +2668,8 @@
 ) %7B%0A
-    String path = getProject().getBaseDir().getPath();%0A    try %7B%0A  
     
@@ -2688,14 +2688,8 @@
 ile(
-path, 
 work
@@ -2700,97 +2700,23 @@
 .get
-CanonicalFile().getAbsolutePath();%0A    %7D catch (IOException e) %7B%0A      return path;%0A    %7D
+AbsolutePath();
 %0A  %7D
@@ -3107,16 +3107,218 @@
 kDir%22);%0A
+    if (!new File(workDir).isAbsolute()) %7B //was stored as relative path, so try to make it absolute here%0A      workDir = new File(getProject().getBaseDir().getPath(), workDir).getAbsolutePath();%0A    %7D%0A
   %7D%0A%0A  p

fix working directory issues