@@ -37,6 +37,34 @@ object PythonBuildPlugin extends AutoPlugin {
3737 val pythonCommand = settingKey[String ](" Python command. Defaults to 'python'" )
3838 val pySetup = inputKey[Int ](" Run 'python setup.py <args>'. Returns exit code." )
3939 val pyWhl = taskKey[File ](" Builds the Python wheel distribution" )
40+ val maven2PEP440 : String => String = {
41+ case VersionNumber (numbers, tags, extras) =>
42+ if (numbers.isEmpty) throw new MessageOnlyException (" Version string is not convertible to PEP440." )
43+
44+ // Reconstruct the primary version number
45+ val base = numbers.mkString(" ." )
46+
47+ // Process items after the `-`. Due to PEP 440 constraints, some tags get converted
48+ // to local version suffixes, while others map directly to prerelease suffixes.
49+ val rc = " ^[Rr][Cc](\\ d+)$" .r
50+ val tag = tags match {
51+ case Seq (" SNAPSHOT" ) => " .dev"
52+ case Seq (rc(num)) => " .rc" + num
53+ case Seq (other) => " .dev+" + other
54+ case many @ Seq (_, _) => " .dev+" + many.mkString(" ." )
55+ case _ => " "
56+ }
57+
58+ // sbt "extras" most closely map to PEP 440 local version suffixes.
59+ // The local version components are separated by `.`, preceded by a single `+`, and not multiple `+` as in sbt.
60+ // These next two expressions do the appropriate separator conversions while concatenating the components.
61+ val ssep = if (tag.contains(" +" )) " ." else " +"
62+ val ext = if (extras.nonEmpty)
63+ extras.map(_.replaceAllLiterally(" +" , " " )).mkString(ssep, " ." , " " )
64+ else " "
65+
66+ base + tag + ext
67+ }
4068 }
4169 import autoImport ._
4270
@@ -97,7 +125,7 @@ object PythonBuildPlugin extends AutoPlugin {
97125 val wd = copyPySources.value
98126 val args = spaceDelimited(" <args>" ).parsed
99127 val cmd = Seq (pythonCommand.value, " setup.py" ) ++ args
100- val ver = version.value
128+ val ver = ( Python / version) .value
101129 s.log.info(s " Running ' ${cmd.mkString(" " )}' in ' $wd' " )
102130 val ec = Process (cmd, wd, " RASTERFRAMES_VERSION" -> ver).!
103131 if (ec != 0 )
@@ -121,6 +149,7 @@ object PythonBuildPlugin extends AutoPlugin {
121149 inConfig(Python )(Seq (
122150 sourceDirectory := (Compile / sourceDirectory).value / " python" ,
123151 sourceDirectories := Seq ((Python / sourceDirectory).value),
152+ version ~= maven2PEP440,
124153 target := (Compile / target).value / " python" ,
125154 includeFilter := " *" ,
126155 excludeFilter := HiddenFileFilter || " __pycache__" || " *.egg-info" ,
0 commit comments