|
| 1 | +//> using dep org.virtuslab::scala-yaml::0.3.1 |
| 2 | +//> using dep co.fs2::fs2-io::3.12.2 |
| 3 | + |
| 4 | +import cats.effect.{IO, IOApp} |
| 5 | +import cats.syntax.all.* |
| 6 | +import fs2.io.file.{Files, Path, Flags} |
| 7 | +import org.virtuslab.yaml.* |
| 8 | + |
| 9 | +case class OldAuthor( |
| 10 | + full_name: String, |
| 11 | + twitter: Option[String], |
| 12 | + github: Option[String], |
| 13 | + bio: Option[String] |
| 14 | +) derives YamlCodec { |
| 15 | + def toNew(key: String): NewAuthor = { |
| 16 | + // Using GH avatars instead of old `portrait` image for now |
| 17 | + val avatar = github.map(gh => s"https://github.com/$gh.png") |
| 18 | + NewAuthor( |
| 19 | + key = key, |
| 20 | + name = full_name, |
| 21 | + avatar = avatar, |
| 22 | + github = github, |
| 23 | + twitter = twitter, |
| 24 | + bio = bio |
| 25 | + ) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +case class NewAuthor( |
| 30 | + key: String, |
| 31 | + name: String, |
| 32 | + avatar: Option[String], |
| 33 | + github: Option[String], |
| 34 | + twitter: Option[String], |
| 35 | + bio: Option[String] |
| 36 | +) { |
| 37 | + def toHocon: String = { |
| 38 | + val avatarLine = avatar.map(av => s""" avatar: "$av"""") |
| 39 | + val githubLine = github.map(gh => s" github: $gh") |
| 40 | + val twitterLine = twitter.map(tw => s" twitter: $tw") |
| 41 | + val bioLine = bio.map(b => s""" bio: "$b"""") |
| 42 | + |
| 43 | + val lines = List( |
| 44 | + Some(s"$key {"), |
| 45 | + Some(s""" name: "$name""""), |
| 46 | + avatarLine, |
| 47 | + githubLine, |
| 48 | + twitterLine, |
| 49 | + bioLine, |
| 50 | + Some("}") |
| 51 | + ).flatten |
| 52 | + |
| 53 | + lines.mkString("\n") + "\n" |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +object MigrateAuthors extends IOApp.Simple { |
| 58 | + val authorsYamlPath = Path("../typelevel.github.com/_data/authors.yml") |
| 59 | + val directoryConfPath = Path("src/blog/directory.conf") |
| 60 | + val alreadyMigrated = Set("typelevel", "foundation", "djspiewak") |
| 61 | + |
| 62 | + def readAuthorsYaml: IO[String] = Files[IO] |
| 63 | + .readAll(authorsYamlPath) |
| 64 | + .through(fs2.text.utf8.decode) |
| 65 | + .compile |
| 66 | + .string |
| 67 | + |
| 68 | + def appendToDirectoryConf(content: String): IO[Unit] = fs2.Stream |
| 69 | + .emit(content) |
| 70 | + .through(fs2.text.utf8.encode) |
| 71 | + .through(Files[IO].writeAll(directoryConfPath, Flags.Append)) |
| 72 | + .compile |
| 73 | + .drain |
| 74 | + |
| 75 | + def run: IO[Unit] = for { |
| 76 | + yamlContent <- readAuthorsYaml |
| 77 | + authorsMap <- IO.fromEither(yamlContent.as[Map[String, OldAuthor]]) |
| 78 | + newAuthors = authorsMap.toList |
| 79 | + .filterNot { case (key, _) => alreadyMigrated.contains(key) } |
| 80 | + .sortBy(_._1) |
| 81 | + .map { case (key, oldAuthor) => oldAuthor.toNew(key) } |
| 82 | + hoconContent = "\n" + newAuthors.map(_.toHocon).mkString("\n") |
| 83 | + |
| 84 | + _ <- appendToDirectoryConf(hoconContent) |
| 85 | + _ <- IO.println(s"Migrated ${newAuthors.size} authors") |
| 86 | + } yield () |
| 87 | +} |
0 commit comments