Skip to content

Commit

Permalink
Merge pull request #3 from gaurav/scalafmt-and-scalafix
Browse files Browse the repository at this point in the history
Added scalafmt and scalafix
  • Loading branch information
gaurav committed Jun 3, 2022
2 parents e9bef30 + 20a17a1 commit f591be1
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 112 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ jobs:
cache: 'sbt'
- name: Run tests
run: sbt test
- name: Check code style with scalafmt
run: sbt scalafmtCheckAll
- name: Check code style with scalafix
run: sbt "scalafixAll --check"
10 changes: 10 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules = [
ExplicitResultTypes,
NoAutoTupling,
RemoveUnused,
DisableSyntax,
LeakingImplicitClassVal,
NoValInForComprehension,
ProcedureSyntax,
RedundantSyntax
]
2 changes: 2 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 3.5.8
runner.dialect = scala213
21 changes: 17 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import Dependencies._

ThisBuild / scalaVersion := "2.13.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "org.renci"
ThisBuild / scalaVersion := "2.13.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "org.renci"
ThisBuild / organizationName := "RENCI"

// Scalafix binary configuration
ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value)

lazy val root = (project in file("."))
.settings(
name := "BabelValidator",

// Scalafix options
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision,

// Scalac options
scalacOptions ++= Seq(
"-Ywarn-unused",
"-deprecation"
),

// Dependencies
libraryDependencies += scallop,
libraryDependencies += scalaLogging,
Expand All @@ -21,7 +34,7 @@ lazy val root = (project in file("."))
libraryDependencies += scalaTest % Test,

// Test settings.
Test / logBuffered := false // Allows scalatest to do a better job with the output.
Test / logBuffered := false // Allows scalatest to do a better job with the output.
)

// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
3 changes: 2 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ object Dependencies {
lazy val zioStreams = "dev.zio" %% "zio-streams" % zioVersion
lazy val zioJson = "dev.zio" %% "zio-json" % "0.1.5"

lazy val scalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % "3.9.4"
lazy val scalaLogging =
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.4"
lazy val logback = "ch.qos.logback" % "logback-classic" % "1.2.11"

// Testing
Expand Down
7 changes: 7 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// For building an uberJAR that contains all necessary libraries.
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.2.0")

// For scalafmt syntax standardization.
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

// For scalafix code linting and validation.
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.10.0")
149 changes: 89 additions & 60 deletions src/main/scala/org/renci/babel/validator/Validator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@ import org.rogach.scallop._
import zio._
import zio.blocking.Blocking
import zio.console._
import zio.stream.{ZSink, ZStream, ZTransducer}
import zio.stream.ZStream

import java.io.{File, FileOutputStream, PrintStream}
import scala.collection.Set
import scala.collection.immutable.Set

object Validator extends zio.App with LazyLogging {
class Conf(args: Seq[String]) extends ScallopConf(args) {
val babelOutput = trailArg[File](descr = "The current Babel output directory", required = true)
val babelPrevOutput = trailArg[File](descr = "The previous Babel output", required = true)
val babelOutput: ScallopOption[File] = trailArg[File](
descr = "The current Babel output directory",
required = true
)
val babelPrevOutput: ScallopOption[File] =
trailArg[File](descr = "The previous Babel output", required = true)
validateFileIsDirectory(babelOutput)
validateFileIsDirectory(babelPrevOutput)

val filterIn = opt[List[String]](descr = "List of filenames to include (matched using startsWith)")
val filterOut = opt[List[String]](descr = "List of filenames to exclude (matched using startsWith)")
val filterIn: ScallopOption[List[String]] = opt[List[String]](descr =
"List of filenames to include (matched using startsWith)"
)
val filterOut: ScallopOption[List[String]] = opt[List[String]](descr =
"List of filenames to exclude (matched using startsWith)"
)

val nCores = opt[Int](descr = "Number of cores to use")
val nCores: ScallopOption[Int] = opt[Int](descr = "Number of cores to use")

val output = opt[File](descr = "Output file")
val output: ScallopOption[File] = opt[File](descr = "Output file")

verify()
}
Expand All @@ -48,10 +54,14 @@ object Validator extends zio.App with LazyLogging {
true
}

def retrievePairedCompendiaSummaries(babelOutput: BabelOutput, babelPrevOutput: BabelOutput): Seq[(String, Compendium#Summary, Compendium#Summary)] = {
def retrievePairedCompendiaSummaries(
babelOutput: BabelOutput,
babelPrevOutput: BabelOutput
): Seq[(String, Compendium#Summary, Compendium#Summary)] = {
for {
summary <- babelOutput.compendiaSummary
summaryPrev <- babelPrevOutput.compendiaSummary if summaryPrev.filename == summary.filename
summaryPrev <- babelPrevOutput.compendiaSummary
if summaryPrev.filename == summary.filename
} yield {
(summary.filename, summary, summaryPrev)
}
Expand All @@ -67,7 +77,7 @@ object Validator extends zio.App with LazyLogging {
val babelPrevOutput = new BabelOutput(conf.babelPrevOutput())
val output = conf.output.toOption match {
case Some(file) => new PrintStream(new FileOutputStream(file))
case _ => System.out
case _ => System.out
}

/*
Expand All @@ -80,65 +90,84 @@ object Validator extends zio.App with LazyLogging {
return xyz.runDrain
*/

val pairedSummaries = retrievePairedCompendiaSummaries(babelOutput, babelPrevOutput)
val pairedSummaries =
retrievePairedCompendiaSummaries(babelOutput, babelPrevOutput)
output.println("Filename\tCount\tPrevCount\tDiff\tPercentageChange")
ZStream.fromIterable(pairedSummaries)
.mapMParUnordered(conf.nCores())(result => result match {
case (filename: String, summary: Compendium#Summary, prevSummary: Compendium#Summary) if filterFilename(conf, filename) => {
for {
count <- summary.countZIO
prevCount <- prevSummary.countZIO
typesChunk <- (for {
row: Compendium#CompendiumRecord <- summary.typesZStream.collectRight
} yield (row.`type`)).runCollect
typesErrors <- summary.typesZStream.collectLeft.runCollect
prevTypesChunk <- (for {
row: Compendium#CompendiumRecord <- prevSummary.typesZStream.collectRight
} yield (row.`type`)).runCollect

// types <- summary.typesZIO
// prevTypes <- prevSummary.typesZIO
} yield {
output.println(s"${filename}\t${count}\t${prevCount}\t${relativePercentChange(count, prevCount)}")

if (typesErrors.nonEmpty) {
logger.error(s"Types errors: ${typesErrors}")
} else {
val types = typesChunk.toSet
val prevTypes = prevTypesChunk.toSet

val added = types -- prevTypes
val deleted = prevTypes -- types
val changeString = (added.toSeq, deleted.toSeq) match {
case (Seq(), Seq()) => "No change"
case (added, Seq()) => s"Added: ${added}"
case (Seq(), deleted) => s"Deleted: ${added}"
case (added, deleted) => s"Added: ${added}, Deleted: ${deleted}"
ZStream
.fromIterable(pairedSummaries)
.mapMParUnordered(conf.nCores())(result =>
result match {
case (
filename: String,
summary: Compendium#Summary,
prevSummary: Compendium#Summary
) if filterFilename(conf, filename) => {
for {
count <- summary.countZIO
prevCount <- prevSummary.countZIO
typesChunk <- (for {
row: Compendium#CompendiumRecord <-
summary.typesZStream.collectRight
} yield (row.`type`)).runCollect
typesErrors <- summary.typesZStream.collectLeft.runCollect
prevTypesChunk <- (for {
row: Compendium#CompendiumRecord <-
prevSummary.typesZStream.collectRight
} yield (row.`type`)).runCollect

// types <- summary.typesZIO
// prevTypes <- prevSummary.typesZIO
} yield {
output.println(
s"${filename}\t${count}\t${prevCount}\t${relativePercentChange(count, prevCount)}"
)

if (typesErrors.nonEmpty) {
logger.error(s"Types errors: ${typesErrors}")
} else {
val types = typesChunk.toSet
val prevTypes = prevTypesChunk.toSet

val added = types -- prevTypes
val deleted = prevTypes -- types
val changeString = (added.toSeq, deleted.toSeq) match {
case (Seq(), Seq()) => "No change"
case (added, Seq()) => s"Added: ${added}"
case (Seq(), _) => s"Deleted: ${added}"
case (added, deleted) =>
s"Added: ${added}, Deleted: ${deleted}"
}

output.println(
s"${filename}\t${types.mkString(", ")} (${typesChunk.length})\t${prevTypes
.mkString(", ")} (${prevTypesChunk.length})\t${changeString}"
)
}

output.println(s"${filename}\t${types.mkString(", ")} (${typesChunk.length})\t${prevTypes.mkString(", ")} (${prevTypesChunk.length})\t${changeString}")
}
}
case (filename: String, _, _) if !filterFilename(conf, filename) => {
logger.info(s"Skipping ${filename}")
ZIO.succeed(())
}
case abc =>
ZIO.fail(new RuntimeException(s"Invalid paired summary: ${abc}"))
}
case (filename: String, _, _) if !filterFilename(conf, filename) => {
logger.info(s"Skipping ${filename}")
ZIO.succeed()
}
case abc => ZIO.fail(new RuntimeException(s"Invalid paired summary: ${abc}"))
})
)
.runDrain
}

// TODO:
// - Add processing time, preferably broken down by compendium or something (maybe just emit logs?)
// - Some stats on memory usage would be great too

/**
* Entrypoint.
*
* @param args Command line arguments.
*/
def run(args: List[String]) = {
/** Entrypoint.
*
* @param args
* Command line arguments.
*/
def run(
args: List[String]
): URIO[Blocking with Console with Console, ExitCode] = {
diffResults(new Conf(args)).exitCode
}
}
}
10 changes: 6 additions & 4 deletions src/main/scala/org/renci/babel/validator/model/BabelOutput.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package org.renci.babel.validator.model

import java.io.File

/**
* A BabelOutput is a directory containing Babel output results.
*/
/** A BabelOutput is a directory containing Babel output results.
*/
class BabelOutput(root: File) {
def getFilesInDir(dirName: String): Seq[String] = {
val dir = new File(root, dirName)
Expand All @@ -14,7 +13,10 @@ class BabelOutput(root: File) {
}

val compendiaDir = new File(root, "compendia")
lazy val compendia: Seq[Compendium] = getFilesInDir("compendia").map(filename => new Compendium(new File(compendiaDir, filename)))
lazy val compendia: Seq[Compendium] =
getFilesInDir("compendia").map(filename =>
new Compendium(new File(compendiaDir, filename))
)

def compendiaSummary: Seq[Compendium#Summary] = compendia.map(_.summary)

Expand Down
Loading

0 comments on commit f591be1

Please sign in to comment.