This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Generating extra class for Widget and Actions registration (#876)
* feat: Created generated class for Widgets and Actions * Remove unused import Co-authored-by: Túlio Magalhães <[email protected]>
- Loading branch information
1 parent
72fee80
commit baefa4d
Showing
6 changed files
with
188 additions
and
34 deletions.
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
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
73 changes: 73 additions & 0 deletions
73
...oid/processor/src/main/java/br/com/zup/beagle/android/compiler/RegisterActionProcessor.kt
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,73 @@ | ||
/* | ||
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package br.com.zup.beagle.android.compiler | ||
|
||
import br.com.zup.beagle.compiler.REGISTERED_ACTIONS | ||
import br.com.zup.beagle.compiler.RegisteredActionGenerator | ||
import br.com.zup.beagle.compiler.error | ||
import br.com.zup.beagle.widget.Widget | ||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import java.io.IOException | ||
import javax.annotation.processing.ProcessingEnvironment | ||
import javax.annotation.processing.RoundEnvironment | ||
|
||
const val REGISTERED_ACTIONS_GENERATED = "RegisteredActions" | ||
|
||
class RegisterActionProcessorProcessor( | ||
private val processingEnv: ProcessingEnvironment, | ||
private val registeredActionGenerator: RegisteredActionGenerator = | ||
RegisteredActionGenerator() | ||
) { | ||
|
||
fun process(packageName: String, roundEnvironment: RoundEnvironment) { | ||
val typeSpec = TypeSpec.classBuilder(REGISTERED_ACTIONS_GENERATED) | ||
.addModifiers(KModifier.PUBLIC, KModifier.FINAL) | ||
.addFunction(createRegisteredActionsFunctionInternal(roundEnvironment)) | ||
.build() | ||
|
||
try { | ||
FileSpec.builder(packageName, REGISTERED_ACTIONS_GENERATED) | ||
.addImport(Widget::class, "") | ||
.addAnnotation( | ||
AnnotationSpec.builder(Suppress::class.java) | ||
.addMember("%S", "UNCHECKED_CAST") | ||
.build() | ||
) | ||
.addType(typeSpec) | ||
.build() | ||
.writeTo(processingEnv.filer) | ||
} catch (e: IOException) { | ||
val errorMessage = "Error when trying to generate code.\n${e.message!!}" | ||
processingEnv.messager.error(errorMessage) | ||
} | ||
} | ||
|
||
private fun createRegisteredActionsFunctionInternal(roundEnvironment: RoundEnvironment): FunSpec { | ||
return registeredActionGenerator.generate(roundEnvironment) | ||
} | ||
|
||
fun createRegisteredActionsFunction(): FunSpec { | ||
return registeredActionGenerator.createFuncSpec() | ||
.addModifiers(KModifier.OVERRIDE) | ||
.addStatement("return $REGISTERED_ACTIONS_GENERATED().$REGISTERED_ACTIONS()") | ||
.build() | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...oid/processor/src/main/java/br/com/zup/beagle/android/compiler/RegisterWidgetProcessor.kt
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,73 @@ | ||
/* | ||
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package br.com.zup.beagle.android.compiler | ||
|
||
import br.com.zup.beagle.compiler.BeagleSetupRegisteredWidgetGenerator | ||
import br.com.zup.beagle.compiler.REGISTERED_WIDGETS | ||
import br.com.zup.beagle.compiler.error | ||
import br.com.zup.beagle.widget.Widget | ||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import java.io.IOException | ||
import javax.annotation.processing.ProcessingEnvironment | ||
import javax.annotation.processing.RoundEnvironment | ||
|
||
const val REGISTERED_WIDGETS_GENERATED = "RegisteredWidgets" | ||
|
||
class RegisterWidgetProcessorProcessor( | ||
private val processingEnv: ProcessingEnvironment, | ||
private val beagleSetupRegisteredWidgetGenerator: BeagleSetupRegisteredWidgetGenerator = | ||
BeagleSetupRegisteredWidgetGenerator() | ||
) { | ||
|
||
fun process(packageName: String, roundEnvironment: RoundEnvironment) { | ||
val typeSpec = TypeSpec.classBuilder(REGISTERED_WIDGETS_GENERATED) | ||
.addModifiers(KModifier.PUBLIC, KModifier.FINAL) | ||
.addFunction(createRegisteredWidgetsFunctionInternal(roundEnvironment)) | ||
.build() | ||
|
||
try { | ||
FileSpec.builder(packageName, REGISTERED_WIDGETS_GENERATED) | ||
.addImport(Widget::class, "") | ||
.addAnnotation( | ||
AnnotationSpec.builder(Suppress::class.java) | ||
.addMember("%S", "UNCHECKED_CAST") | ||
.build() | ||
) | ||
.addType(typeSpec) | ||
.build() | ||
.writeTo(processingEnv.filer) | ||
} catch (e: IOException) { | ||
val errorMessage = "Error when trying to generate code.\n${e.message!!}" | ||
processingEnv.messager.error(errorMessage) | ||
} | ||
} | ||
|
||
private fun createRegisteredWidgetsFunctionInternal(roundEnvironment: RoundEnvironment): FunSpec { | ||
return beagleSetupRegisteredWidgetGenerator.generate(roundEnvironment) | ||
} | ||
|
||
fun createRegisteredWidgetsFunction(): FunSpec { | ||
return beagleSetupRegisteredWidgetGenerator.createFuncSpec() | ||
.addModifiers(KModifier.OVERRIDE) | ||
.addStatement("return $REGISTERED_WIDGETS_GENERATED().$REGISTERED_WIDGETS()") | ||
.build() | ||
} | ||
} |
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
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