~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to project/Versions.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sbt._
 
2
import Keys._
 
3
import java.util.Properties
 
4
import scala.util.control.Exception.catching
 
5
import java.lang.{NumberFormatException => NFE}
 
6
import java.io.FileInputStream
 
7
import com.jsuereth.git.GitRunner
 
8
import com.jsuereth.git.GitKeys.gitRunner
 
9
 
 
10
case class VersionInfo(canonical: String,
 
11
                       maven: String,
 
12
                       osgi: String)
 
13
 
 
14
/** this file is responsible for setting up Scala versioning schemes and updating all the necessary bits. */
 
15
object Versions {
 
16
  val buildNumberFile = SettingKey[File]("scala-build-number-file")
 
17
  // TODO - Make this a setting?
 
18
  val buildNumberProps = SettingKey[BaseBuildNumber]("scala-build-number-props")
 
19
  val buildRelease = SettingKey[Boolean]("scala-build-release", "This is set to true if we're building a release.")
 
20
  val mavenSuffix = SettingKey[String]("scala-maven-suffix", "This is set to whatever maven suffix is required.")
 
21
 
 
22
  val gitSha = TaskKey[String]("scala-git-sha", "The sha of the current git commit.")
 
23
  val gitDate = TaskKey[String]("scala-git-date", "The date of the current git commit.")
 
24
 
 
25
  val mavenVersion = SettingKey[String]("scala-maven-version", "The maven version number.")
 
26
  val osgiVersion = TaskKey[String]("scala-osgi-version", "The OSGi version number.")
 
27
  val canonicalVersion = TaskKey[String]("scala-canonical-version", "The canonical version number.")
 
28
 
 
29
  val scalaVersions = TaskKey[VersionInfo]("scala-version-info", "The scala versions used for this build.")
 
30
  
 
31
 
 
32
  
 
33
  def settings: Seq[Setting[_]] = Seq(
 
34
    buildNumberFile <<= baseDirectory apply (_ / "build.number"),
 
35
    buildNumberProps <<= buildNumberFile apply loadBuildNumberProps,
 
36
    buildRelease := Option(System.getProperty("build.release")) map (!_.isEmpty) getOrElse false,
 
37
    mavenSuffix <<= buildRelease apply pickMavenSuffix,
 
38
    mavenVersion <<= (buildNumberProps, mavenSuffix) apply makeMavenVersion,
 
39
    gitSha <<= (gitRunner, baseDirectory, streams) map getGitSha,
 
40
    gitDate <<= (gitRunner, baseDirectory, streams) map getGitDate,
 
41
    osgiVersion <<= (buildNumberProps, gitDate, gitSha) map makeOsgiVersion,
 
42
    canonicalVersion <<= (buildRelease, mavenVersion, buildNumberProps, gitDate, gitSha) map makeCanonicalVersion,
 
43
    scalaVersions <<= (canonicalVersion, mavenVersion, osgiVersion) map VersionInfo.apply
 
44
  )
 
45
 
 
46
 
 
47
  /** This generates a  properties file, if it does not already exist, with the maximum lastmodified timestamp
 
48
    * of any source file. */
 
49
  def generateVersionPropertiesFile(name: String)(dir: File, versions: VersionInfo, skip: Boolean, s: TaskStreams): Seq[File] = {
 
50
    // TODO - We can probably clean this up by moving caching bits elsewhere perhaps....
 
51
    val target = dir / name        
 
52
    // TODO - Regenerate on triggers, like recompilation or something...
 
53
    def hasSameVersion: Boolean = {
 
54
      val props = new java.util.Properties
 
55
      val in = new java.io.FileInputStream(target)
 
56
      try props.load(in) finally in.close()
 
57
      versions.canonical == (props getProperty "version.number")
 
58
    }
 
59
    if (!target.exists || !(skip || hasSameVersion)) {
 
60
      makeVersionPropertiesFile(target, versions)
 
61
    }
 
62
    target :: Nil
 
63
  }
 
64
  
 
65
  // This creates the *.properties file used to determine the current version of scala at runtime.  TODO - move these somewhere utility like.
 
66
  def makeVersionPropertiesFile(f: File, versions: VersionInfo): Unit =
 
67
    IO.write(f, "version.number = "+versions.canonical+"\n"+
 
68
                "osgi.number = "+versions.osgi+"\n"+
 
69
                "maven.number = "+versions.maven+"\n"+
 
70
                "copyright.string = Copyright 2002-2013, LAMP/EPFL")
 
71
 
 
72
  def makeCanonicalVersion(isRelease: Boolean, mvnVersion: String, base: BaseBuildNumber, gitDate: String, gitSha: String): String =
 
73
    if(isRelease) mvnVersion
 
74
    else {
 
75
      val suffix = if(base.bnum > 0) "-%d".format(base.bnum) else ""
 
76
      "%s.%s.%s%s-%s-%s" format (base.major, base.minor, base.patch, suffix, gitDate, gitSha)
 
77
    }
 
78
 
 
79
  def makeMavenVersion(base: BaseBuildNumber, suffix: String): String = {
 
80
    val firstSuffix = if(base.bnum > 0) "-%d".format(base.bnum) else ""
 
81
    "%d.%d.%d%s%s" format (base.major, base.minor, base.patch, firstSuffix, suffix)
 
82
  }
 
83
 
 
84
  def makeOsgiVersion(base: BaseBuildNumber, gitDate: String, gitSha: String): String = {
 
85
    val suffix = if(base.bnum > 0) "-%d".format(base.bnum) else ""
 
86
    "%s.%s.%s.v%s%s-%s" format (base.major, base.minor, base.patch, gitDate, suffix, gitSha)
 
87
  }
 
88
 
 
89
  /** Determines what the maven sufffix should be for this build. */
 
90
  def pickMavenSuffix(isRelease: Boolean): String = {
 
91
    def default = if(isRelease) "" else "-SNAPSHOT"
 
92
    Option(System.getProperty("maven.version.suffix")) getOrElse default
 
93
  }
 
94
 
 
95
  /** Loads the build.number properties file into SBT. */
 
96
  def loadBuildNumberProps(file: File): BaseBuildNumber = {
 
97
    val fin = new FileInputStream(file)
 
98
    try {
 
99
      val props = new Properties()
 
100
      props.load(fin)
 
101
      def getProp(name: String): Int = 
 
102
        (for {
 
103
          v <- Option(props.getProperty(name))
 
104
          v2 <- catching(classOf[NFE]) opt v.toInt
 
105
        } yield v2) getOrElse sys.error("Could not convert %s to integer!" format (name))
 
106
 
 
107
      BaseBuildNumber(
 
108
        major=getProp("version.major"), 
 
109
        minor=getProp("version.minor"),
 
110
        patch=getProp("version.patch"),
 
111
        bnum =getProp("version.bnum")
 
112
      )
 
113
    } finally fin.close()
 
114
  }
 
115
 
 
116
 
 
117
  def getGitDate(git: GitRunner, baseDirectory: File, s: TaskStreams): String = {
 
118
    val lines = getGitLines("log","-1","--format=\"%ci\"")(git,baseDirectory, s)
 
119
    val line = if(lines.isEmpty) sys.error("Could not retreive git commit sha!") else lines.head
 
120
    // Lines *always* start with " for some reason...
 
121
    line drop 1 split "\\s+" match {
 
122
      case Array(date, time, _*) =>  "%s-%s" format (date.replaceAll("\\-", ""), time.replaceAll(":",""))
 
123
      case _                     => sys.error("Could not parse git date: " + line)
 
124
    }
 
125
  }
 
126
 
 
127
  def getGitSha(git: GitRunner, baseDirectory: File, s: TaskStreams): String = {
 
128
    val lines = getGitLines("log","-1","--format=\"%H\"", "HEAD")(git,baseDirectory, s)
 
129
    val line = if(lines.isEmpty) sys.error("Could not retreive git commit sha!") else lines.head
 
130
    val noquote = if(line startsWith "\"") line drop 1 else line
 
131
    val nog = if(noquote startsWith "g") noquote drop 1 else noquote
 
132
    nog take 10
 
133
  }
 
134
 
 
135
  def getGitLines(args: String*)(git: GitRunner, baseDirectory: File, s: TaskStreams): Seq[String] =
 
136
     git(args: _*)(baseDirectory, s.log) split "[\r\n]+"
 
137
}
 
138
 
 
139
 
 
140
case class BaseBuildNumber(major: Int, minor: Int, patch: Int, bnum: Int) {
 
141
  override def toString = "BaseBuildNumber(%d.%d.%d-%d)" format (major, minor, patch, bnum)
 
142
}