Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 20ec743

Browse files
committed
Provide current api version as a default in the create task
Closes #135
1 parent 8f24998 commit 20ec743

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ import org.codeoverflow.chatoverflow.build.GUIUtility
125125
import org.codeoverflow.chatoverflow.build.deployment.DeploymentUtility
126126
import org.codeoverflow.chatoverflow.build.plugins.{PluginUtility, PluginCreateWizard}
127127

128-
create := new PluginCreateWizard(streams.value.log).createPluginTask(pluginFolderNames.value)
128+
create := new PluginCreateWizard(streams.value.log).createPluginTask(pluginFolderNames.value, PluginCreateWizard.getApiVersion.value)
129129
fetch := new PluginUtility(streams.value.log).fetchPluginsTask(pluginFolderNames.value, pluginBuildFileName.value,
130130
pluginTargetFolderNames.value, apiProjectPath.value)
131131
copy := new PluginUtility(streams.value.log).copyPluginsTask(pluginFolderNames.value, pluginTargetFolderNames.value, scalaMajorVersion)

build/src/main/scala/org/codeoverflow/chatoverflow/build/plugins/PluginCreateWizard.scala

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import java.io.File
55
import org.codeoverflow.chatoverflow.build.BuildUtils
66
import org.codeoverflow.chatoverflow.build.BuildUtils.withTaskInfo
77
import org.codeoverflow.chatoverflow.build.plugins.PluginCreateWizard.askForInput
8+
import sbt.Keys._
89
import sbt.internal.util.ManagedLogger
10+
import sbt.{Def, Task}
911

1012
import scala.annotation.tailrec
13+
import scala.util.Try
1114

1215
class PluginCreateWizard(logger: ManagedLogger) {
1316

@@ -16,7 +19,7 @@ class PluginCreateWizard(logger: ManagedLogger) {
1619
*
1720
* @param pluginFolderNames All folder names, containing plugin source code. Defined in build.sbt.
1821
*/
19-
def createPluginTask(pluginFolderNames: List[String]): Unit = {
22+
def createPluginTask(pluginFolderNames: List[String], apiVersion: Option[(Int, Int)]): Unit = {
2023
withTaskInfo("CREATE PLUGIN", logger) {
2124

2225
// Plugin folders have to be defined in the build.sbt file first
@@ -66,12 +69,18 @@ class PluginCreateWizard(logger: ManagedLogger) {
6669

6770
// In case we couldn't figure out the api version, maybe because the api project didn't exist, we ask the user for it.
6871
val api = {
69-
val validate = (s: String) => s.nonEmpty && s.forall(_.isDigit) // not empty and must be a valid number
70-
val major = askForInput("Please specify the current major version of the api. Check api/build.sbt for it.",
72+
// not empty and must be a valid number, can be skipped if a default is available
73+
val validate = (s: String) => s.nonEmpty && s.forall(_.isDigit) || apiVersion.isDefined
74+
val major = askForInput("Please specify the major version of the api. " +
75+
(if (apiVersion.isEmpty) "Check api/build.sbt for it."
76+
else s"Default is the current version (${apiVersion.get._1})."),
7177
"Major api version", validate, "Api version must be a number")
72-
val minor = askForInput("Please specify the current minor version of the api. Check api/build.sbt for it.",
78+
val minor = askForInput(s"Please specify the minor version of the api. " +
79+
(if (apiVersion.isEmpty) "Check api/build.sbt for it."
80+
else s"Default is the current version (${apiVersion.get._2})."),
7381
"Minor api version", validate, "Api version must be a number")
74-
(major.toInt, minor.toInt)
82+
83+
(if (major.isEmpty) apiVersion.get._1 else major.toInt, if (minor.isEmpty) apiVersion.get._2 else minor.toInt)
7584
}
7685

7786
// Plugin metadata
@@ -155,4 +164,28 @@ object PluginCreateWizard {
155164
if (input.isEmpty) default
156165
else input
157166
}
167+
168+
/**
169+
* Gets the version of the api, if loaded by sbt.
170+
*
171+
* @return a tuple with major and minor version
172+
*/
173+
def getApiVersion: Def.Initialize[Task[Option[(Int, Int)]]] = Def.task {
174+
val apiVersion: Option[String] = Def.taskDyn {
175+
val apiProject = buildStructure.value.allProjectRefs.find(_.project == "apiProject")
176+
if (apiProject.isDefined)
177+
Def.task[Option[String]] {
178+
Some((apiProject.get / version).value)
179+
}
180+
else
181+
Def.task[Option[String]] {
182+
None // Api hasn't been loaded, probably not fetched
183+
}
184+
}.value
185+
186+
apiVersion.flatMap(ver => Try {
187+
val parts = ver.split("[.-]") // Also split at '-' to get rid of suffixes like -SNAPSHOT
188+
(parts.head.toInt, parts(1).toInt)
189+
}.toOption)
190+
}
158191
}

0 commit comments

Comments
 (0)