Fetch targets by running buck target --json.
Summary: We can fetch more detailed and organized data from
`buck target --json` including target type and sources required by a target.
Test Plan: Launch the plugin and click refresh on the right panel window.
diff --git a/plugin/src/com/facebook/buck/plugin/intellij/BuckTarget.java b/plugin/src/com/facebook/buck/plugin/intellij/BuckTarget.java
index bb7f4b0..d035a79 100644
--- a/plugin/src/com/facebook/buck/plugin/intellij/BuckTarget.java
+++ b/plugin/src/com/facebook/buck/plugin/intellij/BuckTarget.java
@@ -1,29 +1,56 @@
+/*
+ * Copyright 2013-present Facebook, Inc.
+ *
+ * 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.facebook.buck.plugin.intellij;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
public class BuckTarget {
- private final String name;
- private String description;
- public BuckTarget(String name) {
+ private final String type;
+ private final String name;
+ private final String basePath;
+ private final ImmutableList<String> sources;
+
+ public BuckTarget(String type, String name, String basePath, ImmutableList<String> sources) {
+ this.type = Preconditions.checkNotNull(type);
this.name = Preconditions.checkNotNull(name);
+ this.basePath = Preconditions.checkNotNull(basePath);
+ this.sources = Preconditions.checkNotNull(sources);
}
public String getName() {
return name;
}
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
+ public String getFullName() {
+ return String.format("//%s:%s", getBasePath(), getName());
}
@Override
public String toString() {
- return name;
+ return getFullName();
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public String getBasePath() {
+ return basePath;
}
}
diff --git a/plugin/src/com/facebook/buck/plugin/intellij/commands/BuildCommand.java b/plugin/src/com/facebook/buck/plugin/intellij/commands/BuildCommand.java
index 755d42a..c01a1e6 100644
--- a/plugin/src/com/facebook/buck/plugin/intellij/commands/BuildCommand.java
+++ b/plugin/src/com/facebook/buck/plugin/intellij/commands/BuildCommand.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2013-present Facebook, Inc.
+ *
+ * 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.facebook.buck.plugin.intellij.commands;
import com.facebook.buck.plugin.intellij.BuckTarget;
@@ -10,7 +26,7 @@
private BuildCommand() {}
public static void build(BuckRunner buckRunner, BuckTarget target) {
- int exitCode = buckRunner.execute("build", target.getName());
+ int exitCode = buckRunner.execute("build", target.getFullName());
if (exitCode != 0) {
LOG.error(buckRunner.getStderr());
return;
diff --git a/plugin/src/com/facebook/buck/plugin/intellij/commands/TargetsCommand.java b/plugin/src/com/facebook/buck/plugin/intellij/commands/TargetsCommand.java
index 48962e1..09925e8 100644
--- a/plugin/src/com/facebook/buck/plugin/intellij/commands/TargetsCommand.java
+++ b/plugin/src/com/facebook/buck/plugin/intellij/commands/TargetsCommand.java
@@ -1,32 +1,74 @@
+/*
+ * Copyright 2013-present Facebook, Inc.
+ *
+ * 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.facebook.buck.plugin.intellij.commands;
import com.facebook.buck.plugin.intellij.BuckTarget;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.intellij.openapi.diagnostic.Logger;
+import java.io.IOException;
+
public class TargetsCommand {
private static final Logger LOG = Logger.getInstance(TargetsCommand.class);
private TargetsCommand() {}
- public static ImmutableList<BuckTarget> getTargets(BuckRunner buckRunner) {
- int exitCode = buckRunner.execute("targets");
- if (exitCode != 0) {
- LOG.error(buckRunner.getStderr());
- return ImmutableList.of();
- }
+ public static ImmutableList<BuckTarget> getTargets(BuckRunner buckRunner) {
+ try {
+ int exitCode = buckRunner.execute("targets", "--json");
+ if (exitCode != 0) {
+ throw new RuntimeException(buckRunner.getStderr());
+ }
- // Parse output
- String targetsText = buckRunner.getStdout();
- ImmutableList<String> targetNames = ImmutableList.copyOf(targetsText.split("\n"));
+ // Parse output
+ ObjectMapper mapper = new ObjectMapper();
+ JsonFactory factory = mapper.getJsonFactory();
+ JsonParser parser = factory.createJsonParser(buckRunner.getStdout());
+ JsonNode jsonNode = mapper.readTree(parser);
+ if (!jsonNode.isArray()) {
+ throw new IllegalStateException();
+ }
- // Create BuckTarget objects
- ImmutableList.Builder<BuckTarget> builder = ImmutableList.builder();
- for (String targetName : targetNames) {
- builder.add(new BuckTarget(targetName));
+ ImmutableList.Builder<BuckTarget> builder = ImmutableList.builder();
+ for (JsonNode target : jsonNode) {
+ if (!target.isObject()) {
+ throw new IllegalStateException();
+ }
+ String basePath = target.get("buck.base_path").asText();
+ String name = target.get("name").asText();
+ String type = target.get("type").asText();
+ ImmutableList.Builder<String> srcBuilder = ImmutableList.builder();
+ JsonNode srcs = target.get("srcs");
+ if (srcs != null && srcs.isArray()) {
+ for (JsonNode src : srcs) {
+ srcBuilder.add(src.asText());
+ }
+ }
+ builder.add(new BuckTarget(type, name, basePath, srcBuilder.build()));
+ }
+ return builder.build();
+ } catch (IOException | RuntimeException e) {
+ LOG.error(e);
}
- ImmutableList<BuckTarget> targets = builder.build();
- return targets;
+ return ImmutableList.of();
}
}
diff --git a/plugin/src/com/facebook/buck/plugin/intellij/ui/BuckTargetsPanel.java b/plugin/src/com/facebook/buck/plugin/intellij/ui/BuckTargetsPanel.java
index 12cabd5..bb7e47b 100644
--- a/plugin/src/com/facebook/buck/plugin/intellij/ui/BuckTargetsPanel.java
+++ b/plugin/src/com/facebook/buck/plugin/intellij/ui/BuckTargetsPanel.java
@@ -93,12 +93,12 @@
boolean cellHasFocus) {
BuckTarget target = (BuckTarget) value;
Component renderer = super.getListCellRendererComponent(list,
- target.getName(),
+ target.getFullName(),
index,
isSelected,
cellHasFocus
);
- ((JComponent) renderer).setToolTipText(target.getDescription());
+ ((JComponent) renderer).setToolTipText(target.getFullName());
return renderer;
}
});