Skip to content

Commit

Permalink
Use path relativize method and create buck files relative to root pro…
Browse files Browse the repository at this point in the history
…ject. (#751)
  • Loading branch information
raviagarwal7 authored and kageiit committed Nov 1, 2018
1 parent a1ff161 commit d8d3247
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def addCommonConfigurationForAndroidModules(Project project) {
}

lintOptions {
lintConfig project.rootProject.file("config/lint/lint.xml")
lintConfig project.rootProject.file("lint.xml")
}
}

Expand Down
11 changes: 7 additions & 4 deletions buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,15 @@ private void writeExportedFileRules(Project rootBuckProject, OkBuckExtension okB
pathToRules.put(containingPath, rules);
}
for (Map.Entry<String, Set<Rule>> entry : pathToRules.entrySet()) {
File buckFile = new File(entry.getKey(), BUCK);
File buckFile =
rootBuckProject.getRootDir().toPath().resolve(entry.getKey()).resolve(BUCK).toFile();
try (OutputStream os =
new FileOutputStream(buckFile, currentProjectPaths.contains(entry.getKey()))) {
for (Rule rule : entry.getValue()) {
rule.render(os);
}
entry
.getValue()
.stream()
.sorted((rule1, rule2) -> rule1.name().compareToIgnoreCase(rule2.name()))
.forEach(rule -> rule.render(os));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.uber.okbuck.core.model.base.Target;
import com.uber.okbuck.core.model.jvm.JvmTarget;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.commons.io.FilenameUtils;

public abstract class BuckRuleComposer {

Expand All @@ -20,18 +21,19 @@ public static Set<String> externalApt(Set<String> deps) {
}

@Nullable
public static String fileRule(@Nullable String filePath) {
if (filePath == null) {
public static String fileRule(@Nullable String fileString) {
if (fileString == null) {
return null;
}

StringBuilder ext = new StringBuilder("//");
ext.append(filePath);
int ind = FilenameUtils.indexOfLastSeparator(filePath) + 2;
if (ind >= 0) {
return ext.replace(ind, ind + 1, ":").toString();
Path filePath = Paths.get(fileString);
Path parentFilePath = filePath.getParent();

if (parentFilePath == null) {
return String.format("//:%s", filePath);
} else {
return String.format("//%s:%s", parentFilePath, parentFilePath.relativize(filePath));
}
return ext.toString();
}

public static Set<String> targets(Set<Target> deps) {
Expand Down
34 changes: 19 additions & 15 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import com.google.common.collect.TreeMultimap;
import com.uber.okbuck.template.common.LoadStatements;
import com.uber.okbuck.template.core.Rule;
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
Expand All @@ -28,6 +23,10 @@
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.gradle.api.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class FileUtil {
private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);
Expand All @@ -39,8 +38,9 @@ private FileUtil() {}
public static String getRelativePath(File root, File f) {
Path fPath = f.toPath().toAbsolutePath();
Path rootPath = root.toPath().toAbsolutePath();

if (fPath.startsWith(rootPath)) {
return fPath.toString().substring(rootPath.toString().length() + 1);
return rootPath.relativize(fPath).toString();
} else {
throw new IllegalStateException(fPath + " must be located inside " + rootPath);
}
Expand Down Expand Up @@ -90,16 +90,16 @@ public static void writeToBuckFile(List<Rule> rules, File buckFile) {
}

@SuppressWarnings("InconsistentOverloads")
public static void writeToBuckFile(Multimap<String, String> loadStatements, List<Rule> rules, File buckFile) {
public static void writeToBuckFile(
Multimap<String, String> loadStatements, List<Rule> rules, File buckFile) {
if (!rules.isEmpty()) {
File parent = buckFile.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IllegalStateException("Couldn't create dir: " + parent);
}

try (
OutputStream fos = new FileOutputStream(buckFile);
BufferedOutputStream os = new BufferedOutputStream(fos)){
try (OutputStream fos = new FileOutputStream(buckFile);
BufferedOutputStream os = new BufferedOutputStream(fos)) {

if (!loadStatements.isEmpty()) {
LoadStatements.template(writableLoadStatements(loadStatements)).render(os);
Expand All @@ -119,11 +119,15 @@ public static void writeToBuckFile(Multimap<String, String> loadStatements, List
}

private static List<String> writableLoadStatements(Multimap<String, String> loadStatements) {
return loadStatements.asMap().entrySet().stream()
.map(loadStatement -> Stream
.concat(Stream.of(loadStatement.getKey()), loadStatement.getValue().stream())
.map(statement -> "'" + statement + "'")
.collect(Collectors.joining(", ", "load(", ")")))
return loadStatements
.asMap()
.entrySet()
.stream()
.map(
loadStatement ->
Stream.concat(Stream.of(loadStatement.getKey()), loadStatement.getValue().stream())
.map(statement -> "'" + statement + "'")
.collect(Collectors.joining(", ", "load(", ")")))
.collect(Collectors.toList());
}

Expand Down
File renamed without changes.

0 comments on commit d8d3247

Please sign in to comment.