@@ -5,9 +5,12 @@ import java.io.File
55import org .codeoverflow .chatoverflow .build .BuildUtils
66import org .codeoverflow .chatoverflow .build .BuildUtils .withTaskInfo
77import org .codeoverflow .chatoverflow .build .plugins .PluginCreateWizard .askForInput
8+ import sbt .Keys ._
89import sbt .internal .util .ManagedLogger
10+ import sbt .{Def , Task }
911
1012import scala .annotation .tailrec
13+ import scala .util .Try
1114
1215class 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