-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerateInstructions.sbt
76 lines (69 loc) · 2.52 KB
/
generateInstructions.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
val generateInstallInstructions = taskKey[Unit]("Generate instructions in README.md")
generateInstallInstructions := {
val info =
Def.task((projectID.value, cssDslConfig.value, (publish / skip).value))
.all(ScopeFilter(inAnyProject -- inProjects(ThisProject))).value
.collect { case (id, cfg, false) =>
(id,
id.name.split('_') match {
case Array(p, s) => (p, s)
},
cfg)
}
.sortBy(_._2)(Ordering.Tuple2(Ordering.String, Ordering.String.reverse))
def mdTable(head: Seq[String], rows: Seq[Seq[String]]) = {
val widths =
(head.map(_.length) +: rows.map(_.map(_.length))).transpose.map(_.max)
.zipWithIndex.map(_.swap).toMap.withDefaultValue(0)
def row(xs: Seq[String]) =
xs.zipWithIndex.map { case (s, i) => s.padTo(widths(i), ' ') }.mkString("| ", " | ", " |\n")
row(head) +
widths.values.map("-" * _).mkString("|-", "-|-", "-|\n") +
rows.map(row).mkString
}
def markerText(side: String) = s"<!-- $side autogenerated via sbt generateInstallInstructions -->"
val artifactsTable = mdTable(List("CSS library", "Scala DOM library", "SBT Module ID"), info.map {
case (moduleId, (_, moduleNameSuffix), config) =>
val (op, library) = moduleNameSuffix match {
case "scalatags" => "%%" -> "`scalatags.Text` (JVM)"
case "scalajsreact" => "%%%" -> "scalajs-react (scala.js)"
}
List(
config.name,
library,
s"""`"${moduleId.organization}" $op "${moduleId.name}" % "${moduleId.revision}"`"""
)
})
val importsTable =
mdTable(
List("CSS library", "Prefix", "Import"),
info.flatMap { case (_, _, cfg) =>
cfg.prefixes.map { p =>
List(
cfg.name,
p.fold("None")("`" + _ + "`"),
"`import cssdsl." + cfg.scalaPackage + "." + p.fold("")(_.capitalize) + "Dsl._`"
)
}
}.distinct
)
val block =
s""">${markerText("Begin")}
>
>#### Artifact
>
>$artifactsTable
>
>### Import
>
>$importsTable
>
>${markerText("End")}
>""".stripMargin('>')
val readmeFile = baseDirectory.value / "README.md"
val currentReadme = IO.readLines(readmeFile)
val (before, rest) = currentReadme.span(_.trim != markerText("Begin"))
val after = rest.reverse.takeWhile(_.trim != markerText("End")).reverse
val newReadme = before.map(_ + "\n").mkString + block + after.map(_ + "\n").mkString
IO.write(readmeFile, newReadme)
}