Skip to content

Commit

Permalink
Better watchman queries in the wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Korlam committed Jan 27, 2017
1 parent 6302bbb commit f950c9e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .watchmanconfig
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"ignore_dirs": [
"buck-out",
".idea",
".buckd",
".idea",
".okbuck",
"build",
".gradle",
".swp",
"java"
],
"fsevents_latency": 0.05,
"suppress_recrawl_warnings": true
Expand Down
30 changes: 15 additions & 15 deletions buckw
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
##
## Buck wrapper script to invoke okbuck when needed, before running buck
##
## Created by OkBuck Gradle Plugin on : Fri Jan 13 16:41:05 PST 2017
## Created by OkBuck Gradle Plugin on : Fri Jan 27 01:29:40 PST 2017
##
#########################################################################

Expand Down Expand Up @@ -69,16 +69,16 @@ removeWatch ( ) {
getToClean ( ) {
ensureWatch

watchman --output-encoding=json -j 2>&1 <<-EOT
watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", "$WORKING_DIR", {
"expression": ["allof",
["type", "f"],
["anyof",
["imatch", "**/BUCK", "wholename"],
["imatch", ".buckconfig.local", "wholename"]
["name", "BUCK"],
["name", ".buckconfig.local"]
],
["not",
["imatch", ".okbuck/**/BUCK", "wholename"]
["dirname", ".okbuck"]
]
],
"fields": ["name"]
Expand All @@ -89,31 +89,31 @@ EOT
getChanges ( ) {
ensureWatch

WATCHED_CHANGES=`watchman --output-encoding=json -j 2>&1 <<-EOT
WATCHED_CHANGES=`watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", "$WORKING_DIR", {
"since": "n:okbuck_trig",
"expression": ["allof",
["type", "f"],
["anyof",
["imatch", "**/*.sq", "wholename"],
["imatch", "**/*.gradle", "wholename"],
["imatch", "**/gradle-wrapper.properties", "wholename"],
["imatch", "**/lint.xml", "wholename"],
["imatch", "**/src/**/AndroidManifest.xml", "wholename"]
["suffix", "sq"],
["suffix", "gradle"],
["name", "gradle-wrapper.properties"],
["name", "lint.xml"],
["match", "**/src/**/AndroidManifest.xml", "wholename"]
]
],
"fields": ["name"]
}]
EOT`
SOURCE_ROOTS=`watchman --output-encoding=json -j 2>&1 <<-EOT
SOURCE_ROOTS=`watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", ".", {
"since": "n:okbuck_source_roots",
"expression": ["allof",
["type", "d"],
["anyof",
["imatch", "**/src/**/java", "wholename"],
["imatch", "**/src/**/res", "wholename"],
["imatch", "**/src/**/resources", "wholename"]
["match", "**/src/**/java", "wholename"],
["match", "**/src/**/res", "wholename"],
["match", "**/src/**/resources", "wholename"]
]
],
"fields": ["new", "exists", "name"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Sets;

import java.util.Collections;
import java.util.Set;

public class WrapperExtension {
Expand All @@ -19,7 +20,7 @@ public class WrapperExtension {
/**
* List of files to leave untouched when generating configuration.
*/
public Set<String> keep = Sets.newHashSet(".okbuck/**/BUCK");
public Set<String> keep = Collections.emptySet();

/**
* List of changed files to trigger okbuck runs on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableMap;
import com.uber.okbuck.core.util.FileUtil;

import org.apache.commons.io.FilenameUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -16,6 +17,8 @@
@SuppressWarnings({"WeakerAccess", "CanBeFinal", "unused", "ResultOfMethodCallIgnored"})
public class BuckWrapperTask extends DefaultTask {

private static String OKBUCK_DIRNAME = " [\"dirname\", \".okbuck\"]";

@Input
public String repo;

Expand All @@ -35,23 +38,52 @@ public class BuckWrapperTask extends DefaultTask {

@TaskAction
void installWrapper() {
String keepExpr = toWatchmanMatchers(keep);
if (keepExpr.isEmpty()) {
keepExpr = OKBUCK_DIRNAME;
} else {
keepExpr = OKBUCK_DIRNAME + "\n" + keepExpr;
}
Map<String, String> templates = ImmutableMap.<String, String>builder()
.put("template-creation-time", new Date().toString())
.put("template-custom-buck-repo", repo)
.put("template-remove", toWatchmanMatchers(remove))
.put("template-keep", toWatchmanMatchers(keep))
.put("template-keep", keepExpr)
.put("template-watch", toWatchmanMatchers(watch))
.put("template-source-roots", toWatchmanMatchers(sourceRoots))
.build();

FileUtil.copyResourceToProject("wrapper/BUCKW_TEMPLATE", wrapper, templates);
wrapper.setExecutable(true);

File watchmanConfig = getProject().file(".watchmanconfig");
if (!watchmanConfig.exists()) {
FileUtil.copyResourceToProject("wrapper/WATCHMAN_CONFIG", getProject().file(".watchmanconfig"));
}
}

private static String watchmanExpr(String wildcardPattern) {
String simplifiedPattern = wildcardPattern;
if (wildcardPattern.startsWith("**/")) {
simplifiedPattern = wildcardPattern.replaceAll("\\*\\*/", "");
}
String basename = FilenameUtils.getBaseName(simplifiedPattern);
String extension = FilenameUtils.getExtension(simplifiedPattern);
if (!simplifiedPattern.contains("/")) {
// simple file name with no path prefixes
if (basename.equals("*")) { // suffix
return " [\"suffix\", \"" + extension + "\"]";
} else { // name
return " [\"name\", \"" + simplifiedPattern + "\"]";
}
}
return " [\"match\", \"" + wildcardPattern + "\", \"wholename\"]";
}

private static String toWatchmanMatchers(Set<String> wildcardPatterns) {
return wildcardPatterns
.parallelStream()
.map(pattern -> " [\"imatch\", \"" + pattern + "\", \"wholename\"]")
.map(BuckWrapperTask::watchmanExpr)
.collect(Collectors.joining(",\n"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ removeWatch ( ) {
getToClean ( ) {
ensureWatch

watchman --output-encoding=json -j 2>&1 <<-EOT
watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", "$WORKING_DIR", {
"expression": ["allof",
["type", "f"],
Expand All @@ -88,7 +88,7 @@ EOT
getChanges ( ) {
ensureWatch

WATCHED_CHANGES=`watchman --output-encoding=json -j 2>&1 <<-EOT
WATCHED_CHANGES=`watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", "$WORKING_DIR", {
"since": "n:okbuck_trig",
"expression": ["allof",
Expand All @@ -100,7 +100,7 @@ ${template-watch}
"fields": ["name"]
}]
EOT`
SOURCE_ROOTS=`watchman --output-encoding=json -j 2>&1 <<-EOT
SOURCE_ROOTS=`watchman --output-encoding=json --no-pretty -j 2>&1 <<-EOT
["query", ".", {
"since": "n:okbuck_source_roots",
"expression": ["allof",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"ignore_dirs": [
"buck-out",
".buckd",
".idea",
".okbuck",
"build",
".gradle",
".swp",
],
"fsevents_latency": 0.05,
"suppress_recrawl_warnings": true
}

0 comments on commit f950c9e

Please sign in to comment.