-
Notifications
You must be signed in to change notification settings - Fork 105
/
Inputs.scala
222 lines (188 loc) · 6.2 KB
/
Inputs.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.olegych.scastie.api
import play.api.libs.json._
import com.olegych.scastie.buildinfo.BuildInfo
import System.{lineSeparator => nl}
sealed trait BaseInputs {
def code: String
def target: ScalaTarget
}
case class ShortInputs(code: String, target: ScalaTarget) extends BaseInputs
object ShortInputs {
implicit val formatShortInputs: OFormat[ShortInputs] = Json.format[ShortInputs]
}
object Inputs {
val defaultCode = """List("Hello", "World").mkString("", ", ", "!")"""
def default: Inputs = Inputs(
_isWorksheetMode = true,
code = defaultCode,
target = ScalaTarget.Scala3.default,
libraries = Set(),
librariesFromList = List(),
sbtConfigExtra = """|scalacOptions ++= Seq(
| "-deprecation",
| "-encoding", "UTF-8",
| "-feature",
| "-unchecked"
|)""".stripMargin,
sbtConfigSaved = None,
sbtPluginsConfigExtra = "",
sbtPluginsConfigSaved = None,
isShowingInUserProfile = false,
forked = None
)
implicit val formatInputs: OFormat[Inputs] = {
val f = Json.format[Inputs]
OFormat(
Reads { v =>
f.reads(
v.asOpt[JsObject]
.fold(Json.obj())(
_ ++ Json.obj(
"_isWorksheetMode" -> (v \ "isWorksheetMode")
.asOpt[Boolean]
.orElse((v \ "_isWorksheetMode").asOpt[Boolean])
)
)
)
},
f
)
}
}
case class Inputs(
_isWorksheetMode: Boolean,
code: String,
target: ScalaTarget,
libraries: Set[ScalaDependency],
librariesFromList: List[(ScalaDependency, Project)],
sbtConfigExtra: String,
sbtConfigSaved: Option[String],
sbtPluginsConfigExtra: String,
sbtPluginsConfigSaved: Option[String],
isShowingInUserProfile: Boolean,
forked: Option[SnippetId] = None
) extends BaseInputs {
val isWorksheetMode = _isWorksheetMode && target.hasWorksheetMode
val librariesFrom: Map[ScalaDependency, Project] = librariesFromList.toMap
private lazy val sbtInputs: (String, String) = (sbtConfig, sbtPluginsConfig)
def needsReload(other: Inputs): Boolean = {
sbtInputs != other.sbtInputs
}
override def toString: String = {
if (this == Inputs.default) {
"Inputs.default"
} else if (this.copy(code = Inputs.default.code) == Inputs.default) {
"Inputs.default" + nl +
code + nl
} else {
val showSbtConfigExtra =
if (sbtConfigExtra == Inputs.default.sbtConfigExtra) "default"
else sbtConfigExtra
s"""|isWorksheetMode = $isWorksheetMode
|target = $target
|libraries
|${libraries.mkString(nl)}
|
|sbtConfigExtra
|$showSbtConfigExtra
|
|sbtPluginsConfigExtra
|$sbtPluginsConfigExtra
|
|code
|$code
|""".stripMargin
}
}
lazy val isDefault: Boolean = copy(code = "").withSavedConfig == Inputs.default.copy(code = "").withSavedConfig
def modifyConfig(inputs: Inputs => Inputs): Inputs = inputs(this).copy(sbtConfigSaved = None, sbtPluginsConfigSaved = None)
def withSavedConfig: Inputs = copy(sbtConfigSaved = Some(sbtConfigGenerated), sbtPluginsConfigSaved = Some(sbtPluginsConfigGenerated))
def clearDependencies: Inputs = {
modifyConfig {
_.copy(
libraries = Set(),
librariesFromList = Nil
)
}
}
def addScalaDependency(scalaDependency: ScalaDependency, project: Project): Inputs = {
modifyConfig {
_.copy(
libraries = libraries + scalaDependency,
librariesFromList = (librariesFrom + (scalaDependency -> project)).toList
)
}
}
def removeScalaDependency(scalaDependency: ScalaDependency): Inputs = {
modifyConfig {
_.copy(
libraries = libraries.filterNot(_.matches(scalaDependency)),
librariesFromList = librariesFrom.filterNot(_._1.matches(scalaDependency)).toList
)
}
}
def updateScalaDependency(scalaDependency: ScalaDependency, version: String): Inputs = {
val newScalaDependency = scalaDependency.copy(version = version)
val newLibraries = libraries.filterNot(_.matches(scalaDependency)) + newScalaDependency
val newLibrariesFromList = librariesFromList.collect {
case (l, p) if l.matches(scalaDependency) =>
newScalaDependency -> p
case (l, p) => l -> p
}
modifyConfig {
_.copy(
libraries = newLibraries,
librariesFromList = newLibrariesFromList
)
}
}
lazy val sbtConfig: String =
s"""|$sbtConfigGenerated
|$sbtConfigExtra""".stripMargin
lazy val sbtConfigGenerated: String = sbtConfigSaved.getOrElse {
val targetConfig = target.sbtConfig
val optionalTargetDependency =
if (target.hasWorksheetMode) target.runtimeDependency
else None
val allLibraries =
optionalTargetDependency.map(libraries + _).getOrElse(libraries)
val librariesConfig =
if (allLibraries.isEmpty) ""
else if (allLibraries.size == 1) {
s"libraryDependencies += " + target.renderSbt(allLibraries.head)
} else {
val nl = "\n"
val tab = " "
"libraryDependencies ++= " +
allLibraries
.map(target.renderSbt)
.mkString(
"Seq(" + nl + tab,
"," + nl + tab,
nl + ")"
)
}
s"""|$targetConfig
|$librariesConfig""".stripMargin
}
lazy val sbtPluginsConfig: String =
s"""|$sbtPluginsConfigGenerated
|$sbtPluginsConfigExtra""".stripMargin
lazy val sbtPluginsConfigGenerated: String = sbtPluginsConfigSaved.getOrElse {
sbtPluginsConfig0(withSbtScastie = true)
}
private def sbtPluginsConfig0(withSbtScastie: Boolean): String = {
val targetConfig = target.sbtPluginsConfig
val sbtScastie =
if (withSbtScastie)
s"""addSbtPlugin("org.scastie" % "sbt-scastie" % "${BuildInfo.versionRuntime}")"""
else ""
s"""|$targetConfig
|$sbtScastie""".stripMargin
}
}
object EditInputs {
implicit val formatEditInputs: OFormat[EditInputs] =
Json.format[EditInputs]
}
case class EditInputs(snippetId: SnippetId, inputs: Inputs)