Skip to content

Commit

Permalink
Add license list
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Feb 28, 2022
1 parent fd131ee commit fa8298d
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 8 deletions.
4 changes: 4 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -1122,3 +1122,7 @@ object ci extends Module {
destJavaHome
}
}

def updateLicensesFile() = T.command {
settings.updateLicensesFile()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scala.build.internal

final case class License(
id: String,
name: String,
url: String
)
495 changes: 495 additions & 0 deletions modules/build/src/main/scala/scala/build/internal/Licenses.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scala.build.options

import scala.build.Positioned
import scala.build.errors.{BuildException, MalformedInputError}
import scala.build.internal.Licenses

final case class PublishOptions(
organization: Option[Positioned[String]] = None,
Expand All @@ -28,7 +29,17 @@ object PublishOptions {
def parseLicense(input: Positioned[String]): Either[BuildException, Positioned[License]] =
input.value.split(":", 2) match {
case Array(name) =>
Right(input.map(_ => License(name, "")))
Licenses.map.get(name) match {
case None =>
Left(new MalformedInputError(
"license",
input.value,
"license-id|license-id:url",
input.positions
))
case Some(license) =>
Right(input.map(_ => License(name, license.url)))
}
case Array(name, url) =>
Right(input.map(_ => License(name, url)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ final case class PublishOptions(
name = moduleName.map(_.trim).filter(_.nonEmpty).map(Positioned.commandLine(_)),
version = version.map(_.trim).filter(_.nonEmpty).map(Positioned.commandLine(_)),
url = url.map(_.trim).filter(_.nonEmpty).map(Positioned.commandLine(_)),
license =
license.map(_.trim).filter(_.nonEmpty) match {
case None => None
case Some(input) =>
val license = value(BPublishOptions.parseLicense(input))
Some(Positioned.commandLine(license))
},
license = value {
license
.map(_.trim).filter(_.nonEmpty)
.map(Positioned.commandLine(_))
.map(BPublishOptions.parseLicense(_))
.sequence
},
versionControl = value {
vcs.map(_.trim).filter(_.nonEmpty)
.map(Positioned.commandLine(_))
Expand Down
56 changes: 56 additions & 0 deletions project/settings.sc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.util.zip.{GZIPInputStream, ZipFile}
import mill._, scalalib._
import scala.collection.JavaConverters._
import scala.util.Properties
import upickle.default._

private def isCI = System.getenv("CI") != null

Expand Down Expand Up @@ -857,3 +858,58 @@ trait ScalaCliCrossSbtModule extends CrossSbtModule {
}

def workspaceDirName = ".scala-build"

final case class License(licenseId: String, name: String, reference: String)
object License {
implicit val rw: ReadWriter[License] = macroRW
}
final case class Licenses(licenses: List[License])
object Licenses {
implicit val rw: ReadWriter[Licenses] = macroRW
}

def updateLicensesFile() = {
val url = "https://github.com/spdx/license-list-data/raw/master/json/licenses.json"
var is: InputStream = null
val b =
try {
is = new java.net.URL(url).openStream()
is.readAllBytes()
}
finally if (is != null) is.close()
val content = new String(b, "UTF-8")

val licenses = read[Licenses](content).licenses

System.err.println(s"Found ${licenses.length} licenses")

val licensesCode = licenses
.sortBy(_.licenseId)
.map { license =>
s""" License("${license.licenseId}", "${license.name.replace(
"\"",
"\\\""
)}", "${license.reference}")"""
}
.mkString(",\n")

val genSource =
s"""package scala.build.internal
|
|object Licenses {
| // format: off
| val list = Seq(
|$licensesCode
| )
| // format: on
|
| lazy val map = list.map(l => l.id -> l).toMap
|}
|""".stripMargin

val dest =
os.rel / "modules" / "build" / "src" / "main" / "scala" / "scala" / "build" / "internal" / "Licenses.scala"
os.write.over(os.pwd / dest, genSource)

System.err.println(s"Wrote $dest")
}

0 comments on commit fa8298d

Please sign in to comment.