use of java.util.regex.Matcher in project buck by facebook.
the class CompileStringsStep method groupFilesByLocale.
/**
* Groups a list of strings.xml files by locale.
* String files with no resource qualifier (eg. values/strings.xml) are mapped to the "en" locale
*
* eg. given the following list:
*
* ImmutableList.of(
* Paths.get("one/res/values-es/strings.xml"),
* Paths.get("two/res/values-es/strings.xml"),
* Paths.get("three/res/values-pt-rBR/strings.xml"),
* Paths.get("four/res/values-pt-rPT/strings.xml"),
* Paths.get("five/res/values/strings.xml"));
*
* returns:
*
* ImmutableMap.of(
* "es", ImmutableList.of(Paths.get("one/res/values-es/strings.xml"),
* Paths.get("two/res/values-es/strings.xml")),
* "pt_BR", ImmutableList.of(Paths.get("three/res/values-pt-rBR/strings.xml'),
* "pt_PT", ImmutableList.of(Paths.get("four/res/values-pt-rPT/strings.xml"),
* "en", ImmutableList.of(Paths.get("five/res/values/strings.xml")));
*/
@VisibleForTesting
ImmutableMultimap<String, Path> groupFilesByLocale(ImmutableList<Path> files) {
ImmutableMultimap.Builder<String, Path> localeToFiles = ImmutableMultimap.builder();
for (Path filepath : files) {
String path = MorePaths.pathWithUnixSeparators(filepath);
Matcher matcher = NON_ENGLISH_STRING_FILE_PATTERN.matcher(path);
if (matcher.matches()) {
String baseLocale = matcher.group(1);
String country = matcher.group(2);
String locale = country == null ? baseLocale : baseLocale + "_" + country;
if (country != null && !regionSpecificToBaseLocaleMap.containsKey(locale)) {
regionSpecificToBaseLocaleMap.put(locale, baseLocale);
}
localeToFiles.put(locale, filepath);
} else {
Preconditions.checkState(path.endsWith(ENGLISH_STRING_PATH_SUFFIX), "Invalid path passed to compile strings: " + path);
localeToFiles.put(ENGLISH_LOCALE, filepath);
}
}
return localeToFiles.build();
}
use of java.util.regex.Matcher in project buck by facebook.
the class AppleCoreSimulatorServiceController method getCoreSimulatorServicePath.
/**
* Returns the path on disk to the running Core Simulator service, if any.
* Returns {@code Optional.empty()} unless exactly one Core Simulator service is running.
*/
public Optional<Path> getCoreSimulatorServicePath(UserIdFetcher userIdFetcher) throws IOException, InterruptedException {
ImmutableSet<String> coreSimulatorServiceNames = getMatchingServiceNames(CORE_SIMULATOR_SERVICE_PATTERN);
if (coreSimulatorServiceNames.size() != 1) {
LOG.debug("Could not get core simulator service name (got %s)", coreSimulatorServiceNames);
return Optional.empty();
}
String coreSimulatorServiceName = Iterables.getOnlyElement(coreSimulatorServiceNames);
ImmutableList<String> launchctlPrintCommand = ImmutableList.of("launchctl", "print", String.format("user/%d/%s", userIdFetcher.getUserId(), coreSimulatorServiceName));
LOG.debug("Getting status of service %s with %s", coreSimulatorServiceName, launchctlPrintCommand);
ProcessExecutorParams launchctlPrintParams = ProcessExecutorParams.builder().setCommand(launchctlPrintCommand).build();
ProcessExecutor.LaunchedProcess launchctlPrintProcess = processExecutor.launchProcess(launchctlPrintParams);
Optional<Path> result = Optional.empty();
try (InputStreamReader stdoutReader = new InputStreamReader(launchctlPrintProcess.getInputStream(), StandardCharsets.UTF_8);
BufferedReader bufferedStdoutReader = new BufferedReader(stdoutReader)) {
String line;
while ((line = bufferedStdoutReader.readLine()) != null) {
Matcher matcher = LAUNCHCTL_PRINT_PATH_PATTERN.matcher(line);
if (matcher.matches()) {
String path = matcher.group(1);
LOG.debug("Found path of service %s: %s", coreSimulatorServiceName, path);
result = Optional.of(Paths.get(path));
break;
}
}
} finally {
processExecutor.destroyLaunchedProcess(launchctlPrintProcess);
processExecutor.waitForLaunchedProcess(launchctlPrintProcess);
}
return result;
}
use of java.util.regex.Matcher in project buck by facebook.
the class AppleCoreSimulatorServiceController method getMatchingServiceNames.
private ImmutableSet<String> getMatchingServiceNames(Pattern serviceNamePattern) throws IOException, InterruptedException {
ImmutableList<String> launchctlListCommand = ImmutableList.of("launchctl", "list");
LOG.debug("Getting list of services with %s", launchctlListCommand);
ProcessExecutorParams launchctlListParams = ProcessExecutorParams.builder().setCommand(launchctlListCommand).build();
ProcessExecutor.LaunchedProcess launchctlListProcess = processExecutor.launchProcess(launchctlListParams);
ImmutableSet.Builder<String> resultBuilder = ImmutableSet.builder();
try (InputStreamReader stdoutReader = new InputStreamReader(launchctlListProcess.getInputStream(), StandardCharsets.UTF_8);
BufferedReader bufferedStdoutReader = new BufferedReader(stdoutReader)) {
String line;
while ((line = bufferedStdoutReader.readLine()) != null) {
Matcher launchctlListOutputMatcher = LAUNCHCTL_LIST_OUTPUT_PATTERN.matcher(line);
if (launchctlListOutputMatcher.matches()) {
String serviceName = launchctlListOutputMatcher.group(3);
Matcher serviceNameMatcher = serviceNamePattern.matcher(serviceName);
if (serviceNameMatcher.find()) {
LOG.debug("Found matching service name: %s", serviceName);
resultBuilder.add(serviceName);
}
}
}
} finally {
processExecutor.destroyLaunchedProcess(launchctlListProcess);
processExecutor.waitForLaunchedProcess(launchctlListProcess);
}
return resultBuilder.build();
}
use of java.util.regex.Matcher in project buck by facebook.
the class AppleSimulatorController method launchInstalledBundleInSimulator.
/**
* Launches a previously-installed bundle in a started simulator.
*
* @return the process ID of the newly-launched process if successful,
* an absent value otherwise.
*/
public Optional<Long> launchInstalledBundleInSimulator(String simulatorUdid, String bundleID, LaunchBehavior launchBehavior, List<String> args) throws IOException, InterruptedException {
ImmutableList.Builder<String> commandBuilder = ImmutableList.builder();
commandBuilder.add(simctlPath.toString(), "launch");
if (launchBehavior == LaunchBehavior.WAIT_FOR_DEBUGGER) {
commandBuilder.add("-w");
}
commandBuilder.add(simulatorUdid, bundleID);
commandBuilder.addAll(args);
ImmutableList<String> command = commandBuilder.build();
ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(command).build();
Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);
String message = String.format("Launching bundle ID %s in simulator %s via command %s", bundleID, simulatorUdid, command);
LOG.debug(message);
ProcessExecutor.Result result = processExecutor.launchAndExecute(processExecutorParams, options, /* stdin */
Optional.empty(), /* timeOutMs */
Optional.empty(), /* timeOutHandler */
Optional.empty());
if (result.getExitCode() != 0) {
LOG.error(result.getMessageForResult(message));
return Optional.empty();
}
Preconditions.checkState(result.getStdout().isPresent());
String trimmedStdout = result.getStdout().get().trim();
Matcher stdoutMatcher = SIMCTL_LAUNCH_OUTPUT_PATTERN.matcher(trimmedStdout);
if (!stdoutMatcher.find()) {
LOG.error("Could not parse output from %s: %s", command, trimmedStdout);
return Optional.empty();
}
return Optional.of(Long.parseLong(stdoutMatcher.group(1), 10));
}
use of java.util.regex.Matcher in project buck by facebook.
the class XctestOutputParsing method handleCaseStarted.
public static void handleCaseStarted(Matcher matcher, BufferedReader outputBufferedReader, XctestEventCallback eventCallback) {
BeginTestCaseEvent event = new BeginTestCaseEvent();
event.test = matcher.group(1);
Matcher nameMatcher = XctestOutputParsing.CASE_PATTERN.matcher(event.test);
if (nameMatcher.find()) {
event.className = nameMatcher.group(1);
event.methodName = nameMatcher.group(2);
}
eventCallback.handleBeginTestCaseEvent(event);
LOG.debug("Started test case: %s", event.test);
StringBuilder output = new StringBuilder();
ImmutableList.Builder<TestError> exceptions = new ImmutableList.Builder<TestError>();
try {
String line;
while ((line = outputBufferedReader.readLine()) != null) {
LOG.debug("Parsing xctest case line: %s", line);
Matcher caseFinishedMatcher = XctestOutputParsing.CASE_FINISHED_PATTERN.matcher(line);
if (caseFinishedMatcher.find()) {
handleCaseFinished(caseFinishedMatcher, exceptions.build(), output.toString(), eventCallback);
return;
}
Matcher errorMatcher = XctestOutputParsing.ERROR_PATTERN.matcher(line);
if (errorMatcher.find()) {
handleError(errorMatcher, exceptions);
continue;
}
output.append(line);
output.append("\n");
}
} catch (IOException e) {
LOG.error("Error reading line");
}
LOG.error("Test case did not end!");
// Synthesize a failure to note the test crashed or stopped.
EndTestCaseEvent endEvent = new EndTestCaseEvent();
endEvent.test = event.test;
endEvent.className = event.className;
endEvent.methodName = event.methodName;
endEvent.totalDuration = 0;
endEvent.output = output.toString();
// failure
endEvent.succeeded = false;
endEvent.exceptions = exceptions.build();
eventCallback.handleEndTestCaseEvent(endEvent);
}
Aggregations