From 3b5e0bd58c42edd6431d0cda0e5a4b03cfa41b25 Mon Sep 17 00:00:00 2001 From: Guillaume Massé Date: Sat, 3 Mar 2018 21:52:26 +0100 Subject: Bugfix/version contol (#192) * Fix several issues in VersionControl & add Test * Move VersionControl to it's own file --- .../src/mill/scalalib/publish/VersionControl.scala | 124 +++++++++++++++++++++ scalalib/src/mill/scalalib/publish/settings.scala | 119 -------------------- .../src/mill/scalalib/VersionControlTests.scala | 74 ++++++++++++ 3 files changed, 198 insertions(+), 119 deletions(-) create mode 100644 scalalib/src/mill/scalalib/publish/VersionControl.scala create mode 100644 scalalib/test/src/mill/scalalib/VersionControlTests.scala diff --git a/scalalib/src/mill/scalalib/publish/VersionControl.scala b/scalalib/src/mill/scalalib/publish/VersionControl.scala new file mode 100644 index 00000000..cfb95402 --- /dev/null +++ b/scalalib/src/mill/scalalib/publish/VersionControl.scala @@ -0,0 +1,124 @@ +package mill.scalalib.publish + +// https://maven.apache.org/pom.html#SCM +/* + * @param browsableRepository: a publicly browsable repository + * (example: https://github.com/lihaoyi/mill) + * @param connection: read-only connection to repository + * (example: scm:git:git://github.com/lihaoyi/mill.git) + * @param developerConnection: read-write connection to repository + * (example: scm:git:git@github.com:lihaoyi/mill.git) + * @param tag: tag that was created for this release. This is useful for + * git and mercurial since it's not possible to include the tag in + * the connection url. + * (example: v2.12.4, HEAD, my-branch, fd8a2567ad32c11bcf8adbaca85bdba72bb4f935, ...) + */ +case class VersionControl( + browsableRepository: Option[String] = None, + connection: Option[String] = None, + developerConnection: Option[String] = None, + tag: Option[String] = None +) + +@deprecated("use VersionControl", "0.1.3") +case class SCM( + url: String, + connection: String +) + +object VersionControl { + def github(owner: String, repo: String, tag: Option[String] = None): VersionControl = + VersionControl( + browsableRepository = Some(s"https://github.com/$owner/$repo"), + connection = Some(VersionControlConnection.gitGit("github.com", s"$owner/$repo.git")), + developerConnection = Some(VersionControlConnection.gitSsh("github.com", s":$owner/$repo.git", username = Some("git"))), + tag = tag + ) +} + +object VersionControlConnection { + def network(scm: String, + protocol: String, + hostname: String, + path: String, + username: Option[String] = None, + password: Option[String] = None, + port: Option[Int] = None): String = { + val portPart = port.map(":" + _).getOrElse("") + val credentials = + username match { + case Some(user) => + val pass = password.map(":" + _).getOrElse("") + s"${user}${pass}@" + case None => + password match { + case Some(p) => sys.error(s"no username set for password: $p") + case _ => "" + } + } + + val path0 = + if(path.startsWith(":") || path.startsWith("/")) path + else "/" + path + + s"scm:${scm}:${protocol}://${credentials}${hostname}${portPart}${path0}" + } + + def file(scm: String, path: String): String = { + s"scm:$scm:file://$path" + } + + def gitGit(hostname: String, + path: String = "", + port: Option[Int] = None): String = + network("git", "git", hostname, path, port = port) + + def gitHttp(hostname: String, + path: String = "", + port: Option[Int] = None): String = + network("git", "http", hostname, path, port = port) + + def gitHttps(hostname: String, + path: String = "", + port: Option[Int] = None): String = + network("git", "https", hostname, path, port = port) + + def gitSsh(hostname: String, + path: String = "", + username: Option[String] = None, + port: Option[Int] = None): String = + network("git", "ssh", hostname, path, username = username, port = port) + + def gitFile(path: String): String = + file("git", path) + + def svnSsh(hostname: String, + path: String = "", + username: Option[String] = None, + port: Option[Int] = None): String = + network("svn", "svn+ssh", hostname, path, username, None, port) + + def svnHttp(hostname: String, + path: String = "", + username: Option[String] = None, + password: Option[String] = None, + port: Option[Int] = None): String = + network("svn", "http", hostname, path, username, password, port) + + def svnHttps(hostname: String, + path: String = "", + username: Option[String] = None, + password: Option[String] = None, + port: Option[Int] = None): String = + network("svn", "https", hostname, path, username, password, port) + + def svnSvn(hostname: String, + path: String = "", + username: Option[String] = None, + password: Option[String] = None, + port: Option[Int] = None): String = + network("svn", "svn", hostname, path, username, password, port) + + def svnFile(path: String): String = + file("svn", path) +} \ No newline at end of file diff --git a/scalalib/src/mill/scalalib/publish/settings.scala b/scalalib/src/mill/scalalib/publish/settings.scala index 665e0ed6..1076fb41 100644 --- a/scalalib/src/mill/scalalib/publish/settings.scala +++ b/scalalib/src/mill/scalalib/publish/settings.scala @@ -52,125 +52,6 @@ case class Dependency( scope: Scope ) -// https://maven.apache.org/pom.html#SCM -/* - * @param browsableRepository: a publicly browsable repository - * (example: https://github.com/lihaoyi/mill) - * @param connection: read-only connection to repository - * (example: scm:git:git://github.com/lihaoyi/mill.git) - * @param developerConnection: read-write connection to repository - * (example: scm:git:git@github.com:lihaoyi/mill.git) - * @param tag: tag that was created for this release. This is useful for - * git and mercurial since it's not possible to include the tag in - * the connection url. - * (example: v2.12.4, HEAD, my-branch, fd8a2567ad32c11bcf8adbaca85bdba72bb4f935, ...) - */ -case class VersionControl( - browsableRepository: Option[String] = None, - connection: Option[String] = None, - developerConnection: Option[String] = None, - tag: Option[String] = None -) - -@deprecated("use VersionControl", "0.1.3") -case class SCM( - url: String, - connection: String -) - -object VersionControl { - def github(owner: String, repo: String, tag: Option[String] = None): VersionControl = - VersionControl( - browsableRepository = Some(s"https://github.com/$owner/$repo"), - connection = Some(VersionControlConnection.gitGit("github.com", "$owner/$repo.git")), - developerConnection = Some(VersionControlConnection.gitSsh("github.com", "$owner/$repo.git")), - tag = tag - ) -} - -object VersionControlConnection { - def network(scm: String, - protocol: String, - hostname: String, - path: String, - username: Option[String] = None, - password: Option[String] = None, - port: Option[Int] = None): String = { - val portPart = port.map(":" + _).getOrElse("") - val credentials = - username match { - case Some(user) => - val pass = password.map(":" + _).getOrElse("") - s"${user}${pass}" - case None => - password match { - case Some(p) => sys.error(s"no username set for password: $p") - case _ => "" - } - } - - s"${scm}:${protocol}://${credentials}${hostname}${portPart}/$path" - } - - def file(scm: String, hostname: Option[String], path: String): String = { - val hostnamePart = hostname.getOrElse("") - "scm:$scm:file://${hostnamePart}/$path" - } - - def gitGit(hostname: String, - path: String, - port: Option[Int] = None): String = - network("git", "git", hostname, path, port = port) - - def gitHttp(hostname: String, - path: String, - port: Option[Int] = None): String = - network("git", "http", hostname, path, port = port) - - def gitHttps(hostname: String, - path: String, - port: Option[Int] = None): String = - network("git", "https", hostname, path, port = port) - - def gitSsh(hostname: String, - path: String, - port: Option[Int] = None): String = - network("git", "ssh", hostname, path, port = port) - - def gitFile(hostname: Option[String], path: String): String = - file("git", hostname, path) - - def svnSsh(hostname: String, - path: String, - username: Option[String], - port: Option[Int]): String = - network("svn", "svn+ssh", hostname, path, username, None, port) - - def svnHttp(hostname: String, - path: String, - port: Option[Int], - username: Option[String], - password: Option[String]): String = - network("svn", "http", hostname, path, username, password) - - def svnHttps(hostname: String, - path: String, - port: Option[Int], - username: Option[String], - password: Option[String]): String = - network("svn", "https", hostname, path, username, password) - - def svnSvn(username: Option[String], - password: Option[String], - hostname: String, - port: Option[Int], - path: String): String = - network("svn", "svn", hostname, path, username, password) - - def svnFile(hostname: Option[String], path: String): String = - file("svn", hostname, path) -} - case class Developer( id: String, name: String, diff --git a/scalalib/test/src/mill/scalalib/VersionControlTests.scala b/scalalib/test/src/mill/scalalib/VersionControlTests.scala new file mode 100644 index 00000000..fafdca2d --- /dev/null +++ b/scalalib/test/src/mill/scalalib/VersionControlTests.scala @@ -0,0 +1,74 @@ +package mill.scalalib + +import mill.scalalib.publish.{VersionControl, VersionControlConnection} + +import utest._ + +object VersionContolTests extends TestSuite { + + import VersionControl._ + import VersionControlConnection._ + + val tests = Tests { + 'github - { + assert( + github("lihaoyi", "mill") == + VersionControl( + browsableRepository = Some("https://github.com/lihaoyi/mill"), + connection = Some("scm:git:git://github.com/lihaoyi/mill.git"), + developerConnection = Some("scm:git:ssh://git@github.com:lihaoyi/mill.git"), + tag = None + ) + ) + } + 'git - { + assert( + gitGit("example.org", "path.git", port = Some(9418)) == + "scm:git:git://example.org:9418/path.git" + ) + + assert( + gitHttp("example.org") == + "scm:git:http://example.org/" + ) + + assert( + gitHttps("example.org", "path.git") == + "scm:git:https://example.org/path.git" + ) + + assert( + gitSsh("example.org", "path.git") == + "scm:git:ssh://example.org/path.git" + ) + + assert( + gitFile("/home/gui/repos/foo/bare.git") == + "scm:git:file:///home/gui/repos/foo/bare.git" + ) + + } + 'svn - { + assert( + svnSsh("example.org", "repo") == + "scm:svn:svn+ssh://example.org/repo" + ) + assert( + svnHttp("example.org", "repo", Some("user"), Some("pass")) == + "scm:svn:http://user:pass@example.org/repo" + ) + assert( + svnHttps("example.org", "repo", Some("user")) == + "scm:svn:https://user@example.org/repo" + ) + assert( + svnSvn("example.org", "repo", port = Some(3690)) == + "scm:svn:svn://example.org:3690/repo" + ) + assert( + svnFile("/var/svn/repo") == + "scm:svn:file:///var/svn/repo" + ) + } + } +} \ No newline at end of file -- cgit v1.2.3