Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2907 from kageiit/buck_support
Add support for the buck build system
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
robolectric/src/main/java/org/robolectric/internal/BuckManifestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.robolectric.internal; | ||
|
||
import org.robolectric.annotation.Config; | ||
import org.robolectric.manifest.AndroidManifest; | ||
import org.robolectric.res.Fs; | ||
import org.robolectric.res.FsFile; | ||
import org.robolectric.res.ResourcePath; | ||
import org.robolectric.util.Logger; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
|
||
public class BuckManifestFactory implements ManifestFactory { | ||
|
||
private static final String BUCK_ROBOLECTRIC_RES_DIRECTORIES = "buck.robolectric_res_directories"; | ||
private static final String BUCK_ROBOLECTRIC_ASSETS_DIRECTORIES = "buck.robolectric_assets_directories"; | ||
private static final String BUCK_ROBOLECTRIC_MANIFEST = "buck.robolectric_manifest"; | ||
|
||
@Override | ||
public ManifestIdentifier identify(Config config) { | ||
String buckManifest = System.getProperty(BUCK_ROBOLECTRIC_MANIFEST); | ||
FsFile manifestFile = Fs.fileFromPath(buckManifest); | ||
return new ManifestIdentifier(manifestFile, null, null, config.packageName(), null); | ||
} | ||
|
||
@Override | ||
public AndroidManifest create(ManifestIdentifier manifestIdentifier) { | ||
String buckResDirs = System.getProperty(BUCK_ROBOLECTRIC_RES_DIRECTORIES); | ||
String buckAssetsDirs = System.getProperty(BUCK_ROBOLECTRIC_ASSETS_DIRECTORIES); | ||
String packageName = manifestIdentifier.getPackageName(); | ||
FsFile manifestFile = manifestIdentifier.getManifestFile(); | ||
|
||
final List<String> buckResources = buckResDirs == null ? null : | ||
Arrays.asList(buckResDirs.split(File.pathSeparator)); | ||
final List<String> buckAssets = buckAssetsDirs == null ? null : | ||
Arrays.asList(buckAssetsDirs.split(File.pathSeparator)); | ||
|
||
final FsFile resDir = (buckResources == null || "".equals(buckResources)) ? null : | ||
Fs.fileFromPath(buckResources.get(buckResources.size() - 1)); | ||
final FsFile assetsDir = (buckAssets == null || "".equals(buckAssets)) ? null : | ||
Fs.fileFromPath(buckAssets.get(buckAssets.size() - 1)); | ||
|
||
Logger.debug("Robolectric assets directory: " + (assetsDir == null ? null : assetsDir.getPath())); | ||
Logger.debug(" Robolectric res directory: " + (resDir == null ? null : resDir.getPath())); | ||
Logger.debug(" Robolectric manifest path: " + manifestFile.getPath()); | ||
Logger.debug(" Robolectric package name: " + packageName); | ||
|
||
return new AndroidManifest(manifestFile, resDir, assetsDir, packageName) { | ||
@Override | ||
public List<ResourcePath> getIncludedResourcePaths() { | ||
Collection<ResourcePath> resourcePaths = new LinkedHashSet<>(); // Needs stable ordering and no duplicates | ||
resourcePaths.add(super.getResourcePath()); | ||
|
||
// Add all the buck resource folders, no duplicates as they are being added to a set. | ||
if (buckResources != null) { | ||
ListIterator<String> it = buckResources.listIterator(buckResources.size()); | ||
while (it.hasPrevious()) { | ||
resourcePaths.add(new ResourcePath( | ||
getRClass(), | ||
Fs.fileFromPath(it.previous()), | ||
getAssetsDirectory())); | ||
} | ||
} | ||
return new ArrayList<>(resourcePaths); | ||
} | ||
}; | ||
} | ||
|
||
public static boolean isBuck() { | ||
return System.getProperty(BUCK_ROBOLECTRIC_MANIFEST) != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
robolectric/src/test/java/org/robolectric/internal/BuckManifestFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.robolectric.internal; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.manifest.AndroidManifest; | ||
import org.robolectric.res.FileFsFile; | ||
import org.robolectric.res.ResourcePath; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class BuckManifestFactoryTest { | ||
|
||
private Config.Builder configBuilder; | ||
private BuckManifestFactory buckManifestFactory; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
configBuilder = Config.Builder.defaults().setPackageName("com.robolectric.buck"); | ||
System.setProperty("buck.robolectric_manifest", "buck/AndroidManifest.xml"); | ||
System.setProperty("buck.robolectric_res_directories", "buck/res1:buck/res2"); | ||
System.setProperty("buck.robolectric_assets_directories", "buck/assets"); | ||
buckManifestFactory = new BuckManifestFactory(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.clearProperty("buck.robolectric_manifest"); | ||
System.clearProperty("buck.robolectric_res_directories"); | ||
System.clearProperty("buck.robolectric_assets_directories"); | ||
} | ||
|
||
@Test public void identify() throws Exception { | ||
ManifestIdentifier manifestIdentifier = buckManifestFactory.identify(configBuilder.build()); | ||
assertThat(manifestIdentifier.getManifestFile()) | ||
.isEqualTo(FileFsFile.from("buck/AndroidManifest.xml")); | ||
assertThat(manifestIdentifier.getPackageName()) | ||
.isEqualTo("com.robolectric.buck"); | ||
} | ||
|
||
@Test public void multiple_res_dirs() throws Exception { | ||
ManifestIdentifier manifestIdentifier = buckManifestFactory.identify(configBuilder.build()); | ||
AndroidManifest manifest = buckManifestFactory.create(manifestIdentifier); | ||
assertThat(manifest.getResDirectory()) | ||
.isEqualTo(FileFsFile.from("buck/res2")); | ||
assertThat(manifest.getAssetsDirectory()) | ||
.isEqualTo(FileFsFile.from("buck/assets")); | ||
|
||
List<ResourcePath> resourcePathList = manifest.getIncludedResourcePaths(); | ||
assertThat(resourcePathList.size()).isEqualTo(2); | ||
assertThat(resourcePathList).containsExactly( | ||
new ResourcePath(manifest.getRClass(), FileFsFile.from("buck/res2"), FileFsFile.from("buck/assets")), | ||
new ResourcePath(manifest.getRClass(), FileFsFile.from("buck/res1"), FileFsFile.from("buck/assets")) | ||
); | ||
} | ||
} |