Skip to content

Commit

Permalink
yaml instead of json (#148)
Browse files Browse the repository at this point in the history
* Add support for YAML: configuration can now also be in bleep.yaml

* fail-fast: false

* snakeyaml 1.30

* GraalVM snapshot

* debug

* quite untested, but switch to yaml only:

concerns the following files:
$PROJECT/bleep.yaml (build file)
$PROJECT/.bleep/bsp/project-selection.yaml (used to only mount some subset of projects in IDE)
$HOME/.config/bleep/config.yaml (user-level config)

* sorting, deduplication

* fmt

* fork and vendor circe-yaml, update to the new snakeyaml library called snakeyaml-engine.

* meh formatting

* dep

* put circe-yaml fork in a better package

Co-authored-by: Øyvind Raddum Berg <elacin@gmail.com>
  • Loading branch information
Ondra Pelech and oyvindberg committed Jun 22, 2022
1 parent d0a8a8d commit acdd7af
Show file tree
Hide file tree
Showing 32 changed files with 3,222 additions and 3,598 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
runs-on: ${{ matrix.os }}
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion bleep-cli/src/main/scala/bleep/CommonOpts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object CommonOpts {
val ignoreVersionInBuildFile: Opts[Boolean] = Opts
.flag(
"ignore-version-in-build-file",
"use the current bleep binary and don't launch the one specified in bleep.json"
"use the current bleep binary and don't launch the one specified in bleep.yaml"
)
.orFalse

Expand Down
7 changes: 3 additions & 4 deletions bleep-cli/src/main/scala/bleep/commands/BuildCreateNew.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package commands

import bleep.SourceLayout.Normal
import bleep.internal.FileUtils.DeleteUnknowns
import bleep.internal.{FileUtils, ShortenAndSortJson, Templates}
import bleep.internal.{asYamlString, FileUtils, Templates}
import bleep.logging.Logger
import cats.data.NonEmptyList
import io.circe.syntax._

import java.nio.file.{Files, Path}

Expand All @@ -32,7 +31,7 @@ case class BuildCreateNew(

syncedFiles.foreach { case (path, synced) => logger.info(s"Wrote $path ($synced)") }

val pre = Prebootstrapped(buildPaths, logger, BuildLoader.Existing(buildPaths.bleepJsonFile))
val pre = Prebootstrapped(buildPaths, logger, BuildLoader.Existing(buildPaths.bleepYamlFile))
val bleepConfig = BleepConfig.lazyForceLoad(pre.userPaths)

bootstrap.from(pre, GenBloopFiles.SyncToDisk, rewrites = Nil, bleepConfig) map { started =>
Expand All @@ -58,7 +57,7 @@ case class BuildCreateNew(
val build = BuildCreateNew.genBuild(exampleFiles, platforms, scalas, name, bleepVersion)

val value = Map[Path, String](
buildPaths.bleepJsonFile -> build.asJson.foldWith(ShortenAndSortJson).spaces2,
buildPaths.bleepYamlFile -> asYamlString(build),
buildPaths.buildDir / exampleFiles.main.relPath -> exampleFiles.main.contents,
buildPaths.buildDir / exampleFiles.test.relPath -> exampleFiles.test.contents
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package bleep
package commands

import bleep.internal.{FileUtils, ShortenAndSortJson, Templates}
import bleep.internal.{asYamlString, FileUtils, Templates}
import bleep.rewrites.normalizeBuild
import io.circe.syntax._

case class BuildReapplyTemplates(started: Started) extends BleepCommand {
override def run(): Either[BuildException, Unit] = {
val normalizedBuild = normalizeBuild(started.build)
val build = Templates.reapply(normalizedBuild, started.rawBuild.templates)

FileUtils.writeString(
started.buildPaths.bleepJsonFile,
build.asJson.foldWith(ShortenAndSortJson).spaces2
)
FileUtils.writeString(started.buildPaths.bleepYamlFile, asYamlString(build))
Right(())
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package bleep
package commands

import bleep.internal.{FileUtils, ShortenAndSortJson, Templates}
import bleep.internal.{asYamlString, FileUtils, Templates}
import bleep.rewrites.normalizeBuild
import io.circe.syntax._

case class BuildReinferTemplates(started: Started, ignoreWhenInferringTemplates: Set[model.ProjectName]) extends BleepCommand {
override def run(): Either[BuildException, Unit] = {
Expand All @@ -21,8 +20,8 @@ case class BuildReinferTemplates(started: Started, ignoreWhenInferringTemplates:
}

FileUtils.writeString(
started.buildPaths.bleepJsonFile,
build.asJson.foldWith(ShortenAndSortJson).spaces2
started.buildPaths.bleepYamlFile,
asYamlString(build)
)
Right(())
}
Expand Down
5 changes: 2 additions & 3 deletions bleep-cli/src/main/scala/bleep/commands/Import.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import bleep.rewrites.normalizeBuild
import bloop.config.Config
import cats.syntax.apply._
import com.monovore.decline.Opts
import io.circe.syntax._

import java.nio.file.{Files, Path}
import scala.jdk.CollectionConverters._
Expand Down Expand Up @@ -181,11 +180,11 @@ addSbtPlugin("build.bleep" % "sbt-export-dependencies" % "0.1.0")

Map(
scriptPath -> scriptSource,
destinationPaths.bleepJsonFile -> buildWithScript.asJson.foldWith(ShortenAndSortJson).spaces2
destinationPaths.bleepYamlFile -> asYamlString(buildWithScript)
)
case None =>
Map(
destinationPaths.bleepJsonFile -> build.asJson.foldWith(ShortenAndSortJson).spaces2
destinationPaths.bleepYamlFile -> asYamlString(build)
)
}
}
Expand Down
5 changes: 2 additions & 3 deletions bleep-cli/src/main/scala/bleep/commands/Patch.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package bleep
package commands

import bleep.internal.FileUtils
import bleep.internal.{asYamlString, FileUtils}
import diffson.circe._
import diffson.jsonpatch.JsonPatch
import io.circe.Json
import io.circe.parser.decode
import io.circe.syntax._

import java.nio.file.{Files, Path}
import scala.io.Source
Expand All @@ -26,7 +25,7 @@ case class Patch(started: Started, file: Option[Path]) extends BleepCommand {

applied match {
case Left(th) => Left(new BuildException.Cause(th, "Couldn't patch build"))
case Right(build) => Right(FileUtils.writeString(started.buildPaths.bleepJsonFile, build.asJson.spaces2))
case Right(build) => Right(FileUtils.writeString(started.buildPaths.bleepYamlFile, asYamlString(build)))
}
}
}
13 changes: 6 additions & 7 deletions bleep-core/src/main/scala/bleep/BleepConfig.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package bleep

import bleep.bsp.CompileServerMode
import bleep.internal.{FileUtils, Lazy}
import bleep.internal.{asYamlString, FileUtils, Lazy}
import bleep.logging.Logger
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.jawn.decode
import io.circe.syntax.EncoderOps
import io.circe.{Decoder, Encoder}

import java.nio.file.Files
Expand All @@ -27,14 +26,14 @@ object BleepConfig {
implicit val encoder: Encoder[BleepConfig] = deriveEncoder

def store(logger: Logger, userPaths: UserPaths, config: BleepConfig): Unit = {
FileUtils.writeString(userPaths.configJson, config.asJson.spaces2)
logger.withContext(userPaths.configJson).debug(s"wrote")
FileUtils.writeString(userPaths.configYaml, asYamlString(config))
logger.withContext(userPaths.configYaml).debug(s"wrote")
}

def load(userPaths: UserPaths): Either[BuildException, Option[BleepConfig]] =
if (FileUtils.exists(userPaths.configJson)) {
decode[BleepConfig](Files.readString(userPaths.configJson)) match {
case Left(e) => Left(new BuildException.InvalidJson(userPaths.configJson, e))
if (FileUtils.exists(userPaths.configYaml)) {
decode[BleepConfig](Files.readString(userPaths.configYaml)) match {
case Left(e) => Left(new BuildException.InvalidJson(userPaths.configYaml, e))
case Right(config) => Right(Some(config))
}
} else Right(None)
Expand Down
16 changes: 10 additions & 6 deletions bleep-core/src/main/scala/bleep/BuildLoader.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package bleep

import bleep.internal.{FileUtils, Lazy}
import io.circe.{parser, Json}
import io.circe.Json
import io.circe.yaml12

import java.nio.file.{Files, Path}
import scala.util.control.NonFatal
Expand Down Expand Up @@ -30,10 +31,10 @@ object BuildLoader {
str.map {
case Left(be) => Left(be)
case Right(jsonStr) =>
val dropComments = jsonStr.linesIterator.filterNot(_.trim().startsWith("//")).mkString("\n")
parser.parse(dropComments) match {
case Left(th) => Left(new BuildException.InvalidJson(bleepJson, th))
case Right(json) => Right(json)
try
yaml12.parser.parse(jsonStr).left.map(th => new BuildException.InvalidJson(bleepJson, th))
catch {
case NonFatal(th) => Left(new BuildException.InvalidJson(bleepJson, th))
}
}

Expand All @@ -50,7 +51,10 @@ object BuildLoader {

def inDirectory(dir: Path): BuildLoader = {
val file = dir / constants.BuildFileName
if (FileUtils.exists(file)) BuildLoader.Existing(file) else BuildLoader.NonExisting(file)
if (FileUtils.exists(file))
BuildLoader.Existing(file)
else
BuildLoader.NonExisting(file)
}

def find(cwd: Path): BuildLoader = {
Expand Down
6 changes: 3 additions & 3 deletions bleep-core/src/main/scala/bleep/BuildPaths.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import bleep.internal.Replacements

import java.nio.file.Path

case class BuildPaths(cwd: Path, bleepJsonFile: Path, mode: BuildPaths.Mode) {
lazy val buildDir = bleepJsonFile.getParent
case class BuildPaths(cwd: Path, bleepYamlFile: Path, mode: BuildPaths.Mode) {
lazy val buildDir = bleepYamlFile.getParent
lazy val bspBleepJsonFile: Path = buildDir / ".bsp" / "bleep.json"
lazy val dotBleepDir: Path = buildDir / ".bleep"
lazy val bleepImportDir: Path = dotBleepDir / "import"
Expand All @@ -20,7 +20,7 @@ case class BuildPaths(cwd: Path, bleepJsonFile: Path, mode: BuildPaths.Mode) {
lazy val bleepBloopDir: Path = dotBleepModeDir / ".bloop"
lazy val digestFile: Path = bleepBloopDir / ".digest"
lazy val logFile: Path = dotBleepModeDir / "last.log"
lazy val bspProjectSelectionJsonFile: Path = dotBleepBspModeDir / "project-selection.json"
lazy val bspProjectSelectionYamlFile: Path = dotBleepBspModeDir / "project-selection.yaml"

final def from(crossName: model.CrossProjectName, p: model.Project): ProjectPaths = {
val dir = buildDir / p.folder.getOrElse(RelPath.force(crossName.name.value))
Expand Down
7 changes: 3 additions & 4 deletions bleep-core/src/main/scala/bleep/ExplodedBuild.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package bleep

import bleep.internal.Functions.stripExtends
import bleep.internal.{rewriteDependentData, ShortenAndSortJson}
import io.circe.syntax._
import bleep.internal.{asYamlString, rewriteDependentData}

import scala.collection.SortedSet
import scala.collection.immutable.SortedMap
Expand Down Expand Up @@ -85,8 +84,8 @@ object ExplodedBuild {
(before.projects.get(projectName), after.projects.get(projectName)) match {
case (Some(before), Some(after)) if after == before => ()
case (Some(before), Some(after)) =>
val onlyInBefore = before.removeAll(after).asJson.foldWith(ShortenAndSortJson).spaces2
val onlyInAfter = Option(after.removeAll(before)).asJson.foldWith(ShortenAndSortJson).spaces2
val onlyInBefore = asYamlString(before.removeAll(after))
val onlyInAfter = asYamlString(Option(after.removeAll(before)))
diffs += ((projectName, s"before: $onlyInBefore, after: $onlyInAfter"))
case (Some(_), None) =>
diffs += ((projectName, "was dropped"))
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/main/scala/bleep/UserPaths.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.nio.file.Path
case class UserPaths(cacheDir: Path, configDir: Path) {
val bspSocketDir = cacheDir / "bsp-socket"
val coursierCacheDir = cacheDir / "coursier"
val configJson = configDir / "config.json"
val configYaml = configDir / "config.yaml"
}

object UserPaths {
Expand Down
13 changes: 6 additions & 7 deletions bleep-core/src/main/scala/bleep/bsp/ProjectSelection.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package bleep.bsp

import bleep.internal.FileUtils
import bleep.internal.{asYamlString, FileUtils}
import bleep.{BuildException, BuildPaths}
import io.circe.syntax.EncoderOps

import java.nio.file.Files

object ProjectSelection {
def store(buildPaths: BuildPaths, maybeSelectedProjectGlobs: Option[List[String]]): Unit =
maybeSelectedProjectGlobs match {
case Some(selectedProjectGlobs) =>
FileUtils.writeString(buildPaths.bspProjectSelectionJsonFile, selectedProjectGlobs.asJson.spaces2)
FileUtils.writeString(buildPaths.bspProjectSelectionYamlFile, asYamlString(selectedProjectGlobs))
case None =>
Files.deleteIfExists(buildPaths.bspProjectSelectionJsonFile)
Files.deleteIfExists(buildPaths.bspProjectSelectionYamlFile)
()
}

def load(buildPaths: BuildPaths): Either[BuildException, Option[List[String]]] =
if (FileUtils.exists(buildPaths.bspProjectSelectionJsonFile)) {
io.circe.parser.decode[List[String]](Files.readString(buildPaths.bspProjectSelectionJsonFile)) match {
case Left(err) => Left(new BuildException.InvalidJson(buildPaths.bspProjectSelectionJsonFile, err))
if (FileUtils.exists(buildPaths.bspProjectSelectionYamlFile)) {
io.circe.parser.decode[List[String]](Files.readString(buildPaths.bspProjectSelectionYamlFile)) match {
case Left(err) => Left(new BuildException.InvalidJson(buildPaths.bspProjectSelectionYamlFile, err))
case Right(selected) => Right(Some(selected))
}
} else Right(None)
Expand Down
2 changes: 1 addition & 1 deletion bleep-core/src/main/scala/bleep/constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object constants {
model.Repository.Maven(None, URI.create(Repositories.central.root))
)

val BuildFileName = "bleep.json"
val BuildFileName = "bleep.yaml"

val ScalaPluginPrefix = "-Xplugin"
}
10 changes: 10 additions & 0 deletions bleep-core/src/main/scala/bleep/internal/package.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bleep

import coursier.Dependency
import io.circe.Encoder
import io.circe.syntax.EncoderOps

package object internal {

Expand Down Expand Up @@ -39,4 +41,12 @@ package object internal {

def throwableMessages(th: Throwable): List[String] =
th.getMessage :: Option(th.getCause).toList.flatMap(throwableMessages)

private val printer = new io.circe.yaml12.Printer(
preserveOrder = true,
dropNullKeys = true
)

def asYamlString[T: Encoder](t: T): String =
printer.pretty(t.asJson.foldWith(ShortenAndSortJson))
}
Loading

0 comments on commit acdd7af

Please sign in to comment.