├── .github └── workflows │ ├── ci.yml │ └── deploy.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.clj ├── deps.edn ├── graalvm └── native-windows-compile.bat ├── pom.xml ├── resources ├── .keep ├── META-INF │ └── native-image │ │ └── clj-easy │ │ └── stub │ │ └── native-image.properties ├── STUB_VERSION └── clj_easy │ └── stub │ └── internal_generator.clj ├── src ├── cli │ └── clj_easy │ │ └── stub │ │ └── main.clj └── lib │ └── clj_easy │ └── stub │ ├── core.clj │ └── utils.clj └── test └── clj_easy └── stub ├── core_test.clj └── main_test.clj /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | paths-ignore: 8 | - '**/README.md' 9 | - '**/CHANGELOG.md' 10 | - 'resources/STUB_VERSION' 11 | - 'docs/**' 12 | pull_request: 13 | 14 | jobs: 15 | lint: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v1 19 | 20 | - name: Install Clojure 21 | uses: DeLaGuardo/setup-clojure@master 22 | with: 23 | cli: '1.10.3.1013' 24 | 25 | - name: Check if namespaces are clean 26 | run: clojure -M:clojure-lsp clean-ns --dry 27 | 28 | - name: Check if namespaces are formatted 29 | run: clojure -M:clojure-lsp format --dry 30 | 31 | - name: Check if namespaces have no diagnostics 32 | run: clojure -M:clojure-lsp diagnostics 33 | 34 | unit-test: 35 | runs-on: ubuntu-latest 36 | strategy: 37 | fail-fast: false 38 | matrix: 39 | jdk: [8, 11, 15, 17] 40 | steps: 41 | - uses: actions/checkout@v1 42 | 43 | - name: Set up JDK ${{ matrix.jdk }} 44 | uses: actions/setup-java@v1 45 | with: 46 | java-version: ${{ matrix.jdk }} 47 | 48 | - name: Install Clojure 49 | uses: DeLaGuardo/setup-clojure@master 50 | with: 51 | cli: '1.10.3.1013' 52 | 53 | - name: Run tests 54 | run: clojure -M:test 55 | 56 | graalvm-build: 57 | runs-on: ubuntu-latest 58 | strategy: 59 | fail-fast: false 60 | steps: 61 | - uses: actions/checkout@v2 62 | - name: Prepare java 63 | uses: actions/setup-java@v1 64 | with: 65 | java-version: 11 66 | 67 | - name: Install Clojure 68 | uses: DeLaGuardo/setup-clojure@master 69 | with: 70 | cli: '1.10.3.1013' 71 | 72 | - name: Install GraalVM 73 | uses: DeLaGuardo/setup-graalvm@master 74 | with: 75 | graalvm: 21.3.0 76 | java: java11 77 | 78 | - name: Install native-image component 79 | run: | 80 | gu install native-image 81 | 82 | - name: Build Linux native image 83 | env: 84 | STUB_XMX: "-J-Xmx6g" 85 | run: | 86 | clojure -T:build native 87 | 88 | - name: Upload 89 | uses: actions/upload-artifact@v2 90 | with: 91 | name: stub-native 92 | path: stub 93 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' 7 | 8 | jobs: 9 | build-jvm: 10 | if: startsWith(github.ref, 'refs/tags/v') 11 | name: Build JVM jar 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Prepare java 16 | uses: actions/setup-java@v1 17 | with: 18 | java-version: 1.8 19 | 20 | - name: Install Clojure 21 | uses: DeLaGuardo/setup-clojure@master 22 | with: 23 | cli: '1.10.3.1013' 24 | 25 | - name: Get latest tag 26 | id: latest-tag 27 | uses: WyriHaximus/github-action-get-previous-tag@v1 28 | 29 | - name: Generate uberjar 30 | run: | 31 | clojure -T:build jar 32 | mv target/stub-*.jar stub.jar 33 | 34 | - name: Upload jar 35 | uses: actions/upload-artifact@v2 36 | with: 37 | path: stub.jar 38 | name: stub.jar 39 | 40 | - name: Generate uberjar 41 | run: | 42 | clojure -T:build uber 43 | mv target/stub-*-standalone.jar stub-standalone.jar 44 | 45 | - name: Upload standalone jar 46 | uses: actions/upload-artifact@v2 47 | with: 48 | path: stub-standalone.jar 49 | name: stub-standalone.jar 50 | 51 | linux-amd64: 52 | name: Build native linux amd64 binary 53 | needs: build-jvm 54 | runs-on: ubuntu-latest 55 | steps: 56 | - uses: actions/checkout@v2 57 | - uses: actions/download-artifact@v2 58 | with: 59 | name: stub-standalone.jar 60 | 61 | - name: Install Clojure 62 | uses: DeLaGuardo/setup-clojure@master 63 | with: 64 | cli: '1.10.3.1013' 65 | 66 | - name: Install GraalVM 67 | uses: DeLaGuardo/setup-graalvm@master 68 | with: 69 | graalvm-version: 21.3.0.java11 70 | 71 | - name: Install native-image component 72 | run: | 73 | gu install native-image 74 | 75 | - name: Build Linux native image 76 | env: 77 | STUB_JAR: stub-standalone.jar 78 | STUB_XMX: "-J-Xmx6g" 79 | run: clojure -T:build native 80 | 81 | - name: Zip binary 82 | run: zip stub-native-linux-amd64.zip stub 83 | 84 | - name: Upload 85 | uses: actions/upload-artifact@v2 86 | with: 87 | path: stub-native-linux-amd64.zip 88 | name: stub-native-linux-amd64.zip 89 | 90 | linux-amd64-static: 91 | name: Build native linux amd64 static binary 92 | needs: build-jvm 93 | runs-on: ubuntu-latest 94 | steps: 95 | - uses: actions/checkout@v2 96 | - uses: actions/download-artifact@v2 97 | with: 98 | name: stub-standalone.jar 99 | 100 | - name: Install Clojure 101 | uses: DeLaGuardo/setup-clojure@master 102 | with: 103 | cli: '1.10.3.1013' 104 | 105 | - name: Install GraalVM 106 | uses: DeLaGuardo/setup-graalvm@master 107 | with: 108 | graalvm-version: 21.3.0.java11 109 | 110 | - name: Install native-image component 111 | run: | 112 | gu install native-image 113 | 114 | - name: Build Linux native image 115 | env: 116 | STUB_JAR: stub-standalone.jar 117 | STUB_XMX: "-J-Xmx6g" 118 | STUB_STATIC: true 119 | run: clojure -T:build native 120 | 121 | - name: Zip binary 122 | run: zip stub-native-static-linux-amd64.zip stub 123 | 124 | - name: Upload 125 | uses: actions/upload-artifact@v2 126 | with: 127 | path: stub-native-static-linux-amd64.zip 128 | name: stub-native-static-linux-amd64.zip 129 | 130 | macos: 131 | name: Build native MacOS amd64 binary 132 | needs: build-jvm 133 | runs-on: ubuntu-latest 134 | steps: 135 | - uses: actions/checkout@v2 136 | - uses: actions/download-artifact@v2 137 | with: 138 | name: stub-standalone.jar 139 | 140 | - name: Install Clojure 141 | uses: DeLaGuardo/setup-clojure@master 142 | with: 143 | cli: '1.10.3.1013' 144 | 145 | - name: Install GraalVM 146 | uses: DeLaGuardo/setup-graalvm@master 147 | with: 148 | graalvm-version: 21.3.0.java11 149 | 150 | - name: Install native-image component 151 | run: | 152 | gu install native-image 153 | 154 | - name: Build MacOS native image 155 | env: 156 | STUB_JAR: stub-standalone.jar 157 | STUB_XMX: "-J-Xmx6g" 158 | run: clojure -T:build native 159 | 160 | - name: Zip binary 161 | run: zip stub-native-macos-amd64.zip stub 162 | 163 | - name: Upload 164 | uses: actions/upload-artifact@v2 165 | with: 166 | path: stub-native-macos-amd64.zip 167 | name: stub-native-macos-amd64.zip 168 | 169 | windows: 170 | name: Build native Windows binary 171 | needs: build-jvm 172 | runs-on: windows-latest 173 | steps: 174 | - uses: actions/checkout@v2 175 | - uses: actions/download-artifact@v2 176 | with: 177 | name: stub-standalone.jar 178 | 179 | - name: Prepare java 180 | uses: actions/setup-java@v1 181 | with: 182 | java-version: 11 183 | 184 | - name: Install Clojure 185 | run: | 186 | iwr -useb download.clojure.org/install/win-install-1.10.3.1013.ps1 | iex 187 | 188 | - name: Install MSVC 189 | uses: ilammy/msvc-dev-cmd@v1 190 | 191 | - name: Install GraalVM 192 | uses: DeLaGuardo/setup-graalvm@master 193 | with: 194 | graalvm-version: 21.3.0.java11 195 | 196 | - name: Install native-image component 197 | run: | 198 | gu.cmd install native-image 199 | 200 | - name: Build Windows native image 201 | env: 202 | STUB_JAR: stub-standalone.jar 203 | STUB_XMX: "-J-Xmx7g" 204 | run: | 205 | .\graalvm\native-windows-compile.bat 206 | 207 | - name: Zip binary 208 | run: | 209 | jar -cMf stub-native-windows-amd64.zip stub.exe 210 | 211 | - name: Upload 212 | uses: actions/upload-artifact@v2 213 | with: 214 | path: stub-native-windows-amd64.zip 215 | name: stub-native-windows-amd64.zip 216 | 217 | deploy-clojars: 218 | needs: [build-jvm, linux-amd64, linux-amd64-static, macos, windows] 219 | runs-on: ubuntu-latest 220 | steps: 221 | - uses: actions/checkout@v2 222 | 223 | - uses: jlesquembre/clojars-publish-action@0.4 224 | env: 225 | USE_GIT_REF: false 226 | CLOJARS_USERNAME: borkdude 227 | CLOJARS_PASSWORD: ${{ secrets.CLOJARS_ORG_TOKEN }} 228 | 229 | release: 230 | name: Create Release 231 | needs: [deploy-clojars] 232 | runs-on: ubuntu-latest 233 | steps: 234 | - uses: actions/checkout@v1 235 | - name: Get latest tag 236 | id: latest-tag 237 | uses: WyriHaximus/github-action-get-previous-tag@v1 238 | 239 | - name: Create Release 240 | id: create_release 241 | uses: softprops/action-gh-release@v1 242 | with: 243 | token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 244 | tag_name: ${{ steps.latest-tag.outputs.tag}} 245 | 246 | upload-to-release: 247 | name: Upload artifacts to release 248 | needs: [release] 249 | runs-on: ubuntu-latest 250 | steps: 251 | - uses: actions/checkout@v1 252 | - name: Get latest tag 253 | id: latest-tag 254 | uses: WyriHaximus/github-action-get-previous-tag@v1 255 | 256 | - uses: actions/download-artifact@v2 257 | 258 | - name: Upload jar 259 | uses: svenstaro/upload-release-action@v2 260 | with: 261 | repo_token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 262 | file: stub.jar/stub.jar 263 | asset_name: stub.jar 264 | tag: ${{ steps.latest-tag.outputs.tag}} 265 | 266 | - name: Upload standalone jar 267 | uses: svenstaro/upload-release-action@v2 268 | with: 269 | repo_token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 270 | file: stub-standalone.jar/stub-standalone.jar 271 | asset_name: stub-standalone.jar 272 | tag: ${{ steps.latest-tag.outputs.tag}} 273 | 274 | - name: Upload Linux amd64 native binary 275 | uses: svenstaro/upload-release-action@v2 276 | with: 277 | repo_token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 278 | file: stub-native-linux-amd64.zip/stub-native-linux-amd64.zip 279 | asset_name: stub-native-linux-amd64.zip 280 | tag: ${{ steps.latest-tag.outputs.tag }} 281 | 282 | - name: Upload static Linux amd64 native binary 283 | uses: svenstaro/upload-release-action@v2 284 | with: 285 | repo_token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 286 | file: stub-native-static-linux-amd64.zip/stub-native-static-linux-amd64.zip 287 | asset_name: stub-native-static-linux-amd64.zip 288 | tag: ${{ steps.latest-tag.outputs.tag }} 289 | 290 | - name: Upload MacOS native binary 291 | uses: svenstaro/upload-release-action@v2 292 | with: 293 | repo_token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 294 | file: stub-native-macos-amd64.zip/stub-native-macos-amd64.zip 295 | asset_name: stub-native-macos-amd64.zip 296 | tag: ${{ steps.latest-tag.outputs.tag}} 297 | 298 | - name: Upload Windows native binary 299 | uses: svenstaro/upload-release-action@v2 300 | with: 301 | repo_token: ${{ secrets.CLJ_EASY_BOT_TOKEN }} 302 | file: stub-native-windows-amd64.zip/stub-native-windows-amd64.zip 303 | asset_name: stub-native-windows-amd64.zip 304 | tag: ${{ steps.latest-tag.outputs.tag}} 305 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .calva/output-window/ 2 | .classpath 3 | .clj-kondo/.cache 4 | .cpcache 5 | .eastwood 6 | .factorypath 7 | .hg/ 8 | .hgignore 9 | .java-version 10 | .lein-* 11 | .lsp/.cache 12 | .lsp/sqlite.db 13 | .nrepl-history 14 | .nrepl-port 15 | .project 16 | .rebel_readline_history 17 | .settings 18 | .socket-repl-port 19 | .sw* 20 | .vscode 21 | *.class 22 | *.jar 23 | *.swp 24 | *~ 25 | /checkouts 26 | /classes 27 | /target 28 | 29 | stubs 30 | *.build_artifacts.txt 31 | reports/ 32 | /stub 33 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## Unreleased 5 | 6 | ## 0.2.3 7 | 8 | - Fix resources not being included for native image 9 | 10 | ## 0.2.2 11 | 12 | - Upload both standalone and normal jar 13 | 14 | ## 0.2.1 15 | 16 | - Remove tools.cli deps from jar 17 | 18 | ## 0.1.1 19 | 20 | - Fix incorrectly unescaped generated doc strings. 21 | 22 | ## 0.1.0 23 | 24 | - First release: add support for generating stubs. 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Clojars Project](https://img.shields.io/clojars/v/com.github.clj-easy/stub.svg)](https://clojars.org/com.github.clj-easy/stub) 2 | [![Slack community](https://img.shields.io/badge/Slack-chat-blue?style=flat-square)](https://clojurians.slack.com/archives/C02DQFVS0MC) 3 | 4 | # stub 5 | 6 | _A tool for generating stubs for open and closed source libraries_ 7 | 8 | ## Instalation 9 | 10 | TODO 11 | 12 | ## Usage 13 | 14 | To generate stubs you need to pass the `classpath` and at least one namespace from `namespaces` to `stub` later require it and generate the stubs for those namespaces. 15 | You can specify a optional `output-dir`, otherwise the `stubs` folder will be used. 16 | For more details check `stub --help`. 17 | 18 | After running successfully, a hierarchy of files and folders with all the stubs should be available with custom metadata. Example: 19 | 20 | `stubs/foo/bar.clj` 21 | ```clojure 22 | (in-ns 'foo.bar) 23 | (defn ^{:clj-easy/stub true, :line 16, :column 1, :file "foo/bar.clj"} something ([]) ([a b])) 24 | (defn ^{:clj-easy/stub true, :line 18, :column 1, :file "foo/bar.clj"} other ([])) 25 | ``` 26 | 27 | ### CLI 28 | 29 | `stub --classpath ".../clojure.jar:/foo/bar.jar" --namespaces foo.bar --namespaces foo.baz` 30 | 31 | `stub -c ".../clojure.jar:/foo/bar.jar" -n foo.bar -o /tmp/stubs` 32 | 33 | ### API 34 | 35 | For now the only entrypoint available is `clj-easy.stub.core/generate!`. 36 | 37 | ## How does it work 38 | 39 | This tool first create a temporary file with a custom clojure code, then shell out a java process with the specified classpath calling the code from the created temporary file which should get all necessary metadata and then finish, then stub should create all files from that metadata. 40 | 41 | ## Develop 42 | 43 | Run `clj -M:run generate --classpath "" --namespaces some.entrypoint-ns` 44 | 45 | ## Build 46 | 47 | JVM cli 48 | 49 | `clj -T:build uber` 50 | 51 | JVM api 52 | 53 | `clj -T:build jar` 54 | 55 | GraalVM native image 56 | 57 | `clj -T:build native` 58 | 59 | ## Deploy 60 | 61 | To tag and deploy to clojars + generate the native image on releases: 62 | 63 | `clj -T:build tag :version '"1.3.4"'` 64 | 65 | ## License 66 | 67 | Copyright © 2021 clj-easy maintainers 68 | 69 | Distributed under the Eclipse Public License version 1.0. 70 | -------------------------------------------------------------------------------- /build.clj: -------------------------------------------------------------------------------- 1 | (ns build 2 | (:require 3 | [clojure.java.io :as io] 4 | [clojure.string :as string] 5 | [clojure.tools.build.api :as b])) 6 | 7 | (def lib 'clj-easy/stub) 8 | (def current-version (string/trim (slurp (io/resource "STUB_VERSION")))) 9 | (def class-dir "target/classes") 10 | (def basis {:project "deps.edn"}) 11 | (def uber-file (format "target/%s-%s-standalone.jar" (name lib) current-version)) 12 | (def jar-file (format "target/%s-%s.jar" (name lib) current-version)) 13 | 14 | (defn clean [_] 15 | (b/delete {:path "target"})) 16 | 17 | (defn jar [opts] 18 | (clean nil) 19 | (b/write-pom {:class-dir class-dir 20 | :lib lib 21 | :version current-version 22 | :basis (b/create-basis (update basis :aliases concat (:extra-aliases opts))) 23 | :src-dirs ["src/lib"]}) 24 | (b/copy-dir {:src-dirs ["src/lib"] 25 | :target-dir class-dir}) 26 | (b/jar {:class-dir class-dir 27 | :jar-file jar-file})) 28 | 29 | (defn uber [opts] 30 | (clean nil) 31 | (let [default-aliases [:cli]] 32 | (b/copy-dir {:src-dirs ["src/lib" "src/cli" "resources"] 33 | :target-dir class-dir}) 34 | (b/compile-clj {:basis (b/create-basis (update basis :aliases concat default-aliases (:extra-aliases opts))) 35 | :src-dirs ["src/lib" "src/cli"] 36 | :class-dir class-dir}) 37 | (b/uber {:class-dir class-dir 38 | :uber-file uber-file 39 | :main 'clj-easy.stub.main 40 | :basis (b/create-basis (update basis :aliases concat default-aliases (:extra-aliases opts)))}))) 41 | 42 | (defn native [opts] 43 | (if-let [graal-home (System/getenv "GRAALVM_HOME")] 44 | (let [jar (or (System/getenv "STUB_JAR") 45 | (do (uber (merge opts {:extra-aliases [:native]})) 46 | uber-file)) 47 | command (->> [(str (io/file graal-home "bin" "native-image")) 48 | "-jar" jar 49 | "-H:+ReportExceptionStackTraces" 50 | "--verbose" 51 | "--no-fallback" 52 | "--native-image-info" 53 | (or (System/getenv "STUB_XMX") 54 | "-J-Xmx4g") 55 | (when (= "true" (System/getenv "STUB_STATIC")) 56 | "--static")] 57 | (remove nil?)) 58 | {:keys [exit]} (b/process {:command-args command})] 59 | (System/exit exit)) 60 | (println "Set GRAALVM_HOME env"))) 61 | 62 | (defn ^:private replace-in-file [file regex content] 63 | (as-> (slurp file) $ 64 | (string/replace $ regex content) 65 | (spit file $))) 66 | 67 | (defn tag [{:keys [version]}] 68 | {:pre [(string? version)]} 69 | (b/process {:command-args ["git" "fetch" "origin"]}) 70 | (b/process {:command-args ["git" "pull" "origin" "HEAD"]}) 71 | (replace-in-file "pom.xml" 72 | (str "" current-version "") 73 | (str "" version "")) 74 | (replace-in-file "pom.xml" 75 | (str "v" current-version "") 76 | (str "v" version "")) 77 | (replace-in-file "CHANGELOG.md" 78 | #"## Unreleased" 79 | (format "## Unreleased\n\n## %s" (name version))) 80 | (replace-in-file "resources/STUB_VERSION" 81 | current-version 82 | version) 83 | (b/process {:command-args ["git" "add" "pom.xml" "CHANGELOG.md" "resources/STUB_VERSION"]}) 84 | (b/process {:command-args ["git" "commit" "-m" (str "\"Release: " version "\"")]}) 85 | (b/process {:command-args ["git" "tag" (str "v" version)]}) 86 | (b/process {:command-args ["git" "push" "origin" "HEAD"]}) 87 | (b/process {:command-args ["git" "push" "origin" "HEAD" "--tags"]})) 88 | 89 | (defn deploy-clojars [opts] 90 | (jar opts) 91 | ((requiring-resolve 'deps-deploy.deps-deploy/deploy) 92 | (merge {:installer :remote 93 | :artifact jar-file 94 | :pom-file (b/pom-path {:lib lib :class-dir class-dir})} 95 | opts)) 96 | opts) 97 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src/lib" "resources"] 2 | :deps {org.clojure/clojure {:mvn/version "1.10.3"}} 3 | :aliases 4 | {:dev {:extra-paths ["src/cli"]} 5 | :run 6 | {:main-opts ["-m" "clj-easy.stub.main"]} 7 | 8 | :test 9 | {:extra-paths ["test" "src/cli"] 10 | :extra-deps {io.github.cognitect-labs/test-runner 11 | {:git/tag "v0.5.0" :git/sha "b3fd0d2"}} 12 | :main-opts ["-m" "cognitect.test-runner"] 13 | :exec-fn cognitect.test-runner.api/test} 14 | 15 | :build 16 | {:extra-paths ["resources"] 17 | :deps {io.github.clojure/tools.build {:tag "v0.5.1" :sha "21da7d4"} 18 | slipset/deps-deploy {:mvn/version "0.2.0"}} 19 | :ns-default build} 20 | 21 | :clojure-lsp 22 | {:replace-deps {com.github.clojure-lsp/clojure-lsp {:mvn/version "2021.11.16-16.52.14"}} 23 | :main-opts ["-m" "clojure-lsp.main"]} 24 | 25 | :cli 26 | {:extra-paths ["src/cli"] 27 | :extra-deps {org.clojure/tools.cli {:mvn/version "1.0.206"}}} 28 | 29 | :native 30 | {:extra-deps {com.github.clj-easy/graal-build-time {:mvn/version "0.1.4"}}} 31 | 32 | :kaocha 33 | {:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.887"}}}}} 34 | -------------------------------------------------------------------------------- /graalvm/native-windows-compile.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | echo Building stub %STUB_JAR% with Xmx of %STUB_XMX% 4 | 5 | rem the --no-server option is not supported in GraalVM Windows. 6 | call %GRAALVM_HOME%\bin\native-image.cmd ^ 7 | "-jar" "%STUB_JAR%" ^ 8 | "-H:+ReportExceptionStackTraces" ^ 9 | "--verbose" ^ 10 | "--no-fallback" ^ 11 | "--native-image-info" ^ 12 | "%STUB_XMX%" 13 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.github.clj-easy 5 | stub 6 | 0.2.3 7 | clj-easy/stub 8 | Library to generate stubs for other Clojure libraries. 9 | https://github.com/clj-easy/stub 10 | 11 | 12 | Eclipse Public License 13 | http://www.eclipse.org/legal/epl-v10.html 14 | 15 | 16 | 17 | 18 | Greg 19 | 20 | 21 | 22 | https://github.com/clj-easy/stub 23 | scm:git:git://github.com/clj-easy/stub.git 24 | scm:git:ssh://git@github.com/clj-easy/stub.git 25 | v0.2.3 26 | 27 | 28 | 29 | org.clojure 30 | clojure 31 | 1.10.3 32 | 33 | 34 | 35 | src 36 | 37 | 38 | 39 | clojars 40 | https://repo.clojars.org/ 41 | 42 | 43 | sonatype 44 | https://oss.sonatype.org/content/repositories/snapshots/ 45 | 46 | 47 | 48 | 49 | clojars 50 | Clojars repository 51 | https://clojars.org/repo 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /resources/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clj-easy/stub/129ddf57b1cff04e9c28050260d927321d2214c7/resources/.keep -------------------------------------------------------------------------------- /resources/META-INF/native-image/clj-easy/stub/native-image.properties: -------------------------------------------------------------------------------- 1 | ImageName=stub 2 | Args=-J-Dclojure.compiler.direct-linking=true \ 3 | -J-Dclojure.spec.skip-macros=true \ 4 | -H:-CheckToolchain \ 5 | -H:+InlineBeforeAnalysis \ 6 | -H:Log=registerResource: \ 7 | -H:IncludeResources=STUB_VERSION \ 8 | -H:IncludeResources=clj_easy/stub/internal_generator.clj \ 9 | --report-unsupported-elements-at-runtime \ 10 | --allow-incomplete-classpath \ 11 | --no-server \ 12 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.sampled.spi.AudioFileReader \ 13 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.midi.spi.MidiFileReader \ 14 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.sampled.spi.MixerProvider \ 15 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.sampled.spi.FormatConversionProvider \ 16 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.sampled.spi.AudioFileWriter \ 17 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.midi.spi.MidiDeviceProvider \ 18 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.midi.spi.SoundbankReader \ 19 | -H:ServiceLoaderFeatureExcludeServices=javax.sound.midi.spi.MidiFileWriter 20 | -------------------------------------------------------------------------------- /resources/STUB_VERSION: -------------------------------------------------------------------------------- 1 | 0.2.3 2 | -------------------------------------------------------------------------------- /resources/clj_easy/stub/internal_generator.clj: -------------------------------------------------------------------------------- 1 | (ns clj-easy.stub.internal-generator 2 | "This code runs on another shell process spawned by stub. 3 | It requires the namespaces at runtime.") 4 | 5 | (defn ^:private sanitize-meta [meta] 6 | (-> meta 7 | (select-keys [:ns :name :arglists :doc :file :line :column]) 8 | (update :ns ns-name))) 9 | 10 | (let [require-ns (->> *command-line-args* 11 | (map symbol) 12 | set)] 13 | (doseq [namespace require-ns] 14 | (require namespace)) 15 | (->> (all-ns) 16 | (filter #(contains? require-ns (ns-name %))) 17 | (map ns-publics) 18 | (mapcat vals) 19 | (map (comp sanitize-meta meta)) 20 | pr-str 21 | println)) 22 | -------------------------------------------------------------------------------- /src/cli/clj_easy/stub/main.clj: -------------------------------------------------------------------------------- 1 | (ns clj-easy.stub.main 2 | (:refer-clojure :exclude [run!]) 3 | (:gen-class) 4 | (:require 5 | [clj-easy.stub.core :as core] 6 | [clojure.java.io :as io] 7 | [clojure.string :as string] 8 | [clojure.tools.cli :as t.cli])) 9 | 10 | (set! *warn-on-reflection* true) 11 | 12 | (defn ^:private version [] 13 | (->> [(str "stub v" (string/trim (slurp (io/resource "STUB_VERSION"))))] 14 | (string/join \newline))) 15 | 16 | (defn ^:private help [options-summary] 17 | (->> ["Clojure tool for generating stubs for a classpath." 18 | "" 19 | "Usage: stub []" 20 | "" 21 | "All options:" 22 | options-summary 23 | "" 24 | "Available commands:" 25 | " generate Generate the stubs." 26 | "" 27 | ;; "Run \"stub help \" for more information about a command." 28 | "See https://github.com/clj-easy/stub for detailed documentation."] 29 | (string/join \newline))) 30 | 31 | (defn ^:private cli-options [] 32 | [["-h" "--help" "Print the available commands and its options"] 33 | [nil "--version" "Print stub version"] 34 | 35 | ["-c" "--classpath CLASSPATH" "The classpath string used to search for libraries to then generate the stubs." 36 | :id :classpath 37 | :validate [string? "Specify a valid classpath string after --classpath"]] 38 | 39 | ["-n" "--namespaces NS" "Namespaces to require to later then generate the stubs. This flag accepts multiple values" 40 | :id :namespaces 41 | :default [] 42 | :multi true 43 | :update-fn conj] 44 | 45 | ["-o" "--output-dir DIR" "The directory to spit out the generated stubs." 46 | :id :output-dir 47 | :parse-fn io/file]]) 48 | 49 | (defn ^:private error-msg [errors] 50 | (str "The following errors occurred while parsing your command:\n\n" 51 | (string/join \newline errors))) 52 | 53 | (defn ^:private parse [args] 54 | (let [{:keys [options arguments errors summary]} (t.cli/parse-opts args (cli-options))] 55 | (cond 56 | (:help options) 57 | {:exit-message (help summary) :ok? true} 58 | 59 | (:version options) 60 | {:exit-message (version) :ok? true} 61 | 62 | errors 63 | {:exit-message (error-msg errors)} 64 | 65 | (and (= 1 (count arguments)) 66 | (#{"generate"} (first arguments))) 67 | {:action (first arguments) :options options} 68 | 69 | :else 70 | {:exit-message (help summary)}))) 71 | 72 | (defn ^:private exit [status msg] 73 | (when msg 74 | (println msg)) 75 | (System/exit status)) 76 | 77 | (defn ^:private with-required-options [options required fn] 78 | (doseq [option required] 79 | (when-not (get options option) 80 | (exit 1 (format "Missing required %s option for this command. Check stub --help for more details." option)))) 81 | (when (every? options required) 82 | (apply fn [options]))) 83 | 84 | (defn ^:private handle-action! 85 | [action options] 86 | (case action 87 | "generate" (with-required-options 88 | options 89 | [:classpath :namespaces] 90 | core/generate!))) 91 | 92 | (defn run! [& args] 93 | (let [{:keys [action options exit-message ok?]} (parse args)] 94 | (if exit-message 95 | {:result-code (if ok? 0 1) 96 | :message exit-message} 97 | (handle-action! action options)))) 98 | 99 | (defn -main [& args] 100 | (let [{:keys [result-code message]} (apply run! args)] 101 | (exit result-code message))) 102 | -------------------------------------------------------------------------------- /src/lib/clj_easy/stub/core.clj: -------------------------------------------------------------------------------- 1 | (ns clj-easy.stub.core 2 | (:require 3 | [clj-easy.stub.utils :as utils] 4 | [clojure.edn :as edn] 5 | [clojure.java.io :as io] 6 | [clojure.java.shell :refer [sh]] 7 | [clojure.string :as string]) 8 | (:import 9 | (java.io File))) 10 | 11 | (set! *warn-on-reflection* true) 12 | 13 | (defn ^:private internal-generator-code [] 14 | (slurp (io/resource "clj_easy/stub/internal_generator.clj"))) 15 | 16 | (defn ^:private tmp-file [] 17 | (File/createTempFile "clj-easy-stub." ".clj")) 18 | 19 | (defn ^:private meta->stub 20 | [{:keys [ns name doc arglists file line column]}] 21 | (let [definition-macro (if arglists 22 | "defn" 23 | "def") 24 | file-ext (if file (last (string/split file #"\.")) "clj") 25 | metadata (utils/assoc-some 26 | {:clj-easy/stub true} 27 | :line line 28 | :column column 29 | :file file)] 30 | {:ns ns 31 | :filename (-> ns 32 | (string/replace "." (System/getProperty "file.separator")) 33 | (string/replace "-" "_") 34 | (str "." file-ext)) 35 | :declaration (str "(" definition-macro (str " ^" metadata) " " name 36 | (if doc (str " \"" (string/escape doc {\" "\\\""}) "\"") "") 37 | (if arglists 38 | (->> arglists 39 | (map #(str "(" % ")")) 40 | (string/join " ") 41 | (str " ")) 42 | "") 43 | ")\n")})) 44 | 45 | (defn ^:private metas->stubs-by-filename [metas] 46 | (->> metas 47 | (map meta->stub) 48 | (reduce (fn [stubs-map {:keys [ns filename declaration]}] 49 | (if-let [existing-content (get stubs-map filename)] 50 | (assoc stubs-map filename (str existing-content declaration)) 51 | (let [ns-macro "in-ns" 52 | ns-content (format "(%s %s)\n" 53 | ns-macro 54 | (if (= "in-ns" ns-macro) 55 | (str "'" ns) 56 | ns))] 57 | (assoc stubs-map filename (str ns-content declaration))))) 58 | {}))) 59 | 60 | (defn ^:private spit-stubs! [output-dir stubs-by-filename] 61 | (mapv (fn [[filename content]] 62 | (let [output-file ^File (io/file output-dir filename)] 63 | (io/make-parents output-file) 64 | (spit (.getAbsolutePath output-file) content))) 65 | stubs-by-filename)) 66 | 67 | (defn ^:private create-script-tmp-file! [] 68 | (let [script-tmp-file ^File (tmp-file)] 69 | (spit script-tmp-file (internal-generator-code)) 70 | script-tmp-file)) 71 | 72 | (defn generate! 73 | "Generate stubs for the given `classpath` string, requiring `namespaces`, a 74 | list of strings, and saving the namespaces hierarchy into `output-dir`. 75 | This function spawns a clojure program using the provided or default 76 | `java-command`. 77 | If `dry?` is truthy, return the stubs on the result map without creating the file hierarchy. 78 | 79 | Return a map with: 80 | `result-code` the status code of the result. Anything different from `0` means an error. 81 | `message` A message about the result. 82 | `stubs` The generated stubs if `dry?` flag is truthy." 83 | [{:keys [classpath namespaces output-dir java-command dry?] 84 | :or {output-dir (io/file "stubs") 85 | java-command "java"}}] 86 | {:pre [(instance? File output-dir) 87 | (string? classpath) 88 | (string? java-command) 89 | (seq namespaces)]} 90 | (io/make-parents output-dir) 91 | (when-not (.exists ^File output-dir) 92 | (.mkdirs ^File output-dir)) 93 | (let [script-tmp-file ^File (create-script-tmp-file!) 94 | {:keys [out err exit]} (apply sh 95 | java-command "-cp" classpath 96 | "clojure.main" 97 | (.getAbsolutePath script-tmp-file) 98 | namespaces)] 99 | (if (= exit 0) 100 | (let [metas (edn/read-string out) 101 | stubs-by-filename (metas->stubs-by-filename metas)] 102 | (if dry? 103 | {:result-code 0 104 | :stubs stubs-by-filename 105 | :message (str "Stubs generated sucessfully")} 106 | (do 107 | (spit-stubs! output-dir stubs-by-filename) 108 | {:result-code 0 109 | :stubs stubs-by-filename 110 | :message (str "Stubs generated and persisted sucessfully")}))) 111 | {:result-code exit 112 | :message (or (and (not (string/blank? err)) 113 | err) 114 | out)}))) 115 | -------------------------------------------------------------------------------- /src/lib/clj_easy/stub/utils.clj: -------------------------------------------------------------------------------- 1 | (ns clj-easy.stub.utils) 2 | 3 | (defn assoc-some 4 | "Assoc[iate] if the value is not nil. " 5 | ([m k v] 6 | (if (nil? v) m (assoc m k v))) 7 | ([m k v & kvs] 8 | (let [ret (assoc-some m k v)] 9 | (if kvs 10 | (if (next kvs) 11 | (recur ret (first kvs) (second kvs) (nnext kvs)) 12 | (throw (IllegalArgumentException. 13 | "assoc-some expects even number of arguments after map/vector, found odd number"))) 14 | ret)))) 15 | -------------------------------------------------------------------------------- /test/clj_easy/stub/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-easy.stub.core-test 2 | (:require 3 | [clj-easy.stub.core :as core] 4 | [clojure.java.io :as io] 5 | [clojure.java.shell :as shell] 6 | [clojure.string :as string] 7 | [clojure.test :refer [deftest is testing]])) 8 | 9 | (defn code [& strings] (string/join "\n" strings)) 10 | 11 | (def success-single-ns-metas 12 | [{:ns 'some.ns 13 | :name 'something 14 | :doc "Some cool doc" 15 | :arglists ["a" "b"] 16 | :file "src/some/ns.clj" 17 | :line 2 18 | :column 3}]) 19 | 20 | (def success-multiple-ns-metas 21 | [{:ns 'some.ns 22 | :name 'something 23 | :doc "Some cool doc" 24 | :arglists ["a" "b"] 25 | :file "src/some/ns.clj" 26 | :line 2 27 | :column 3} 28 | {:ns 'some.ns 29 | :name 'otherthing 30 | :doc "Some other cool doc" 31 | :file "src/some/ns.clj" 32 | :line 5 33 | :column 6} 34 | {:ns 'another.cool-ns 35 | :name 'foo 36 | :doc "Some foo doc" 37 | :file "src/another/cool_ns.clj" 38 | :line 10 39 | :column 2}]) 40 | 41 | (deftest generate! 42 | (testing "When classpath is not passed" 43 | (is (thrown? AssertionError (= nil (core/generate! {}))))) 44 | (testing "When namespaces is not passed or empty" 45 | (is (thrown? AssertionError (= nil (core/generate! {:classpath "foo"})))) 46 | (is (thrown? AssertionError (= nil (core/generate! {:classpath "foo" 47 | :namespaces []}))))) 48 | (testing "When java command returns an error" 49 | (with-redefs [core/create-script-tmp-file! (constantly (io/file "tmp")) 50 | shell/sh (constantly {:out "" 51 | :err "Some error" 52 | :exit 1})] 53 | (is (= {:result-code 1 54 | :message "Some error"} 55 | (core/generate! {:classpath "foo:bar" 56 | :namespaces ["some.ns"]}))))) 57 | (testing "single namespace" 58 | (testing "When java command returns success edn meta" 59 | (with-redefs [core/create-script-tmp-file! (constantly (io/file "tmp")) 60 | shell/sh (constantly {:out (str success-single-ns-metas) 61 | :err "" 62 | :exit 0}) 63 | io/make-parents (constantly nil) 64 | spit (constantly nil)] 65 | (is (= {:result-code 0 66 | :stubs {"some/ns.clj" (code "(in-ns 'some.ns)" 67 | "(defn ^{:clj-easy/stub true, :line 2, :column 3, :file \"src/some/ns.clj\"} something \"Some cool doc\" (a) (b))" 68 | "")} 69 | :message "Stubs generated and persisted sucessfully"} 70 | (core/generate! {:classpath "foo:bar" 71 | :namespaces ["some.ns"]}))))) 72 | (testing "When java command returns success edn meta for dry?" 73 | (with-redefs [core/create-script-tmp-file! (constantly (io/file "tmp")) 74 | shell/sh (constantly {:out (str success-single-ns-metas) 75 | :err "" 76 | :exit 0}) 77 | io/make-parents (constantly nil)] 78 | (is (= {:result-code 0 79 | :stubs {"some/ns.clj" (code "(in-ns 'some.ns)" 80 | "(defn ^{:clj-easy/stub true, :line 2, :column 3, :file \"src/some/ns.clj\"} something \"Some cool doc\" (a) (b))" 81 | "")} 82 | :message "Stubs generated sucessfully"} 83 | (core/generate! {:classpath "foo:bar" 84 | :dry? true 85 | :namespaces ["some.ns"]})))))) 86 | (testing "multiple namespaces" 87 | (testing "with multiple vars" 88 | (with-redefs [core/create-script-tmp-file! (constantly (io/file "tmp")) 89 | shell/sh (constantly {:out (str success-multiple-ns-metas) 90 | :err "" 91 | :exit 0}) 92 | io/make-parents (constantly nil) 93 | spit (constantly nil)] 94 | (is (= {:result-code 0 95 | :stubs {"some/ns.clj" (code "(in-ns 'some.ns)" 96 | "(defn ^{:clj-easy/stub true, :line 2, :column 3, :file \"src/some/ns.clj\"} something \"Some cool doc\" (a) (b))" 97 | "(def ^{:clj-easy/stub true, :line 5, :column 6, :file \"src/some/ns.clj\"} otherthing \"Some other cool doc\")" 98 | "") 99 | "another/cool_ns.clj" (code "(in-ns 'another.cool-ns)" 100 | "(def ^{:clj-easy/stub true, :line 10, :column 2, :file \"src/another/cool_ns.clj\"} foo \"Some foo doc\")" 101 | "")} 102 | :message "Stubs generated and persisted sucessfully"} 103 | (core/generate! {:classpath "foo:bar" 104 | :namespaces ["some.ns"]}))))))) 105 | -------------------------------------------------------------------------------- /test/clj_easy/stub/main_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-easy.stub.main-test 2 | (:require 3 | [clj-easy.stub.main :as main] 4 | [clojure.java.io :as io] 5 | [clojure.test :refer [deftest is testing]])) 6 | 7 | (def default-root (.getAbsolutePath (io/file "src"))) 8 | 9 | (deftest parse 10 | (testing "parsing options" 11 | (testing "classpath" 12 | (is (= nil (:classpath (:options (#'main/parse []))))) 13 | (is (= "a:b:c" (:classpath (:options (#'main/parse ["generate" "--classpath" "a:b:c"]))))) 14 | (is (= "a:b:c" (:classpath (:options (#'main/parse ["generate" "-c" "a:b:c"]))))) 15 | (is (= nil (:classpath (:options (#'main/parse ["-c"])))))) 16 | (testing "namespaces" 17 | (is (= [] (:namespaces (:options (#'main/parse ["generate"]))))) 18 | (is (= '["abc"] (:namespaces (:options (#'main/parse ["generate" "--namespaces" "abc"]))))) 19 | (is (= '["abc"] (:namespaces (:options (#'main/parse ["generate" "-n" "abc"]))))) 20 | (is (= '["abc" "bcd"] (:namespaces (:options (#'main/parse ["generate" "-n" "abc" "-n" "bcd"])))))) 21 | (testing "output-dir" 22 | (is (= default-root (.getAbsolutePath (:output-dir (:options (#'main/parse ["generate" "--output-dir" "src"])))))) 23 | (is (= default-root (.getAbsolutePath (:output-dir (:options (#'main/parse ["generate" "-o" "src"])))))) 24 | (is (= nil (:project-root (:options (#'main/parse ["generate" "-o" "1"]))))) 25 | (is (= nil (:project-root (:options (#'main/parse ["generate" "p" "/this/is/not/a/valid/path"]))))))) 26 | (testing "commands" 27 | (is (string? (:exit-message (#'main/parse [])))) 28 | (is (= "generate" (:action (#'main/parse ["generate"]))))) 29 | (testing "final options" 30 | (is (string? (:exit-message (#'main/parse ["--help"])))) 31 | (is (string? (:exit-message (#'main/parse ["-h"])))) 32 | (is (string? (:exit-message (#'main/parse ["--version"])))))) 33 | --------------------------------------------------------------------------------