Skip to content

Commit

Permalink
Scala 3 (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Jul 7, 2021
1 parent 42c15de commit 815c9e7
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 141 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.6]
scala: [3.0.0]
java: [graalvm-ce-java11@20.3.0]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.6]
scala: [3.0.0]
java: [graalvm-ce-java11@20.3.0]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -101,12 +101,12 @@ jobs:
~/Library/Caches/Coursier/v1
key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

- name: Download target directories (2.13.6)
- name: Download target directories (3.0.0)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-2.13.6-${{ matrix.java }}
name: target-${{ matrix.os }}-3.0.0-${{ matrix.java }}

- name: Inflate target directories (2.13.6)
- name: Inflate target directories (3.0.0)
run: |
tar xf targets.tar
rm targets.tar
Expand Down
3 changes: 2 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version = "2.7.5"
runner.dialect = scala3
version = "3.0.0-RC5"
maxColumn = 140
align.preset = some
align.tokens.add = [
Expand Down
38 changes: 12 additions & 26 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ inThisBuild(
)
)

(ThisBuild / scalaVersion) := "2.13.6"
(ThisBuild / scalaVersion) := "3.0.0"

val GraalVM11 = "graalvm-ce-java11@20.3.0"
ThisBuild / githubWorkflowJavaVersions := Seq(GraalVM11)
Expand Down Expand Up @@ -46,30 +46,18 @@ ThisBuild / libraryDependencySchemes ++= Seq(
def crossPlugin(x: sbt.librarymanagement.ModuleID) = compilerPlugin(x.cross(CrossVersion.full))

val addCompilerPlugins = libraryDependencies ++= {
if (scalaVersion.value.startsWith("2"))
List(
crossPlugin("org.typelevel" % "kind-projector" % "0.13.0"),
crossPlugin("com.github.cb372" % "scala-typed-holes" % "0.1.9"),
crossPlugin("com.kubukoz" % "better-tostring" % "0.3.3"),
//gonna regret this one huh
compilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1")
)
else Nil
}

val addVersionSpecificScalacSettings = scalacOptions ++= {
if (scalaVersion.value.startsWith("2")) Nil
else List("-Ykind-projector")
List(
// https://github.com/polyvariant/better-tostring/issues/56
// crossPlugin("com.kubukoz" % "better-tostring" % "0.3.3")
)
}

val commonSettings = Seq(
scalacOptions -= "-Xfatal-warnings",
scalacOptions ++= Seq("-Ymacro-annotations"),
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % "3.1.1"
),
addCompilerPlugins,
addVersionSpecificScalacSettings
addCompilerPlugins
)

val core = project
Expand Down Expand Up @@ -123,17 +111,15 @@ val next =
.settings(commonSettings)
.settings(
libraryDependencies ++= Seq(
// no macros
"org.typelevel" %% "cats-mtl" % "1.2.1",
"com.monovore" %% "decline-effect" % "2.0.0",
"org.http4s" %% "http4s-dsl" % "1.0.0-M23",
"org.http4s" %% "http4s-blaze-server" % "1.0.0-M23",
"org.http4s" %% "http4s-blaze-client" % "1.0.0-M23",
"org.http4s" %% "http4s-circe" % "1.0.0-M23",
"com.monovore" %% "decline-effect" % "2.1.0",
"org.http4s" %% "http4s-dsl" % "0.23.0-RC1",
"org.http4s" %% "http4s-blaze-server" % "0.23.0-RC1",
"org.http4s" %% "http4s-blaze-client" % "0.23.0-RC1",
"org.http4s" %% "http4s-circe" % "0.23.0-RC1",
"ch.qos.logback" % "logback-classic" % "1.2.3",
"io.circe" %% "circe-parser" % "0.14.1",
// yes macros
"com.github.julien-truffaut" %% "monocle-macro" % "3.0.0-M6"
"dev.optics" %% "monocle-core" % "3.0.0-RC2"
),
buildInfoKeys := Seq[BuildInfoKey](version),
buildInfoPackage := "com.kubukoz.next"
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.3
sbt.version=1.5.4
11 changes: 5 additions & 6 deletions src/main/scala/com/kubukoz/next/Login.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ object Login {
Config
.ask[F]
.map { config =>
Request[F](POST, Uri.uri("https://accounts.spotify.com/api/token"))
Request[F](POST, uri"https://accounts.spotify.com/api/token")
.withEntity(body)
.putHeaders(Authorization(BasicCredentials(config.clientId, config.clientSecret)))
}
.flatMap(client.fetchAs[RefreshedTokenResponse])
.map(_.accessToken)
.map(_.access_token)
.map(Token(_))
}

Expand All @@ -70,8 +70,7 @@ object Login {
private val showUri = Config
.ask[F]
.map { config =>
Uri
.uri("https://accounts.spotify.com/authorize")
uri"https://accounts.spotify.com/authorize"
.withQueryParam("client_id", config.clientId)
.withQueryParam("client_secret", config.clientSecret)
.withQueryParam("scope", scopes.mkString(" "))
Expand Down Expand Up @@ -101,9 +100,9 @@ object Login {
)

client
.expect[TokenResponse](Request[F](POST, Uri.uri("https://accounts.spotify.com/api/token")).withEntity(body))
.expect[TokenResponse](Request[F](POST, uri"https://accounts.spotify.com/api/token").withEntity(body))
.map { response =>
Tokens(Token(response.accessToken), RefreshToken(response.refreshToken))
Tokens(Token(response.access_token), RefreshToken(response.refresh_token))
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/com/kubukoz/next/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import com.monovore.decline.effect._

import java.io.EOFException

sealed trait Choice extends Product with Serializable
enum Choice {
case Login
case SkipTrack
case DropTrack
case FastForward(percentage: Int)
}

object Choice {
case object Login extends Choice
case object SkipTrack extends Choice
case object DropTrack extends Choice
final case class FastForward(percentage: Int) extends Choice

val ffOpts = Opts.argument[Int]("step").map(FastForward).withDefault(FastForward(10))
val ffOpts = Opts.argument[Int]("step").map(FastForward(_)).withDefault(FastForward(10))

val opts: Opts[Choice] =
NonEmptyList
Expand Down Expand Up @@ -58,7 +58,7 @@ object Main extends CommandIOApp(name = "spotify-next", header = "spotify-next:
implicit val login: Login[F] = Login.blaze[F](rawClient)
implicit val loginProcess: LoginProcess[F] = LoginProcess.instance

implicit val spotify = makeSpotify(apiClient[F].apply(rawClient))
implicit val spotify: Spotify[F] = makeSpotify(apiClient[F].apply(rawClient))

Runner.instance[F]
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/kubukoz/next/Program.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object Program {

def makeSpotify[F[_]: UserOutput: Concurrent](client: Client[F]): Spotify[F] = {
implicit val theClient = client
implicit val playback = Spotify.Playback.spotify[F](client)
implicit val playback: Spotify.Playback[F] = Spotify.Playback.spotify[F](client)

Spotify.instance[F]
}
Expand Down
23 changes: 13 additions & 10 deletions src/main/scala/com/kubukoz/next/Spotify.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ object Spotify {

def apply[F[_]](implicit F: Spotify[F]): Spotify[F] = F

sealed trait Error extends Throwable
case object NotPlaying extends Error
case class InvalidStatus(status: Status) extends Error
case object NoContext extends Error
case object NoItem extends Error
final case class InvalidContext[T](ctx: T) extends Error
final case class InvalidItem[T](ctx: T) extends Error
enum Error extends Throwable {
case NotPlaying
case InvalidStatus(status: Status)
case NoContext
case NoItem
case InvalidContext[T](ctx: T)
case InvalidItem[T](item: T)
}

import Error._

def instance[F[_]: Playback: Client: UserOutput: Concurrent]: Spotify[F] =
new Spotify[F] {
Expand Down Expand Up @@ -69,8 +72,8 @@ object Spotify {
.run(client)
.flatMap(requireTrack)
.fproduct { player =>
val currentLength = player.progressMs
val totalLength = player.item.durationMs
val currentLength = player.progress_ms
val totalLength = player.item.duration_ms
((currentLength * 100 / totalLength) + percentage)
}
.flatMap {
Expand All @@ -79,7 +82,7 @@ object Spotify {
Playback[F].seek(0)

case (player, desiredProgressPercent) =>
val desiredProgressMs = desiredProgressPercent * player.item.durationMs / 100
val desiredProgressMs = desiredProgressPercent * player.item.duration_ms / 100
UserOutput[F].print(UserMessage.Seeking(desiredProgressPercent)) *>
Playback[F].seek(desiredProgressMs)
}
Expand Down
23 changes: 10 additions & 13 deletions src/main/scala/com/kubukoz/next/UserMessage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ import org.http4s.Uri

import java.nio.file.Path

sealed trait UserMessage extends Product with Serializable

object UserMessage {
final case class GoToUri(uri: Uri) extends UserMessage
final case class ConfigFileNotFound(path: Path, validInput: String) extends UserMessage
final case class SavedConfig(path: Path) extends UserMessage
case object SavedToken extends UserMessage
case object RefreshedToken extends UserMessage

enum UserMessage {
case GoToUri(uri: Uri)
case ConfigFileNotFound(path: Path, validInput: String)
case SavedConfig(path: Path)
case SavedToken
case RefreshedToken
// playback
case object SwitchingToNext extends UserMessage
final case class RemovingCurrentTrack(player: Player[PlayerContext.playlist, Item.track]) extends UserMessage
case object TooCloseToEnd extends UserMessage
final case class Seeking(desiredProgressPercent: Int) extends UserMessage
case SwitchingToNext
case RemovingCurrentTrack(player: Player[PlayerContext.playlist, Item.track])
case TooCloseToEnd
case Seeking(desiredProgressPercent: Int)
}

trait UserOutput[F[_]] {
Expand Down
Loading

0 comments on commit 815c9e7

Please sign in to comment.