package jetbrains.mps.vcs.platform.mergedriver;
/*Generated by MPS */
import java.io.File;
import com.intellij.openapi.project.Project;
import jetbrains.mps.vcs.platform.util.PluginUtil;
import org.jetbrains.idea.svn.SvnConfiguration;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.ui.Messages;
import java.util.List;
import jetbrains.mps.util.StringsIO;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.vcs.core.mergedriver.MergeDriverMain;
import java.io.IOException;
/*package*/ class SvnInstaller extends AbstractInstaller {
private File myConfigFile;
private File myConfigDir;
private File myScriptFile;
private boolean myUseIdeConfig;
public SvnInstaller(Project project, boolean useIdeConfig) {
super(project);
myConfigDir = new File(System.getProperty("user.home") + File.separator + ".subversion");
myUseIdeConfig = useIdeConfig;
if (PluginUtil.isSvnPluginEnabled()) {
if (useIdeConfig) {
myConfigDir = new File(SvnConfiguration.getInstance(project).getConfigurationDirectory());
} else {
myConfigDir = SVNWCUtil.getDefaultConfigurationDirectory();
}
}
myConfigFile = new File(myConfigDir, "config");
myScriptFile = new File(myConfigDir, "mps-merger." + ((SystemInfo.isWindows ? "bat" : "sh")));
}
@NotNull
@Override
protected AbstractInstaller.State install(boolean dryRun) {
if (!(PluginUtil.isSvnPluginEnabled())) {
return AbstractInstaller.State.NOT_ENABLED;
}
if (!(dryRun)) {
// copy driver files to the proper place
MergeDriverPacker.getInstance().pack(myProject);
}
if (!(myConfigFile.exists())) {
if (!(dryRun)) {
Messages.showErrorDialog(myProject, String.format("Could not find Subversion configuration file (%s).", myConfigFile.getAbsolutePath()), "Subversion Config Not Found");
}
return AbstractInstaller.State.NOT_INSTALLED;
}
if (!(myConfigFile.canWrite()) && myConfigDir.canWrite()) {
if (!(dryRun)) {
Messages.showErrorDialog(myProject, String.format("Can't write to Subversion config (%s).", myConfigFile.getAbsolutePath()), "Can't Write");
}
return AbstractInstaller.State.NOT_INSTALLED;
}
String configLine = String.format("diff3-cmd = %s", myScriptFile.getAbsolutePath().replace("\\", "\\\\"));
List<String> lines = StringsIO.readLines(myConfigFile);
int lineToReplace = -1;
for (int i = 0; i < ListSequence.fromList(lines).count(); i++) {
String line = ListSequence.fromList(lines).getElement(i);
if (line.trim().startsWith("diff3-cmd")) {
// Some diff3 is already present
Matcher matcher = Pattern.compile("^\\s*diff3-cmd\\s*=\\s*(.+)$").matcher(line);
if (matcher.matches()) {
String cmd = matcher.group(1);
if (cmd.contains("mps-merger.")) {
// already installed
if (dryRun && neq_k2wvr2_a0a2a2a3a1a01a5(line, configLine)) {
return AbstractInstaller.State.OUTDATED;
}
lineToReplace = i;
break;
} else {
// another is installed
if (!(dryRun)) {
if (Messages.showYesNoDialog(myProject, String.format("You already have custom diff3-cmd in your Subversion config (%s).\nIt will be overriden by MPS custom diff3. Continue?", cmd), "diff3-cmd is already present", Messages.getQuestionIcon()) != 0) {
return AbstractInstaller.State.NOT_INSTALLED;
}
}
lineToReplace = i;
break;
}
}
}
}
if (lineToReplace == -1) {
String commented = ListSequence.fromList(lines).findFirst(new IWhereFilter<String>() {
public boolean accept(String line) {
return line.trim().startsWith("# diff3-cmd");
}
});
if (commented != null) {
lineToReplace = ListSequence.fromList(lines).indexOf(commented);
} else {
int helpersStart = ListSequence.fromList(lines).indexOf(ListSequence.fromList(lines).findFirst(new IWhereFilter<String>() {
public boolean accept(String line) {
return line.trim().equals("[helpers]");
}
}));
if (helpersStart != -1) {
// [helpers] section is present, finding next section start
int nextStart = ListSequence.fromList(lines).indexOf(ListSequence.fromList(lines).skip(helpersStart + 1).findFirst(new IWhereFilter<String>() {
public boolean accept(String line) {
return line.trim().startsWith("[");
}
}));
if (nextStart == -1) {
// [helpers] is the last section
ListSequence.fromList(lines).addElement("");
lineToReplace = ListSequence.fromList(lines).count() - 1;
} else {
Iterable<String> section = ListSequence.fromList(lines).page(helpersStart + 1, nextStart);
// Finding last non-comment line
int nonComment = Sequence.fromIterable(section).indexOf(Sequence.fromIterable(section).findLast(new IWhereFilter<String>() {
public boolean accept(String line) {
return !(line.trim().startsWith("#")) && !(line.trim().isEmpty());
}
}));
if (nonComment == -1) {
lineToReplace = helpersStart + 1;
} else {
lineToReplace = nonComment + helpersStart + 1;
}
ListSequence.fromList(lines).insertElement(lineToReplace, "");
}
}
}
}
AbstractInstaller.State createScriptResult = ScriptGenerator.generateScript(myProject, MergeDriverMain.NO_FILETYPE, ScriptGenerator.SVN, myScriptFile, dryRun);
if (createScriptResult != AbstractInstaller.State.INSTALLED) {
return createScriptResult;
}
if (dryRun) {
if (lineToReplace != -1 && eq_k2wvr2_a0a0a71a5(ListSequence.fromList(lines).getElement(lineToReplace), configLine)) {
return AbstractInstaller.State.INSTALLED;
} else {
return AbstractInstaller.State.NOT_INSTALLED;
}
}
if (lineToReplace == -1) {
ListSequence.fromList(lines).addElement("[helpers]");
ListSequence.fromList(lines).addElement(configLine);
} else {
ListSequence.fromList(lines).setElement(lineToReplace, configLine);
}
try {
StringsIO.writeLines(myConfigFile, lines);
Messages.showInfoMessage(myProject, "Successfully installed MPS merger for Subversion", "Subversion Merger Installed");
return AbstractInstaller.State.INSTALLED;
} catch (IOException e) {
Messages.showErrorDialog(myProject, String.format("Could not update Subversion configuration file (%s). %s", myConfigFile.getAbsolutePath(), e.getMessage()), "Could Not Save Config");
return AbstractInstaller.State.NOT_INSTALLED;
}
}
@Override
public String getActionTitle() {
return String.format("Subversion custom diff3 cmd (%s, %s)", (myUseIdeConfig ? "MPS config" : "common"), myConfigFile.getAbsolutePath());
}
@Override
public String getAffectedVcsName() {
return "svn";
}
public boolean sameAs(SvnInstaller other) {
return eq_k2wvr2_a0a0i(other.myConfigDir.getAbsolutePath(), myConfigDir.getAbsolutePath());
}
private static boolean neq_k2wvr2_a0a2a2a3a1a01a5(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
private static boolean eq_k2wvr2_a0a0a71a5(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
private static boolean eq_k2wvr2_a0a0i(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
}