-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GUI for
faspass amend
command (#528)
Allows user to include (or remove) new pants targets into already imported project
- Loading branch information
Tomasz Pasternak
authored
May 18, 2020
1 parent
6b782c3
commit a35f379
Showing
20 changed files
with
1,171 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package com.twitter.intellij.pants.bsp; | ||
|
||
import java.util.List; | ||
|
||
class BloopConfig { | ||
private final List<String> pantsTargets; | ||
|
||
public List<String> getPantsTargets() { | ||
return pantsTargets; | ||
} | ||
|
||
BloopConfig(List<String> pantsTargets) { | ||
this.pantsTargets = pantsTargets; | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
src/com/twitter/intellij/pants/bsp/FastpassBspAmendAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package com.twitter.intellij.pants.bsp; | ||
|
||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.openapi.progress.ProgressManager; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.Messages; | ||
import com.twitter.intellij.pants.PantsBundle; | ||
import com.twitter.intellij.pants.bsp.ui.FastpassManagerDialog; | ||
import com.twitter.intellij.pants.util.ExternalProjectUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.bsp.BSP; | ||
import org.jetbrains.bsp.BspUtil; | ||
|
||
import javax.swing.SwingUtilities; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class FastpassBspAmendAction extends AnAction { | ||
|
||
private final Logger logger = Logger.getInstance(FastpassBspAmendAction.class); | ||
|
||
@Override | ||
public void update(@NotNull AnActionEvent e) { | ||
super.update(e); | ||
boolean isBsp = e.getProject() != null && BspUtil.isBspProject(e.getProject()); | ||
e.getPresentation().setEnabledAndVisible(isBsp); | ||
} | ||
|
||
@Override | ||
public void actionPerformed(@NotNull AnActionEvent event) { | ||
try { | ||
Project project = event.getProject(); | ||
if (project != null) { | ||
Set<PantsBspData> linkedProjects = PantsBspData.importsFor(project); | ||
if (linkedProjects.size() > 1) { | ||
Messages.showErrorDialog( | ||
PantsBundle.message("pants.bsp.error.failed.more.than.one.bsp.project.not.supported.message"), | ||
PantsBundle.message("pants.bsp.error.action.not.supported.title") | ||
); | ||
} | ||
else if (linkedProjects.size() < 1) { | ||
Messages.showErrorDialog( | ||
PantsBundle.message("pants.bsp.error.failed.not.a.bsp.pants.project.message"), | ||
PantsBundle.message("pants.bsp.error.action.not.supported.title") | ||
); | ||
} | ||
else { | ||
PantsBspData importData = linkedProjects.stream().findFirst().get(); | ||
startAmendProcedure(project, importData); | ||
} | ||
} | ||
else { | ||
Messages.showErrorDialog( | ||
PantsBundle.message("pants.bsp.error.no.project.found"), | ||
PantsBundle.message("pants.bsp.error.action.not.supported.title") | ||
); | ||
} | ||
} catch (Throwable e) { | ||
logger.error(e); | ||
} | ||
} | ||
|
||
private void startAmendProcedure(Project project, PantsBspData firstProject) { | ||
CompletableFuture<Set<String>> oldTargets = FastpassUtils.selectedTargets(firstProject); | ||
|
||
FastpassTargetListCache targetsListCache = new FastpassTargetListCache(Paths.get(firstProject.getPantsRoot().getPath())); | ||
PantsTargetsRepository getPreview = targets -> FastpassUtils.validateAndGetPreview(firstProject.getPantsRoot(), targets, | ||
targetsListCache::getTargetsList | ||
); | ||
Optional<Set<String>> newTargets = FastpassManagerDialog | ||
.promptForTargetsToImport(project, oldTargets, getPreview); | ||
amendAndRefreshIfNeeded(project, firstProject, oldTargets, newTargets); | ||
} | ||
|
||
private void amendAndRefreshIfNeeded( | ||
@NotNull Project project, | ||
@NotNull PantsBspData basePath, | ||
@NotNull CompletableFuture<Set<String>> oldTargets, | ||
@NotNull Optional<Set<String>> newTargets | ||
) { | ||
oldTargets.thenAccept( | ||
oldTargetsVal -> newTargets.ifPresent(newTargetsVal -> { | ||
if(newTargetsVal.isEmpty()) { | ||
SwingUtilities.invokeLater(() -> { | ||
Messages.showErrorDialog(PantsBundle.message("pants.bsp.msg.box.empty.list.content"), | ||
PantsBundle.message("pants.bsp.msg.box.amend.title")); | ||
}); | ||
} else if (!newTargetsVal.equals(oldTargetsVal)) { | ||
try { | ||
refreshProjectsWithNewTargetsList(project, newTargets.get(), basePath); | ||
} | ||
catch (Throwable e) { | ||
logger.error(e); | ||
} | ||
} else { | ||
SwingUtilities.invokeLater(() -> { | ||
Messages.showInfoMessage(PantsBundle.message("pants.bsp.msg.box.specs.unchanged.content"), | ||
PantsBundle.message("pants.bsp.msg.box.amend.title")); | ||
}); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
private void refreshProjectsWithNewTargetsList( | ||
@NotNull Project project, | ||
Collection<String> newTargets, | ||
PantsBspData basePath | ||
) { | ||
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> { | ||
try { | ||
FastpassUtils.amendAll(basePath, new ArrayList<>(newTargets), project).get(); | ||
ExternalProjectUtil.refresh(project, BSP.ProjectSystemId()); | ||
} catch (Throwable e){ | ||
logger.error(e); | ||
} | ||
},"Amending", false, project ); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/com/twitter/intellij/pants/bsp/FastpassTargetListCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package com.twitter.intellij.pants.bsp; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
final public class FastpassTargetListCache { | ||
|
||
private final Path myPantsRoot; | ||
|
||
public FastpassTargetListCache(Path pantsRoot) { | ||
|
||
myPantsRoot = pantsRoot; | ||
} | ||
|
||
final ConcurrentHashMap<Path, CompletableFuture<Collection<PantsTargetAddress>>> cache = new ConcurrentHashMap<>(); | ||
|
||
public CompletableFuture<Collection<PantsTargetAddress>> getTargetsList(Path path) { | ||
return cache.computeIfAbsent(path, path1 -> FastpassUtils.availableTargetsIn(myPantsRoot.resolve(path1))); | ||
} | ||
} |
Oops, something went wrong.