-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathSettings.scala
140 lines (119 loc) · 4.21 KB
/
Settings.scala
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import com.jsuereth.sbtpgp._
import sbt._
import sbt.Keys._
object Settings {
lazy val customSourceGenerators = TaskKey[Seq[sbt.File]]("custom-source-generators")
lazy val generateCustomSources = Seq(
customSourceGenerators := {
val dir = target.value
val f = dir / "Properties.scala"
dir.mkdirs()
def gitCommit =
sys.process.Process(Seq("git", "rev-parse", "HEAD")).!!.trim
val w = new java.io.FileOutputStream(f)
w.write(
s"""package plotly.demo
|
|object Properties {
|
| val version = "${version.value}"
| val commitHash = "$gitCommit"
|
|}
""".stripMargin
.getBytes("UTF-8")
)
w.close()
println(s"Wrote $f")
val files = new collection.mutable.ArrayBuffer[File]
files += f
val tq = "\"\"\""
def process(destDir: File, pathComponents: Seq[String], file: File): Unit = {
if (file.isDirectory) {
val destDir0 = destDir / file.getName
val pathComponents0 = pathComponents :+ file.getName
for (f <- file.listFiles())
process(destDir0, pathComponents0, f)
} else {
val lines = new String(java.nio.file.Files.readAllBytes(file.toPath), "UTF-8").linesIterator.toVector
val demoLines = lines
.dropWhile(!_.contains("demo source start"))
.drop(1)
.takeWhile(!_.contains("demo source end"))
.map(l => l.replace(tq, tq + " + \"\\\"\\\"\\\"\" + " + tq))
if (demoLines.nonEmpty) {
val dest = destDir / (file.getName.stripSuffix(".scala") + "Source.scala")
destDir.mkdirs()
val w = new java.io.FileOutputStream(dest)
w.write(
s"""package plotly${pathComponents.map("." + _).mkString}
|
|object ${file.getName.stripSuffix(".scala")}Source {
|
| val source = $tq${demoLines.mkString("\n")}$tq
|
|}
""".stripMargin
.getBytes("UTF-8")
)
w.close()
files += dest
}
}
}
process(dir / "plotly", Vector(), (Compile / scalaSource).value / "plotly" / "demo")
files
},
(Compile / sourceGenerators) += customSourceGenerators.taskValue
)
private val scala212 = "2.12.20"
private val scala213 = "2.13.14"
private lazy val isAtLeastScala213 = Def.setting {
import Ordering.Implicits._
CrossVersion.partialVersion(scalaVersion.value).exists(_ >= (2, 13))
}
lazy val shared = Def.settings(
crossScalaVersions := Seq(scala213, scala212),
scalaVersion := scala213,
resolvers += "jitpack" at "https://jitpack.io",
libraryDependencies ++= {
if (isAtLeastScala213.value) Nil
else Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full))
},
scalacOptions ++= Seq("-deprecation", "-feature"),
scalacOptions ++= {
if (isAtLeastScala213.value) Seq("-Ymacro-annotations")
else Nil
}
)
lazy val plotlyPrefix = {
name := "plotly-" + name.value
}
val gitLock = new Object
def runCommand(cmd: Seq[String], dir: File): Unit = {
val b = new ProcessBuilder(cmd: _*)
b.directory(dir)
b.inheritIO()
val p = b.start()
val retCode = p.waitFor()
if (retCode != 0)
sys.error(s"Command ${cmd.mkString(" ")} failed (return code $retCode)")
}
lazy val fetchTestData = {
(Test / unmanagedResources) ++= {
val log = streams.value.log
val baseDir = (LocalRootProject / baseDirectory).value
val testsPostsDir = baseDir / "plotly-documentation" / "_posts"
if (!testsPostsDir.exists())
gitLock.synchronized {
if (!testsPostsDir.exists()) {
val cmd = Seq("git", "submodule", "update", "--init", "--recursive", "--", "plotly-documentation")
log.info("Fetching submodule plotly-documentation (this may take some time)")
runCommand(cmd, baseDir)
log.info("Successfully fetched submodule plotly-documentation")
}
}
Nil
}
}
}