summaryrefslogtreecommitdiff
path: root/plugins/git4idea/remote-servers-git/src/com/intellij/remoteServer/util/importProject/CloudGitRemoteDetector.java
blob: f253a3de2894f0bc02a4dd9c7135b4c7a8267408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright 2000-2014 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 com.intellij.remoteServer.util.importProject;

import com.intellij.execution.RunManager;
import com.intellij.execution.RunManagerAdapter;
import com.intellij.execution.RunManagerEx;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.ide.actions.ImportModuleAction;
import com.intellij.ide.util.importProject.RootsDetectionStep;
import com.intellij.ide.util.newProjectWizard.AbstractProjectWizard;
import com.intellij.ide.util.newProjectWizard.AddModuleWizard;
import com.intellij.ide.util.newProjectWizard.StepSequence;
import com.intellij.ide.util.projectWizard.ImportFromSourcesProvider;
import com.intellij.ide.wizard.Step;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationListener;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.remoteServer.ServerType;
import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
import com.intellij.remoteServer.configuration.deployment.ModuleDeploymentSource;
import com.intellij.remoteServer.impl.configuration.deployment.DeployToServerConfigurationTypesRegistrar;
import com.intellij.remoteServer.impl.configuration.deployment.DeployToServerRunConfiguration;
import com.intellij.remoteServer.impl.configuration.deployment.ModuleDeploymentSourceImpl;
import com.intellij.remoteServer.util.CloudBundle;
import com.intellij.remoteServer.util.CloudGitDeploymentDetector;
import com.intellij.remoteServer.util.CloudNotifier;
import com.intellij.util.Function;
import com.intellij.util.containers.hash.HashMap;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryChangeListener;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;

import javax.swing.event.HyperlinkEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author michael.golubev
 */
public class CloudGitRemoteDetector extends AbstractProjectComponent implements GitRepositoryChangeListener {

  private final GitRepositoryManager myRepositoryManager;
  private final RunManagerEx myRunManager;

  private final CloudNotifier myNotifier;

  private final List<CloudTypeDelegate> myDelegates;

  public CloudGitRemoteDetector(Project project, GitRepositoryManager repositoryManager, RunManager runManager) {
    super(project);
    myRepositoryManager = repositoryManager;
    myRunManager = (RunManagerEx)runManager;

    myNotifier = new CloudNotifier("Git remotes detector");

    myDelegates = new ArrayList<CloudTypeDelegate>();
    for (CloudGitDeploymentDetector deploymentDetector : CloudGitDeploymentDetector.EP_NAME.getExtensions()) {
      myDelegates.add(new CloudTypeDelegate(deploymentDetector));
    }
  }

  @Override
  public void projectOpened() {
    myProject.getMessageBus().connect().subscribe(GitRepository.GIT_REPO_CHANGE, this);

    myRunManager.addRunManagerListener(new RunManagerAdapter() {

      @Override
      public void runConfigurationAdded(@NotNull RunnerAndConfigurationSettings settings) {
        onRunConfigurationAddedOrChanged(settings);
      }

      @Override
      public void runConfigurationChanged(@NotNull RunnerAndConfigurationSettings settings) {
        onRunConfigurationAddedOrChanged(settings);
      }
    });
  }

  private void onRunConfigurationAddedOrChanged(RunnerAndConfigurationSettings settings) {
    RunConfiguration runConfiguration = settings.getConfiguration();
    for (CloudTypeDelegate delegate : myDelegates) {
      delegate.onRunConfigurationAddedOrChanged(runConfiguration);
    }
  }

  @Override
  public void repositoryChanged(@NotNull GitRepository repository) {
    for (CloudTypeDelegate delegate : myDelegates) {
      delegate.repositoryChanged(repository);
    }
  }

  private class CloudTypeDelegate {

    private final CloudGitDeploymentDetector myDeploymentDetector;

    private Map<GitRepository, RepositoryNotifier> myRepositoryToNotifier = new HashMap<GitRepository, RepositoryNotifier>();

    public CloudTypeDelegate(CloudGitDeploymentDetector deploymentDetector) {
      myDeploymentDetector = deploymentDetector;
    }

    private ServerType getCloudType() {
      return myDeploymentDetector.getCloudType();
    }

    public void repositoryChanged(@NotNull GitRepository repository) {
      String applicationName = myDeploymentDetector.getFirstApplicationName(repository);
      if (applicationName == null) {
        forget(repository);
      }
      else {
        RepositoryNotifier notifier = myRepositoryToNotifier.get(repository);
        if (notifier == null && !hasRunConfig4Repository(repository)) {
          notifier = new RepositoryNotifier(myDeploymentDetector, repository);
          myRepositoryToNotifier.put(repository, notifier);
        }
        if (notifier != null) {
          notifier.setApplicationName(applicationName);
        }
      }
    }

    private boolean hasRunConfig4Repository(GitRepository repository) {
      List<RunConfiguration> runConfigurations
        = myRunManager.getConfigurationsList(DeployToServerConfigurationTypesRegistrar.getDeployConfigurationType(getCloudType()));

      VirtualFile repositoryRoot = repository.getRoot();

      for (RunConfiguration runConfiguration : runConfigurations) {
        if (repositoryRoot.equals(getContentRoot(runConfiguration))) {
          return true;
        }
      }
      return false;
    }

    private VirtualFile getContentRoot(RunConfiguration runConfiguration) {
      if (!(runConfiguration instanceof DeployToServerRunConfiguration)) {
        return null;
      }

      DeployToServerRunConfiguration deployRunConfiguration = (DeployToServerRunConfiguration)runConfiguration;
      if (deployRunConfiguration.getServerType() != getCloudType()) {
        return null;
      }

      DeploymentSource deploymentSource = deployRunConfiguration.getDeploymentSource();
      if (!(deploymentSource instanceof ModuleDeploymentSource)) {
        return null;
      }

      return ((ModuleDeploymentSource)deploymentSource).getContentRoot();
    }

    public void onRunConfigurationAddedOrChanged(RunConfiguration runConfiguration) {
      VirtualFile contentRoot = getContentRoot(runConfiguration);
      if (contentRoot == null) {
        return;
      }

      GitRepository repository = myRepositoryManager.getRepositoryForRoot(contentRoot);
      if (repository == null) {
        return;
      }

      forget(repository);
    }

    private void forget(GitRepository repository) {
      RepositoryNotifier notifier = myRepositoryToNotifier.remove(repository);
      if (notifier != null) {
        notifier.expire();
      }
    }
  }

  private class RepositoryNotifier {

    private final CloudGitDeploymentDetector myDeploymentDetector;

    private final VirtualFile myRepositoryRoot;
    private final Notification myNotification;

    private final String myCloudName;

    private String myApplicationName;

    public RepositoryNotifier(CloudGitDeploymentDetector deploymentDetector, GitRepository repository) {
      myDeploymentDetector = deploymentDetector;
      myRepositoryRoot = repository.getRoot();
      myCloudName = deploymentDetector.getCloudType().getPresentableName();
      String path = FileUtil.toSystemDependentName(myRepositoryRoot.getPath());
      myNotification = myNotifier.showMessage(CloudBundle.getText("git.cloud.app.detected", myCloudName, path),
                                              MessageType.INFO,
                                              new NotificationListener() {

                                                @Override
                                                public void hyperlinkUpdate(@NotNull Notification notification,
                                                                            @NotNull HyperlinkEvent event) {
                                                  setupRunConfiguration();
                                                }
                                              });
    }

    public void setApplicationName(String applicationName) {
      myApplicationName = applicationName;
    }

    public void expire() {
      myNotification.expire();
    }

    public void setupRunConfiguration() {
      Module targetModule = null;
      for (Module module : ModuleManager.getInstance(myProject).getModules()) {
        if (myRepositoryRoot.equals(ModuleDeploymentSourceImpl.getContentRoot(module))) {
          targetModule = module;
          break;
        }
      }

      if (targetModule == null) {
        AddModuleWizard wizard = new AddModuleWizard(myProject, myRepositoryRoot.getPath(), new ImportFromSourcesProvider());
        wizard.navigateToStep(new Function<Step, Boolean>() {
          @Override
          public Boolean fun(Step step) {
            return step instanceof RootsDetectionStep;
          }
        });
        if (wizard.getStepCount() > 0 && !wizard.showAndGet()) {
          return;
        }
        ImportModuleAction.createFromWizard(myProject, wizard);
      }
      else {
        final CloudGitChooseAccountStepBase chooseAccountStep
          = new CloudGitChooseAccountStepBase(myDeploymentDetector) {

          @Override
          public void updateDataModel() {

          }
        };

        if (!new AbstractProjectWizard(CloudBundle.getText("choose.account.wizzard.title", myCloudName), myProject, (String)null) {

          final StepSequence myStepSequence;

          {
            myStepSequence = new StepSequence(chooseAccountStep);
            addStep(chooseAccountStep);
            init();
          }

          @Override
          public StepSequence getSequence() {
            return myStepSequence;
          }
        }.showAndGet()) {
          return;
        }
        chooseAccountStep.createRunConfiguration(targetModule, myApplicationName);
      }
    }
  }
}