use of com.google.common.collect.ImmutableMap in project buck by facebook.
the class CxxDescriptionEnhancer method parseOnlyPlatformHeaders.
static ImmutableMap<String, SourcePath> parseOnlyPlatformHeaders(BuildTarget buildTarget, BuildRuleResolver resolver, SourcePathRuleFinder ruleFinder, SourcePathResolver sourcePathResolver, CxxPlatform cxxPlatform, String headersParameterName, SourceList headers, String platformHeadersParameterName, PatternMatchedCollection<SourceList> platformHeaders) throws NoSuchBuildTargetException {
ImmutableMap.Builder<String, SourcePath> parsed = ImmutableMap.builder();
java.util.function.Function<SourcePath, SourcePath> fixup = path -> {
try {
return CxxGenruleDescription.fixupSourcePath(resolver, ruleFinder, cxxPlatform, path);
} catch (NoSuchBuildTargetException e) {
throw new RuntimeException(e);
}
};
// Include all normal exported headers that are generated by `cxx_genrule`.
parsed.putAll(headers.toNameMap(buildTarget, sourcePathResolver, headersParameterName, path -> CxxGenruleDescription.wrapsCxxGenrule(ruleFinder, path), fixup));
// Include all platform specific headers.
for (SourceList sourceList : platformHeaders.getMatchingValues(cxxPlatform.getFlavor().toString())) {
parsed.putAll(sourceList.toNameMap(buildTarget, sourcePathResolver, platformHeadersParameterName, path -> true, fixup));
}
return parsed.build();
}
use of com.google.common.collect.ImmutableMap in project buck by facebook.
the class TargetNodeTranslator method translateNode.
/**
* @return a copy of the given {@link TargetNode} with all found {@link BuildTarget}s translated,
* or {@link Optional#empty()} if the node requires no translation.
*/
public <A> Optional<TargetNode<A, ?>> translateNode(TargetNode<A, ?> node) {
CellPathResolver cellPathResolver = node.getCellNames();
BuildTargetPatternParser<BuildTargetPattern> pattern = BuildTargetPatternParser.forBaseName(node.getBuildTarget().getBaseName());
Optional<BuildTarget> target = translateBuildTarget(node.getBuildTarget());
Optional<A> constructorArg = translateConstructorArg(cellPathResolver, pattern, node);
Optional<ImmutableSet<BuildTarget>> declaredDeps = translateSet(cellPathResolver, pattern, node.getDeclaredDeps());
Optional<ImmutableSet<BuildTarget>> extraDeps = translateSet(cellPathResolver, pattern, node.getExtraDeps());
Optional<ImmutableMap<BuildTarget, Version>> newSelectedVersions = getSelectedVersions(node.getBuildTarget());
Optional<ImmutableMap<BuildTarget, Version>> oldSelectedVersions = node.getSelectedVersions();
Optional<Optional<ImmutableMap<BuildTarget, Version>>> selectedVersions = oldSelectedVersions.equals(newSelectedVersions) ? Optional.empty() : Optional.of(newSelectedVersions);
// If nothing has changed, don't generate a new node.
if (!target.isPresent() && !constructorArg.isPresent() && !declaredDeps.isPresent() && !extraDeps.isPresent() && !selectedVersions.isPresent()) {
return Optional.empty();
}
return Optional.of(node.withTargetConstructorArgDepsAndSelectedVerisons(target.orElse(node.getBuildTarget()), constructorArg.orElse(node.getConstructorArg()), declaredDeps.orElse(node.getDeclaredDeps()), extraDeps.orElse(node.getExtraDeps()), selectedVersions.orElse(oldSelectedVersions)));
}
use of com.google.common.collect.ImmutableMap in project buck by facebook.
the class CompileStringsStepTest method testScrapePluralsNodes.
@Test
public void testScrapePluralsNodes() throws IOException, SAXException {
String xmlInput = "<plurals name='name1' gender='unknown'>" + "<item quantity='zero'>%d people saw this</item>" + "<item quantity='one'>%d person saw this</item>" + "<item quantity='many'>%d people saw this</item>" + "</plurals>" + "<plurals name='name1_f1gender' gender='female'>" + "<item quantity='zero'>%d people saw this f1</item>" + "<item quantity='one'>%d person saw this f1</item>" + "<item quantity='many'>%d people saw this f1</item>" + "</plurals>" + "<plurals name='name2' gender='unknown'>" + "<item quantity='zero'>%d people ate this</item>" + "<item quantity='many'>%d people ate this</item>" + "</plurals>" + "<plurals name='name2_m2gender' gender='male'>" + "<item quantity='zero'>%d people ate this m2</item>" + "<item quantity='many'>%d people ate this m2</item>" + "</plurals>" + // Test empty array.
"<plurals name='name3' gender='unknown'></plurals>" + // Ignored since "name2" already found.
"<plurals name='name2' gender='unknown'></plurals>";
NodeList pluralsNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("plurals");
EnumMap<Gender, ImmutableMap<String, String>> map1 = Maps.newEnumMap(Gender.class);
map1.put(Gender.unknown, ImmutableMap.of("zero", "%d people saw this", "one", "%d person saw this", "many", "%d people saw this"));
map1.put(Gender.female, ImmutableMap.of("zero", "%d people saw this f1", "one", "%d person saw this f1", "many", "%d people saw this f1"));
EnumMap<Gender, ImmutableMap<String, String>> map2 = Maps.newEnumMap(Gender.class);
map2.put(Gender.unknown, ImmutableMap.of("zero", "%d people ate this", "many", "%d people ate this"));
map2.put(Gender.male, ImmutableMap.of("zero", "%d people ate this m2", "many", "%d people ate this m2"));
EnumMap<Gender, ImmutableMap<String, String>> map3 = Maps.newEnumMap(Gender.class);
map3.put(Gender.unknown, ImmutableMap.of());
Map<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newHashMap();
CompileStringsStep step = createNonExecutingStep();
step.addPluralsResourceNameToIdMap(ImmutableMap.of("name1", 1, "name2", 2, "name3", 3));
step.scrapePluralsNodes(pluralsNodes, pluralsMap);
assertEquals("Incorrect map of resource id to plural values.", ImmutableMap.of(1, map1, 2, map2, 3, map3), pluralsMap);
}
use of com.google.common.collect.ImmutableMap in project buck by facebook.
the class CompileStringsStepTest method testScrapeNodesWithSameName.
@Test
public void testScrapeNodesWithSameName() throws IOException, SAXException {
String xmlInput = "<string name='name1' gender='unknown'>1</string>" + "<string name='name1_f1gender' gender='female'>1 f1</string>" + "<plurals name='name1' gender='unknown'>" + "<item quantity='one'>2</item>" + "<item quantity='other'>3</item>" + "</plurals>" + "<plurals name='name1_f1gender' gender='female'>" + "<item quantity='one'>2 f1</item>" + "<item quantity='other'>3 f1</item>" + "</plurals>" + "<string-array name='name1' gender='unknown'>" + "<item>4</item>" + "<item>5</item>" + "</string-array>" + "<string-array name='name1_f1gender' gender='female'>" + "<item>4 f1</item>" + "<item>5 f1</item>" + "</string-array>";
NodeList stringNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("string");
NodeList pluralsNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("plurals");
NodeList arrayNodes = XmlDomParser.parse(createResourcesXml(xmlInput)).getElementsByTagName("string-array");
Map<Integer, EnumMap<Gender, String>> stringMap = Maps.newTreeMap();
Map<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newTreeMap();
Map<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap = Maps.newTreeMap();
EnumMap<Gender, String> map1 = Maps.newEnumMap(Gender.class);
map1.put(Gender.unknown, "1");
map1.put(Gender.female, "1 f1");
EnumMap<Gender, Map<String, String>> map2 = Maps.newEnumMap(Gender.class);
map2.put(Gender.unknown, ImmutableMap.of("one", "2", "other", "3"));
map2.put(Gender.female, ImmutableMap.of("one", "2 f1", "other", "3 f1"));
EnumMap<Gender, ImmutableList<String>> map3 = Maps.newEnumMap(Gender.class);
map3.put(Gender.unknown, ImmutableList.of("4", "5"));
map3.put(Gender.female, ImmutableList.of("4 f1", "5 f1"));
CompileStringsStep step = createNonExecutingStep();
step.addStringResourceNameToIdMap(ImmutableMap.of("name1", 1));
step.addPluralsResourceNameToIdMap(ImmutableMap.of("name1", 2));
step.addArrayResourceNameToIdMap(ImmutableMap.of("name1", 3));
step.scrapeStringNodes(stringNodes, stringMap);
step.scrapePluralsNodes(pluralsNodes, pluralsMap);
step.scrapeStringArrayNodes(arrayNodes, arraysMap);
assertEquals("Incorrect map of resource id to string.", ImmutableMap.of(1, map1), stringMap);
assertEquals("Incorrect map of resource id to plurals.", ImmutableMap.of(2, map2), pluralsMap);
assertEquals("Incorrect map of resource id to string arrays.", ImmutableMap.of(3, map3), arraysMap);
}
use of com.google.common.collect.ImmutableMap in project buck by facebook.
the class AndroidNativeLibsPackageableGraphEnhancerTest method testNdkLibrary.
@Test
public void testNdkLibrary() throws Exception {
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
NdkLibrary ndkLibrary = new NdkLibraryBuilder(BuildTargetFactory.newInstance("//:ndklib")).build(ruleResolver);
BuildTarget target = BuildTargetFactory.newInstance("//:target");
BuildRuleParams originalParams = new FakeBuildRuleParamsBuilder(target).setDeclaredDeps(ImmutableSortedSet.of(ndkLibrary)).build();
APKModuleGraph apkModuleGraph = new APKModuleGraph(TargetGraph.EMPTY, target, Optional.empty());
AndroidNativeLibsPackageableGraphEnhancer enhancer = new AndroidNativeLibsPackageableGraphEnhancer(ruleResolver, originalParams, ImmutableMap.of(), ImmutableSet.of(), CxxPlatformUtils.DEFAULT_CONFIG, /* nativeLibraryMergeMap */
Optional.empty(), /* nativeLibraryMergeGlue */
Optional.empty(), AndroidBinary.RelinkerMode.DISABLED, apkModuleGraph);
AndroidPackageableCollector collector = new AndroidPackageableCollector(target, ImmutableSet.of(), ImmutableSet.of(), apkModuleGraph);
collector.addPackageables(AndroidPackageableCollector.getPackageableRules(ImmutableSet.of(ndkLibrary)));
Optional<ImmutableMap<APKModule, CopyNativeLibraries>> copyNativeLibrariesOptional = enhancer.enhance(collector.build()).getCopyNativeLibraries();
CopyNativeLibraries copyNativeLibraries = copyNativeLibrariesOptional.get().get(apkModuleGraph.getRootAPKModule());
assertThat(copyNativeLibraries.getStrippedObjectDescriptions(), Matchers.empty());
assertThat(copyNativeLibraries.getNativeLibDirectories().stream().map(sourcePathResolver::getRelativePath).collect(MoreCollectors.toImmutableList()), Matchers.contains(ndkLibrary.getLibraryPath()));
}
Aggregations