Loading resources from the manifest on android (#40)

* Adding support for forked resources in app.json

* Running pod install to update podspec

* Fixing template build

* Using yarn link before installing pods

* Using file name to find res dir
This commit is contained in:
Araz Abishov 2020-03-28 21:43:59 +01:00 коммит произвёл GitHub
Родитель e9d5d14548
Коммит ceebd15e7c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
11 изменённых файлов: 106 добавлений и 31 удалений

Просмотреть файл

@ -1,7 +1,7 @@
PODS:
- boost-for-react-native (1.63.0)
- DoubleConversion (1.1.6)
- Example-Tests (0.0.5):
- Example-Tests (0.0.6):
- React
- Folly (2018.10.22.00):
- boost-for-react-native
@ -82,7 +82,7 @@ PODS:
- React-Core (= 0.60.6)
- React-RCTWebSocket (0.60.6):
- React-Core (= 0.60.6)
- ReactTestApp-DevSupport (0.0.5)
- ReactTestApp-DevSupport (0.0.6)
- ReactTestApp-Resources (1.0.0-dev)
- SwiftLint (0.39.1)
- yoga (0.60.6.React)
@ -174,7 +174,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c
DoubleConversion: 5805e889d232975c086db112ece9ed034df7a0b2
Example-Tests: b14ab3ffce3280c74aa12c317565337b690c2363
Example-Tests: 547933002efe786a5344e5a4f2dced6c53af3786
Folly: 30e7936e1c45c08d884aa59369ed951a8e68cf51
glog: 1f3da668190260b06b429bb211bfbee5cd790c28
QRCodeReader.swift: 373a389fe9a22d513c879a32a6f647c58f4ef572
@ -195,7 +195,7 @@ SPEC CHECKSUMS:
React-RCTText: d91537e29e38dc69cf09cbca0875cf5dc7402da6
React-RCTVibration: 7655d72dfb919dd6d8e135ca108a5a2bd9fcd7b4
React-RCTWebSocket: 7cd2c8d0f8ddd680dc76404defba7ab1f56b83af
ReactTestApp-DevSupport: 803633ac5e8d24c19e7e56b54f859ddef9647a12
ReactTestApp-DevSupport: 0077760690c56aeec1d1b1fb1dfb53ae9027245b
ReactTestApp-Resources: 18abf3923397be7cd4eb2e247e954dcbf5b687e6
SwiftLint: 55e96a4a4d537d4a3156859fc1c54bd24851a046
yoga: 5079887aa3e4c62142d6bcee493022643ee4d730

Просмотреть файл

@ -6,8 +6,14 @@
"displayName": "App"
}
},
"resources": [
"dist/assets",
"dist/main.jsbundle"
]
"resources": {
"ios": [
"dist/assets",
"dist/main.jsbundle"
],
"android": [
"dist/res",
"dist/main.jsbundle"
]
}
}

Просмотреть файл

@ -1,7 +1,3 @@
# These properties are required to enable AndroidX for the test app.
android.useAndroidX=true
android.enableJetifier=true
# Assets that are expected to be copied to the test app.
testApp.bundle=dist/main.jsbundle
testApp.resources=dist/res

Просмотреть файл

@ -52,7 +52,11 @@ module.exports = (plop) => {
displayName: name,
},
},
resources: ["assets", "main.jsbundle"],
resources: [
"dist/res",
"dist/assets",
"dist/main.jsbundle"
],
},
undefined,
2
@ -144,14 +148,6 @@ module.exports = (plop) => {
type: "add",
path: `${prefix}gradle.properties`,
templateFile: path.join(androidTemplateDir, "gradle.properties"),
transform: (template) =>
[
template,
"# Assets that are expected to be copied to the test app.",
"testApp.bundle=dist/main.jsbundle",
"testApp.resources=dist/res",
"",
].join("\n"),
});
actions.push({
type: "add",

Просмотреть файл

@ -1,3 +1,4 @@
import groovy.json.JsonSlurper
import org.gradle.initialization.DefaultSettings
private static void apply(Settings settings) {
@ -21,29 +22,54 @@ ext.applyTestAppSettings = { DefaultSettings defaultSettings ->
ext.applyTestAppModule = { Project project, String packageName ->
applyNativeModulesAppBuildGradle(project, packageName)
def manifestFile = file("$rootDir/app.json")
def manifest = new JsonSlurper().parseText(manifestFile.text)
def resources = manifest["resources"]
def androidResources = null
if (resources instanceof List) {
androidResources = resources
} else if (resources.containsKey("android")) {
androidResources = resources["android"]
} else {
throw new IllegalArgumentException("The \"resources\" property of the app.json has " +
"to be either a list of paths, or must contain a nested list with the \"android\" key")
}
def androidResourceFiles = androidResources
.collect { file("$rootDir/$it") }
def androidResDir = androidResourceFiles
.find { it.isDirectory() && it.name == "res" }
androidResourceFiles.remove(androidResDir)
def generatedAssetsDir = file("${buildDir}/generated/rncli/src/main/assets/")
generatedAssetsDir.mkdirs()
def generatedResDir = file("${buildDir}/generated/rncli/src/main/res/")
generatedResDir.mkdirs()
task copyBundle(type: Copy) {
def bundlePath = project.getProperties()
.get("testApp.bundle")
task copyAssets(type: Copy) {
androidResourceFiles.each {
from it
}
from "${rootDir}/${bundlePath}"
into generatedAssetsDir
}
task copyResources(type: Copy) {
def resourcesPath = project.getProperties()
.get("testApp.resources")
if (androidResDir == null) {
throw new IllegalArgumentException(
"The res path for android must be present")
}
from "${rootDir}/${resourcesPath}"
from androidResDir
into generatedResDir
}
preBuild.dependsOn(copyBundle)
preBuild.dependsOn(copyAssets)
preBuild.dependsOn(copyResources)
android {

15
test/fixtures/with_ios_resources/app.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,15 @@
{
"name": "TestFixture",
"displayName": "TestFixture",
"components": {
"TestFixture": {
"displayName": "Test"
}
},
"resources": {
"ios": [
"dist/assets",
"dist/main.jsbundle"
]
}
}

2
test/fixtures/with_ios_resources/ios/.gitignore поставляемый Normal file
Просмотреть файл

@ -0,0 +1,2 @@
*
!.gitignore

11
test/fixtures/without_ios_resources/app.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,11 @@
{
"name": "TestFixture",
"displayName": "TestFixture",
"components": {
"TestFixture": {
"displayName": "Test"
}
},
"resources": {
}
}

2
test/fixtures/without_ios_resources/ios/.gitignore поставляемый Normal file
Просмотреть файл

@ -0,0 +1,2 @@
*
!.gitignore

Просмотреть файл

@ -24,17 +24,31 @@ class TestTestApp < Minitest::Test
project_root = File.join(__dir__, 'fixtures', 'without_resources', 'ios')
assert_nil(resources_pod(project_root))
project_root = File.join(__dir__, 'fixtures', 'without_ios_resources')
assert_nil(resources_pod(project_root))
project_root = File.join(__dir__, 'fixtures', 'without_ios_resources', 'ios')
assert_nil(resources_pod(project_root))
project_root = File.join(__dir__, 'fixtures', 'with_resources')
assert_equal('.', resources_pod(project_root))
project_root = File.join(__dir__, 'fixtures', 'with_resources', 'ios')
assert_equal('..', resources_pod(project_root))
project_root = File.join(__dir__, 'fixtures', 'with_ios_resources')
assert_equal('.', resources_pod(project_root))
project_root = File.join(__dir__, 'fixtures', 'with_ios_resources', 'ios')
assert_equal('..', resources_pod(project_root))
end
def test_resources_pod_writes_podspec
[
File.join(__dir__, 'fixtures', 'with_resources'),
File.join(__dir__, 'fixtures', 'with_resources', 'ios')
File.join(__dir__, 'fixtures', 'with_resources', 'ios'),
File.join(__dir__, 'fixtures', 'with_ios_resources'),
File.join(__dir__, 'fixtures', 'with_ios_resources', 'ios')
].each do |project_root|
begin
podspec_path = resources_pod(project_root)

Просмотреть файл

@ -38,7 +38,7 @@ def resources_pod(project_root)
app_manifest = find_file('app.json', project_root)
return if app_manifest.nil?
resources = JSON.parse(File.read(app_manifest))['resources']
resources = resolve_resources(JSON.parse(File.read(app_manifest)))
return if !resources.instance_of?(Array) || resources.empty?
spec = {
@ -59,6 +59,13 @@ def resources_pod(project_root)
Pathname.new(app_dir).relative_path_from(project_root).to_s
end
def resolve_resources(manifest)
resources = manifest['resources']
return if !resources || resources.empty?
resources.instance_of?(Array) ? resources : resources['ios']
end
def use_react_native!(project_root)
react_native = Pathname.new(resolve_module('react-native'))