├── .circleci └── config.yml ├── .gitattributes ├── .gitignore ├── .scalafmt.conf ├── LICENSE ├── README.md ├── build.sbt ├── project ├── BuildHelper.scala ├── build.properties └── plugins.sbt ├── sbt └── src ├── main └── scala │ └── zio │ └── interop │ └── javaz.scala └── test └── scala └── zio └── interop └── JavaSpec.scala /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | scala_211: &scala_211 4 | SCALA_VERSION: 2.11.12 5 | 6 | scala_212: &scala_212 7 | SCALA_VERSION: 2.12.9 8 | 9 | scala_213: &scala_213 10 | SCALA_VERSION: 2.13.0 11 | 12 | jdk_8: &jdk_8 13 | JDK_VERSION: 8 14 | 15 | jdk_11: &jdk_11 16 | JDK_VERSION: 11 17 | 18 | machine_resource: &machine_resource 19 | resource_class: large 20 | 21 | machine_ubuntu: &machine_ubuntu 22 | machine: 23 | image: ubuntu-1604:201903-01 24 | 25 | install_jdk: &install_jdk 26 | - run: 27 | name: Install JDK 28 | command: | 29 | while $(ps aux | grep -i ' apt ' | grep -v grep > /dev/null); do sleep 1; done # Wait for apt to be ready 30 | 31 | sudo rm /etc/apt/sources.list.d/* 32 | sudo tee /etc/apt/sources.list > /dev/null \<< 'EOF' 33 | deb http://mirror.math.princeton.edu/pub/ubuntu/ xenial main universe 34 | deb http://mirror.math.princeton.edu/pub/ubuntu/ xenial-updates main universe 35 | deb http://mirror.math.princeton.edu/pub/ubuntu/ xenial-backports main universe 36 | deb http://mirror.math.princeton.edu/pub/ubuntu/ xenial-security main restricted universe 37 | EOF 38 | 39 | if [ $JDK_VERSION == 11 ]; then 40 | wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add - 41 | sudo add-apt-repository https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/ -y 42 | fi 43 | sudo apt update 44 | if [ $JDK_VERSION == 11 ]; then 45 | sudo apt install -y adoptopenjdk-11-hotspot 46 | elif [ $JDK_VERSION == 8 ]; then 47 | sudo apt install -y openjdk-8-jdk 48 | fi 49 | java -version 50 | 51 | load_cache: &load_cache 52 | - restore_cache: 53 | key: sbt-cache-interop-java 54 | 55 | clean_cache: &clean_cache 56 | - run: 57 | name: Clean unwanted files from cache 58 | command: | 59 | rm -fv $HOME/.ivy2/.sbt.ivy.lock 60 | find $HOME/.ivy2/cache -name "ivydata-*.properties" -print -delete 61 | find $HOME/.sbt -name "*.lock" -print -delete 62 | 63 | save_cache: &save_cache 64 | - save_cache: 65 | key: sbt-cache-interop-java 66 | paths: 67 | - "~/.ivy2/cache" 68 | - "~/.sbt" 69 | - "~/.m2" 70 | - "~/.cache" 71 | 72 | compile: &compile 73 | steps: 74 | - checkout 75 | - <<: *load_cache 76 | - run: 77 | name: Compile code 78 | command: ./sbt ++${SCALA_VERSION}! compile 79 | - <<: *clean_cache 80 | - <<: *save_cache 81 | 82 | filter_tags: &filter_tags 83 | tags: 84 | only: /^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/ 85 | 86 | lint: &lint 87 | steps: 88 | - checkout 89 | - <<: *load_cache 90 | - run: 91 | name: Lint code 92 | command: ./sbt ++${SCALA_VERSION}! check 93 | - <<: *clean_cache 94 | - <<: *save_cache 95 | 96 | testJVM: &testJVM 97 | steps: 98 | - checkout 99 | - <<: *load_cache 100 | - <<: *install_jdk 101 | - run: 102 | name: Run tests 103 | command: ./sbt ++${SCALA_VERSION}! test 104 | - <<: *clean_cache 105 | - <<: *save_cache 106 | - store_test_results: 107 | path: core-tests/jvm/target/test-reports 108 | 109 | release: &release 110 | steps: 111 | - checkout 112 | - run: 113 | name: Fetch git tags 114 | command: git fetch --tags 115 | - <<: *load_cache 116 | - run: 117 | name: Write sonatype credentials 118 | command: echo "credentials += Credentials(\"Sonatype Nexus Repository Manager\", \"oss.sonatype.org\", \"${SONATYPE_USER}\", \"${SONATYPE_PASSWORD}\")" > ~/.sbt/1.0/sonatype.sbt 119 | - run: 120 | name: Write PGP public key 121 | command: echo -n "${PGP_PUBLIC}" | base64 -di > /tmp/public.asc 122 | - run: 123 | name: Write PGP secret key 124 | command: echo -n "${PGP_SECRET}" | base64 -di > /tmp/secret.asc 125 | - run: 126 | name: Release artifacts 127 | command: | 128 | mkdir -p $HOME/bin 129 | sudo apt-get update && sudo apt-get -y install gnupg2 130 | echo pinentry-mode loopback >> ~/.gnupg/gpg.conf 131 | echo allow-loopback-pinentry >> ~/.gnupg/gpg-agent.conf 132 | chmod 600 ~/.gnupg/* 133 | ln -s /usr/bin/gpg2 $HOME/bin/gpg 134 | $HOME/bin/gpg --version 135 | echo RELOADAGENT | gpg-connect-agent 136 | echo $PGP_SECRET | base64 -di | gpg2 --import --no-tty --batch --yes 137 | PATH=$HOME/bin:$PATH ./sbt ++${SCALA_VERSION}! clean ci-release 138 | 139 | jobs: 140 | lint: 141 | <<: *lint 142 | <<: *machine_ubuntu 143 | environment: 144 | - <<: *scala_212 145 | - <<: *jdk_8 146 | 147 | test_211_jdk8_jvm: 148 | <<: *testJVM 149 | <<: *machine_ubuntu 150 | <<: *machine_resource 151 | environment: 152 | - <<: *scala_211 153 | - <<: *jdk_8 154 | 155 | test_212_jdk8_jvm: 156 | <<: *testJVM 157 | <<: *machine_ubuntu 158 | <<: *machine_resource 159 | environment: 160 | - <<: *scala_212 161 | - <<: *jdk_8 162 | 163 | test_213_jdk8_jvm: 164 | <<: *testJVM 165 | <<: *machine_ubuntu 166 | <<: *machine_resource 167 | environment: 168 | - <<: *scala_213 169 | - <<: *jdk_8 170 | 171 | test_212_jdk11_jvm: 172 | <<: *testJVM 173 | <<: *machine_ubuntu 174 | <<: *machine_resource 175 | environment: 176 | - <<: *scala_212 177 | - <<: *jdk_11 178 | 179 | release: 180 | <<: *release 181 | <<: *machine_ubuntu 182 | environment: 183 | - <<: *scala_213 184 | - <<: *jdk_8 185 | 186 | workflows: 187 | version: 2 188 | build: 189 | jobs: 190 | - lint: 191 | filters: 192 | <<: *filter_tags 193 | - test_211_jdk8_jvm: 194 | requires: 195 | - lint 196 | filters: 197 | <<: *filter_tags 198 | - test_212_jdk8_jvm: 199 | requires: 200 | - lint 201 | filters: 202 | <<: *filter_tags 203 | - test_213_jdk8_jvm: 204 | requires: 205 | - lint 206 | filters: 207 | <<: *filter_tags 208 | - test_212_jdk11_jvm: 209 | requires: 210 | - lint 211 | filters: 212 | <<: *filter_tags 213 | - release: 214 | context: Sonatype2 215 | requires: 216 | - test_211_jdk8_jvm 217 | - test_212_jdk8_jvm 218 | - test_212_jdk11_jvm 219 | - test_213_jdk8_jvm 220 | filters: 221 | <<: *filter_tags 222 | branches: 223 | only: 224 | - master 225 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | sbt linguist-vendored 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bloop 2 | .metals 3 | .idea 4 | .sbtopts 5 | project/.sbt 6 | project/travis-deploy-key 7 | project/secrets.tar.xz 8 | project/zecret 9 | target 10 | test-output/ 11 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | version = "2.3.2" 2 | maxColumn = 120 3 | align = most 4 | continuationIndent.defnSite = 2 5 | assumeStandardLibraryStripMargin = true 6 | docstrings = JavaDoc 7 | lineEndings = preserve 8 | includeCurlyBraceInSelectChains = false 9 | danglingParentheses = true 10 | spaces { 11 | inImportCurlyBraces = true 12 | } 13 | optIn.annotationNewlines = true 14 | 15 | rewrite.rules = [SortImports, RedundantBraces] 16 | -------------------------------------------------------------------------------- /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 | # Interop Java 2 | 3 | [![CircleCI][Badge-Circle]][Link-Circle] 4 | [![Releases][Badge-SonatypeReleases]][Link-SonatypeReleases] 5 | [![Snapshots][Badge-SonatypeSnapshots]][Link-SonatypeSnapshots] 6 | 7 | This library provides an interoperability layer with Java future API. 8 | 9 | [Badge-Circle]: https://circleci.com/gh/zio/interop-java/tree/master.svg?style=svg 10 | [Badge-SonatypeReleases]: https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-interop-java_2.12.svg "Sonatype Releases" 11 | [Badge-SonatypeSnapshots]: https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-interop-java_2.12.svg "Sonatype Snapshots" 12 | [Link-Circle]: https://circleci.com/gh/zio/interop-java/tree/master 13 | [Link-SonatypeReleases]: https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-interop-java_2.12/ "Sonatype Releases" 14 | [Link-SonatypeSnapshots]: https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-interop-java_2.12/ "Sonatype Snapshots" 15 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | import BuildHelper._ 2 | 3 | inThisBuild( 4 | List( 5 | organization := "dev.zio", 6 | homepage := Some(url("https://zio.dev")), 7 | licenses := List("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")), 8 | developers := List( 9 | Developer( 10 | "jdegoes", 11 | "John De Goes", 12 | "john@degoes.net", 13 | url("http://degoes.net") 14 | ) 15 | ), 16 | pgpPassphrase := sys.env.get("PGP_PASSWORD").map(_.toArray), 17 | pgpPublicRing := file("/tmp/public.asc"), 18 | pgpSecretRing := file("/tmp/secret.asc"), 19 | scmInfo := Some( 20 | ScmInfo(url("https://github.com/zio/interop-java/"), "scm:git:git@github.com:zio/interop-java.git") 21 | ) 22 | ) 23 | ) 24 | 25 | addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt") 26 | addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck") 27 | 28 | lazy val java = project 29 | .in(file(".")) 30 | .enablePlugins(BuildInfoPlugin) 31 | .settings(stdSettings("zio-interop-java")) 32 | .settings(buildInfoSettings) 33 | .settings(testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")) 34 | .settings( 35 | libraryDependencies ++= Seq( 36 | "dev.zio" %% "zio" % "1.0.0-RC17", 37 | "dev.zio" %% "zio-test" % "1.0.0-RC17" % Test, 38 | "dev.zio" %% "zio-test-sbt" % "1.0.0-RC17" % Test 39 | ) 40 | ) 41 | -------------------------------------------------------------------------------- /project/BuildHelper.scala: -------------------------------------------------------------------------------- 1 | import sbt._ 2 | import Keys._ 3 | 4 | import explicitdeps.ExplicitDepsPlugin.autoImport._ 5 | import sbtbuildinfo._ 6 | import BuildInfoKeys._ 7 | 8 | object BuildHelper { 9 | private val SilencerVersion = "1.4.3" 10 | 11 | val testDeps = Seq("org.scalacheck" %% "scalacheck" % "1.14.3" % "test") 12 | val compileOnlyDeps = Seq("com.github.ghik" % "silencer-lib" % SilencerVersion % Provided cross CrossVersion.full) 13 | 14 | private val stdOptions = Seq( 15 | "-deprecation", 16 | "-encoding", 17 | "UTF-8", 18 | "-feature", 19 | "-unchecked" 20 | ) 21 | 22 | private val std2xOptions = Seq( 23 | "-Xfatal-warnings", 24 | "-language:higherKinds", 25 | "-language:existentials", 26 | "-explaintypes", 27 | "-Yrangepos", 28 | "-Xsource:2.13", 29 | "-Xlint:_,-type-parameter-shadow", 30 | "-Ywarn-numeric-widen", 31 | "-Ywarn-value-discard" 32 | ) 33 | 34 | val buildInfoSettings = Seq( 35 | buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion, isSnapshot), 36 | buildInfoPackage := "zio", 37 | buildInfoObject := "BuildInfo" 38 | ) 39 | 40 | def extraOptions(scalaVersion: String) = 41 | CrossVersion.partialVersion(scalaVersion) match { 42 | case Some((2, 13)) => 43 | std2xOptions 44 | case Some((2, 12)) => 45 | Seq( 46 | "-opt-warnings", 47 | "-Ywarn-extra-implicit", 48 | "-Ywarn-unused:_,imports", 49 | "-Ywarn-unused:imports", 50 | "-opt:l:inline", 51 | "-opt-inline-from:zio.internal.**", 52 | "-Ypartial-unification", 53 | "-Yno-adapted-args", 54 | "-Ywarn-inaccessible", 55 | "-Ywarn-infer-any", 56 | "-Ywarn-nullary-override", 57 | "-Ywarn-nullary-unit", 58 | "-Xfuture" 59 | ) ++ std2xOptions 60 | case Some((2, 11)) => 61 | Seq( 62 | "-Ypartial-unification", 63 | "-Yno-adapted-args", 64 | "-Ywarn-inaccessible", 65 | "-Ywarn-infer-any", 66 | "-Ywarn-nullary-override", 67 | "-Ywarn-nullary-unit", 68 | "-Xexperimental", 69 | "-Ywarn-unused-import", 70 | "-Xfuture" 71 | ) ++ std2xOptions 72 | case _ => Seq.empty 73 | } 74 | 75 | def stdSettings(prjName: String) = Seq( 76 | name := s"$prjName", 77 | scalacOptions := stdOptions, 78 | crossScalaVersions := Seq("2.13.0", "2.12.10", "2.11.12"), 79 | scalaVersion in ThisBuild := crossScalaVersions.value.head, 80 | scalacOptions := stdOptions ++ extraOptions(scalaVersion.value), 81 | libraryDependencies ++= compileOnlyDeps ++ testDeps ++ Seq( 82 | compilerPlugin("org.typelevel" %% "kind-projector" % "0.10.3"), 83 | compilerPlugin("com.github.ghik" % "silencer-plugin" % SilencerVersion cross CrossVersion.full) 84 | ), 85 | parallelExecution in Test := true, 86 | incOptions ~= (_.withLogRecompileOnMacro(false)) 87 | ) 88 | } 89 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.3.7 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.2.1") 2 | addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0") 3 | addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1") 4 | addSbtPlugin("com.github.cb372" % "sbt-explicit-dependencies" % "0.2.12") 5 | addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.4.0") 6 | addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.3.5") 7 | addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.0") 8 | -------------------------------------------------------------------------------- /sbt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # A more capable sbt runner, coincidentally also called sbt. 4 | # Author: Paul Phillips 5 | # https://github.com/paulp/sbt-extras 6 | 7 | set -o pipefail 8 | 9 | declare -r sbt_release_version="1.3.2" 10 | declare -r sbt_unreleased_version="1.3.2" 11 | 12 | declare -r latest_213="2.13.1" 13 | declare -r latest_212="2.12.10" 14 | declare -r latest_211="2.11.12" 15 | declare -r latest_210="2.10.7" 16 | declare -r latest_29="2.9.3" 17 | declare -r latest_28="2.8.2" 18 | 19 | declare -r buildProps="project/build.properties" 20 | 21 | declare -r sbt_launch_ivy_release_repo="https://repo.typesafe.com/typesafe/ivy-releases" 22 | declare -r sbt_launch_ivy_snapshot_repo="https://repo.scala-sbt.org/scalasbt/ivy-snapshots" 23 | declare -r sbt_launch_mvn_release_repo="https://repo.scala-sbt.org/scalasbt/maven-releases" 24 | declare -r sbt_launch_mvn_snapshot_repo="https://repo.scala-sbt.org/scalasbt/maven-snapshots" 25 | 26 | declare -r default_jvm_opts_common="-Xms512m -Xss2m" 27 | declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" 28 | 29 | declare sbt_jar sbt_dir sbt_create sbt_version sbt_script sbt_new 30 | declare sbt_explicit_version 31 | declare verbose noshare batch trace_level 32 | declare debugUs 33 | 34 | declare java_cmd="java" 35 | declare sbt_launch_dir="$HOME/.sbt/launchers" 36 | declare sbt_launch_repo 37 | 38 | # pull -J and -D options to give to java. 39 | declare -a java_args scalac_args sbt_commands residual_args 40 | 41 | # args to jvm/sbt via files or environment variables 42 | declare -a extra_jvm_opts extra_sbt_opts 43 | 44 | echoerr () { echo >&2 "$@"; } 45 | vlog () { [[ -n "$verbose" ]] && echoerr "$@"; } 46 | die () { echo "Aborting: $*" ; exit 1; } 47 | 48 | setTrapExit () { 49 | # save stty and trap exit, to ensure echo is re-enabled if we are interrupted. 50 | SBT_STTY="$(stty -g 2>/dev/null)" 51 | export SBT_STTY 52 | 53 | # restore stty settings (echo in particular) 54 | onSbtRunnerExit() { 55 | [ -t 0 ] || return 56 | vlog "" 57 | vlog "restoring stty: $SBT_STTY" 58 | stty "$SBT_STTY" 59 | } 60 | 61 | vlog "saving stty: $SBT_STTY" 62 | trap onSbtRunnerExit EXIT 63 | } 64 | 65 | # this seems to cover the bases on OSX, and someone will 66 | # have to tell me about the others. 67 | get_script_path () { 68 | local path="$1" 69 | [[ -L "$path" ]] || { echo "$path" ; return; } 70 | 71 | local -r target="$(readlink "$path")" 72 | if [[ "${target:0:1}" == "/" ]]; then 73 | echo "$target" 74 | else 75 | echo "${path%/*}/$target" 76 | fi 77 | } 78 | 79 | script_path="$(get_script_path "${BASH_SOURCE[0]}")" 80 | declare -r script_path 81 | script_name="${script_path##*/}" 82 | declare -r script_name 83 | 84 | init_default_option_file () { 85 | local overriding_var="${!1}" 86 | local default_file="$2" 87 | if [[ ! -r "$default_file" && "$overriding_var" =~ ^@(.*)$ ]]; then 88 | local envvar_file="${BASH_REMATCH[1]}" 89 | if [[ -r "$envvar_file" ]]; then 90 | default_file="$envvar_file" 91 | fi 92 | fi 93 | echo "$default_file" 94 | } 95 | 96 | sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)" 97 | jvm_opts_file="$(init_default_option_file JVM_OPTS .jvmopts)" 98 | 99 | build_props_sbt () { 100 | [[ -r "$buildProps" ]] && \ 101 | grep '^sbt\.version' "$buildProps" | tr '=\r' ' ' | awk '{ print $2; }' 102 | } 103 | 104 | set_sbt_version () { 105 | sbt_version="${sbt_explicit_version:-$(build_props_sbt)}" 106 | [[ -n "$sbt_version" ]] || sbt_version=$sbt_release_version 107 | export sbt_version 108 | } 109 | 110 | url_base () { 111 | local version="$1" 112 | 113 | case "$version" in 114 | 0.7.*) echo "https://simple-build-tool.googlecode.com" ;; 115 | 0.10.* ) echo "$sbt_launch_ivy_release_repo" ;; 116 | 0.11.[12]) echo "$sbt_launch_ivy_release_repo" ;; 117 | 0.*-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]) # ie "*-yyyymmdd-hhMMss" 118 | echo "$sbt_launch_ivy_snapshot_repo" ;; 119 | 0.*) echo "$sbt_launch_ivy_release_repo" ;; 120 | *-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]) # ie "*-yyyymmdd-hhMMss" 121 | echo "$sbt_launch_mvn_snapshot_repo" ;; 122 | *) echo "$sbt_launch_mvn_release_repo" ;; 123 | esac 124 | } 125 | 126 | make_url () { 127 | local version="$1" 128 | 129 | local base="${sbt_launch_repo:-$(url_base "$version")}" 130 | 131 | case "$version" in 132 | 0.7.*) echo "$base/files/sbt-launch-0.7.7.jar" ;; 133 | 0.10.* ) echo "$base/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;; 134 | 0.11.[12]) echo "$base/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;; 135 | 0.*) echo "$base/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" ;; 136 | *) echo "$base/org/scala-sbt/sbt-launch/$version/sbt-launch-${version}.jar" ;; 137 | esac 138 | } 139 | 140 | addJava () { vlog "[addJava] arg = '$1'" ; java_args+=("$1"); } 141 | addSbt () { vlog "[addSbt] arg = '$1'" ; sbt_commands+=("$1"); } 142 | addScalac () { vlog "[addScalac] arg = '$1'" ; scalac_args+=("$1"); } 143 | addResidual () { vlog "[residual] arg = '$1'" ; residual_args+=("$1"); } 144 | 145 | addResolver () { addSbt "set resolvers += $1"; } 146 | addDebugger () { addJava "-Xdebug" ; addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"; } 147 | setThisBuild () { 148 | vlog "[addBuild] args = '$*'" 149 | local key="$1" && shift 150 | addSbt "set $key in ThisBuild := $*" 151 | } 152 | setScalaVersion () { 153 | [[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")' 154 | addSbt "++ $1" 155 | } 156 | setJavaHome () { 157 | java_cmd="$1/bin/java" 158 | setThisBuild javaHome "_root_.scala.Some(file(\"$1\"))" 159 | export JAVA_HOME="$1" 160 | export JDK_HOME="$1" 161 | export PATH="$JAVA_HOME/bin:$PATH" 162 | } 163 | 164 | getJavaVersion() { 165 | local -r str=$("$1" -version 2>&1 | grep -E -e '(java|openjdk) version' | awk '{ print $3 }' | tr -d '"') 166 | 167 | # java -version on java8 says 1.8.x 168 | # but on 9 and 10 it's 9.x.y and 10.x.y. 169 | if [[ "$str" =~ ^1\.([0-9]+)\..*$ ]]; then 170 | echo "${BASH_REMATCH[1]}" 171 | elif [[ "$str" =~ ^([0-9]+)\..*$ ]]; then 172 | echo "${BASH_REMATCH[1]}" 173 | elif [[ -n "$str" ]]; then 174 | echoerr "Can't parse java version from: $str" 175 | fi 176 | } 177 | 178 | checkJava() { 179 | # Warn if there is a Java version mismatch between PATH and JAVA_HOME/JDK_HOME 180 | 181 | [[ -n "$JAVA_HOME" && -e "$JAVA_HOME/bin/java" ]] && java="$JAVA_HOME/bin/java" 182 | [[ -n "$JDK_HOME" && -e "$JDK_HOME/lib/tools.jar" ]] && java="$JDK_HOME/bin/java" 183 | 184 | if [[ -n "$java" ]]; then 185 | pathJavaVersion=$(getJavaVersion java) 186 | homeJavaVersion=$(getJavaVersion "$java") 187 | if [[ "$pathJavaVersion" != "$homeJavaVersion" ]]; then 188 | echoerr "Warning: Java version mismatch between PATH and JAVA_HOME/JDK_HOME, sbt will use the one in PATH" 189 | echoerr " Either: fix your PATH, remove JAVA_HOME/JDK_HOME or use -java-home" 190 | echoerr " java version from PATH: $pathJavaVersion" 191 | echoerr " java version from JAVA_HOME/JDK_HOME: $homeJavaVersion" 192 | fi 193 | fi 194 | } 195 | 196 | java_version () { 197 | local -r version=$(getJavaVersion "$java_cmd") 198 | vlog "Detected Java version: $version" 199 | echo "$version" 200 | } 201 | 202 | # MaxPermSize critical on pre-8 JVMs but incurs noisy warning on 8+ 203 | default_jvm_opts () { 204 | local -r v="$(java_version)" 205 | if [[ $v -ge 8 ]]; then 206 | echo "$default_jvm_opts_common" 207 | else 208 | echo "-XX:MaxPermSize=384m $default_jvm_opts_common" 209 | fi 210 | } 211 | 212 | build_props_scala () { 213 | if [[ -r "$buildProps" ]]; then 214 | versionLine="$(grep '^build.scala.versions' "$buildProps")" 215 | versionString="${versionLine##build.scala.versions=}" 216 | echo "${versionString%% .*}" 217 | fi 218 | } 219 | 220 | execRunner () { 221 | # print the arguments one to a line, quoting any containing spaces 222 | vlog "# Executing command line:" && { 223 | for arg; do 224 | if [[ -n "$arg" ]]; then 225 | if printf "%s\n" "$arg" | grep -q ' '; then 226 | printf >&2 "\"%s\"\n" "$arg" 227 | else 228 | printf >&2 "%s\n" "$arg" 229 | fi 230 | fi 231 | done 232 | vlog "" 233 | } 234 | 235 | setTrapExit 236 | 237 | if [[ -n "$batch" ]]; then 238 | "$@" < /dev/null 239 | else 240 | "$@" 241 | fi 242 | } 243 | 244 | jar_url () { make_url "$1"; } 245 | 246 | is_cygwin () { [[ "$(uname -a)" == "CYGWIN"* ]]; } 247 | 248 | jar_file () { 249 | is_cygwin \ 250 | && cygpath -w "$sbt_launch_dir/$1/sbt-launch.jar" \ 251 | || echo "$sbt_launch_dir/$1/sbt-launch.jar" 252 | } 253 | 254 | download_url () { 255 | local url="$1" 256 | local jar="$2" 257 | 258 | mkdir -p "${jar%/*}" && { 259 | if command -v curl > /dev/null 2>&1; then 260 | curl --fail --silent --location "$url" --output "$jar" 261 | elif command -v wget > /dev/null 2>&1; then 262 | wget -q -O "$jar" "$url" 263 | fi 264 | } && [[ -r "$jar" ]] 265 | } 266 | 267 | acquire_sbt_jar () { 268 | { 269 | sbt_jar="$(jar_file "$sbt_version")" 270 | [[ -r "$sbt_jar" ]] 271 | } || { 272 | sbt_jar="$HOME/.ivy2/local/org.scala-sbt/sbt-launch/$sbt_version/jars/sbt-launch.jar" 273 | [[ -r "$sbt_jar" ]] 274 | } || { 275 | sbt_jar="$(jar_file "$sbt_version")" 276 | jar_url="$(make_url "$sbt_version")" 277 | 278 | echoerr "Downloading sbt launcher for ${sbt_version}:" 279 | echoerr " From ${jar_url}" 280 | echoerr " To ${sbt_jar}" 281 | 282 | download_url "${jar_url}" "${sbt_jar}" 283 | 284 | case "${sbt_version}" in 285 | 0.*) vlog "SBT versions < 1.0 do not have published MD5 checksums, skipping check"; echo "" ;; 286 | *) verify_sbt_jar "${sbt_jar}" ;; 287 | esac 288 | } 289 | } 290 | 291 | verify_sbt_jar() { 292 | local jar="${1}" 293 | local md5="${jar}.md5" 294 | 295 | download_url "$(make_url "${sbt_version}").md5" "${md5}" > /dev/null 2>&1 296 | 297 | if command -v md5sum > /dev/null 2>&1; then 298 | if echo "$(cat "${md5}") ${jar}" | md5sum -c -; then 299 | rm -rf "${md5}" 300 | return 0 301 | else 302 | echoerr "Checksum does not match" 303 | return 1 304 | fi 305 | elif command -v md5 > /dev/null 2>&1; then 306 | if [ "$(md5 -q "${jar}")" == "$(cat "${md5}")" ]; then 307 | rm -rf "${md5}" 308 | return 0 309 | else 310 | echoerr "Checksum does not match" 311 | return 1 312 | fi 313 | elif command -v openssl > /dev/null 2>&1; then 314 | if [ "$(openssl md5 -r "${jar}" | awk '{print $1}')" == "$(cat "${md5}")" ]; then 315 | rm -rf "${md5}" 316 | return 0 317 | else 318 | echoerr "Checksum does not match" 319 | return 1 320 | fi 321 | else 322 | echoerr "Could not find an MD5 command" 323 | return 1 324 | fi 325 | } 326 | 327 | usage () { 328 | set_sbt_version 329 | cat < display stack traces with a max of frames (default: -1, traces suppressed) 348 | -debug-inc enable debugging log for the incremental compiler 349 | -no-colors disable ANSI color codes 350 | -sbt-create start sbt even if current directory contains no sbt project 351 | -sbt-dir path to global settings/plugins directory (default: ~/.sbt/) 352 | -sbt-boot path to shared boot directory (default: ~/.sbt/boot in 0.11+) 353 | -ivy path to local Ivy repository (default: ~/.ivy2) 354 | -no-share use all local caches; no sharing 355 | -offline put sbt in offline mode 356 | -jvm-debug Turn on JVM debugging, open at the given port. 357 | -batch Disable interactive mode 358 | -prompt Set the sbt prompt; in expr, 's' is the State and 'e' is Extracted 359 | -script Run the specified file as a scala script 360 | 361 | # sbt version (default: sbt.version from $buildProps if present, otherwise $sbt_release_version) 362 | -sbt-force-latest force the use of the latest release of sbt: $sbt_release_version 363 | -sbt-version use the specified version of sbt (default: $sbt_release_version) 364 | -sbt-dev use the latest pre-release version of sbt: $sbt_unreleased_version 365 | -sbt-jar use the specified jar as the sbt launcher 366 | -sbt-launch-dir directory to hold sbt launchers (default: $sbt_launch_dir) 367 | -sbt-launch-repo repo url for downloading sbt launcher jar (default: $(url_base "$sbt_version")) 368 | 369 | # scala version (default: as chosen by sbt) 370 | -28 use $latest_28 371 | -29 use $latest_29 372 | -210 use $latest_210 373 | -211 use $latest_211 374 | -212 use $latest_212 375 | -213 use $latest_213 376 | -scala-home use the scala build at the specified directory 377 | -scala-version use the specified version of scala 378 | -binary-version use the specified scala version when searching for dependencies 379 | 380 | # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) 381 | -java-home alternate JAVA_HOME 382 | 383 | # passing options to the jvm - note it does NOT use JAVA_OPTS due to pollution 384 | # The default set is used if JVM_OPTS is unset and no -jvm-opts file is found 385 | $(default_jvm_opts) 386 | JVM_OPTS environment variable holding either the jvm args directly, or 387 | the reference to a file containing jvm args if given path is prepended by '@' (e.g. '@/etc/jvmopts') 388 | Note: "@"-file is overridden by local '.jvmopts' or '-jvm-opts' argument. 389 | -jvm-opts file containing jvm args (if not given, .jvmopts in project root is used if present) 390 | -Dkey=val pass -Dkey=val directly to the jvm 391 | -J-X pass option -X directly to the jvm (-J is stripped) 392 | 393 | # passing options to sbt, OR to this runner 394 | SBT_OPTS environment variable holding either the sbt args directly, or 395 | the reference to a file containing sbt args if given path is prepended by '@' (e.g. '@/etc/sbtopts') 396 | Note: "@"-file is overridden by local '.sbtopts' or '-sbt-opts' argument. 397 | -sbt-opts file containing sbt args (if not given, .sbtopts in project root is used if present) 398 | -S-X add -X to sbt's scalacOptions (-S is stripped) 399 | EOM 400 | } 401 | 402 | process_args () { 403 | require_arg () { 404 | local type="$1" 405 | local opt="$2" 406 | local arg="$3" 407 | 408 | if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then 409 | die "$opt requires <$type> argument" 410 | fi 411 | } 412 | while [[ $# -gt 0 ]]; do 413 | case "$1" in 414 | -h|-help) usage; exit 0 ;; 415 | -v) verbose=true && shift ;; 416 | -d) addSbt "--debug" && shift ;; 417 | -w) addSbt "--warn" && shift ;; 418 | -q) addSbt "--error" && shift ;; 419 | -x) debugUs=true && shift ;; 420 | -trace) require_arg integer "$1" "$2" && trace_level="$2" && shift 2 ;; 421 | -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; 422 | -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; 423 | -no-share) noshare=true && shift ;; 424 | -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; 425 | -sbt-dir) require_arg path "$1" "$2" && sbt_dir="$2" && shift 2 ;; 426 | -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; 427 | -offline) addSbt "set offline in Global := true" && shift ;; 428 | -jvm-debug) require_arg port "$1" "$2" && addDebugger "$2" && shift 2 ;; 429 | -batch) batch=true && shift ;; 430 | -prompt) require_arg "expr" "$1" "$2" && setThisBuild shellPrompt "(s => { val e = Project.extract(s) ; $2 })" && shift 2 ;; 431 | -script) require_arg file "$1" "$2" && sbt_script="$2" && addJava "-Dsbt.main.class=sbt.ScriptMain" && shift 2 ;; 432 | 433 | -sbt-create) sbt_create=true && shift ;; 434 | -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;; 435 | -sbt-version) require_arg version "$1" "$2" && sbt_explicit_version="$2" && shift 2 ;; 436 | -sbt-force-latest) sbt_explicit_version="$sbt_release_version" && shift ;; 437 | -sbt-dev) sbt_explicit_version="$sbt_unreleased_version" && shift ;; 438 | -sbt-launch-dir) require_arg path "$1" "$2" && sbt_launch_dir="$2" && shift 2 ;; 439 | -sbt-launch-repo) require_arg path "$1" "$2" && sbt_launch_repo="$2" && shift 2 ;; 440 | -scala-version) require_arg version "$1" "$2" && setScalaVersion "$2" && shift 2 ;; 441 | -binary-version) require_arg version "$1" "$2" && setThisBuild scalaBinaryVersion "\"$2\"" && shift 2 ;; 442 | -scala-home) require_arg path "$1" "$2" && setThisBuild scalaHome "_root_.scala.Some(file(\"$2\"))" && shift 2 ;; 443 | -java-home) require_arg path "$1" "$2" && setJavaHome "$2" && shift 2 ;; 444 | -sbt-opts) require_arg path "$1" "$2" && sbt_opts_file="$2" && shift 2 ;; 445 | -jvm-opts) require_arg path "$1" "$2" && jvm_opts_file="$2" && shift 2 ;; 446 | 447 | -D*) addJava "$1" && shift ;; 448 | -J*) addJava "${1:2}" && shift ;; 449 | -S*) addScalac "${1:2}" && shift ;; 450 | -28) setScalaVersion "$latest_28" && shift ;; 451 | -29) setScalaVersion "$latest_29" && shift ;; 452 | -210) setScalaVersion "$latest_210" && shift ;; 453 | -211) setScalaVersion "$latest_211" && shift ;; 454 | -212) setScalaVersion "$latest_212" && shift ;; 455 | -213) setScalaVersion "$latest_213" && shift ;; 456 | new) sbt_new=true && : ${sbt_explicit_version:=$sbt_release_version} && addResidual "$1" && shift ;; 457 | *) addResidual "$1" && shift ;; 458 | esac 459 | done 460 | } 461 | 462 | # process the direct command line arguments 463 | process_args "$@" 464 | 465 | # skip #-styled comments and blank lines 466 | readConfigFile() { 467 | local end=false 468 | until $end; do 469 | read -r || end=true 470 | [[ $REPLY =~ ^# ]] || [[ -z $REPLY ]] || echo "$REPLY" 471 | done < "$1" 472 | } 473 | 474 | # if there are file/environment sbt_opts, process again so we 475 | # can supply args to this runner 476 | if [[ -r "$sbt_opts_file" ]]; then 477 | vlog "Using sbt options defined in file $sbt_opts_file" 478 | while read -r opt; do extra_sbt_opts+=("$opt"); done < <(readConfigFile "$sbt_opts_file") 479 | elif [[ -n "$SBT_OPTS" && ! ("$SBT_OPTS" =~ ^@.*) ]]; then 480 | vlog "Using sbt options defined in variable \$SBT_OPTS" 481 | IFS=" " read -r -a extra_sbt_opts <<< "$SBT_OPTS" 482 | else 483 | vlog "No extra sbt options have been defined" 484 | fi 485 | 486 | [[ -n "${extra_sbt_opts[*]}" ]] && process_args "${extra_sbt_opts[@]}" 487 | 488 | # reset "$@" to the residual args 489 | set -- "${residual_args[@]}" 490 | argumentCount=$# 491 | 492 | # set sbt version 493 | set_sbt_version 494 | 495 | checkJava 496 | 497 | # only exists in 0.12+ 498 | setTraceLevel() { 499 | case "$sbt_version" in 500 | "0.7."* | "0.10."* | "0.11."* ) echoerr "Cannot set trace level in sbt version $sbt_version" ;; 501 | *) setThisBuild traceLevel "$trace_level" ;; 502 | esac 503 | } 504 | 505 | # set scalacOptions if we were given any -S opts 506 | [[ ${#scalac_args[@]} -eq 0 ]] || addSbt "set scalacOptions in ThisBuild += \"${scalac_args[*]}\"" 507 | 508 | [[ -n "$sbt_explicit_version" && -z "$sbt_new" ]] && addJava "-Dsbt.version=$sbt_explicit_version" 509 | vlog "Detected sbt version $sbt_version" 510 | 511 | if [[ -n "$sbt_script" ]]; then 512 | residual_args=( "$sbt_script" "${residual_args[@]}" ) 513 | else 514 | # no args - alert them there's stuff in here 515 | (( argumentCount > 0 )) || { 516 | vlog "Starting $script_name: invoke with -help for other options" 517 | residual_args=( shell ) 518 | } 519 | fi 520 | 521 | # verify this is an sbt dir, -create was given or user attempts to run a scala script 522 | [[ -r ./build.sbt || -d ./project || -n "$sbt_create" || -n "$sbt_script" || -n "$sbt_new" ]] || { 523 | cat < Unit): Task[T] = 30 | Task.effectSuspendTotalWith { p => 31 | Task.effectAsync { k => 32 | val handler = new CompletionHandler[T, Any] { 33 | def completed(result: T, u: Any): Unit = k(Task.succeed(result)) 34 | 35 | def failed(t: Throwable, u: Any): Unit = t match { 36 | case e if !p.fatal(e) => k(Task.fail(e)) 37 | case _ => k(Task.die(t)) 38 | } 39 | } 40 | 41 | try { 42 | op(handler) 43 | } catch { 44 | case e if !p.fatal(e) => k(Task.fail(e)) 45 | } 46 | } 47 | } 48 | 49 | private def catchFromGet(isFatal: Throwable => Boolean): PartialFunction[Throwable, Task[Nothing]] = { 50 | case e: CompletionException => 51 | Task.fail(e.getCause) 52 | case e: ExecutionException => 53 | Task.fail(e.getCause) 54 | case _: InterruptedException => 55 | Task.interrupt 56 | case e if !isFatal(e) => 57 | Task.fail(e) 58 | } 59 | 60 | private def unwrapDone[A](isFatal: Throwable => Boolean)(f: Future[A]): Task[A] = 61 | try { 62 | Task.succeed(f.get()) 63 | } catch catchFromGet(isFatal) 64 | 65 | def fromCompletionStage[A](csUio: UIO[CompletionStage[A]]): Task[A] = 66 | csUio.flatMap { cs => 67 | Task.effectSuspendTotalWith { p => 68 | val cf = cs.toCompletableFuture 69 | if (cf.isDone) { 70 | unwrapDone(p.fatal)(cf) 71 | } else { 72 | Task.effectAsync { cb => 73 | val _ = cs.handle[Unit] { (v: A, t: Throwable) => 74 | val io = Option(t).fold[Task[A]](Task.succeed(v)) { t => 75 | catchFromGet(p.fatal).lift(t).getOrElse(Task.die(t)) 76 | } 77 | cb(io) 78 | } 79 | } 80 | } 81 | } 82 | } 83 | 84 | /** WARNING: this uses the blocking Future#get, consider using `fromCompletionStage` */ 85 | def fromFutureJava[A](futureUio: UIO[Future[A]]): RIO[Blocking, A] = 86 | futureUio.flatMap { future => 87 | RIO.effectSuspendTotalWith { p => 88 | if (future.isDone) { 89 | unwrapDone(p.fatal)(future) 90 | } else { 91 | blocking(Task.effectSuspend(unwrapDone(p.fatal)(future))) 92 | } 93 | } 94 | } 95 | 96 | implicit class CompletionStageJavaconcurrentOps[A](private val csUio: UIO[CompletionStage[A]]) extends AnyVal { 97 | def toZio: Task[A] = ZIO.fromCompletionStage(csUio) 98 | } 99 | 100 | implicit class FutureJavaconcurrentOps[A](private val futureUio: UIO[Future[A]]) extends AnyVal { 101 | 102 | /** WARNING: this uses the blocking Future#get, consider using `CompletionStage` */ 103 | def toZio: RIO[Blocking, A] = ZIO.fromFutureJava(futureUio) 104 | } 105 | 106 | implicit class ZioObjJavaconcurrentOps(private val zioObj: ZIO.type) extends AnyVal { 107 | def withCompletionHandler[T](op: CompletionHandler[T, Any] => Unit): Task[T] = 108 | javaz.withCompletionHandler(op) 109 | 110 | def fromCompletionStage[A](csUio: UIO[CompletionStage[A]]): Task[A] = javaz.fromCompletionStage(csUio) 111 | 112 | /** WARNING: this uses the blocking Future#get, consider using `fromCompletionStage` */ 113 | def fromFutureJava[A](futureUio: UIO[Future[A]]): RIO[Blocking, A] = javaz.fromFutureJava(futureUio) 114 | } 115 | 116 | implicit class FiberObjOps(private val fiberObj: Fiber.type) extends AnyVal { 117 | def fromCompletionStage[A](thunk: => CompletionStage[A]): Fiber[Throwable, A] = { 118 | lazy val cs: CompletionStage[A] = thunk 119 | 120 | new Fiber[Throwable, A] { 121 | override def await: UIO[Exit[Throwable, A]] = ZIO.fromCompletionStage(UIO.effectTotal(cs)).run 122 | 123 | override def poll: UIO[Option[Exit[Throwable, A]]] = 124 | UIO.effectSuspendTotal { 125 | val cf = cs.toCompletableFuture 126 | if (cf.isDone) { 127 | Task 128 | .effectSuspendWith(p => unwrapDone(p.fatal)(cf)) 129 | .fold(Exit.fail, Exit.succeed) 130 | .map(Some(_)) 131 | } else { 132 | UIO.succeed(None) 133 | } 134 | } 135 | 136 | final def children: UIO[Iterable[Fiber[Any, Any]]] = UIO(Nil) 137 | 138 | final def getRef[A](ref: FiberRef[A]): UIO[A] = UIO(ref.initial) 139 | 140 | final def id: UIO[Option[Fiber.Id]] = UIO.none 141 | 142 | final def interruptAs(id: Fiber.Id): UIO[Exit[Throwable, A]] = join.fold(Exit.fail, Exit.succeed) 143 | 144 | final def inheritRefs: UIO[Unit] = IO.unit 145 | 146 | final def status: UIO[Fiber.Status] = UIO { 147 | // TODO: Avoid toCompletableFuture? 148 | if (thunk.toCompletableFuture.isDone) Status.Done else Status.Running 149 | } 150 | 151 | final def trace: UIO[Option[ZTrace]] = UIO.none 152 | } 153 | } 154 | 155 | /** WARNING: this uses the blocking Future#get, consider using `fromCompletionStage` */ 156 | def fromFutureJava[A](thunk: => Future[A]): Fiber[Throwable, A] = { 157 | lazy val ftr: Future[A] = thunk 158 | 159 | new Fiber[Throwable, A] { 160 | def await: UIO[Exit[Throwable, A]] = 161 | ZIO.fromFutureJava(UIO.effectTotal(ftr)).provide(Blocking.Live).run 162 | 163 | def poll: UIO[Option[Exit[Throwable, A]]] = 164 | UIO.effectSuspendTotal { 165 | if (ftr.isDone) { 166 | Task 167 | .effectSuspendWith(p => unwrapDone(p.fatal)(ftr)) 168 | .fold(Exit.fail, Exit.succeed) 169 | .map(Some(_)) 170 | } else { 171 | UIO.succeed(None) 172 | } 173 | } 174 | 175 | def children: UIO[Iterable[Fiber[Any, Any]]] = UIO(Nil) 176 | 177 | def getRef[A](ref: FiberRef[A]): UIO[A] = UIO(ref.initial) 178 | 179 | def id: UIO[Option[Fiber.Id]] = UIO.none 180 | 181 | def interruptAs(id: Fiber.Id): UIO[Exit[Throwable, A]] = join.fold(Exit.fail, Exit.succeed) 182 | 183 | def inheritRefs: UIO[Unit] = UIO.unit 184 | 185 | def status: UIO[Fiber.Status] = UIO { 186 | if (thunk.isDone) Status.Done else Status.Running 187 | } 188 | 189 | def trace: UIO[Option[ZTrace]] = UIO.none 190 | } 191 | } 192 | } 193 | 194 | /** 195 | * CompletableFuture#failedFuture(Throwable) available only since Java 9 196 | */ 197 | object CompletableFuture_ { 198 | def failedFuture[A](e: Throwable): CompletableFuture[A] = { 199 | val f = new CompletableFuture[A] 200 | f.completeExceptionally(e) 201 | f 202 | } 203 | } 204 | 205 | implicit class TaskCompletableFutureOps[A](private val io: Task[A]) extends AnyVal { 206 | def toCompletableFuture: UIO[CompletableFuture[A]] = 207 | io.fold(CompletableFuture_.failedFuture, CompletableFuture.completedFuture[A]) 208 | } 209 | 210 | implicit class IOCompletableFutureOps[E, A](private val io: IO[E, A]) extends AnyVal { 211 | def toCompletableFutureWith(f: E => Throwable): UIO[CompletableFuture[A]] = 212 | io.mapError(f).toCompletableFuture 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /src/test/scala/zio/interop/JavaSpec.scala: -------------------------------------------------------------------------------- 1 | package zio 2 | package interop 3 | 4 | import _root_.java.net.InetSocketAddress 5 | import _root_.java.nio.ByteBuffer 6 | import _root_.java.nio.channels.{ AsynchronousServerSocketChannel, AsynchronousSocketChannel } 7 | import _root_.java.util.concurrent.{ CompletableFuture, CompletionStage, Future } 8 | import zio.interop.javaz._ 9 | import zio.test.Assertion._ 10 | import zio.test._ 11 | 12 | object JavaSpec { 13 | def spec = suite("JavaSpec")( 14 | suite("`Task.fromFutureJava` must")( 15 | testM("be lazy on the `Future` parameter") { 16 | var evaluated = false 17 | def ftr: Future[Unit] = CompletableFuture.supplyAsync(() => evaluated = true) 18 | assertM(ZIO.fromFutureJava(UIO.effectTotal(ftr)).when(false).as(evaluated), isFalse) 19 | }, 20 | testM("catch exceptions thrown by lazy block") { 21 | val ex = new Exception("no future for you!") 22 | val noFuture: UIO[Future[Unit]] = UIO.effectTotal(throw ex) 23 | assertM(ZIO.fromFutureJava(noFuture).run, dies(equalTo(ex))) 24 | }, 25 | testM("return an `IO` that fails if `Future` fails (failedFuture)") { 26 | val ex = new Exception("no value for you!") 27 | val noValue: UIO[Future[Unit]] = UIO.effectTotal(CompletableFuture_.failedFuture(ex)) 28 | assertM(ZIO.fromFutureJava(noValue).run, fails[Throwable](equalTo(ex))) 29 | }, 30 | testM("return an `IO` that fails if `Future` fails (supplyAsync)") { 31 | val ex = new Exception("no value for you!") 32 | val noValue: UIO[Future[Unit]] = UIO.effectTotal(CompletableFuture.supplyAsync(() => throw ex)) 33 | assertM(ZIO.fromFutureJava(noValue).run, fails[Throwable](equalTo(ex))) 34 | }, 35 | testM("return an `IO` that produces the value from `Future`") { 36 | val someValue: UIO[Future[Int]] = UIO.effectTotal(CompletableFuture.completedFuture(42)) 37 | assertM(ZIO.fromFutureJava(someValue), equalTo(42)) 38 | }, 39 | testM("handle null produced by the completed `Future`") { 40 | val someValue: UIO[Future[String]] = UIO.effectTotal(CompletableFuture.completedFuture[String](null)) 41 | assertM(ZIO.fromFutureJava(someValue).map(Option(_)), isNone) 42 | } 43 | ), 44 | suite("`Task.fromCompletionStage` must")( 45 | testM("be lazy on the `Future` parameter") { 46 | var evaluated = false 47 | def cs: CompletionStage[Unit] = CompletableFuture.supplyAsync(() => evaluated = true) 48 | assertM(ZIO.fromCompletionStage(UIO.effectTotal(cs)).when(false).as(evaluated), isFalse) 49 | }, 50 | testM("catch exceptions thrown by lazy block") { 51 | val ex = new Exception("no future for you!") 52 | val noFuture: UIO[CompletionStage[Unit]] = UIO.effectTotal(throw ex) 53 | assertM(ZIO.fromCompletionStage(noFuture).run, dies(equalTo(ex))) 54 | }, 55 | testM("return an `IO` that fails if `Future` fails (failedFuture)") { 56 | val ex = new Exception("no value for you!") 57 | val noValue: UIO[CompletionStage[Unit]] = UIO.effectTotal(CompletableFuture_.failedFuture(ex)) 58 | assertM(ZIO.fromCompletionStage(noValue).run, fails[Throwable](equalTo(ex))) 59 | }, 60 | testM("return an `IO` that fails if `Future` fails (supplyAsync)") { 61 | val ex = new Exception("no value for you!") 62 | val noValue: UIO[CompletionStage[Unit]] = UIO.effectTotal(CompletableFuture.supplyAsync(() => throw ex)) 63 | assertM(ZIO.fromCompletionStage(noValue).run, fails[Throwable](equalTo(ex))) 64 | }, 65 | testM("return an `IO` that produces the value from `Future`") { 66 | val someValue: UIO[CompletionStage[Int]] = UIO.effectTotal(CompletableFuture.completedFuture(42)) 67 | assertM(ZIO.fromCompletionStage(someValue), equalTo(42)) 68 | }, 69 | testM("handle null produced by the completed `Future`") { 70 | val someValue: UIO[CompletionStage[String]] = UIO.effectTotal(CompletableFuture.completedFuture[String](null)) 71 | assertM(ZIO.fromCompletionStage(someValue).map(Option(_)), isNone) 72 | } 73 | ), 74 | suite("`Task.toCompletableFuture` must")( 75 | testM("produce always a successful `IO` of `Future`") { 76 | val failedIO = IO.fail[Throwable](new Exception("IOs also can fail")) 77 | assertM(failedIO.toCompletableFuture, isSubtype[CompletableFuture[Unit]](anything)) 78 | }, 79 | test("be polymorphic in error type") { 80 | val unitIO: Task[Unit] = Task.unit 81 | val polyIO: IO[String, CompletableFuture[Unit]] = unitIO.toCompletableFuture 82 | assert(polyIO, anything) 83 | }, 84 | testM("return a `CompletableFuture` that fails if `IO` fails") { 85 | val ex = new Exception("IOs also can fail") 86 | val failedIO: Task[Unit] = IO.fail[Throwable](ex) 87 | val failedFuture: Task[Unit] = failedIO.toCompletableFuture.flatMap(f => Task(f.get())) 88 | assertM( 89 | failedFuture.run, 90 | fails[Throwable](hasField("message", _.getMessage, equalTo("java.lang.Exception: IOs also can fail"))) 91 | ) 92 | }, 93 | testM("return a `CompletableFuture` that produces the value from `IO`") { 94 | val someIO = Task.succeed[Int](42) 95 | assertM(someIO.toCompletableFuture.map(_.get()), equalTo(42)) 96 | } 97 | ), 98 | suite("`Task.toCompletableFutureE` must")( 99 | testM("convert error of type `E` to `Throwable`") { 100 | val failedIO: IO[String, Unit] = IO.fail[String]("IOs also can fail") 101 | val failedFuture: Task[Unit] = 102 | failedIO.toCompletableFutureWith(new Exception(_)).flatMap(f => Task(f.get())) 103 | assertM( 104 | failedFuture.run, 105 | fails[Throwable](hasField("message", _.getMessage, equalTo("java.lang.Exception: IOs also can fail"))) 106 | ) 107 | } 108 | ), 109 | suite("`Fiber.fromCompletionStage`")( 110 | test("be lazy on the `Future` parameter") { 111 | var evaluated = false 112 | def ftr: CompletionStage[Unit] = CompletableFuture.supplyAsync(() => evaluated = true) 113 | Fiber.fromCompletionStage(ftr) 114 | assert(evaluated, isFalse) 115 | }, 116 | testM("catch exceptions thrown by lazy block") { 117 | val ex = new Exception("no future for you!") 118 | def noFuture: CompletionStage[Unit] = throw ex 119 | assertM(Fiber.fromCompletionStage(noFuture).join.run, dies(equalTo(ex))) 120 | }, 121 | testM("return an `IO` that fails if `Future` fails (failedFuture)") { 122 | val ex = new Exception("no value for you!") 123 | def noValue: CompletionStage[Unit] = CompletableFuture_.failedFuture(ex) 124 | assertM(Fiber.fromCompletionStage(noValue).join.run, fails[Throwable](equalTo(ex))) 125 | }, 126 | testM("return an `IO` that fails if `Future` fails (supplyAsync)") { 127 | val ex = new Exception("no value for you!") 128 | def noValue: CompletionStage[Unit] = CompletableFuture.supplyAsync(() => throw ex) 129 | assertM(Fiber.fromCompletionStage(noValue).join.run, fails[Throwable](equalTo(ex))) 130 | }, 131 | testM("return an `IO` that produces the value from `Future`") { 132 | def someValue: CompletionStage[Int] = CompletableFuture.completedFuture(42) 133 | assertM(Fiber.fromCompletionStage(someValue).join.run, succeeds(equalTo(42))) 134 | } 135 | ), 136 | suite("`Fiber.fromFutureJava` must")( 137 | test("be lazy on the `Future` parameter") { 138 | var evaluated = false 139 | def ftr: Future[Unit] = CompletableFuture.supplyAsync(() => evaluated = true) 140 | Fiber.fromFutureJava(ftr) 141 | assert(evaluated, isFalse) 142 | }, 143 | testM("catch exceptions thrown by lazy block") { 144 | val ex = new Exception("no future for you!") 145 | def noFuture: Future[Unit] = throw ex 146 | assertM(Fiber.fromFutureJava(noFuture).join.run, dies(equalTo(ex))) 147 | }, 148 | testM("return an `IO` that fails if `Future` fails (failedFuture)") { 149 | val ex = new Exception("no value for you!") 150 | def noValue: Future[Unit] = CompletableFuture_.failedFuture(ex) 151 | assertM(Fiber.fromFutureJava(noValue).join.run, fails[Throwable](equalTo(ex))) 152 | }, 153 | testM("return an `IO` that fails if `Future` fails (failedFuture)") { 154 | val ex = new Exception("no value for you!") 155 | def noValue: Future[Unit] = CompletableFuture.supplyAsync(() => throw ex) 156 | assertM(Fiber.fromFutureJava(noValue).join.run, fails[Throwable](equalTo(ex))) 157 | }, 158 | testM("return an `IO` that produces the value from `Future`") { 159 | def someValue: Future[Int] = CompletableFuture.completedFuture(42) 160 | assertM(Fiber.fromFutureJava(someValue).join.run, succeeds(equalTo(42))) 161 | } 162 | ), 163 | suite("`Task.withCompletionHandler` must")( 164 | testM("write and read to and from AsynchronousSocketChannel") { 165 | val list: List[Byte] = List(13) 166 | val address = new InetSocketAddress(54321) 167 | val server = AsynchronousServerSocketChannel.open().bind(address) 168 | val client = AsynchronousSocketChannel.open() 169 | 170 | val taskServer = for { 171 | c <- ZIO.withCompletionHandler[AsynchronousSocketChannel](server.accept((), _)) 172 | w <- ZIO.withCompletionHandler[Integer](c.write(ByteBuffer.wrap(list.toArray), (), _)) 173 | } yield w 174 | 175 | val taskClient = for { 176 | _ <- ZIO.withCompletionHandler[Void](client.connect(address, (), _)) 177 | buffer = ByteBuffer.allocate(1) 178 | r <- ZIO.withCompletionHandler[Integer](client.read(buffer, (), _)) 179 | } yield (r, buffer.array.toList) 180 | 181 | val task = for { 182 | fiberServer <- taskServer.fork 183 | fiberClient <- taskClient.fork 184 | resultServer <- fiberServer.join 185 | resultClient <- fiberClient.join 186 | _ <- ZIO.effectTotal(server.close()) 187 | } yield (resultServer, resultClient) 188 | 189 | assertM(task.run, succeeds[(Integer, (Integer, List[Byte]))](equalTo((1, (1, list))))) 190 | } 191 | ) 192 | ) 193 | } 194 | 195 | object JavaSpecMain extends DefaultRunnableSpec(JavaSpec.spec) 196 | --------------------------------------------------------------------------------