Skip to content

Commit

Permalink
refactor: update sbt-by-example
Browse files Browse the repository at this point in the history
I love this guide. I always recommend it to new people getting starting
with sbt. The last couple times I did however they always came back to
me ab out certain things not working anymore, mainly the console
examples. I decided to just go through the whole thing myself and update
all of the examples using a more modern sbt version and updating the
output to match. There is also a couple other choices that I made while
updating things like:

- Changing the weather site we use as an example since it no longer is
  active
- Changing the libraries being used from gigahorse to sttp and Play JSON
  to uJson. I didn't do this _just because_ but rather to more align
  with the libraries that are being used by the scala toolkit. Hopefully
  people will be a bit more familiar with these as I don't assume a
  newcomer to Scala will be using gigahorse or really Play JSON _unless_
  they are using Play!. If this isn't ok, let me know and I can try to
  recreate it using the old libraries, but I think these examples are
  clearer.
  • Loading branch information
ckipp01 committed Aug 4, 2023
1 parent 922107f commit 18f165e
Show file tree
Hide file tree
Showing 18 changed files with 497 additions and 471 deletions.
246 changes: 122 additions & 124 deletions src/reference/00-Getting-Started/02-sbt-by-example.md

Large diffs are not rendered by default.

259 changes: 129 additions & 130 deletions src/reference/es/00-Getting-Started/02-sbt-by-example.md

Large diffs are not rendered by default.

273 changes: 136 additions & 137 deletions src/reference/ja/00-Getting-Started/02-sbt-by-example.md

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/sbt-test/ref/example-library/build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.settings(
name := "Hello",
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.9.2",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.7" % Test,
libraryDependencies ++= Seq(
"com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2",
"com.lihaoyi" %% "ujson" % "3.1.2",
"org.scalatest" %% "scalatest" % "3.2.16" % Test
)
)
5 changes: 3 additions & 2 deletions src/sbt-test/ref/example-scalatest/build.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.settings(
name := "Hello",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.7" % Test,
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.16" % Test
)
2 changes: 1 addition & 1 deletion src/sbt-test/ref/example-scalatest/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> compile
-> testQuick
$copy-file changes/HelloSpec.scala src/test/scala/HelloSpec.scala
$copy-file changes/HelloSpec.scala src/test/scala/example/HelloSpec.scala
> testQuick
15 changes: 10 additions & 5 deletions src/sbt-test/ref/example-sub1/build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.settings(
name := "Hello",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.7" % Test,
libraryDependencies ++= Seq(
"com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2",
"com.lihaoyi" %% "ujson" % "3.1.2",
"org.scalatest" %% "scalatest" % "3.2.16" % Test
)
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
name := "Hello Core"
)
17 changes: 11 additions & 6 deletions src/sbt-test/ref/example-sub2/build.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.7"
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.16"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.settings(
name := "Hello",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += scalaTest % Test,
libraryDependencies ++= Seq(
"com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2",
"com.lihaoyi" %% "ujson" % "3.1.2",
scalaTest % Test
)
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
libraryDependencies += scalaTest % Test,
libraryDependencies += scalaTest % Test
)
17 changes: 11 additions & 6 deletions src/sbt-test/ref/example-sub3/build.sbt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.7"
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.16"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.aggregate(helloCore)
.settings(
name := "Hello",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += scalaTest % Test,
libraryDependencies ++= Seq(
"com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2",
"com.lihaoyi" %% "ujson" % "3.1.2",
scalaTest % Test
)
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
libraryDependencies += scalaTest % Test,
libraryDependencies += scalaTest % Test
)
17 changes: 11 additions & 6 deletions src/sbt-test/ref/example-sub4/build.sbt
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.7"
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.16"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.aggregate(helloCore)
.dependsOn(helloCore)
.settings(
name := "Hello",
libraryDependencies += scalaTest % Test,
libraryDependencies ++= Seq(
"com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2",
"com.lihaoyi" %% "ujson" % "3.1.2",
scalaTest % Test
)
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
libraryDependencies += "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0",
libraryDependencies += scalaTest % Test,
libraryDependencies += scalaTest % Test
)
21 changes: 13 additions & 8 deletions src/sbt-test/ref/example-weather/build.sbt
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.7"
val gigahorse = "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0"
val playJson = "com.typesafe.play" %% "play-json" % "2.9.2"
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.16"
val sttp = "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2"
val ujson = "com.lihaoyi" %% "ujson" % "3.1.2"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.aggregate(helloCore)
.dependsOn(helloCore)
.settings(
name := "Hello",
libraryDependencies += scalaTest % Test,
libraryDependencies += scalaTest % Test
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
libraryDependencies ++= Seq(gigahorse, playJson),
libraryDependencies += scalaTest % Test,
libraryDependencies ++= Seq(
scalaTest % Test,
sttp,
ujson
)
)
21 changes: 13 additions & 8 deletions src/sbt-test/ref/example-weather/changes/build.sbt
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.7"
val gigahorse = "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0"
val playJson = "com.typesafe.play" %% "play-json" % "2.9.2"
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.16"
val sttp = "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2"
val ujson = "com.lihaoyi" %% "ujson" % "3.1.2"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.aggregate(helloCore)
.dependsOn(helloCore)
.enablePlugins(JavaAppPackaging)
.settings(
name := "Hello",
libraryDependencies += scalaTest % Test,
libraryDependencies += scalaTest % Test
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
libraryDependencies ++= Seq(gigahorse, playJson),
libraryDependencies += scalaTest % Test,
libraryDependencies ++= Seq(
scalaTest % Test,
sttp,
ujson
)
)
23 changes: 14 additions & 9 deletions src/sbt-test/ref/example-weather/changes/build3.sbt
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
ThisBuild / version := "0.1.0"
ThisBuild / version := "0.1.0"
ThisBuild / scalaVersion := "2.13.6"
ThisBuild / organization := "com.example"

val scalaTest = "org.scalatest" %% "scalatest" % "3.2.7"
val gigahorse = "com.eed3si9n" %% "gigahorse-okhttp" % "0.5.0"
val playJson = "com.typesafe.play" %% "play-json" % "2.9.2"
val scalaTest = "org.scalatest" %% "scalatest" % "3.2.16"
val sttp = "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2"
val ujson = "com.lihaoyi" %% "ujson" % "3.1.2"

lazy val hello = (project in file("."))
lazy val hello = project
.in(file("."))
.aggregate(helloCore)
.dependsOn(helloCore)
.enablePlugins(JavaAppPackaging)
.settings(
name := "Hello",
libraryDependencies += scalaTest % Test,
libraryDependencies += scalaTest % Test
)

lazy val helloCore = (project in file("core"))
lazy val helloCore = project
.in(file("core"))
.settings(
name := "Hello Core",
libraryDependencies ++= Seq(gigahorse, playJson),
libraryDependencies += scalaTest % Test,
libraryDependencies ++= Seq(
scalaTest % Test,
sttp,
ujson
)
)
2 changes: 1 addition & 1 deletion src/sbt-test/ref/example-weather/changes/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.4")
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.4")
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package example.core

import gigahorse._, support.okhttp.Gigahorse
import scala.concurrent._, duration._
import play.api.libs.json._
import sttp.client4.quick._
import sttp.client4.Response

object Weather {
lazy val http = Gigahorse.http(Gigahorse.config)

def weather: Future[String] = {
val baseUrl = "https://www.metaweather.com/api/location"
val locUrl = baseUrl + "/search/"
val weatherUrl = baseUrl + "/%s/"
val rLoc = Gigahorse.url(locUrl).get.
addQueryString("query" -> "New York")
import ExecutionContext.Implicits.global
for {
loc <- http.run(rLoc, parse)
woeid = (loc \ 0 \ "woeid").get
rWeather = Gigahorse.url(weatherUrl format woeid).get
weather <- http.run(rWeather, parse)
} yield (weather \\ "weather_state_name")(0).as[String].toLowerCase
def weather() = {
val response: Response[String] = quickRequest
.get(
uri"https://api.open-meteo.com/v1/forecast?latitude=$newYorkLatitude&longitude=$newYorkLongitude&current_weather=true"
)
.send()
val json = ujson.read(response.body)
json.obj("current_weather")("temperature").num
}

private def parse = Gigahorse.asString andThen Json.parse
private val newYorkLatitude: Double = 40.7143
private val newYorkLongitude: Double = -74.006
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package example

import scala.concurrent._, duration._
import core.Weather
import example.core.Weather

object Hello {
def main(args: Array[String]): Unit = {
val w = Await.result(Weather.weather, 10.seconds)
println(s"Hello! The weather in New York is $w.")
Weather.http.close()
val temp = Weather.weather()
println(s"Hello! The current temperature in New York is $temp C.")
}
}
File renamed without changes.

0 comments on commit 18f165e

Please sign in to comment.