Skip to content

Commit 5cfafbb

Browse files
maciekstosiokkafar
andauthored
feat: Add gradle task to automatically copy the codegen artifacts for paper (#2168)
## Description When changing native props on Fabric, codegen generates corresponding interfaces and delegates. To make sure both implementations are consistent, we implement those interfaces on Paper too. Currently, after generating interfaces using codegen, developer needs to copy corresponding files for paper manually. This task adds Gradle task, that automates this. ## Changes Add new task to build Gradle and necessary properties: - codegen artifacts dir and paper dir - flag in both fabric apps that indicates that copying should be performed (we do want this task to be performed only when developing the library) ## Test code and steps to reproduce Remove `textColor` from `src/fabric/SearchBarNativeComponent.ts` and run ` ./gradlew generateCodegenArtifactsFromSchema` in `/react-native-screens/FabricExample/android`. That should automatically copy regenerated files to paper directory. --------- Co-authored-by: Kacper Kafara <kacper.kafara@swmansion.com>
1 parent 77effdb commit 5cfafbb

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed

Example/android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ hermesEnabled=true
4949
# This is necessary, because the code checking for multiple instances
5050
# detects package versions from other example applications in the repository.
5151
disableMultipleInstancesCheck=true
52+
53+
isScreensExampleApp=true

FabricExample/android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ newArchEnabled=true
3939
# Use this property to enable or disable the Hermes JS engine.
4040
# If set to false, you will be using JSC instead.
4141
hermesEnabled=true
42+
43+
isScreensExampleApp=true

FabricTestExample/android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ newArchEnabled=true
4141
hermesEnabled=true
4242

4343
disableMultipleInstancesCheck=true
44+
45+
isScreensExampleApp=true

TVOSExample/android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ android.enableJetifier=true
2626

2727
# Version of flipper SDK to use with React Native
2828
FLIPPER_VERSION=0.75.1
29+
30+
isScreensExampleApp=true

TestsExample/android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ newArchEnabled=false
4141
hermesEnabled=true
4242

4343
disableMultipleInstancesCheck=true
44+
45+
isScreensExampleApp=true

android/build.gradle

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,79 @@ dependencies {
192192
}
193193
}
194194
}
195+
196+
def isScreensExampleApp() {
197+
return project.hasProperty('isScreensExampleApp') && project.property('isScreensExampleApp') == "true"
198+
}
199+
200+
def getAbsoluteCodegenArtifactsPaperDestination() {
201+
if (!project.hasProperty('codegenArtifactsPaperDestination')) {
202+
throw new Exception('[RNScreens] Please fill codegenArtifactsPaperDestination variable in android/gradle.properties correct path to paper paper destination')
203+
}
204+
205+
return "${project.rootDir}/../../${project.property('codegenArtifactsPaperDestination')}"
206+
}
207+
208+
def getAbsoluteCodegenArtifactsSource() {
209+
if (!project.hasProperty('codegenArtifactsSource')) {
210+
throw new Exception('[RNScreens] Please fill codegenArtifactsSource variable in android/gradle.properties correct path to codegenerated artifacts')
211+
}
212+
213+
return "${project.rootDir}/../../${project.property('codegenArtifactsSource')}"
214+
}
215+
216+
217+
tasks.register("copyCodegenArtifacts") {
218+
group 'After build tasks'
219+
description 'Tasks which copy codegen artifacts to paper architecture'
220+
221+
if (!isScreensExampleApp() || !isNewArchitectureEnabled()) {
222+
return
223+
}
224+
225+
dependsOn tasks.generateCodegenArtifactsFromSchema
226+
227+
doLast {
228+
229+
def absoluteCodegenArtifactsPaperDestination = getAbsoluteCodegenArtifactsPaperDestination()
230+
def absoluteCodegenArtifactsSource = getAbsoluteCodegenArtifactsSource()
231+
232+
def existingFiles = fileTree(absoluteCodegenArtifactsPaperDestination).matching {
233+
include '**/*.java'
234+
}
235+
236+
def generatedFiles = fileTree(absoluteCodegenArtifactsSource).matching {
237+
include '**/*.java'
238+
}
239+
240+
def existingFilesMap = [:]
241+
242+
existingFiles.forEach { existingFile ->
243+
existingFilesMap[existingFile.name] = 1
244+
}
245+
246+
generatedFiles.forEach { generatedFile ->
247+
if (!existingFilesMap.containsKey(generatedFile.name)) {
248+
logger.warn("[RNScreens] ${generatedFile.name} not found in paper dir, if it's used in Android you need to copy it manually and implement yourself before using auto-copy feature")
249+
}
250+
}
251+
252+
if (existingFiles.size() == 0) {
253+
logger.warn("[RNScreens] Paper destination with codegen interfaces is empty. This might be okay if you don't have any interfaces/delegates used in Android, if that's not the case please check if codegenArtifactsPaperDestination in android/gradle.properties is correct")
254+
}
255+
256+
if (existingFiles.size() > generatedFiles.size()) {
257+
throw new Exception("[RNScreens] Number od generated artifacts should be greather then or equal to paper interfaces. Please check if codegenArtifactsSource in android/gradle.properties is correct")
258+
}
259+
260+
copy {
261+
from absoluteCodegenArtifactsSource
262+
include existingFiles.collect { it.name }
263+
into absoluteCodegenArtifactsPaperDestination
264+
}
265+
}
266+
}
267+
268+
if (isScreensExampleApp() && isNewArchitectureEnabled()) {
269+
tasks.generateCodegenArtifactsFromSchema.finalizedBy('copyCodegenArtifacts')
270+
}

android/gradle.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ android.useAndroidX=true
2121
android.enableJetifier=true
2222

2323
kotlin.code.style=official
24+
25+
# Path to codegen output directory with this library view manager's interfaces & delegates. Used by `copyCodegenArtifacts` task that helps to synchronise newly generated files with their Paper conterparts.
26+
codegenArtifactsSource=android/build/generated/source/codegen/java/com/facebook/react/viewmanagers
27+
28+
# Path to directory with view manager's interfaces & delegates used while running on Paper architecture. This property is used as output path for `copyCodegenArtifacts` task.
29+
# Used for task (copyCodegenArtifacts) that automates copying those interfaces/delegates when codegen is run
30+
codegenArtifactsPaperDestination=android/src/paper/java/com/facebook/react/viewmanagers/

0 commit comments

Comments
 (0)