Skip to content
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

Pick up .ij.import.rc file for dynamic pantsrc insertion #324

Merged
merged 14 commits into from
Dec 26, 2017
29 changes: 29 additions & 0 deletions common/com/twitter/intellij/pants/model/IJRC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package com.twitter.intellij.pants.model;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Optional;


/**
* This class tries to find the pantsrc files associated with the pants repo and construct the command line arg if they exist.
*/
public class IJRC {

public static final String IMPORT_RC_FILENAME = ".ij.import.rc";

// TODO(wisechengyi): add functionality for runConfiguration stage.

public static Optional<String> getImportPantsRc(@NotNull final String buildRoot) {
// At import time.
File importRc = new File(buildRoot, IMPORT_RC_FILENAME);
if (importRc.isFile()) {
return Optional.of("--pantsrc-files=" + importRc.getPath());
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.twitter.intellij.pants.PantsBundle;
import com.twitter.intellij.pants.PantsExecutionException;
import com.twitter.intellij.pants.metrics.PantsMetrics;
import com.twitter.intellij.pants.model.IJRC;
import com.twitter.intellij.pants.model.PantsCompileOptions;
import com.twitter.intellij.pants.model.PantsExecutionOptions;
import com.twitter.intellij.pants.settings.PantsExecutionSettings;
Expand Down Expand Up @@ -168,7 +169,7 @@ private String loadProjectStructureFromTargets(
@Nullable ProcessAdapter processAdapter
) throws IOException, ExecutionException {
final File outputFile = FileUtil.createTempFile("pants_depmap_run", ".out");
final GeneralCommandLine command = getCommand(outputFile, statusConsumer);
final GeneralCommandLine command = getPantsExportCommand(outputFile, statusConsumer);
statusConsumer.consume("Resolving dependencies...");
PantsMetrics.markExportStart();
final ProcessOutput processOutput = getProcessOutput(command);
Expand All @@ -195,21 +196,22 @@ private ProcessOutput getProcessOutput(
}

@NotNull
private GeneralCommandLine getCommand(final File outputFile, @NotNull Consumer<String> statusConsumer)
throws IOException, ExecutionException {
private GeneralCommandLine getPantsExportCommand(final File outputFile, @NotNull Consumer<String> statusConsumer) {
final GeneralCommandLine commandLine = PantsUtil.defaultCommandLine(getProjectPath());

// Grab the import stage pants rc file for IntelliJ.
Optional<String> rcArg = IJRC.getImportPantsRc(commandLine.getWorkDirectory().getPath());
rcArg.ifPresent(commandLine::addParameter);

commandLine.addParameter("--no-quiet");
commandLine.addParameter("export");
commandLine.addParameter("--formatted"); // json outputs in a compact format
if (myResolveSourcesAndDocsForJars) {
commandLine.addParameter("--export-libraries-sources");
commandLine.addParameter("--export-libraries-javadocs");
}

commandLine.addParameters(getTargetSpecs());
System.out.println(getTargetSpecs());
commandLine.addParameter("--export-output-file=" + outputFile.getPath());
LOG.debug(commandLine.toString());
return commandLine;
}

Expand Down
1 change: 1 addition & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ junit_tests(
'com/twitter/intellij/pants/service/util/*.java',
'com/twitter/intellij/pants/settings/*.java',
'com/twitter/intellij/pants/util/*.java',
'com/twitter/intellij/pants/rc/*.java',
)
)

Expand Down
39 changes: 39 additions & 0 deletions tests/com/twitter/intellij/pants/rc/IJRCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package com.twitter.intellij.pants.rc;

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.testFramework.UsefulTestCase;
import com.twitter.intellij.pants.model.IJRC;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

public class IJRCTest extends UsefulTestCase {
private File temp;

@Override
protected void setUp() throws Exception {
super.setUp();
temp = new File(getHomePath(), IJRC.IMPORT_RC_FILENAME);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
temp.delete();
}

public void testInvalidPath() {
assertFalse(IJRC.getImportPantsRc("/invalid/").isPresent());
}

public void testRcPickup() throws IOException {
FileUtil.writeToFile(temp, "123");
Optional<String> rc = IJRC.getImportPantsRc(temp.getParent());
assertTrue(rc.isPresent());
assertEquals(String.format("--pantsrc-files=%s", temp.getPath()), rc.get());
}
}