package jetbrains.mps.execution.configurations.implementation.plugin.plugin;
/*Generated by MPS */
import jetbrains.mps.execution.api.settings.IPersistentConfiguration;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.execution.api.settings.PersistentConfigurationContext;
import com.intellij.execution.configurations.RuntimeConfigurationException;
import com.intellij.execution.configurations.RuntimeConfigurationError;
import org.jdom.Element;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.util.xmlb.XmlSerializer;
import com.intellij.openapi.util.InvalidDataException;
import jetbrains.mps.util.MacrosFactory;
import java.io.File;
import jetbrains.mps.baseLanguage.tuples.runtime.Tuples;
import com.intellij.openapi.project.Project;
import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple;
import jetbrains.mps.util.FileUtil;
import org.jdom.Document;
import jetbrains.mps.util.JDOMUtil;
import org.jdom.JDOMException;
import java.io.IOException;
import jetbrains.mps.vfs.FileSystem;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import org.apache.log4j.Level;
public class MpsStartupSettings_Configuration implements IPersistentConfiguration {
private static final Logger LOG = LogManager.getLogger(MpsStartupSettings_Configuration.class);
@NotNull
private MpsStartupSettings_Configuration.MyState myState = new MpsStartupSettings_Configuration.MyState();
public void checkConfiguration(final PersistentConfigurationContext context) throws RuntimeConfigurationException {
if (isEmptyString(this.getConfigurationPath())) {
throw new RuntimeConfigurationError("Configuration path is empty.");
}
if (isEmptyString(this.getSystemPath())) {
throw new RuntimeConfigurationError("System path is empty.");
}
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
element.addContent(XmlSerializer.serialize(myState));
}
@Override
public void readExternal(Element element) throws InvalidDataException {
if (element == null) {
throw new InvalidDataException("Cant read " + this + ": element is null.");
}
XmlSerializer.deserializeInto(myState, (Element) element.getChildren().get(0));
}
public String getVmOptions() {
return myState.myVmOptions;
}
public String getJrePath() {
return myState.myJrePath;
}
public String getSystemPath() {
return myState.mySystemPath;
}
public String getConfigurationPath() {
return myState.myConfigurationPath;
}
public boolean getOpenCurrentProject() {
return myState.myOpenCurrentProject;
}
public String getProjectToOpen() {
return myState.myProjectToOpen;
}
public void setVmOptions(String value) {
myState.myVmOptions = value;
}
public void setJrePath(String value) {
myState.myJrePath = value;
}
public void setSystemPath(String value) {
myState.mySystemPath = value;
}
public void setConfigurationPath(String value) {
myState.myConfigurationPath = value;
}
public void setOpenCurrentProject(boolean value) {
myState.myOpenCurrentProject = value;
}
public void setProjectToOpen(String value) {
myState.myProjectToOpen = value;
}
public String expandPath(String path) {
if ((path == null || path.length() == 0)) {
return path;
}
return MacrosFactory.getGlobal().expandPath(path).replace(File.separator, "/");
}
public String shinkPath(String path) {
if ((path == null || path.length() == 0)) {
return path;
}
return MacrosFactory.getGlobal().shrinkPath(path).replace(File.separator, "/");
}
public Tuples._2<File, File> prepareFilesToOpenAndToDelete(Project project) {
File projectDir = getProjectDir(project);
if (!(this.getOpenCurrentProject())) {
return MultiTuple.<File,File>from(projectDir, (File) null);
}
// not my best code, not at all
File temporalDir = FileUtil.createTmpDir();
File mpsDir = new File(temporalDir, ".mps");
mpsDir.mkdir();
File tmpProjectFile = new File(mpsDir, "modules.xml");
FileUtil.copyDir(new File(projectDir, ".mps"), mpsDir);
// replace project macro
// todo: do that foreach file
try {
Document document = JDOMUtil.loadDocument(tmpProjectFile);
replacePathMacro(document.getRootElement(), project);
JDOMUtil.writeDocument(document, tmpProjectFile);
projectDir = temporalDir;
} catch (JDOMException e) {
// ignore and hope for the best
} catch (IOException e) {
// same as previous
}
return MultiTuple.<File,File>from(projectDir, temporalDir);
}
private File getProjectDir(Project currentProject) {
if (this.getOpenCurrentProject()) {
return new File(currentProject.getBasePath());
}
if (this.getProjectToOpen() != null) {
return new File(expandPath(this.getProjectToOpen()));
}
return null;
}
private void replacePathMacro(Element element, Project project) {
String path = "path";
String value = element.getAttributeValue(path);
if ((value != null && value.length() > 0)) {
// nooooooooo
element.setAttribute(path, MacrosFactory.forProjectFile(FileSystem.getInstance().getFileByPath(getProjectDir(project).getPath())).expandPath(value.replace("$PROJECT_DIR$", getProjectDir(project).getPath())));
}
for (Element child : ListSequence.fromList(element.getChildren())) {
replacePathMacro((Element) child, project);
}
}
@Override
public MpsStartupSettings_Configuration clone() {
MpsStartupSettings_Configuration clone = null;
try {
clone = createCloneTemplate();
clone.myState = (MpsStartupSettings_Configuration.MyState) myState.clone();
return clone;
} catch (CloneNotSupportedException ex) {
if (LOG.isEnabledFor(Level.ERROR)) {
LOG.error("", ex);
}
}
return clone;
}
public class MyState {
public String myVmOptions;
public String myJrePath;
public String mySystemPath = shinkPath(Mps_Command.getDefaultSystemPath());
public String myConfigurationPath = shinkPath(Mps_Command.getDefaultConfigurationPath());
public boolean myOpenCurrentProject = false;
public String myProjectToOpen;
public MyState() {
}
@Override
public Object clone() throws CloneNotSupportedException {
MpsStartupSettings_Configuration.MyState state = new MpsStartupSettings_Configuration.MyState();
state.myVmOptions = myVmOptions;
state.myJrePath = myJrePath;
state.mySystemPath = mySystemPath;
state.myConfigurationPath = myConfigurationPath;
state.myOpenCurrentProject = myOpenCurrentProject;
state.myProjectToOpen = myProjectToOpen;
return state;
}
}
public MpsStartupSettings_Configuration() {
}
public MpsStartupSettings_Configuration createCloneTemplate() {
return new MpsStartupSettings_Configuration();
}
public MpsStartupSettings_Configuration_Editor getEditor() {
return new MpsStartupSettings_Configuration_Editor();
}
private static boolean isEmptyString(String str) {
return str == null || str.length() == 0;
}
}