Skip to content

Commit

Permalink
Add code macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
zainab-ali committed Jan 16, 2025
1 parent ed6487b commit 1a063f0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
20 changes: 19 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,25 @@ lazy val core = crossProject(JVMPlatform, JSPlatform)
"org.scalameta" %%% "munit" % "1.0.4" % Test,
"org.typelevel" %%% "cats-effect-testkit" % "3.5.7" % Test,
"org.typelevel" %%% "munit-cats-effect" % "2.0.0" % Test,
"com.siriusxm" %%% "snapshot4s-munit" % snapshot4sVersion % Test
("com.siriusxm" %%% "snapshot4s-munit" % snapshot4sVersion % Test)
.exclude(
"com.lihaoyi",
"sourcecode_sjs1_3"
)
.exclude(
"com.lihaoyi",
"sourcecode_3"
)
.exclude(
"com.lihaoyi",
"fansi_3"
),

// scalameta and scalafmt are required for the code macro.
("org.scalameta" %%% "scalameta" % "4.9.9" % Compile)
.cross(CrossVersion.for3Use2_13),
("org.scalameta" %% "scalafmt-core" % "3.8.3" % Compile)
.cross(CrossVersion.for3Use2_13)
),
buildInfoKeys := Seq[BuildInfoKey](ThisBuild / baseDirectory),
buildInfoPackage := "aquascape"
Expand Down
6 changes: 6 additions & 0 deletions core/jvm/src/main/scala/aquascape/PlatformCompanion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ import cats.effect.*
import doodle.core.format.*
import doodle.java2d.*
import doodle.syntax.all.*
import fs2.io.file.Path
import fs2.io.file.Files
import fs2.Stream

trait PlatformCompanion {

def code(text: String, name: String): IO[Unit] =
Files[IO].writeUtf8(Path(s"$name.txt"))(Stream(text)).compile.drain
def draw(picture: Picture[Unit], name: String): IO[Unit] =
picture.writeToIO[Png](s"$name.png")
}
10 changes: 7 additions & 3 deletions core/shared/src/main/scala/aquascape/AquascapeApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package aquascape

import aquascape.drawing.Config
import aquascape.code.StreamCode
import cats.effect.*
import cats.syntax.all.*

Expand All @@ -28,6 +29,8 @@ trait Aquascape {
def config: Config = Config.default

def stream(using Scape[IO]): IO[Unit]

def streamCode(using Scape[IO]): StreamCode = StreamCode(code = None, stream = stream)
}

object AquascapeApp extends PlatformCompanion {
Expand All @@ -36,13 +39,14 @@ object AquascapeApp extends PlatformCompanion {
val name: String,
val chunked: Boolean,
val config: Config,
val stream: Scape[IO] ?=> IO[Unit]
val streamCode: Scape[IO] ?=> StreamCode
)

def run(args: Args): IO[Unit] = for {
scape <- if (args.chunked) Scape.chunked[IO] else Scape.unchunked[IO]
given Scape[IO] = scape
picture <- args.stream(using scape).draw(args.config)
streamCode = args.streamCode(using scape)
picture <- streamCode.stream.draw(args.config)
_ <- draw(picture, args.name)
} yield ()

Expand All @@ -65,5 +69,5 @@ object AquascapeApp extends PlatformCompanion {

trait AquascapeApp extends IOApp.Simple with Aquascape {
final def run: IO[Unit] =
AquascapeApp.run(AquascapeApp.Args(name, chunked, config, stream))
AquascapeApp.run(AquascapeApp.Args(name, chunked, config, streamCode))
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package aquascape.examples
package aquascape.code

import cats.effect.IO
import org.scalafmt.Scalafmt
Expand All @@ -23,7 +23,7 @@ import org.scalafmt.config.ScalafmtConfig
import scala.meta.*
import scala.quoted.*

final case class StreamCode(code: String, stream: IO[Any])
final case class StreamCode(code: Option[String], stream: IO[Any])

inline def code(stream: IO[Any]): StreamCode = ${ codeImpl('stream) }

Expand All @@ -36,7 +36,7 @@ private def codeImpl(
}))
'{
StreamCode(
code = $txt,
code = Some($txt),
stream = $stream
)
}
Expand Down

0 comments on commit 1a063f0

Please sign in to comment.