Skip to content

Commit eff9d18

Browse files
authored
Merge pull request #54 from sbt/wip/scalafmt
Scalafmt
2 parents abf977b + 086acce commit eff9d18

File tree

10 files changed

+157
-50
lines changed

10 files changed

+157
-50
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ jobs:
2424
java-version: ${{ matrix.java }}
2525
cache: 'sbt'
2626

27-
- run: sbt +scripted
27+
- run: sbt -v +scripted
28+
- name: Scalafmt
29+
shell: bash
30+
run: sbt -v clean scalafmtSbtCheck scalafmtCheckAll

.scalafmt.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version = 2.0.0
2+
maxColumn = 120
3+
project.git = true
4+
5+
# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style.
6+
# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala
7+
docstrings = JavaDoc
8+
9+
# This also seems more idiomatic to include whitespace in import x.{ yyy }
10+
spaces.inImportCurlyBraces = true

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ developers := List(
3232
)
3333
description := "An sbt plugin to report on licenses used in a project."
3434
homepage := Some(url(s"https://github.com/$repoSlug"))
35-
pomIncludeRepository := { _ => false }
35+
pomIncludeRepository := { _ =>
36+
false
37+
}
3638
publishMavenStyle := true

project/plugins.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
2+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")

src/main/scala/sbtlicensereport/SbtLicenseReport.scala

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,27 @@ object SbtLicenseReport extends AutoPlugin {
2222

2323
// Keys
2424
val updateLicenses = taskKey[LicenseReport]("Construct a report of used licenses in a project.")
25-
val licenseReportConfigurations = taskKey[Seq[LicenseReportConfiguration]]("Configuration for each license report we're generating.")
25+
val licenseReportConfigurations =
26+
taskKey[Seq[LicenseReportConfiguration]]("Configuration for each license report we're generating.")
2627
val dumpLicenseReport = taskKey[File]("Dumps a report file of the license report (using the target language).")
2728
val licenseReportDir = settingKey[File]("The location where we'll write the license reports.")
2829
val licenseReportStyleRules = settingKey[Option[String]]("The style rules for license report styling.")
2930
val licenseReportTitle = settingKey[String]("The name of the license report.")
3031
val licenseConfigurations = settingKey[Set[String]]("The ivy configurations we wish a report of.")
31-
val licenseSelection = settingKey[Seq[LicenseCategory]]("A priority-order list mechanism we can use to select licenses for projects that have more than one.")
32-
val licenseReportMakeHeader = settingKey[TargetLanguage => String]("A mechanism of generating the header for the license report file.")
32+
val licenseSelection = settingKey[Seq[LicenseCategory]](
33+
"A priority-order list mechanism we can use to select licenses for projects that have more than one."
34+
)
35+
val licenseReportMakeHeader =
36+
settingKey[TargetLanguage => String]("A mechanism of generating the header for the license report file.")
3337
val licenseReportTypes = settingKey[Seq[TargetLanguage]]("The license report files to generate.")
34-
val licenseReportNotes = settingKey[PartialFunction[DepModuleInfo, String]]("A partial functoin that will obtain license report notes based on module.")
35-
val licenseOverrides = settingKey[PartialFunction[DepModuleInfo, LicenseInfo]]("A list of license overrides for artifacts with bad infomration on maven.")
36-
val licenseFilter = settingKey[LicenseCategory => Boolean]("Configuration for what licenses to include in the report, by default.")
38+
val licenseReportNotes = settingKey[PartialFunction[DepModuleInfo, String]](
39+
"A partial functoin that will obtain license report notes based on module."
40+
)
41+
val licenseOverrides = settingKey[PartialFunction[DepModuleInfo, LicenseInfo]](
42+
"A list of license overrides for artifacts with bad infomration on maven."
43+
)
44+
val licenseFilter =
45+
settingKey[LicenseCategory => Boolean]("Configuration for what licenses to include in the report, by default.")
3746
}
3847
// Workaround for broken autoImport in sbt 0.13.5
3948
val autoImport = autoImportImpl
@@ -51,7 +60,13 @@ object SbtLicenseReport extends AutoPlugin {
5160
updateLicenses := {
5261
val ignore = update.value
5362
val overrides = licenseOverrides.value.lift
54-
license.LicenseReport.makeReport(ivyModule.value, licenseConfigurations.value, licenseSelection.value, overrides, streams.value.log)
63+
license.LicenseReport.makeReport(
64+
ivyModule.value,
65+
licenseConfigurations.value,
66+
licenseSelection.value,
67+
overrides,
68+
streams.value.log
69+
)
5570
},
5671
// TODO - A default header.
5772
licenseReportMakeHeader := (language => language.header1(licenseReportTitle.value)),
@@ -65,16 +80,23 @@ object SbtLicenseReport extends AutoPlugin {
6580
// TODO - Configurable language (markdown/html/csv) rather than all always
6681
val reportTypes = licenseReportTypes.value
6782
val notesLookup = licenseReportNotes.value.lift
68-
val config = LicenseReportConfiguration(licenseReportTitle.value, reportTypes, licenseReportMakeHeader.value, notesLookup, licenseFilter.value, dir, styleRules)
83+
val config = LicenseReportConfiguration(
84+
licenseReportTitle.value,
85+
reportTypes,
86+
licenseReportMakeHeader.value,
87+
notesLookup,
88+
licenseFilter.value,
89+
dir,
90+
styleRules
91+
)
6992
Seq(config)
7093
},
7194
dumpLicenseReport := {
7295
val report = updateLicenses.value
7396
val dir = licenseReportDir.value
74-
for(config <- licenseReportConfigurations.value)
97+
for (config <- licenseReportConfigurations.value)
7598
LicenseReport.dumpLicenseReport(report, config)
7699
dir
77100
}
78-
79101
)
80102
}

src/main/scala/sbtlicensereport/license/LicenseCategory.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object LicenseCategory {
1919
override def unapply(license: String): Boolean = {
2020
val name = license.toLowerCase
2121
((name.contains("gpl") || name.contains("general public license")) &&
22-
name.contains("classpath"))
22+
name.contains("classpath"))
2323
}
2424
}
2525
val GPL = LicenseCategory("GPL", Seq("general public license"))

src/main/scala/sbtlicensereport/license/LicenseInfo.scala

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,49 @@ case class LicenseInfo(category: LicenseCategory, name: String, url: String) {
1010

1111
/** Companion object containing several common usage licenses. */
1212
object LicenseInfo {
13-
val GPL2 = LicenseInfo(LicenseCategory.GPL, "GNU General Public License (GPL), Version 2.0", "http://opensource.org/licenses/GPL-2.0")
14-
val GPL3 = LicenseInfo(LicenseCategory.GPL, "GNU General Public License (GPL), Version 3.0", "http://opensource.org/licenses/GPL-3.0")
15-
val LGPL2 = LicenseInfo(LicenseCategory.LGPL, "GNU Library or \"Lesser\" General Public License, Version 2.1 (LGPL-2.1)", "http://opensource.org/licenses/LGPL-2.1")
16-
val LGPL3 = LicenseInfo(LicenseCategory.LGPL, "GNU Library or \"Lesser\" General Public License, Version 3.0 (LGPL-3.0)", "http://opensource.org/licenses/LGPL-3.0")
17-
val CDDL = LicenseInfo(LicenseCategory.CDDL, "Common Development and Distribution License (CDDL-1.0)", "http://opensource.org/licenses/CDDL-1.0")
18-
val CDDL_GPL = LicenseInfo(LicenseCategory.CDDL, "CDDL + GPLv2 License", "https://glassfish.dev.java.net/nonav/public/CDDL+GPL.html")
19-
val APACHE2 = LicenseInfo(LicenseCategory.Apache, "The Apache Software License, Version 2.0", "http://www.apache.org/licenses/LICENSE-2.0.txt")
13+
val GPL2 = LicenseInfo(
14+
LicenseCategory.GPL,
15+
"GNU General Public License (GPL), Version 2.0",
16+
"http://opensource.org/licenses/GPL-2.0"
17+
)
18+
val GPL3 = LicenseInfo(
19+
LicenseCategory.GPL,
20+
"GNU General Public License (GPL), Version 3.0",
21+
"http://opensource.org/licenses/GPL-3.0"
22+
)
23+
val LGPL2 = LicenseInfo(
24+
LicenseCategory.LGPL,
25+
"GNU Library or \"Lesser\" General Public License, Version 2.1 (LGPL-2.1)",
26+
"http://opensource.org/licenses/LGPL-2.1"
27+
)
28+
val LGPL3 = LicenseInfo(
29+
LicenseCategory.LGPL,
30+
"GNU Library or \"Lesser\" General Public License, Version 3.0 (LGPL-3.0)",
31+
"http://opensource.org/licenses/LGPL-3.0"
32+
)
33+
val CDDL = LicenseInfo(
34+
LicenseCategory.CDDL,
35+
"Common Development and Distribution License (CDDL-1.0)",
36+
"http://opensource.org/licenses/CDDL-1.0"
37+
)
38+
val CDDL_GPL = LicenseInfo(
39+
LicenseCategory.CDDL,
40+
"CDDL + GPLv2 License",
41+
"https://glassfish.dev.java.net/nonav/public/CDDL+GPL.html"
42+
)
43+
val APACHE2 = LicenseInfo(
44+
LicenseCategory.Apache,
45+
"The Apache Software License, Version 2.0",
46+
"http://www.apache.org/licenses/LICENSE-2.0.txt"
47+
)
2048
val BSD2 = LicenseInfo(LicenseCategory.BSD, "BSD 2-Clause", "http://opensource.org/licenses/BSD-2-Clause")
2149
val BSD3 = LicenseInfo(LicenseCategory.BSD, "BSD 3-Clause", "http://opensource.org/licenses/BSD-3-Clause")
2250
val MIT = LicenseInfo(LicenseCategory.MIT, "MIT License", "http://opensource.org/licenses/MIT")
2351
val EPL = LicenseInfo(LicenseCategory.EPL, "Eclipse Public License", "https://www.eclipse.org/legal/epl-v10.html")
24-
val EDL = LicenseInfo(LicenseCategory.BSD, "Eclipse Distribution License 1.0", "http://www.eclipse.org/org/documents/edl-v10.php")
52+
val EDL = LicenseInfo(
53+
LicenseCategory.BSD,
54+
"Eclipse Distribution License 1.0",
55+
"http://www.eclipse.org/org/documents/edl-v10.php"
56+
)
2557
val MPL = LicenseInfo(LicenseCategory.Mozilla, "Mozilla Public License 2.0", "https://www.mozilla.org/MPL/2.0/")
2658
}

src/main/scala/sbtlicensereport/license/LicenseReport.scala

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ case class DepModuleInfo(organization: String, name: String, version: String) {
1111
override def toString = s"${organization} # ${name} # ${version}"
1212
}
1313
case class DepLicense(module: DepModuleInfo, license: LicenseInfo, homepage: Option[URL], configs: Set[String]) {
14-
override def toString = s"$module ${homepage.map(url => s" from $url")} on $license in ${configs.mkString("(", ",", ")")}"
14+
override def toString =
15+
s"$module ${homepage.map(url => s" from $url")} on $license in ${configs.mkString("(", ",", ")")}"
1516
}
1617

1718
case class LicenseReport(licenses: Seq[DepLicense], orig: ResolveReport) {
@@ -55,14 +56,10 @@ object LicenseReport {
5556
for (dep <- ordered) {
5657
val licenseLink = language.createHyperLink(dep.license.url, dep.license.name)
5758
val moduleLink = dep.homepage match {
58-
case None => dep.module.toString
59+
case None => dep.module.toString
5960
case Some(url) => language.createHyperLink(url.toExternalForm, dep.module.toString)
6061
}
61-
print(language.tableRow(
62-
dep.license.category.name,
63-
licenseLink,
64-
moduleLink,
65-
notes(dep.module) getOrElse ""))
62+
print(language.tableRow(dep.license.category.name, licenseLink, moduleLink, notes(dep.module) getOrElse ""))
6663
}
6764
print(language.tableEnd)
6865
print(language.documentEnd)
@@ -75,16 +72,25 @@ object LicenseReport {
7572
DepModuleInfo(dep.getModuleId.getOrganisation, dep.getModuleId.getName, dep.getModuleRevision.getId.getRevision)
7673
}
7774

78-
def makeReport(module: IvySbt#Module, configs: Set[String], licenseSelection: Seq[LicenseCategory], overrides: DepModuleInfo => Option[LicenseInfo], log: Logger): LicenseReport = {
75+
def makeReport(
76+
module: IvySbt#Module,
77+
configs: Set[String],
78+
licenseSelection: Seq[LicenseCategory],
79+
overrides: DepModuleInfo => Option[LicenseInfo],
80+
log: Logger
81+
): LicenseReport = {
7982
val (report, err) = resolve(module, log)
8083
err foreach (x => throw x) // Bail on error
8184
makeReportImpl(report, configs, licenseSelection, overrides, log)
8285
}
86+
8387
/**
8488
* given a set of categories and an array of ivy-resolved licenses, pick the first one from our list, or
8589
* default to 'none specified'.
8690
*/
87-
private def pickLicense(categories: Seq[LicenseCategory])(licenses: Array[org.apache.ivy.core.module.descriptor.License]): LicenseInfo = {
91+
private def pickLicense(
92+
categories: Seq[LicenseCategory]
93+
)(licenses: Array[org.apache.ivy.core.module.descriptor.License]): LicenseInfo = {
8894
if (licenses.isEmpty) {
8995
return LicenseInfo(LicenseCategory.NoneSpecified, "", "")
9096
}
@@ -102,35 +108,55 @@ object LicenseReport {
102108
}
103109

104110
/** Picks a single license (or none) for this dependency. */
105-
private def pickLicenseForDep(dep: IvyNode, configs: Set[String], categories: Seq[LicenseCategory]): Option[DepLicense] =
111+
private def pickLicenseForDep(
112+
dep: IvyNode,
113+
configs: Set[String],
114+
categories: Seq[LicenseCategory]
115+
): Option[DepLicense] =
106116
for {
107117
d <- Option(dep)
108118
cs = dep.getRootModuleConfigurations.toSet
109119
filteredConfigs = if (cs.isEmpty) cs else cs.filter(configs)
110120
if !filteredConfigs.isEmpty
111121
if !filteredConfigs.forall(d.isEvicted)
112122
desc <- Option(dep.getDescriptor)
113-
licenses = Option(desc.getLicenses).filterNot(_.isEmpty).getOrElse(Array(new org.apache.ivy.core.module.descriptor.License("none specified", "none specified")))
114-
homepage = Option.apply(desc.getHomePage).flatMap(loc =>
115-
nonFatalCatch[Option[URL]]
116-
.withApply((_: Throwable) => Option.empty[URL])
117-
.apply(Some(url(loc))))
123+
licenses = Option(desc.getLicenses)
124+
.filterNot(_.isEmpty)
125+
.getOrElse(Array(new org.apache.ivy.core.module.descriptor.License("none specified", "none specified")))
126+
homepage = Option
127+
.apply(desc.getHomePage)
128+
.flatMap(
129+
loc =>
130+
nonFatalCatch[Option[URL]]
131+
.withApply((_: Throwable) => Option.empty[URL])
132+
.apply(Some(url(loc)))
133+
)
118134
// TODO - grab configurations.
119135
} yield DepLicense(getModuleInfo(dep), pickLicense(categories)(licenses), homepage, filteredConfigs)
120136

121-
private def getLicenses(report: ResolveReport, configs: Set[String] = Set.empty, categories: Seq[LicenseCategory] = LicenseCategory.all): Seq[DepLicense] = {
137+
private def getLicenses(
138+
report: ResolveReport,
139+
configs: Set[String] = Set.empty,
140+
categories: Seq[LicenseCategory] = LicenseCategory.all
141+
): Seq[DepLicense] = {
122142
import collection.JavaConverters._
123143
for {
124144
dep <- report.getDependencies.asInstanceOf[java.util.List[IvyNode]].asScala
125145
report <- pickLicenseForDep(dep, configs, categories)
126146
} yield report
127147
}
128148

129-
private def makeReportImpl(report: ResolveReport, configs: Set[String], categories: Seq[LicenseCategory], overrides: DepModuleInfo => Option[LicenseInfo], log: Logger): LicenseReport = {
149+
private def makeReportImpl(
150+
report: ResolveReport,
151+
configs: Set[String],
152+
categories: Seq[LicenseCategory],
153+
overrides: DepModuleInfo => Option[LicenseInfo],
154+
log: Logger
155+
): LicenseReport = {
130156
val licenses = getLicenses(report, configs, categories) map { l =>
131157
overrides(l.module) match {
132158
case Some(o) => l.copy(license = o)
133-
case _ => l
159+
case _ => l
134160
}
135161
}
136162
// TODO - Filter for a real report...

src/main/scala/sbtlicensereport/license/LicenseReportConfiguration.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ package license
44
import sbt._
55

66
case class LicenseReportConfiguration(
7-
title: String,
8-
languages: Seq[TargetLanguage],
9-
makeHeader: TargetLanguage => String,
10-
notes: DepModuleInfo => Option[String],
11-
licenseFilter: LicenseCategory => Boolean,
12-
reportDir: File,
13-
reportStyleRules: Option[String] = None)
7+
title: String,
8+
languages: Seq[TargetLanguage],
9+
makeHeader: TargetLanguage => String,
10+
notes: DepModuleInfo => Option[String],
11+
licenseFilter: LicenseCategory => Boolean,
12+
reportDir: File,
13+
reportStyleRules: Option[String] = None
14+
)

src/main/scala/sbtlicensereport/license/TargetLanguage.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ package license
77
sealed trait TargetLanguage {
88
def documentStart(title: String, reportStyleRules: Option[String]): String
99
def documentEnd(): String
10+
1011
/** Create a link with the given content string as the displayed text. */
1112
def createHyperLink(link: String, content: String): String
13+
1214
/** Add a blank line. Note: For HTML may not be a blank string. */
1315
def blankLine(): String
16+
1417
/** Creates something equivalent to an html &lt;h1&gt; tag. */
1518
def header1(msg: String): String
19+
1620
/** The syntax for the header of a table. */
1721
def tableHeader(firstColumn: String, secondColumn: String, thirdColumn: String, fourthColumn: String): String
22+
1823
/** The syntax for a row of a table. */
1924
def tableRow(firstColumn: String, secondColumn: String, thirdColumn: String, fourthColumn: String): String
25+
2026
/** And a "table" */
2127
def tableEnd: String
28+
2229
/** File extension for this style of report. */
2330
def ext: String
2431
}
@@ -36,17 +43,18 @@ case object MarkDown extends TargetLanguage {
3643
$firstColumn | $secondColumn | $thirdColumn | $fourthColumn
3744
--- | --- | --- | ---
3845
"""
39-
def tableRow(firstColumn: String, secondColumn: String, thirdColumn: String, fourthColumn: String): String = s"$firstColumn | $secondColumn | $thirdColumn | <notextile>${escapeHtml(fourthColumn)}</notextile>\n"
46+
def tableRow(firstColumn: String, secondColumn: String, thirdColumn: String, fourthColumn: String): String =
47+
s"$firstColumn | $secondColumn | $thirdColumn | <notextile>${escapeHtml(fourthColumn)}</notextile>\n"
4048
def tableEnd: String = "\n"
4149

4250
def markdownEncode(s: String): String = s.flatMap {
4351
case c if (List('*', '`', '[', ']', '#').contains(c)) => "\\" + c
44-
case x => x.toString
52+
case x => x.toString
4553
}
4654

4755
def escapeHtml(s: String): String = Html.htmlEncode(s).flatMap {
4856
case '|' => "&#124;" // it would destroy tables!
49-
case c => c.toString
57+
case c => c.toString
5058
}
5159
}
5260
case object Html extends TargetLanguage {
@@ -68,7 +76,9 @@ case object Html extends TargetLanguage {
6876
<thead><tr><th>$firstColumn</th><th>$secondColumn</th><th>$thirdColumn</th><th>$fourthColumn</th></tr></thead>
6977
<tbody>"""
7078
def tableRow(firstColumn: String, secondColumn: String, thirdColumn: String, fourthColumn: String): String =
71-
s"""<tr><td>${firstColumn}&nbsp;</td><td>${secondColumn}&nbsp;</td><td>${thirdColumn}&nbsp;</td><td>${htmlEncode(fourthColumn)}</td></tr>"""
79+
s"""<tr><td>${firstColumn}&nbsp;</td><td>${secondColumn}&nbsp;</td><td>${thirdColumn}&nbsp;</td><td>${htmlEncode(
80+
fourthColumn
81+
)}</td></tr>"""
7282
def tableEnd: String = "</tbody></table>"
7383

7484
def htmlEncode(s: String) = org.apache.commons.lang3.StringEscapeUtils.escapeHtml4(s)

0 commit comments

Comments
 (0)