-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add IntelliJ IDEA plugin for generating with bzlgen #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattem
wants to merge
1
commit into
master
Choose a base branch
from
feat/ij-plugin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,16 @@ | ||
java_library( | ||
name = "ij_sdk", | ||
neverlink = True, | ||
exports = [ | ||
"@intellij_ce_2019_3//:sdk", | ||
], | ||
) | ||
|
||
java_binary( | ||
name = "bzlgen_plugin", | ||
srcs = glob(["src/**/*.java"]), | ||
create_executable = False, | ||
resource_strip_prefix = "plugin/resources", | ||
resources = glob(["resources/**"]), | ||
deps = [":ij_sdk"], | ||
) |
This file contains hidden or 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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="PLUGIN_MODULE" version="4"> | ||
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" /> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="IntelliJ IDEA IU-193.6494.35" jdkType="IDEA JDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains hidden or 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,53 @@ | ||
<idea-plugin> | ||
<id>com.evertz.devtools.bzlgen.ij</id> | ||
<name>Bzlgen</name> | ||
<version>0.1.0</version> | ||
<vendor email="[email protected]" url="https://www.evertz.com">Evertz Microsystems</vendor> | ||
|
||
<description><![CDATA[ | ||
Bazel build file generator for various rules, driven by bzlgen | ||
]]></description> | ||
|
||
<change-notes><![CDATA[ | ||
Initial release | ||
]]> | ||
</change-notes> | ||
|
||
<idea-version since-build="173.0"/> | ||
|
||
<depends>com.intellij.modules.platform</depends> | ||
|
||
<extensions defaultExtensionNs="com.intellij"></extensions> | ||
|
||
<actions> | ||
<group id="Bglgen.Generate" | ||
text="Bzlgen" | ||
description="Bzlgen BUILD file generation" | ||
popup="true" | ||
compact="true"> | ||
|
||
<action id="Bglgen.Generate.container_layer" | ||
class="com.evertz.devtools.bzlgen.ij.actions.GenerateContainerLayerRuleAction"> | ||
</action> | ||
|
||
<action id="Bglgen.Generate.filegroup" | ||
class="com.evertz.devtools.bzlgen.ij.actions.GenerateFilegroupRuleAction"> | ||
</action> | ||
|
||
<action id="Bglgen.Generate.ng_module" | ||
class="com.evertz.devtools.bzlgen.ij.actions.GenerateNgModuleRuleAction"> | ||
</action> | ||
|
||
<action id="Bglgen.Generate.nodejs_binary" | ||
class="com.evertz.devtools.bzlgen.ij.actions.GenerateNodeJsBinaryRuleAction"> | ||
</action> | ||
|
||
<action id="Bglgen.Generate.ts_library" | ||
class="com.evertz.devtools.bzlgen.ij.actions.GenerateTsLibraryRuleAction"> | ||
</action> | ||
|
||
<add-to-group group-id="ProjectViewPopupMenu" /> | ||
</group> | ||
</actions> | ||
|
||
</idea-plugin> |
This file contains hidden or 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,75 @@ | ||
package com.evertz.devtools.bzlgen.ij; | ||
|
||
import com.intellij.openapi.fileTypes.FileType; | ||
import com.intellij.openapi.fileTypes.FileTypes; | ||
import com.intellij.openapi.vfs.VfsUtil; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.search.FileTypeIndex; | ||
import com.intellij.util.EnvironmentUtil; | ||
import com.intellij.util.indexing.FileBasedIndex; | ||
|
||
import java.io.File; | ||
import java.util.Set; | ||
|
||
public final class BzlgenUtil { | ||
private BzlgenUtil() {} | ||
|
||
public static final String BZLGEN_BINARY = "bzlgen"; | ||
|
||
public static final String MESSAGE_TITLE = "bzlgen"; | ||
|
||
public static File findBzlgenBinaryOnPath() { | ||
String path = EnvironmentUtil.getValue("PATH"); | ||
|
||
if (path == null) { | ||
return null; | ||
} | ||
|
||
for (String entry : path.split(File.pathSeparator)) { | ||
File file = new File(entry, BZLGEN_BINARY); | ||
if (file.exists() && file.isFile() && file.canExecute()) { | ||
return file; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static boolean directoryContainsFileWithExtension(VirtualFile directory, String type) { | ||
if (!directory.isDirectory()) { | ||
return false; | ||
} | ||
|
||
for (VirtualFile child : VfsUtil.getChildren(directory)) { | ||
if (type.equals(child.getExtension())) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static boolean directoryContainsFileWithAnyExtension(VirtualFile directory, Set<String> types) { | ||
if (!directory.isDirectory()) { | ||
return false; | ||
} | ||
|
||
if (types.isEmpty()) { | ||
// empty set is considered that all files are supported | ||
return true; | ||
} | ||
|
||
for (String type : types) { | ||
if (directoryContainsFileWithExtension(directory, type)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static boolean isInEv() { | ||
// pathetic check to try and see if we are inside ev, or somewhere else | ||
return EnvironmentUtil.getEnvironmentMap().containsKey("build_tools_dir"); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
plugin/src/com/evertz/devtools/bzlgen/ij/actions/AbstractBzlgenAction.java
This file contains hidden or 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,91 @@ | ||
package com.evertz.devtools.bzlgen.ij.actions; | ||
|
||
import com.evertz.devtools.bzlgen.ij.BzlgenUtil; | ||
import com.evertz.devtools.bzlgen.ij.process.BzlgenCommand; | ||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.actionSystem.Presentation; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.roots.ProjectFileIndex; | ||
import com.intellij.openapi.vfs.VfsUtilCore; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public abstract class AbstractBzlgenAction extends AnAction { | ||
private final static ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
|
||
@Override | ||
public void update(AnActionEvent event) { | ||
VirtualFile vf = event.getData(CommonDataKeys.VIRTUAL_FILE); | ||
|
||
if (vf.isDirectory() && !supportsDirectories()) { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
|
||
// no point carrying on | ||
return; | ||
} | ||
|
||
String extension = vf.getExtension(); | ||
Set<String> supportedExtensions = getSupportedFileExtensions(); | ||
|
||
boolean containsSupportedFiles = BzlgenUtil.directoryContainsFileWithAnyExtension(vf, supportedExtensions); | ||
|
||
if (containsSupportedFiles || supportedExtensions.contains(extension)) { | ||
setEnabledForRule(event.getPresentation(), vf); | ||
} else { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
} | ||
} | ||
|
||
@Override | ||
public void actionPerformed(AnActionEvent event) { | ||
VirtualFile selected = event.getData(CommonDataKeys.VIRTUAL_FILE); | ||
VirtualFile contentRoot = getContentRoot(event.getProject(), selected); | ||
|
||
BzlgenCommand.Builder builder = new BzlgenCommand.Builder() | ||
.type(getRuleType()) | ||
.path(VfsUtilCore.getRelativePath(selected, contentRoot)) | ||
.workingDirectory(contentRoot.getPath()) | ||
.executor(executor); | ||
|
||
decorateBzlgenCommandBuilder(builder); | ||
|
||
builder.build().run(); | ||
|
||
if (selected.isDirectory()) { | ||
selected.refresh(false, false); | ||
} else { | ||
selected.getParent().refresh(false, false); | ||
} | ||
Comment on lines
+60
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not convinced this is doing what I think it's doing |
||
} | ||
|
||
protected void setEnabledForRule(Presentation presentation, VirtualFile vf) { | ||
String type = getRuleType(); | ||
presentation.setText("Generate " + type + " for " + vf.getName(), false); | ||
presentation.setEnabledAndVisible(true); | ||
} | ||
|
||
protected boolean supportsDirectories() { | ||
return true; | ||
} | ||
|
||
protected Set<String> getSupportedFileExtensions() { | ||
return Collections.emptySet(); | ||
} | ||
|
||
protected VirtualFile getContentRoot(Project project, VirtualFile file) { | ||
ProjectFileIndex index = ProjectFileIndex.getInstance(project); | ||
return index.getContentRootForFile(file); | ||
} | ||
|
||
protected void decorateBzlgenCommandBuilder(BzlgenCommand.Builder builder) { | ||
// allow subclass to override this and add to the builder | ||
}; | ||
|
||
protected abstract String getRuleType(); | ||
} |
9 changes: 9 additions & 0 deletions
9
plugin/src/com/evertz/devtools/bzlgen/ij/actions/GenerateContainerLayerRuleAction.java
This file contains hidden or 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,9 @@ | ||
package com.evertz.devtools.bzlgen.ij.actions; | ||
|
||
public class GenerateContainerLayerRuleAction extends AbstractBzlgenAction { | ||
|
||
@Override | ||
protected String getRuleType() { | ||
return "container_layer"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
plugin/src/com/evertz/devtools/bzlgen/ij/actions/GenerateFilegroupRuleAction.java
This file contains hidden or 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,9 @@ | ||
package com.evertz.devtools.bzlgen.ij.actions; | ||
|
||
public class GenerateFilegroupRuleAction extends AbstractBzlgenAction { | ||
|
||
@Override | ||
protected String getRuleType() { | ||
return "filegroup"; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
plugin/src/com/evertz/devtools/bzlgen/ij/actions/GenerateNgModuleRuleAction.java
This file contains hidden or 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,42 @@ | ||
package com.evertz.devtools.bzlgen.ij.actions; | ||
|
||
import com.evertz.devtools.bzlgen.ij.BzlgenUtil; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.vfs.VfsUtil; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
|
||
import java.util.Arrays; | ||
|
||
public class GenerateNgModuleRuleAction extends AbstractBzlgenAction { | ||
|
||
@Override | ||
public void update(AnActionEvent event) { | ||
VirtualFile vf = event.getData(CommonDataKeys.VIRTUAL_FILE); | ||
|
||
// ng_module is only supported on directories | ||
if (!vf.isDirectory()) { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
return; | ||
} | ||
|
||
// look for a .module.ts, and .component.ts, although this is a convention, it's well followed within ev | ||
// and this somewhat enforces it ;) | ||
boolean containsNgFiles = Arrays.stream(VfsUtil.getChildren(vf)) | ||
.filter(child -> !child.isDirectory() | ||
&& (child.getPath().endsWith(".component.ts") || child.getPath().endsWith(".module.ts"))) | ||
.count() == 2; | ||
|
||
if (!containsNgFiles) { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
return; | ||
} | ||
|
||
setEnabledForRule(event.getPresentation(), vf); | ||
} | ||
|
||
@Override | ||
protected String getRuleType() { | ||
return BzlgenUtil.isInEv() ? "ng_bundle" : "ng_module"; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
plugin/src/com/evertz/devtools/bzlgen/ij/actions/GenerateNodeJsBinaryRuleAction.java
This file contains hidden or 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,24 @@ | ||
package com.evertz.devtools.bzlgen.ij.actions; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class GenerateNodeJsBinaryRuleAction extends AbstractBzlgenAction { | ||
private static final Set<String> SUPPORTED_EXTS = new HashSet<String>(Arrays.asList("ts", "js")); | ||
|
||
@Override | ||
protected boolean supportsDirectories() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected Set<String> getSupportedFileExtensions() { | ||
return SUPPORTED_EXTS; | ||
} | ||
|
||
@Override | ||
protected String getRuleType() { | ||
return "js_binary"; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
plugin/src/com/evertz/devtools/bzlgen/ij/actions/GenerateTsLibraryRuleAction.java
This file contains hidden or 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,19 @@ | ||
package com.evertz.devtools.bzlgen.ij.actions; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class GenerateTsLibraryRuleAction extends AbstractBzlgenAction { | ||
private static final Set<String> SUPPORTED_EXTS = new HashSet<String>(Collections.singleton("ts")); | ||
|
||
@Override | ||
protected Set<String> getSupportedFileExtensions() { | ||
return SUPPORTED_EXTS; | ||
} | ||
|
||
@Override | ||
protected String getRuleType() { | ||
return "ts_library"; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making sure people have the right version of intellij could be fun
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah I don't really know the compatibility here, I just went with the version I have installed now. It's probably worth checking. These deps are set to
neverlink
so they don't make it into the final deploy jar, so just depends if the IJ API has changed I guess