├── .DS_Store ├── .circleci └── config.yml ├── .gitignore ├── LICENSE ├── README.md ├── annotation ├── build.gradle ├── gradle.properties └── src │ └── main │ └── kotlin │ └── com │ └── autodsl │ └── annotation │ ├── AutoDsl.kt │ ├── AutoDslCollection.kt │ ├── AutoDslConstructor.kt │ └── AutoDslMarker.kt ├── app ├── .DS_Store ├── build.gradle └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── autodsl │ │ │ └── app │ │ │ └── general │ │ │ └── Attempts.java │ └── kotlin │ │ └── com │ │ ├── .DS_Store │ │ └── autodsl │ │ ├── .DS_Store │ │ └── app │ │ ├── Person.kt │ │ ├── StampType.kt │ │ ├── general │ │ └── Scores.kt │ │ └── personal │ │ └── Rating.kt │ └── test │ └── kotlin │ └── com │ └── autodsl │ └── app │ ├── AutoDslJavaTest.java │ └── AutoDslTest.kt ├── bintray └── bintray.gradle ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── processor ├── build.gradle ├── gradle.properties └── src │ └── main │ └── kotlin │ └── com │ └── autodsl │ └── processor │ ├── AutoDslProcessor.kt │ ├── Constants.kt │ ├── MetadataExt.kt │ ├── ProcessingException.kt │ ├── ProcessorExt.kt │ └── internal │ ├── AutoDslClassGen.kt │ ├── AutoDslImportSpec.kt │ ├── AutoDslParam.kt │ ├── AutoDslParamGen.kt │ ├── AutoDslParamSpec.kt │ ├── NamesExt.kt │ ├── TargetConstructor.kt │ ├── TargetParameter.kt │ └── TargetType.kt ├── release-bintray.gradle ├── samples └── android-autodsl │ ├── .gitignore │ ├── README.md │ ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── autodsl │ │ │ └── sample │ │ │ ├── Anim.kt │ │ │ └── MainActivity.kt │ │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── resources │ └── showcase_anim.gif │ └── settings.gradle └── settings.gradle /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/.DS_Store -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/openjdk:8-jdk 6 | 7 | working_directory: ~/repo 8 | 9 | environment: 10 | JVM_OPTS: -Xmx3200m 11 | TERM: dumb 12 | 13 | steps: 14 | - checkout 15 | 16 | - restore_cache: 17 | key: v1-gradle-wrapper-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }} 18 | - restore_cache: 19 | key: v1-gradle-cache-{{ checksum "build.gradle" }} 20 | 21 | - run: 22 | name: Run Dependencies 23 | command: | 24 | ./gradlew dependencies 25 | 26 | - save_cache: 27 | paths: 28 | - ~/.gradle/wrapper 29 | key: v1-gradle-wrapper-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }} 30 | - save_cache: 31 | paths: 32 | - ~/.gradle/caches 33 | key: v1-gradle-cache-{{ checksum "build.gradle" }} 34 | 35 | - run: 36 | name: Run Tests 37 | command: | 38 | ./gradlew test 39 | 40 | - run: 41 | name: Assemble JAR 42 | command: | 43 | # Skip this for other nodes 44 | if [ "$CIRCLE_NODE_INDEX" == 0 ]; then 45 | ./gradlew build 46 | fi 47 | - store_artifacts: 48 | path: build/libs 49 | 50 | workflows: 51 | version: 2 52 | workflow: 53 | jobs: 54 | - build -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | **/build/ 3 | **/out/ 4 | **/out/**/* 5 | 6 | # Ignore Gradle GUI config 7 | gradle-app.setting 8 | 9 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 10 | !gradle-wrapper.jar 11 | 12 | # Cache of project 13 | .gradletasknamecache 14 | 15 | */*.iml 16 | **/*.iml 17 | */.idea/** 18 | .idea/** 19 | 20 | local.properties -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #[DEPRECATED] 2 | Unfortunately, since Kotlin 1.4.x this library doesn't work anymore. It has a strong dependency on me.eugeniomarletti.kotlin.metadata:kotlin-metadata 3 | lib which doesn't work anymore in 1.4.x to extract Kotlin Metadata. 4 | 5 | If you think this feature should be back into 1.4.x then please let me know in the Issue section, 6 | probably we could invest some time trying to achieve the same with the new Kotlin Backend. 7 | 8 | # AutoDsl for Kotlin 9 | Auto-generates [DSL (Domain Specific Language)](https://en.wikipedia.org/wiki/Domain-specific_language) 10 | for your Kotlin projects using annotations. 11 | 12 | [![CircleCI](https://circleci.com/gh/juanchosaravia/autodsl.svg?style=svg)](https://circleci.com/gh/juanchosaravia/autodsl) 13 | [ ![Download](https://api.bintray.com/packages/juanchosaravia/autodsl/io.github.juanchosaravia.autodsl%3Aprocessor/images/download.svg) ](https://bintray.com/juanchosaravia/autodsl/io.github.juanchosaravia.autodsl%3Aprocessor/_latestVersion) 14 | 15 | ## Table of Contents 16 | 1. [Documentation](#documentation) 17 | 2. [Download](#download) 18 | 19 | 20 | Create expressive, immutable and type-safe DSL **without boilerplate code**: 21 | ```kotlin 22 | person { 23 | name = "Juan" 24 | age = 34 25 | newAddress { 26 | street = "200 Celebration Bv" 27 | zipCode = 34747 28 | location { 29 | lat = 100.0 30 | lng = 100.0 31 | } 32 | } 33 | friends { 34 | +person { 35 | name = "Arturo" 36 | age = 28 37 | } 38 | +person { 39 | name = "Tiwa" 40 | age = 30 41 | } 42 | } 43 | } 44 | ``` 45 | 46 | To generate the previous DSL you just need to provide your desired classes with `@AutoDsl`: 47 | ```kotlin 48 | @AutoDsl 49 | class Person( 50 | val name: String, 51 | val age: Int, 52 | val address: Address?, 53 | val friends: List? 54 | ) 55 | 56 | @AutoDsl("newAddress") // set custom name for DSL 57 | data class Address( // can be used in data classes 58 | val street: String, 59 | val zipCode: Int, 60 | val location: Location? 61 | ) 62 | 63 | @AutoDsl 64 | class Location { 65 | val lat: Double 66 | val lng: Double 67 | 68 | constructor() { 69 | lat = 0.0 70 | lng = 0.0 71 | } 72 | 73 | // in multiple constructors you can specify which one to use. 74 | @AutoDslConstructor 75 | constructor(lat: Double, lng: Double) { 76 | this.lat = lat 77 | this.lng = lng 78 | } 79 | } 80 | ``` 81 | 82 | AutoDsl will be generating a builder class and extension function for 83 | the annotated class providing this super expressive DSL. 84 | 85 | For required parameters like `name` the DSL will throw an exception at 86 | runtime indicating exactly which field is missed. 87 | To make it optional just set the property as nullable with the 88 | question mark like `friends`. The value will be null in 89 | case it's not set. 90 | 91 | > Note: Default parameters in constructor is not currently supported 92 | as there is no way to get that value in the process to generate the code. 93 | There is a workaround that you can use explained in the 94 | [wiki page](https://github.com/juanchosaravia/autodsl/wiki#default-parameters). 95 | 96 | ## Documentation 97 | Visit the Wiki for a full list of features and more details: [AutoDsl-Wiki](https://github.com/juanchosaravia/autodsl/wiki) 98 | 99 | For more Examples 100 | - Annotation examples: [Person.kt](app/src/main/kotlin/com/autodsl/app/Person.kt) 101 | - DSL examples usage: [AutoDslTest.kt](app/src/test/kotlin/com/autodsl/app/AutoDslTest.kt) 102 | - [Android Sample](samples/android-autodsl/README.md) 103 | 104 | ## Download 105 | 106 | ##### Add JCenter repository: 107 | ```groovy 108 | repositories { 109 | jcenter() 110 | } 111 | ``` 112 | 113 | ##### Add the dependencies 114 | [ ![Download](https://api.bintray.com/packages/juanchosaravia/autodsl/io.github.juanchosaravia.autodsl%3Aprocessor/images/download.svg) ](https://bintray.com/juanchosaravia/autodsl/io.github.juanchosaravia.autodsl%3Aprocessor/_latestVersion) 115 | ```groovy 116 | dependencies { 117 | api "io.github.juanchosaravia.autodsl:annotation:latest_version" 118 | kapt "io.github.juanchosaravia.autodsl:processor:latest_version" 119 | } 120 | ``` 121 | 122 | ## Debug 123 | If you want to debug the processor do the following steps: 124 | 125 | 1. Run this command: 126 | ```text 127 | ./gradlew clean :app:build --no-daemon -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy="in-process" -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n" 128 | ``` 129 | 2. In IntelliJ Idea go to Tools > Edit Configurations > press "+" icon in the left top corner. 130 | Add a new "Remote". Set a Name and check the "Single instance only" flag to true. 131 | 3. Press "Debug" button to run the newly created "Remote" configuration. 132 | 133 | ## Publish 134 | * Update version in release-brintray.gradle file: 135 | ```text 136 | libraryVersion = 'x.y.z' 137 | ``` 138 | * Setup bintray user and pass in local.properties: 139 | ```text 140 | bintray.user=username 141 | bintray.apikey=apikey 142 | ``` 143 | * Run: 144 | ``` 145 | ./gradlew :annotation:bintrayUpload 146 | ./gradlew :processor:bintrayUpload 147 | ``` 148 | 149 | 150 | ## License 151 | 152 | Copyright 2018 Juan Ignacio Saravia 153 | 154 | Licensed under the Apache License, Version 2.0 (the "License"); 155 | you may not use this file except in compliance with the License. 156 | You may obtain a copy of the License at 157 | 158 | http://www.apache.org/licenses/LICENSE-2.0 159 | 160 | Unless required by applicable law or agreed to in writing, software 161 | distributed under the License is distributed on an "AS IS" BASIS, 162 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163 | See the License for the specific language governing permissions and 164 | limitations under the License. 165 | -------------------------------------------------------------------------------- /annotation/build.gradle: -------------------------------------------------------------------------------- 1 | // leave this at the end 2 | apply from: rootProject.file('release-bintray.gradle') -------------------------------------------------------------------------------- /annotation/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_ARTIFACT_ID=annotation 2 | POM_NAME=AutoDsl Annotation -------------------------------------------------------------------------------- /annotation/src/main/kotlin/com/autodsl/annotation/AutoDsl.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.annotation 17 | 18 | /** 19 | * Use it in a class to generate the DSL. 20 | */ 21 | @Retention(AnnotationRetention.SOURCE) 22 | @Target(AnnotationTarget.CLASS) 23 | annotation class AutoDsl(val dslName: String = "") -------------------------------------------------------------------------------- /annotation/src/main/kotlin/com/autodsl/annotation/AutoDslCollection.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.annotation 17 | 18 | import kotlin.reflect.KClass 19 | 20 | /** 21 | * Indicates to create a DSL for the annotated Collection property with the specified [concreteType]. 22 | * Optional: [inline] will provides a unaryPlus for this collection at the Builder level. 23 | */ 24 | @Retention(AnnotationRetention.SOURCE) 25 | @Target(AnnotationTarget.VALUE_PARAMETER) 26 | annotation class AutoDslCollection(val concreteType: KClass<*>, 27 | val inline: Boolean = false) -------------------------------------------------------------------------------- /annotation/src/main/kotlin/com/autodsl/annotation/AutoDslConstructor.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.annotation 17 | 18 | /** 19 | * AutoDsl to indicate the constructor to be used in a class. 20 | */ 21 | @Retention(AnnotationRetention.SOURCE) 22 | @Target(AnnotationTarget.CONSTRUCTOR) 23 | annotation class AutoDslConstructor() -------------------------------------------------------------------------------- /annotation/src/main/kotlin/com/autodsl/annotation/AutoDslMarker.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.annotation 17 | 18 | @DslMarker 19 | annotation class AutoDslMarker -------------------------------------------------------------------------------- /app/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/app/.DS_Store -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | implementation project(":annotation") 3 | kapt project(":processor") 4 | 5 | testImplementation 'junit:junit:4.12' 6 | } -------------------------------------------------------------------------------- /app/src/main/java/com/autodsl/app/general/Attempts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.app.general; 17 | 18 | public class Attempts { 19 | 20 | private final int counter; 21 | 22 | public Attempts(int counter) { 23 | this.counter = counter; 24 | } 25 | 26 | public int getCounter() { 27 | return counter; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/kotlin/com/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/app/src/main/kotlin/com/.DS_Store -------------------------------------------------------------------------------- /app/src/main/kotlin/com/autodsl/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/app/src/main/kotlin/com/autodsl/.DS_Store -------------------------------------------------------------------------------- /app/src/main/kotlin/com/autodsl/app/Person.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.app 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | import com.autodsl.annotation.AutoDslCollection 20 | import com.autodsl.annotation.AutoDslConstructor 21 | import java.util.* 22 | 23 | @AutoDsl // indicates to create an associated DSL for this class 24 | class Person( 25 | val name: String, 26 | val age: Int, 27 | val address: Address?, 28 | val friends: List?, 29 | @AutoDslCollection(concreteType = TreeSet::class) // specify concrete type 30 | val keys: Set?, 31 | val contact: Contact? 32 | ) { 33 | @AutoDsl 34 | class Contact(val name: String) 35 | } 36 | 37 | @AutoDsl("newAddress") 38 | data class Address( // can be used in data classes 39 | val street: String, 40 | val zipCode: Int, 41 | internal val location: Location? // supports internal fields 42 | ) 43 | 44 | @AutoDsl 45 | class Location { 46 | val lat: Double 47 | val lng: Double 48 | 49 | constructor() { 50 | lat = 0.0 51 | lng = 0.0 52 | } 53 | 54 | // in multiple constructors you can specify which one to use. 55 | @AutoDslConstructor 56 | constructor(lat: Double, lng: Double) { 57 | this.lat = lat 58 | this.lng = lng 59 | } 60 | } 61 | 62 | @AutoDsl 63 | internal class Box( // supports internal classes 64 | val items: Set, 65 | @AutoDslCollection(concreteType = LinkedList::class, inline = true) // custom concrete type 66 | val stamps: List? 67 | ) 68 | 69 | @AutoDsl 70 | internal class Stamp( 71 | val names: List, 72 | val type: StampType? 73 | ) -------------------------------------------------------------------------------- /app/src/main/kotlin/com/autodsl/app/StampType.kt: -------------------------------------------------------------------------------- 1 | package com.autodsl.app 2 | 3 | import com.autodsl.annotation.AutoDsl 4 | 5 | internal sealed class StampType 6 | @AutoDsl 7 | internal class GoldStamp(val price: Double): StampType() 8 | internal object MetalStamp : StampType() 9 | internal object BronzeStamp : StampType() -------------------------------------------------------------------------------- /app/src/main/kotlin/com/autodsl/app/general/Scores.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.app.general 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | import com.autodsl.app.personal.Rating 20 | 21 | @AutoDsl 22 | class Scores( 23 | val points: Double, 24 | val rating: Rating, 25 | val attempts: Attempts? 26 | ) -------------------------------------------------------------------------------- /app/src/main/kotlin/com/autodsl/app/personal/Rating.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.app.personal 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | 20 | @AutoDsl 21 | class Rating( 22 | val stars: Int 23 | ) -------------------------------------------------------------------------------- /app/src/test/kotlin/com/autodsl/app/AutoDslJavaTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.app; 17 | 18 | import org.junit.Test; 19 | 20 | public class AutoDslJavaTest { 21 | 22 | @Test 23 | public void testPerson() { 24 | new PersonAutoDslBuilder() 25 | .withName("Juan") 26 | .withAge(12) 27 | .withAddress(new AddressAutoDslBuilder() 28 | .withStreet("200 Celebration Bv") 29 | .withZipCode(34747) 30 | .build()) 31 | .build(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/test/kotlin/com/autodsl/app/AutoDslTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.app 17 | 18 | import com.autodsl.app.general.Attempts 19 | import com.autodsl.app.general.scores 20 | import junit.framework.TestCase.assertEquals 21 | import junit.framework.TestCase.assertTrue 22 | import org.junit.Test 23 | import java.io.InvalidObjectException 24 | import java.util.* 25 | 26 | class AutoDslTest { 27 | 28 | @Test 29 | fun builderPersonTest() { 30 | val me = person { 31 | name = "Juan" 32 | age = 34 33 | newAddress { 34 | street = "200 Celebration Bv" 35 | zipCode = 34747 36 | location { 37 | lat = 100.0 38 | lng = 100.0 39 | } 40 | } 41 | friends { 42 | +person { 43 | name = "Arturo" 44 | age = 30 45 | } 46 | +person { 47 | name = "Tiwa" 48 | age = 31 49 | } 50 | } 51 | keys = setOf("home_key", "work_key") 52 | contact { name = "Test" } 53 | } 54 | assertEquals(2, me.friends?.size) 55 | assertEquals("Test", me.contact?.name) 56 | assertEquals("Juan", me.name) 57 | assertEquals(34, me.age) 58 | assertEquals(34747, me.address?.zipCode) 59 | } 60 | 61 | @Test 62 | fun encapsulatedListOfStrings() { 63 | val box = box { 64 | items { 65 | +"Hello" 66 | +"World" 67 | } 68 | } 69 | assertEquals(2, box.items.size) 70 | } 71 | 72 | @Test 73 | fun definedInlinedStamps() { 74 | val usaStamp = "USA" 75 | val box = box { 76 | items { 77 | +"Hello" 78 | +"World" 79 | } 80 | +stamp { 81 | names { 82 | +usaStamp 83 | +"ARG" 84 | } 85 | } 86 | } 87 | assertEquals(2, box.items.size) 88 | assertEquals(usaStamp, box.stamps?.first()?.names?.first()) 89 | } 90 | 91 | @Test 92 | fun sealedClassTest() { 93 | val box = box { 94 | items { 95 | +"Hello World" 96 | } 97 | +stamp { 98 | names { 99 | +"ARG" 100 | } 101 | type = goldStamp { 102 | price = 15.0 103 | } 104 | } 105 | } 106 | 107 | when (val stampType = box.stamps?.first()?.type) { 108 | is GoldStamp -> assertEquals(15.0, stampType.price) 109 | else -> throw InvalidObjectException("incorrect type solved.") 110 | } 111 | } 112 | 113 | @Test 114 | fun validStructureFromDifferentPackages() { 115 | scores { 116 | points = 5.0 117 | rating { 118 | stars = 5 119 | } 120 | attempts = Attempts(1) 121 | } 122 | } 123 | 124 | @Test 125 | fun interopWithJavaClass() { 126 | val result = scores { 127 | points = 5.0 128 | rating { 129 | stars = 5 130 | } 131 | attempts = Attempts(1) 132 | } 133 | 134 | assertEquals(1, result.attempts?.counter) 135 | } 136 | 137 | @Test 138 | fun validStructureWithNoLocationAddress() { 139 | newAddress { 140 | street = "Street" 141 | zipCode = 1000 142 | } 143 | } 144 | 145 | @Test(expected = IllegalStateException::class) 146 | fun invalidAddressStructure() { 147 | newAddress { 148 | street = "Street" 149 | location = Location() 150 | } 151 | } 152 | 153 | @Test(expected = IllegalStateException::class) 154 | fun invalidPersonStructure() { 155 | person { 156 | name = "Pepe" 157 | } 158 | } 159 | 160 | @Test 161 | fun validateCustomCollectionType() { 162 | val box = box { 163 | items { 164 | +"Hello" 165 | +"World" 166 | } 167 | +stamp { 168 | names { 169 | +"ARG" 170 | } 171 | type = MetalStamp 172 | } 173 | +stamp { 174 | names { 175 | +"ARG" 176 | } 177 | type = BronzeStamp 178 | } 179 | } 180 | 181 | assertTrue(box.stamps is LinkedList) 182 | assertTrue(box.stamps?.first()?.type == MetalStamp) 183 | assertTrue(box.stamps?.last()?.type == BronzeStamp) 184 | } 185 | } -------------------------------------------------------------------------------- /bintray/bintray.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | apply plugin: 'maven-publish' 3 | 4 | version = libraryVersion 5 | group = publishedGroupId 6 | 7 | task sourcesJar(type: Jar) { 8 | classifier 'sources' 9 | from sourceSets.main.allSource 10 | } 11 | 12 | javadoc.failOnError = false 13 | task javadocJar(type: Jar, dependsOn: javadoc) { 14 | classifier = 'javadoc' 15 | from javadoc.destinationDir 16 | } 17 | 18 | artifacts { 19 | archives sourcesJar 20 | archives javadocJar 21 | } 22 | 23 | def pomConfig = { 24 | licenses { 25 | license { 26 | name licenseName 27 | url licenseUrl 28 | distribution licenseDist 29 | } 30 | } 31 | developers { 32 | developer { 33 | id developerId 34 | name developerName 35 | email developerEmail 36 | } 37 | } 38 | scm { 39 | connection gitUrl 40 | developerConnection gitUrl 41 | url siteUrl 42 | } 43 | } 44 | 45 | publishing { 46 | publications { 47 | mavenPublication(MavenPublication) { 48 | from components.java 49 | artifact sourcesJar 50 | artifact javadocJar 51 | pom.withXml { 52 | def root = asNode() 53 | root.appendNode('description', libraryDescription) 54 | root.appendNode('name', libraryName) 55 | root.appendNode('url', siteUrl) 56 | root.children().last() + pomConfig 57 | } 58 | } 59 | } 60 | } 61 | 62 | bintrayUpload.dependsOn assemble 63 | bintrayUpload.dependsOn sourcesJar 64 | bintrayUpload.dependsOn javadocJar 65 | 66 | artifacts { 67 | archives javadocJar 68 | archives sourcesJar 69 | } 70 | 71 | def bintrayUser = System.getenv("bintray_user") 72 | def bintrayKey = System.getenv("bintray_apikey") 73 | 74 | Properties properties = new Properties() 75 | def propertiesFile = project.rootProject.file('local.properties') 76 | if (propertiesFile.exists()) { 77 | properties.load(propertiesFile.newDataInputStream()) 78 | bintrayUser = properties.getProperty("bintray.user") 79 | bintrayKey = properties.getProperty("bintray.apikey") 80 | } 81 | 82 | bintray { 83 | user = bintrayUser 84 | key = bintrayKey 85 | 86 | publish = true 87 | override = false 88 | publications = ['mavenPublication'] 89 | 90 | pkg { 91 | repo = bintrayRepo 92 | name = bintrayName 93 | desc = libraryDescription 94 | 95 | websiteUrl = siteUrl 96 | vcsUrl = gitUrl 97 | licenses = allLicenses 98 | publicDownloadNumbers = true 99 | version { 100 | desc = libraryDescription 101 | released = new Date() 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | jcenter() 5 | } 6 | } 7 | plugins { 8 | id 'org.jetbrains.kotlin.jvm' version "1.3.0" 9 | id "org.jetbrains.kotlin.kapt" version "1.3.0" 10 | id "com.jfrog.bintray" version "1.8.4" 11 | } 12 | allprojects { 13 | apply plugin: 'org.jetbrains.kotlin.jvm' 14 | apply plugin: 'org.jetbrains.kotlin.kapt' 15 | 16 | repositories { 17 | mavenCentral() 18 | } 19 | dependencies { 20 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" 21 | } 22 | compileKotlin { 23 | kotlinOptions.jvmTarget = "1.8" 24 | } 25 | compileTestKotlin { 26 | kotlinOptions.jvmTarget = "1.8" 27 | } 28 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Oct 26 14:15:01 EDT 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /processor/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | implementation project(":annotation") 3 | 4 | // kotlin metadata 5 | implementation "me.eugeniomarletti.kotlin.metadata:kotlin-metadata:1.4.0" 6 | implementation files("${System.properties['java.home']}/../lib/tools.jar") 7 | implementation 'com.squareup:kotlinpoet:1.4.3' 8 | implementation "com.google.auto.service:auto-service:1.0-rc7" 9 | kapt "com.google.auto.service:auto-service:1.0-rc7" 10 | 11 | testImplementation group: 'junit', name: 'junit', version: '4.4' 12 | } 13 | 14 | // leave this at the end 15 | apply from: rootProject.file('release-bintray.gradle') -------------------------------------------------------------------------------- /processor/gradle.properties: -------------------------------------------------------------------------------- 1 | POM_ARTIFACT_ID=processor 2 | POM_NAME=AutoDsl Processor -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/AutoDslProcessor.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | import com.autodsl.processor.internal.TargetType 20 | import com.autodsl.processor.internal.generateClass 21 | import com.google.auto.service.AutoService 22 | import me.eugeniomarletti.kotlin.metadata.KotlinClassMetadata 23 | import me.eugeniomarletti.kotlin.metadata.KotlinMetadata 24 | import me.eugeniomarletti.kotlin.metadata.kotlinMetadata 25 | import javax.annotation.processing.AbstractProcessor 26 | import javax.annotation.processing.Processor 27 | import javax.annotation.processing.RoundEnvironment 28 | import javax.annotation.processing.SupportedOptions 29 | import javax.lang.model.SourceVersion 30 | import javax.lang.model.element.TypeElement 31 | 32 | 33 | /** 34 | * AutoDsl Processor 35 | */ 36 | @AutoService(Processor::class) 37 | @SupportedOptions(AutoDslProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME) 38 | class AutoDslProcessor : AbstractProcessor() { 39 | 40 | override fun process(annotations: MutableSet, roundEnv: RoundEnvironment): Boolean { 41 | roundEnv.getElementsAnnotatedWith(AutoDsl::class.java) 42 | .asSequence() 43 | .map { it as TypeElement } 44 | .forEach { classElement -> 45 | val typeMetadata: KotlinMetadata? = classElement.kotlinMetadata 46 | if (typeMetadata !is KotlinClassMetadata) { 47 | processingEnv.error( 48 | classElement, 49 | "@AutoDsl must be used in a Kotlin class, cannot be used in $classElement" 50 | ) 51 | return@forEach 52 | } 53 | 54 | try { 55 | val targetType = TargetType.get( 56 | processingEnv.messager, 57 | processingEnv.elementUtils, 58 | classElement 59 | ) ?: return@forEach 60 | 61 | processingEnv.generateClass(targetType) 62 | } catch (pe: ProcessingException) { 63 | processingEnv.error(pe) 64 | return@forEach 65 | } catch (e: Throwable) { 66 | processingEnv.error( 67 | classElement, 68 | "There was an error while processing your annotated classes. error = ${e.message.orEmpty()}" 69 | ) 70 | return@forEach 71 | } 72 | } 73 | return true // false=continue; true=exit process 74 | } 75 | 76 | override fun getSupportedAnnotationTypes(): MutableSet { 77 | return mutableSetOf(AutoDsl::class.java.canonicalName) 78 | } 79 | 80 | override fun getSupportedSourceVersion(): SourceVersion { 81 | return SourceVersion.latestSupported() 82 | } 83 | 84 | companion object { 85 | const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated" 86 | } 87 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/Constants.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor 17 | 18 | object Constants { 19 | const val LIST_TYPE_NAME = "kotlin.collections.List" 20 | const val MUTABLE_LIST_TYPE_NAME = "kotlin.collections.MutableList" 21 | 22 | const val SET_TYPE_NAME = "kotlin.collections.Set" 23 | const val MUTABLE_SET_TYPE_NAME = "kotlin.collections.MutableSet" 24 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/MetadataExt.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor 17 | 18 | import com.squareup.kotlinpoet.* 19 | import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy 20 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf 21 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Type 22 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.TypeParameter 23 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.deserialization.NameResolver 24 | 25 | internal fun ProtoBuf.TypeParameter.asTypeName( 26 | nameResolver: NameResolver, 27 | getTypeParameter: (index: Int) -> ProtoBuf.TypeParameter, 28 | resolveAliases: Boolean = false 29 | ): TypeVariableName { 30 | return TypeVariableName( 31 | name = nameResolver.getString(name), 32 | bounds = *(upperBoundList.map { 33 | it.asTypeName(nameResolver, getTypeParameter, resolveAliases) 34 | } 35 | .toTypedArray()), 36 | variance = variance.asKModifier() 37 | ) 38 | } 39 | 40 | internal fun ProtoBuf.TypeParameter.Variance.asKModifier(): KModifier? { 41 | return when (this) { 42 | ProtoBuf.TypeParameter.Variance.IN -> KModifier.IN 43 | ProtoBuf.TypeParameter.Variance.OUT -> KModifier.OUT 44 | ProtoBuf.TypeParameter.Variance.INV -> null 45 | } 46 | } 47 | 48 | /** 49 | * Returns the TypeName of this typeInfo as it would be seen in the source code, including nullability 50 | * and generic typeInfo parameters. 51 | * 52 | * @param [nameResolver] a [NameResolver] instance from the source proto 53 | * @param [getTypeParameter] a function that returns the typeInfo parameter for the given index. **Only 54 | * called if [ProtoBuf.Type.hasTypeParameter] is true!** 55 | */ 56 | internal fun ProtoBuf.Type.asTypeName( 57 | nameResolver: NameResolver, 58 | getTypeParameter: (index: Int) -> ProtoBuf.TypeParameter, 59 | useAbbreviatedType: Boolean = true 60 | ): TypeName { 61 | 62 | val argumentList = when { 63 | useAbbreviatedType && hasAbbreviatedType() -> abbreviatedType.argumentList 64 | else -> argumentList 65 | } 66 | 67 | if (hasFlexibleUpperBound()) { 68 | return WildcardTypeName.consumerOf( 69 | flexibleUpperBound.asTypeName(nameResolver, getTypeParameter, useAbbreviatedType) 70 | ) 71 | } else if (hasOuterType()) { 72 | return WildcardTypeName.consumerOf( 73 | outerType.asTypeName(nameResolver, getTypeParameter, useAbbreviatedType) 74 | ) 75 | .asNullableIf(nullable) 76 | } 77 | 78 | val realType = when { 79 | hasTypeParameter() -> return getTypeParameter(typeParameter) 80 | .asTypeName(nameResolver, getTypeParameter, useAbbreviatedType) 81 | .asNullableIf(nullable) 82 | hasTypeParameterName() -> typeParameterName 83 | useAbbreviatedType && hasAbbreviatedType() -> abbreviatedType.typeAliasName 84 | else -> className 85 | } 86 | 87 | var typeName: TypeName = 88 | ClassName.bestGuess( 89 | nameResolver.getString(realType) 90 | .replace("/", ".") 91 | ) 92 | 93 | if (argumentList.isNotEmpty()) { 94 | val remappedArgs: Array = argumentList.map { argumentType -> 95 | val nullableProjection = if (argumentType.hasProjection()) { 96 | argumentType.projection 97 | } else null 98 | if (argumentType.hasType()) { 99 | argumentType.type.asTypeName(nameResolver, getTypeParameter, useAbbreviatedType) 100 | .let { argumentTypeName -> 101 | nullableProjection?.let { projection -> 102 | when (projection) { 103 | ProtoBuf.Type.Argument.Projection.IN -> WildcardTypeName.consumerOf(argumentTypeName) 104 | ProtoBuf.Type.Argument.Projection.OUT -> { 105 | if (argumentTypeName == ANY) { 106 | // This becomes a *, which we actually don't want here. 107 | // List works with List<*>, but List<*> doesn't work with List 108 | argumentTypeName 109 | } else { 110 | WildcardTypeName.consumerOf(argumentTypeName) 111 | } 112 | } 113 | ProtoBuf.Type.Argument.Projection.STAR -> WildcardTypeName.consumerOf(ANY) 114 | ProtoBuf.Type.Argument.Projection.INV -> TODO("INV projection is unsupported") 115 | } 116 | } ?: argumentTypeName 117 | } 118 | } else { 119 | WildcardTypeName.consumerOf(ANY) 120 | } 121 | }.toTypedArray() 122 | typeName = (typeName as ClassName).parameterizedBy(*remappedArgs) 123 | } 124 | 125 | return typeName.asNullableIf(nullable) 126 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/ProcessingException.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor 17 | 18 | import javax.lang.model.element.Element 19 | 20 | internal class ProcessingException( 21 | val element: Element, 22 | msg: String, 23 | vararg args: String 24 | ) : Exception(String.format(msg, args)) -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/ProcessorExt.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | import com.squareup.kotlinpoet.ClassName 20 | import com.squareup.kotlinpoet.TypeName 21 | import javax.annotation.processing.ProcessingEnvironment 22 | import javax.lang.model.element.Element 23 | import javax.tools.Diagnostic 24 | 25 | internal fun ProcessingEnvironment.getClassName(element: Element, className: String): ClassName { 26 | return ClassName(elementUtils.getPackageOf(element).toString(), className) 27 | } 28 | 29 | internal fun ProcessingEnvironment.error(e: ProcessingException) { 30 | this.error(e.element, e.message ?: "There was an error processing this element.") 31 | } 32 | 33 | internal fun ProcessingEnvironment.error(e: Element, msg: String, vararg args: String) { 34 | messager?.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), e) 35 | } 36 | 37 | internal fun ProcessingEnvironment.getGeneratedSourcesRoot(): String { 38 | return this.options[AutoDslProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME] 39 | ?: throw IllegalStateException("No source root for generated file") 40 | } 41 | 42 | internal fun AutoDsl?.getDslNameOrDefault(defaultString: String): String { 43 | return if (this == null || dslName.isEmpty()) { 44 | defaultString 45 | } else { 46 | dslName 47 | } 48 | } 49 | 50 | internal fun TypeName.asNullableIf(condition: Boolean): TypeName { 51 | return if (condition) copy(nullable = true) else this 52 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/AutoDslClassGen.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | import com.autodsl.annotation.AutoDslMarker 20 | import com.autodsl.processor.* 21 | import com.squareup.kotlinpoet.* 22 | import me.eugeniomarletti.kotlin.metadata.KotlinClassMetadata 23 | import me.eugeniomarletti.kotlin.metadata.kotlinMetadata 24 | import java.io.File 25 | import javax.annotation.processing.ProcessingEnvironment 26 | 27 | /** 28 | * Generates code for AutoDsl. 29 | */ 30 | internal fun ProcessingEnvironment.generateClass(targetType: TargetType) { 31 | val classElement = targetType.element 32 | val packageOfClass = this.elementUtils.getPackageOf(classElement).toString() 33 | val builderClassName = targetType.builderName 34 | 35 | val generatedSourcesRoot: String = getGeneratedSourcesRoot() 36 | if (generatedSourcesRoot.isEmpty()) { 37 | throw ProcessingException( 38 | classElement, 39 | "Can't find the target directory for generated Kotlin files." 40 | ) 41 | } 42 | val file = File(generatedSourcesRoot) 43 | file.mkdir() 44 | val fileSpec = FileSpec.builder(packageOfClass, builderClassName) 45 | 46 | val classBuilderClassName = getClassName(classElement, builderClassName) 47 | // create builder class 48 | val classBuilder = TypeSpec.classBuilder(builderClassName) 49 | .primaryConstructor(FunSpec.constructorBuilder().build()) 50 | .addAnnotation(AutoDslMarker::class) 51 | 52 | if (targetType.isInternal) { 53 | classBuilder.addModifiers(KModifier.INTERNAL) 54 | } 55 | 56 | 57 | val typeMetadata: KotlinClassMetadata = classElement.kotlinMetadata as? KotlinClassMetadata ?: return 58 | 59 | // setup properties from the available constructor 60 | targetType.constructor.parameters.forEach { param -> 61 | val builder = 62 | generateParamCode( 63 | AutoDslParam( 64 | param.key, param.value, 65 | targetType.proto, typeMetadata.data.nameResolver 66 | ), classBuilderClassName 67 | ) 68 | builder.imports.forEach { fileSpec.addImport(it.packageName, it.name) } 69 | builder.properties.forEach { classBuilder.addProperty(it) } 70 | builder.functions.forEach { classBuilder.addFunction(it) } 71 | builder.types.forEach { classBuilder.addType(it) } 72 | } 73 | 74 | val classElementTypeName = classElement.asType().asTypeName() 75 | // add build function to create real object 76 | val buildFunSpec = targetType.createBuildFun(classElementTypeName) 77 | classBuilder.addFunction(buildFunSpec) 78 | 79 | // create extension function for DSL 80 | val extFun = targetType.createDslExtFun(classBuilderClassName, classElementTypeName) 81 | 82 | fileSpec.addFunction(extFun.build()) 83 | .addComment("Code generated by AutoDsl. Do not edit.") 84 | .addType(classBuilder.build()) 85 | .build().writeTo(file) 86 | } 87 | 88 | private fun TargetType.createBuildFun(classElementTypeName: TypeName): FunSpec { 89 | return FunSpec.builder("build") 90 | .returns(classElementTypeName) 91 | // todo loop could be improved 92 | .addStatement("return $classElementTypeName(${constructor.parameters.keys.joinToString { it }})") 93 | .build() 94 | } 95 | 96 | private fun TargetType.createDslExtFun( 97 | classBuilderClassName: ClassName, 98 | classElementTypeName: TypeName 99 | ): FunSpec.Builder { 100 | val extensionFunParams = ParameterSpec.builder( 101 | BLOCK_FUN_NAME, 102 | LambdaTypeName.get( 103 | receiver = classBuilderClassName, 104 | returnType = Unit::class.asTypeName() 105 | ) 106 | ).build() 107 | 108 | val classElementAnnotation = element.getAnnotation(AutoDsl::class.java) 109 | val extFunName = classElementAnnotation.getDslNameOrDefault(element.simpleName.toString().decapitalize()) 110 | val extFun = FunSpec.builder(extFunName) 111 | .addParameter(extensionFunParams) 112 | .returns(classElementTypeName) 113 | .addStatement("return $builderName().apply($BLOCK_FUN_NAME).build()") 114 | 115 | if (isInternal) { 116 | extFun.addModifiers(KModifier.INTERNAL) 117 | } 118 | return extFun 119 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/AutoDslImportSpec.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | internal class AutoDslImportSpec( 19 | val packageName: String, 20 | val name: String 21 | ) -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/AutoDslParam.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import com.autodsl.annotation.AutoDsl 19 | import com.autodsl.annotation.AutoDslCollection 20 | import com.autodsl.processor.asTypeName 21 | import com.sun.tools.javac.code.Symbol 22 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf 23 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.deserialization.NameResolver 24 | 25 | internal class AutoDslParam( 26 | val name: String, 27 | param: TargetParameter, 28 | protoClass: ProtoBuf.Class, 29 | nameResolver: NameResolver 30 | ) { 31 | val element = param.element 32 | val typeInfo = AutoDslParamType(element as Symbol.VarSymbol) 33 | val typeName = param.proto.type.asTypeName(nameResolver, protoClass::getTypeParameter) 34 | 35 | fun isNullable() = typeName.isNullable 36 | fun getAutoDslCollectionAnnotation(): AutoDslCollection? = element.getAnnotation(AutoDslCollection::class.java) 37 | } 38 | 39 | class AutoDslParamType( 40 | param: Symbol.VarSymbol 41 | ) { 42 | val element: Symbol.TypeSymbol = param.asType().asElement() 43 | val name = element.simpleName.toString() 44 | val autoDslAnnotation: AutoDsl? = element.getAnnotation(AutoDsl::class.java) 45 | } -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/AutoDslParamGen.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import com.autodsl.annotation.AutoDslMarker 19 | import com.autodsl.processor.* 20 | import com.squareup.kotlinpoet.* 21 | import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter 22 | import javax.annotation.processing.ProcessingEnvironment 23 | import javax.lang.model.element.TypeElement 24 | import javax.lang.model.type.DeclaredType 25 | import javax.lang.model.type.MirroredTypeException 26 | import kotlin.properties.Delegates 27 | 28 | /** 29 | * Generates code for [AutoDslParam]. 30 | */ 31 | internal fun ProcessingEnvironment.generateParamCode( 32 | param: AutoDslParam, 33 | builderClassName: ClassName 34 | ): AutoDslParamSpec { 35 | 36 | val properties = mutableListOf(createPropertySpec(param).build()) 37 | val imports = mutableListOf() 38 | val functions = mutableListOf() 39 | val types = mutableListOf() 40 | 41 | // check param has an associated auto-generated builder and create DSL function 42 | createFunIfAnnotatedWithAutoDsl( 43 | param, 44 | builderClassName, 45 | this 46 | )?.let { 47 | imports.add(it.importData) 48 | functions.add(it.funSpec) 49 | } 50 | 51 | try { 52 | val autoDslCollectionData = 53 | createFunIfAnnotatedWithCollection(param, builderClassName) 54 | if (autoDslCollectionData != null) { 55 | autoDslCollectionData.nestedClass?.let { 56 | types.add(it) 57 | } 58 | autoDslCollectionData.propertySpec?.let { 59 | properties.add(it) 60 | } 61 | functions.add(autoDslCollectionData.collectionFun) 62 | } else { 63 | // if not annotated then try to check for default supported collections 64 | createFunIfSupportedCollectionAndNoAnnotation(param, builderClassName)?.let { data -> 65 | data.propertySpec?.let { properties.add(it) } 66 | data.nestedClass?.let { types.add(it) } 67 | functions.add(data.collectionFun) 68 | } 69 | } 70 | } catch (e: ProcessingException) { 71 | error(e) 72 | } 73 | 74 | // creates function for builder to be used in Java: "withVariable(..) = this.apply { .. } " 75 | functions.add(createWithFun(param, builderClassName).build()) 76 | 77 | return AutoDslParamSpec(imports, properties, functions, types) 78 | } 79 | 80 | private fun createWithFun( 81 | param: AutoDslParam, 82 | builderClassName: ClassName 83 | ): FunSpec.Builder { 84 | val paramName = param.name 85 | return FunSpec.builder("with${paramName.capitalize()}") 86 | .addParameter(paramName, param.typeName) 87 | .returns(builderClassName) 88 | .addStatement("return this.apply·{ this.$paramName·=·$paramName}") 89 | } 90 | 91 | private fun createPropertySpec( 92 | param: AutoDslParam 93 | ): PropertySpec.Builder { 94 | val propBuilder = PropertySpec.builder(param.name, param.typeName).mutable() 95 | if (param.isNullable()) { 96 | // nullable element 97 | propBuilder.initializer("null") 98 | } else { 99 | // non null element 100 | propBuilder.delegate("%T.notNull()", Delegates::class) 101 | } 102 | return propBuilder 103 | } 104 | 105 | private fun createFunIfSupportedCollectionAndNoAnnotation( 106 | param: AutoDslParam, 107 | builderClassName: ClassName 108 | ): AutoDslCollectionData? { 109 | val concreteClassName = 110 | when ((param.typeName as? ParameterizedTypeName)?.rawType?.canonicalName) { 111 | Constants.LIST_TYPE_NAME -> ArrayList::class.asClassName() 112 | Constants.MUTABLE_LIST_TYPE_NAME -> ArrayList::class.asClassName() 113 | Constants.SET_TYPE_NAME -> HashSet::class.asClassName() 114 | Constants.MUTABLE_SET_TYPE_NAME -> HashSet::class.asClassName() 115 | else -> { 116 | return null 117 | } 118 | } 119 | return createCollectionData(param, builderClassName, concreteClassName) 120 | } 121 | 122 | private fun createFunIfAnnotatedWithCollection( 123 | param: AutoDslParam, 124 | builderClassName: ClassName 125 | ): AutoDslCollectionData? { 126 | val collectionAnnotation = param.getAutoDslCollectionAnnotation() 127 | ?: return null 128 | 129 | val collectionAnnotationClassName: ClassName = try { 130 | collectionAnnotation.concreteType.asClassName() 131 | } catch (e: MirroredTypeException) { 132 | if (e.typeMirror !is DeclaredType) { 133 | throw ProcessingException( 134 | param.element, 135 | "The given type is not supported by AutoDslCollection. Not able to retrieve type." 136 | ) 137 | } 138 | ((e.typeMirror as DeclaredType).asElement() as? TypeElement)?.asClassName() 139 | ?: throw ProcessingException( 140 | param.element, 141 | "The given type is not supported by AutoDslCollection. Type or class not resolved." 142 | ) 143 | } 144 | 145 | return createCollectionData(param, builderClassName, collectionAnnotationClassName) 146 | } 147 | 148 | private fun createCollectionData( 149 | param: AutoDslParam, 150 | builderClassName: ClassName, 151 | concreteCollectionClassName: ClassName 152 | ): AutoDslCollectionData { 153 | val parameterizedClassName = 154 | ((param.typeName as? ParameterizedTypeName)?.typeArguments?.get(0) as? ClassName) 155 | ?: throw ProcessingException(param.element, "Collection has no parameterized value") 156 | /* 157 | Review: This could be improved if we detect there is no repeated parameterized type so we can create a list 158 | directly in the builder and leverage the use of a class only if it's repeated so we can avoid issues with 159 | two lists having the same parameterized type and both trying to define unaryPLus method. 160 | 161 | example: 162 | private val __auto_dsl_collection: ArrayList = ArrayList() 163 | operator fun Person.unaryPlus() { 164 | __auto_dsl_collection.add(this)} 165 | */ 166 | 167 | val paramName = param.name 168 | val collectionFieldName = "_${paramName}AutoDslCollection" 169 | 170 | val unaryPlusFunc = FunSpec.builder("unaryPlus") 171 | .addModifiers(KModifier.OPERATOR) 172 | .receiver(parameterizedClassName) 173 | .addStatement("$collectionFieldName.add(this)") 174 | 175 | val collectionPropertySpecBuilder = PropertySpec.builder( 176 | collectionFieldName, 177 | concreteCollectionClassName.plusParameter(parameterizedClassName) 178 | ) 179 | .initializer("%T()", concreteCollectionClassName) 180 | 181 | if (param.getAutoDslCollectionAnnotation()?.inline == true) { 182 | return AutoDslCollectionData( 183 | collectionFun = unaryPlusFunc 184 | // includes assigment to param 185 | .addStatement("$paramName = $collectionFieldName") 186 | .build(), 187 | propertySpec = collectionPropertySpecBuilder.apply { modifiers.add(KModifier.PRIVATE) }.build() 188 | ) 189 | } 190 | val collectionClassNameValue = paramName.toAutoDslCollectionClassName() // todo review this 191 | val nestedClass = TypeSpec.classBuilder(collectionClassNameValue) 192 | .primaryConstructor(FunSpec.constructorBuilder().addModifiers(KModifier.INTERNAL).build()) 193 | .addAnnotation(AutoDslMarker::class) 194 | .addProperty(collectionPropertySpecBuilder.apply { modifiers.add(KModifier.INTERNAL) }.build()) 195 | .addFunction(unaryPlusFunc.build()) 196 | .build() 197 | 198 | val collectionFun = FunSpec.builder(paramName) 199 | .addParameter( 200 | ParameterSpec.builder( 201 | BLOCK_FUN_NAME, 202 | LambdaTypeName.get( 203 | receiver = ClassName.bestGuess(collectionClassNameValue), 204 | returnType = Unit::class.asTypeName() 205 | ) 206 | ).build() 207 | ) 208 | .returns(builderClassName) 209 | .addStatement("this.$paramName·= $collectionClassNameValue().apply·{ $BLOCK_FUN_NAME() }.$collectionFieldName") 210 | .addStatement("return this") 211 | .build() 212 | 213 | return AutoDslCollectionData(collectionFun, nestedClass) 214 | } 215 | 216 | private class AutoDslCollectionData( 217 | val collectionFun: FunSpec, 218 | val nestedClass: TypeSpec? = null, 219 | val propertySpec: PropertySpec? = null 220 | ) 221 | 222 | private fun createFunIfAnnotatedWithAutoDsl( 223 | param: AutoDslParam, 224 | builderClassName: ClassName, 225 | processingEnv: ProcessingEnvironment 226 | ): AutoDslFunctionData? { 227 | val paramType = param.typeInfo 228 | val paramTypeElementAnnotation = paramType.autoDslAnnotation ?: return null 229 | 230 | // fun address(block: AddressBuilder.() -> Unit): PersonBuilder = this.apply { this.address = AddressBuilder().apply(block).build() } 231 | val paramBuilderName = paramType.name.toAutoDslBuilderName() 232 | val paramBuilderClassName = processingEnv.getClassName(paramType.element, paramBuilderName) 233 | val funSpec = FunSpec.builder(paramTypeElementAnnotation.getDslNameOrDefault(param.name)) 234 | .addParameter( 235 | ParameterSpec.builder( 236 | BLOCK_FUN_NAME, 237 | LambdaTypeName.get( 238 | receiver = paramBuilderClassName, 239 | returnType = Unit::class.asTypeName() 240 | ) 241 | ).build() 242 | ) 243 | .returns(builderClassName) 244 | .addStatement("this.${param.name} = $paramBuilderName().apply($BLOCK_FUN_NAME).build()") 245 | .addStatement("return this") 246 | .build() 247 | 248 | return AutoDslFunctionData( 249 | funSpec, 250 | AutoDslImportSpec( 251 | paramBuilderClassName.packageName, 252 | paramBuilderClassName.simpleName 253 | ) 254 | ) 255 | } 256 | 257 | private class AutoDslFunctionData( 258 | val funSpec: FunSpec, 259 | val importData: AutoDslImportSpec 260 | ) -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/AutoDslParamSpec.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import com.squareup.kotlinpoet.FunSpec 19 | import com.squareup.kotlinpoet.PropertySpec 20 | import com.squareup.kotlinpoet.TypeSpec 21 | 22 | internal class AutoDslParamSpec( 23 | val imports: List, 24 | val properties: List, 25 | val functions: List, 26 | val types: List 27 | ) -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/NamesExt.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | const val BLOCK_FUN_NAME = "block" 19 | 20 | internal fun String.toAutoDslBuilderName() = "${this}AutoDslBuilder" 21 | internal fun String.toAutoDslCollectionClassName() = "${this.capitalize()}AutoDslCollection" -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/TargetConstructor.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import com.autodsl.annotation.AutoDslConstructor 19 | import com.autodsl.processor.ProcessingException 20 | import com.sun.tools.javac.code.Symbol 21 | import me.eugeniomarletti.kotlin.metadata.KotlinClassMetadata 22 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf 23 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Constructor 24 | import me.eugeniomarletti.kotlin.metadata.visibility 25 | import javax.lang.model.element.ElementKind 26 | import javax.lang.model.element.ExecutableElement 27 | import javax.lang.model.util.Elements 28 | 29 | internal data class TargetConstructor( 30 | val element: ExecutableElement, 31 | val proto: Constructor, 32 | val parameters: Map 33 | ) { 34 | companion object { 35 | fun targetConstructor(metadata: KotlinClassMetadata, elements: Elements): TargetConstructor { 36 | val (nameResolver, classProto) = metadata.data 37 | 38 | val constructorElements = classProto.fqName 39 | .let(nameResolver::getString) 40 | .replace('/', '.') 41 | .let(elements::getTypeElement) 42 | .enclosedElements 43 | .mapNotNull { element -> 44 | element.takeIf { it.kind == ElementKind.CONSTRUCTOR }?.let { it as ExecutableElement } 45 | } 46 | 47 | var proto: ProtoBuf.Constructor = classProto.constructorList.first() 48 | var element: ExecutableElement = constructorElements.first() 49 | for ((index, constructorElement) in constructorElements.withIndex()) { 50 | if (constructorElement.isAutoDslCollection()) { 51 | proto = classProto.constructorList.getOrNull(index) ?: proto 52 | element = constructorElement 53 | break 54 | } 55 | } 56 | 57 | if (proto.visibility != ProtoBuf.Visibility.INTERNAL && proto.visibility != ProtoBuf.Visibility.PUBLIC) { 58 | throw ProcessingException( 59 | element, "@AutoDsl can't be applied to $element: " + 60 | "constructor is not internal or public" 61 | ) 62 | } 63 | 64 | val parameters = mutableMapOf() 65 | for (parameter in proto.valueParameterList) { 66 | val name = nameResolver.getString(parameter.name) 67 | val index = proto.valueParameterList.indexOf(parameter) 68 | parameters[name] = TargetParameter(name, parameter, index, element.parameters[index]) 69 | } 70 | 71 | return TargetConstructor(element, proto, parameters) 72 | } 73 | 74 | // FIXME review how to accomplish this as getAnnotation was not returning it 75 | private fun ExecutableElement.isAutoDslCollection(): Boolean { 76 | (this as? Symbol.MethodSymbol)?.annotationMirrors?.forEach { 77 | if (it.value.type.asElement().simpleName.toString() == AutoDslConstructor::class.java.simpleName) 78 | return true 79 | } 80 | return false 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/TargetParameter.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.ValueParameter 19 | import javax.lang.model.element.VariableElement 20 | 21 | internal data class TargetParameter( 22 | val name: String, 23 | val proto: ValueParameter, 24 | val index: Int, 25 | val element: VariableElement 26 | ) 27 | -------------------------------------------------------------------------------- /processor/src/main/kotlin/com/autodsl/processor/internal/TargetType.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.processor.internal 17 | 18 | import com.autodsl.processor.asKModifier 19 | import com.autodsl.processor.asTypeName 20 | import com.squareup.kotlinpoet.* 21 | import me.eugeniomarletti.kotlin.metadata.* 22 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Class 23 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Modality.ABSTRACT 24 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.TypeParameter 25 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Visibility.INTERNAL 26 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.ProtoBuf.Visibility.LOCAL 27 | import me.eugeniomarletti.kotlin.metadata.shadow.metadata.deserialization.NameResolver 28 | import javax.annotation.processing.Messager 29 | import javax.lang.model.element.Element 30 | import javax.lang.model.element.TypeElement 31 | import javax.lang.model.util.Elements 32 | import javax.tools.Diagnostic.Kind.ERROR 33 | 34 | internal data class TargetType( 35 | val proto: Class, 36 | val element: TypeElement, 37 | val constructor: TargetConstructor, 38 | val typeVariables: List 39 | ) { 40 | val name = element.className.simpleName 41 | val builderName = element.simpleName.toString().toAutoDslBuilderName() 42 | val isInternal = proto.visibility == INTERNAL 43 | 44 | companion object { 45 | fun get(messager: Messager, elements: Elements, element: Element): TargetType? { 46 | val typeMetadata: KotlinMetadata? = element.kotlinMetadata 47 | if (element !is TypeElement || typeMetadata !is KotlinClassMetadata) { 48 | messager.printMessage( 49 | ERROR, "@AutoDsl can't be applied to $element: must be a Kotlin class", element 50 | ) 51 | return null 52 | } 53 | 54 | val proto = typeMetadata.data.classProto 55 | when { 56 | proto.classKind == Class.Kind.ENUM_CLASS -> { 57 | messager.printMessage( 58 | ERROR, 59 | "@AutoDsl with 'generateAdapter = \"true\"' can't be applied to $element: code gen for enums is not supported or necessary", 60 | element 61 | ) 62 | return null 63 | } 64 | proto.classKind != Class.Kind.CLASS -> { 65 | messager.printMessage( 66 | ERROR, "@AutoDsl can't be applied to $element: must be a Kotlin class", element 67 | ) 68 | return null 69 | } 70 | proto.isInnerClass -> { 71 | messager.printMessage( 72 | ERROR, "@AutoDsl can't be applied to $element: must not be an inner class", element 73 | ) 74 | return null 75 | } 76 | proto.modality == ABSTRACT -> { 77 | messager.printMessage( 78 | ERROR, "@AutoDsl can't be applied to $element: must not be abstract", element 79 | ) 80 | return null 81 | } 82 | proto.visibility == LOCAL -> { 83 | messager.printMessage( 84 | ERROR, "@AutoDsl can't be applied to $element: must not be local", element 85 | ) 86 | return null 87 | } 88 | } 89 | 90 | val typeVariables = genericTypeNames(proto, typeMetadata.data.nameResolver) 91 | 92 | val constructor = TargetConstructor.targetConstructor(typeMetadata, elements) 93 | return TargetType(proto, element, constructor, typeVariables) 94 | } 95 | 96 | private val Element.className: ClassName 97 | get() { 98 | val typeName = asType().asTypeName() 99 | return when (typeName) { 100 | is ClassName -> typeName 101 | is ParameterizedTypeName -> typeName.rawType 102 | else -> throw IllegalStateException("unexpected TypeName: ${typeName::class}") 103 | } 104 | } 105 | 106 | private fun genericTypeNames(proto: Class, nameResolver: NameResolver): List { 107 | return proto.typeParameterList.map { 108 | val possibleBounds = it.upperBoundList 109 | .map { it.asTypeName(nameResolver, proto::getTypeParameter, false) } 110 | return@map if (possibleBounds.isEmpty()) { 111 | TypeVariableName( 112 | name = nameResolver.getString(it.name), 113 | variance = it.varianceModifier 114 | ) 115 | } else { 116 | TypeVariableName( 117 | name = nameResolver.getString(it.name), 118 | bounds = *possibleBounds.toTypedArray(), 119 | variance = it.varianceModifier 120 | ) 121 | }.copy(reified = it.reified) 122 | } 123 | } 124 | 125 | private val TypeParameter.varianceModifier: KModifier? 126 | get() { 127 | return variance.asKModifier().let { 128 | // We don't redeclare out variance here 129 | if (it == KModifier.OUT) { 130 | null 131 | } else { 132 | it 133 | } 134 | } 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /release-bintray.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | libraryVersion = '0.0.11' 3 | 4 | def groupName = "io.github.juanchosaravia.autodsl" 5 | bintrayRepo = 'autodsl' 6 | bintrayName = "$groupName:$POM_ARTIFACT_ID" 7 | 8 | publishedGroupId = groupName 9 | libraryName = POM_NAME 10 | artifact = POM_ARTIFACT_ID 11 | 12 | libraryDescription = 'Auto-generates DSL for your Kotlin classes using annotations.' 13 | 14 | siteUrl = 'https://github.com/juanchosaravia/autodsl' 15 | gitUrl = 'https://github.com/juanchosaravia/autodsl.git' 16 | 17 | developerId = 'juanchosaravia' 18 | developerName = 'Juan Ignacio Saravia' 19 | developerEmail = 'juanchosaravia@gmail.com' 20 | 21 | licenseName = "The Apache Software License, Version 2.0" 22 | licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.txt" 23 | licenseDist = "repo" 24 | allLicenses = ['Apache-2.0'] 25 | } 26 | 27 | // Publish on Bintray. 28 | apply from: rootProject.file('bintray/bintray.gradle') -------------------------------------------------------------------------------- /samples/android-autodsl/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | .DS_Store 4 | /build 5 | /captures 6 | .externalNativeBuild 7 | 8 | .gradle 9 | **/build/ 10 | **/out/ 11 | **/out/**/* 12 | 13 | # Ignore Gradle GUI config 14 | gradle-app.setting 15 | 16 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 17 | !gradle-wrapper.jar 18 | 19 | # Cache of project 20 | .gradletasknamecache 21 | 22 | */*.iml 23 | **/*.iml 24 | */.idea/** 25 | .idea/** 26 | 27 | local.properties -------------------------------------------------------------------------------- /samples/android-autodsl/README.md: -------------------------------------------------------------------------------- 1 | # AutoDsl in Android 2 | 3 | This sample App showcase how to create a DSL around `AnimatorSet` and `Animator` 4 | to perform animations over Views in a more expressive way: 5 | 6 | ```kotlin 7 | sequence { 8 | +together { 9 | +TranslateX(0f, 150f) 10 | +TranslateY(0f, -150f) 11 | } 12 | +sequence { 13 | +translateX { 14 | from = -150f 15 | to = 150f 16 | } 17 | +TranslateY(150f, 0f) 18 | } 19 | // ... 20 | 21 | }.runOn(view) 22 | ``` 23 | 24 | It will concatenate different `Animators` and at the end will execute them in the given order. 25 | 26 | ## Animation 27 | ![Anim not found](https://github.com/juanchosaravia/autodsl/blob/master/samples/android-autodsl/resources/showcase_anim.gif?raw=true) 28 | 29 | ## License 30 | 31 | Copyright 2018 Juan Ignacio Saravia 32 | 33 | Licensed under the Apache License, Version 2.0 (the "License"); 34 | you may not use this file except in compliance with the License. 35 | You may obtain a copy of the License at 36 | 37 | http://www.apache.org/licenses/LICENSE-2.0 38 | 39 | Unless required by applicable law or agreed to in writing, software 40 | distributed under the License is distributed on an "AS IS" BASIS, 41 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 42 | See the License for the specific language governing permissions and 43 | limitations under the License. -------------------------------------------------------------------------------- /samples/android-autodsl/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion 27 8 | defaultConfig { 9 | applicationId "com.autodsl.sample" 10 | minSdkVersion 24 11 | targetSdkVersion 27 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | implementation fileTree(dir: 'libs', include: ['*.jar']) 26 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 27 | implementation 'com.android.support:appcompat-v7:27.1.1' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 29 | testImplementation 'junit:junit:4.12' 30 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 31 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 32 | 33 | // AutoDsl 34 | implementation "io.github.juanchosaravia.autodsl:annotation:$autodsl_version" 35 | kapt "io.github.juanchosaravia.autodsl:processor:$autodsl_version" 36 | } 37 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/java/com/autodsl/sample/Anim.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.sample 17 | 18 | import android.animation.Animator 19 | import android.animation.AnimatorSet 20 | import android.animation.ObjectAnimator 21 | import android.view.View 22 | import com.autodsl.annotation.AutoDsl 23 | import com.autodsl.annotation.AutoDslCollection 24 | 25 | /** 26 | * DSL for Animations. 27 | * 28 | * @author juan.saravia 29 | */ 30 | interface Anim { 31 | fun createAnimator(): Animator 32 | 33 | fun runOn(view: View) { 34 | createAnimator().apply { 35 | setTarget(view) 36 | start() 37 | } 38 | } 39 | } 40 | 41 | @AutoDsl(dslName = "sequence") 42 | class AnimSequence( 43 | @AutoDslCollection(concreteType = ArrayList::class, inline = true) 44 | val anim: List 45 | ) : Anim { 46 | 47 | override fun createAnimator(): Animator { 48 | return AnimatorSet().apply { 49 | playSequentially(anim.map { it.createAnimator() }) 50 | } 51 | } 52 | } 53 | 54 | @AutoDsl(dslName = "together") 55 | class AnimTogether( 56 | @AutoDslCollection(concreteType = ArrayList::class, inline = true) 57 | val anim: List 58 | ) : Anim { 59 | 60 | override fun createAnimator(): Animator { 61 | return AnimatorSet().apply { 62 | playTogether(anim.map { it.createAnimator() }) 63 | } 64 | } 65 | } 66 | 67 | @AutoDsl 68 | class TranslateX(from: Float, to: Float) : TranslateAnim("translationX", from, to) 69 | 70 | class TranslateY(from: Float, to: Float) : TranslateAnim("translationY", from, to) 71 | 72 | open class TranslateAnim( 73 | private val propertyName: String, 74 | private val from: Float, 75 | private val to: Float 76 | ) : Anim { 77 | 78 | override fun createAnimator(): Animator { 79 | return ObjectAnimator().apply { 80 | propertyName = this@TranslateAnim.propertyName 81 | setFloatValues(from, to) 82 | } 83 | } 84 | } -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/java/com/autodsl/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 Juan Ignacio Saravia 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.autodsl.sample 17 | 18 | import android.os.Bundle 19 | import android.support.v7.app.AppCompatActivity 20 | import kotlinx.android.synthetic.main.activity_main.* 21 | 22 | class MainActivity : AppCompatActivity() { 23 | 24 | override fun onCreate(savedInstanceState: Bundle?) { 25 | super.onCreate(savedInstanceState) 26 | setContentView(R.layout.activity_main) 27 | 28 | imageView.setOnClickListener { 29 | sequence { 30 | // up right 31 | +together { 32 | +TranslateX(0f, 150f) 33 | +TranslateY(0f, -150f) 34 | } 35 | // left 36 | +TranslateX(150f, -150f) 37 | // down right 38 | +together { 39 | +translateX { 40 | from = -150f 41 | to = 150f 42 | } 43 | +TranslateY(-150f, 150f) 44 | } 45 | // left 46 | +TranslateX(150f, -150f) 47 | // center 48 | +together { 49 | +TranslateX(-150f, 0f) 50 | +TranslateY(150f, 0f) 51 | } 52 | }.runOn(it) 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidAutoDsl 3 | 4 | -------------------------------------------------------------------------------- /samples/android-autodsl/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /samples/android-autodsl/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.72' 5 | ext.autodsl_version = '0.0.11' 6 | repositories { 7 | google() 8 | jcenter() 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:3.2.1' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 | 14 | // NOTE: Do not place your application dependencies here; they belong 15 | // in the individual module build.gradle files 16 | } 17 | } 18 | 19 | allprojects { 20 | repositories { 21 | google() 22 | jcenter() 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /samples/android-autodsl/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # Kotlin code style for this project: "official" or "obsolete": 15 | kotlin.code.style=official 16 | -------------------------------------------------------------------------------- /samples/android-autodsl/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /samples/android-autodsl/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /samples/android-autodsl/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /samples/android-autodsl/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /samples/android-autodsl/resources/showcase_anim.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juanchosaravia/autodsl/5e0a2f7fad26d52e766d3a330768ebb3b1698435/samples/android-autodsl/resources/showcase_anim.gif -------------------------------------------------------------------------------- /samples/android-autodsl/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | maven { url 'https://plugins.gradle.org/m2/' } 4 | mavenCentral() 5 | } 6 | } 7 | rootProject.name = 'core' 8 | 9 | include 'app' 10 | include 'annotation' 11 | include 'processor' --------------------------------------------------------------------------------