HgPushAction refactoring
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Wed, 13 Apr 2011 07:51:30 +0000 (11:51 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Wed, 13 Apr 2011 07:53:06 +0000 (11:53 +0400)
Transform to HgAction, get rid of HgBuilderCommands.
HgPusher to show dialog and perform push, to be able to call it without action.

plugins/hg4idea/src/org/zmlx/hg4idea/HgPusher.java [new file with mode: 0644]
plugins/hg4idea/src/org/zmlx/hg4idea/HgUtil.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgAbstractGlobalAction.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgAction.java [new file with mode: 0644]
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgCommandResultNotifier.java
plugins/hg4idea/src/org/zmlx/hg4idea/action/HgPushAction.java

diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/HgPusher.java b/plugins/hg4idea/src/org/zmlx/hg4idea/HgPusher.java
new file mode 100644 (file)
index 0000000..6d99323
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2000-2011 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.zmlx.hg4idea;
+
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.openapi.vcs.ProjectLevelVcsManager;
+import org.jetbrains.annotations.Nullable;
+import org.zmlx.hg4idea.action.HgCommandResultNotifier;
+import org.zmlx.hg4idea.command.HgPushCommand;
+import org.zmlx.hg4idea.execution.HgCommandResult;
+import org.zmlx.hg4idea.execution.HgCommandResultHandler;
+import org.zmlx.hg4idea.ui.HgPushDialog;
+
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @author Kirill Likhodedov
+ */
+public class HgPusher {
+
+  private static final Logger LOG = Logger.getInstance(HgPusher.class);
+  private static Pattern PUSH_COMMITS_PATTERN = Pattern.compile(".*added (\\d+) changesets.*");
+
+  private final Project myProject;
+  private final ProjectLevelVcsManager myVcsManager;
+
+  public HgPusher(Project project) {
+    myProject = project;
+    myVcsManager = ProjectLevelVcsManager.getInstance(project);
+  }
+
+  public void showDialogAndPush() {
+    HgPushDialog dialog = new HgPushDialog(myProject);
+    dialog.setRoots(HgUtil.getHgRepositories(myProject));
+    dialog.show();
+    if (dialog.isOK()) {
+      push(myProject, dialog);
+    }
+  }
+
+  private static void push(final Project project, HgPushDialog dialog) {
+    final HgPushCommand command = new HgPushCommand(project, dialog.getRepository(), dialog.getTarget());
+    command.setRevision(dialog.getRevision());
+    command.setForce(dialog.isForce());
+    command.setBranch(dialog.getBranch());
+    command.execute(new HgCommandResultHandler() {
+      @Override
+      public void process(@Nullable HgCommandResult result) {
+        int commitsNum = getNumberOfPushedCommits(result);
+        String title = null;
+        String description = null;
+        if (commitsNum >= 0) {
+          title = "Pushed successfully";
+          description = "Pushed " + commitsNum + " " + StringUtil.pluralize("commit", commitsNum) + ".";
+        }
+        new HgCommandResultNotifier(project).process(result, title, description);
+      }
+    });
+  }
+
+  private static int getNumberOfPushedCommits(HgCommandResult result) {
+    if (!HgErrorUtil.isAbort(result)) {
+      final List<String> outputLines = result.getOutputLines();
+      for (String outputLine : outputLines) {
+        final Matcher matcher = PUSH_COMMITS_PATTERN.matcher(outputLine.trim());
+        if (matcher.matches()) {
+          try {
+            return Integer.parseInt(matcher.group(1));
+          }
+          catch (NumberFormatException e) {
+            LOG.info("getNumberOfPushedCommits ", e);
+            return -1;
+          }
+        }
+      }
+    }
+    return -1;
+  }
+
+}
index 610c5281f4c3e80809c88f0beab6478fb3b5cb44..9a8c4b6fc9892d9d24096a4e5fdaf741038f50e2 100644 (file)
@@ -20,9 +20,7 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.ShutDownTracker;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vcs.FilePath;
-import com.intellij.openapi.vcs.FileStatus;
-import com.intellij.openapi.vcs.VcsException;
+import com.intellij.openapi.vcs.*;
 import com.intellij.openapi.vcs.changes.Change;
 import com.intellij.openapi.vcs.changes.ChangeListManager;
 import com.intellij.openapi.vcs.changes.ContentRevision;
@@ -323,4 +321,18 @@ public abstract class HgUtil {
       return filePath;
     }
   }
+
+  /**
+   * Returns all HG roots in the project.
+   */
+  public static @NotNull List<VirtualFile> getHgRepositories(@NotNull Project project) {
+    final List<VirtualFile> repos = new LinkedList<VirtualFile>();
+    for (VcsRoot root : ProjectLevelVcsManager.getInstance(project).getAllVcsRoots()) {
+      if (HgVcs.VCS_NAME.equals(root.vcs.getName())) {
+        repos.add(root.path);
+      }
+    }
+    return repos;
+  }
+
 }
index 5a08c2c6122edcd69d3907fec8c8a4ad513c840b..e752629af8471e49f95d03e3ca4196d16e634c8a 100644 (file)
@@ -15,19 +15,14 @@ package org.zmlx.hg4idea.action;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.vcs.ProjectLevelVcsManager;
-import com.intellij.openapi.vcs.VcsRoot;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.vcsUtil.VcsUtil;
 import org.jetbrains.annotations.Nullable;
 import org.zmlx.hg4idea.HgUtil;
-import org.zmlx.hg4idea.HgVcs;
 import org.zmlx.hg4idea.execution.HgCommandException;
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
 
 abstract class HgAbstractGlobalAction extends AnAction {
 
@@ -41,7 +36,7 @@ abstract class HgAbstractGlobalAction extends AnAction {
       return;
     }
 
-    HgGlobalCommand command = getHgGlobalCommandBuilder(project).build(findRepos(project));
+    HgGlobalCommand command = getHgGlobalCommandBuilder(project).build(HgUtil.getHgRepositories(project));
     if (command == null) {
       return;
     }
@@ -67,19 +62,7 @@ abstract class HgAbstractGlobalAction extends AnAction {
     Project project = PlatformDataKeys.PROJECT.getData(dataContext);
     if (project == null) {
       presentation.setEnabled(false);
-      return;
-    }
-  }
-
-  private List<VirtualFile> findRepos(Project project) {
-    List<VirtualFile> repos = new LinkedList<VirtualFile>();
-    VcsRoot[] roots = ProjectLevelVcsManager.getInstance(project).getAllVcsRoots();
-    for (VcsRoot root : roots) {
-      if (HgVcs.VCS_NAME.equals(root.vcs.getName())) {
-        repos.add(root.path);
-      }
     }
-    return repos;
   }
 
   protected interface HgGlobalCommand {
diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/action/HgAction.java b/plugins/hg4idea/src/org/zmlx/hg4idea/action/HgAction.java
new file mode 100644 (file)
index 0000000..b2f9c62
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2000-2011 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.zmlx.hg4idea.action;
+
+import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vcs.ProjectLevelVcsManager;
+import com.intellij.openapi.vcs.VcsRoot;
+import com.intellij.openapi.vfs.VirtualFile;
+import org.zmlx.hg4idea.HgVcs;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @author Kirill Likhodedov
+ */
+public abstract class HgAction extends AnAction {
+  @Override
+  public void actionPerformed(AnActionEvent event) {
+    final DataContext dataContext = event.getDataContext();
+    final Project project = PlatformDataKeys.PROJECT.getData(dataContext);
+    if (project == null) {
+      return;
+    }
+
+    execute(project);
+  }
+
+  @Override
+  public void update(AnActionEvent e) {
+    Presentation presentation = e.getPresentation();
+    final DataContext dataContext = e.getDataContext();
+
+    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
+    if (project == null) {
+      presentation.setEnabled(false);
+    }
+  }
+
+  public abstract void execute(Project project);
+
+  private static List<VirtualFile> findRepos(Project project) {
+    List<VirtualFile> repos = new LinkedList<VirtualFile>();
+    VcsRoot[] roots = ProjectLevelVcsManager.getInstance(project).getAllVcsRoots();
+    for (VcsRoot root : roots) {
+      if (HgVcs.VCS_NAME.equals(root.vcs.getName())) {
+        repos.add(root.path);
+      }
+    }
+    return repos;
+  }
+
+}
index a89a642854c770a6132d7c45f6266fcc2e7fe2a3..7b2aba8799527a6fac1fe3783c263c29fc47fb72 100644 (file)
@@ -25,11 +25,11 @@ import org.zmlx.hg4idea.execution.HgCommandResult;
 
 import java.util.List;
 
-final class HgCommandResultNotifier {
+public final class HgCommandResultNotifier {
 
   private final Project myProject;
 
-  HgCommandResultNotifier(Project project) {
+  public HgCommandResultNotifier(Project project) {
     myProject = project;
   }
 
index b4e70d27d85d1fb0761642500e47f1f5e801ad28..b55bfc50abf7916ac9dd1ded557f5d0c91dc18e8 100644 (file)
@@ -14,83 +14,14 @@ package org.zmlx.hg4idea.action;
 
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vfs.VirtualFile;
-import org.jetbrains.annotations.Nullable;
-import org.zmlx.hg4idea.HgErrorUtil;
-import org.zmlx.hg4idea.command.HgPushCommand;
-import org.zmlx.hg4idea.execution.HgCommandResult;
-import org.zmlx.hg4idea.execution.HgCommandResultHandler;
-import org.zmlx.hg4idea.ui.HgPushDialog;
+import org.zmlx.hg4idea.HgPusher;
 
-import java.util.Collection;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public class HgPushAction extends HgAbstractGlobalAction {
+public class HgPushAction extends HgAction {
   private static final Logger LOG = Logger.getInstance(HgPushAction.class);
-  private static Pattern PUSH_COMMITS_PATTERN = Pattern.compile(".*added (\\d+) changesets.*");
-
-  protected HgGlobalCommandBuilder getHgGlobalCommandBuilder(final Project project) {
-    return new HgGlobalCommandBuilder() {
-      public HgGlobalCommand build(Collection<VirtualFile> repos) {
-        HgPushDialog dialog = new HgPushDialog(project);
-        dialog.setRoots(repos);
-        dialog.show();
-        if (dialog.isOK()) {
-          return buildCommand(dialog, project);
-        }
-        return null;
-      }
-    };
-  }
-
-  private HgGlobalCommand buildCommand(final HgPushDialog dialog, final Project project) {
-    return new HgGlobalCommand() {
-      public VirtualFile getRepo() {
-        return dialog.getRepository();
-      }
-
-      public void execute() {
-        HgPushCommand command = new HgPushCommand(project, dialog.getRepository(), dialog.getTarget());
-        command.setRevision(dialog.getRevision());
-        command.setForce(dialog.isForce());
-        command.setBranch(dialog.getBranch());
-        command.execute(new HgCommandResultHandler() {
-          @Override
-          public void process(@Nullable HgCommandResult result) {
-            int commitsNum = getNumberOfPushedCommits(result);
-            String title = null;
-            String description = null;
-            if (commitsNum >= 0) {
-              title = "Pushed successfully";
-              description = "Pushed " + commitsNum + " " + StringUtil.pluralize("commit", commitsNum) + ".";
-            }
-            new HgCommandResultNotifier(project).process(result, title, description);
-          }
-        });
-      }
-    };
-  }
 
-  private static int getNumberOfPushedCommits(HgCommandResult result) {
-    if (!HgErrorUtil.isAbort(result)) {
-      final List<String> outputLines = result.getOutputLines();
-      for (String outputLine : outputLines) {
-        final Matcher matcher = PUSH_COMMITS_PATTERN.matcher(outputLine.trim());
-        if (matcher.matches()) {
-          try {
-            return Integer.parseInt(matcher.group(1));
-          }
-          catch (NumberFormatException e) {
-            LOG.info("getNumberOfPushedCommits ", e);
-            return -1;
-          }
-        }
-      }
-    }
-    return -1;
+  @Override
+  public void execute(final Project project) {
+    new HgPusher(project).showDialogAndPush();
   }
 
 }