summaryrefslogtreecommitdiff
path: root/scalalib/src/publish/VersionControl.scala
blob: aad38ac31ca4f1a11f9edb48563240fb9214b3ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
    )
  def gitlab(owner: String, repo: String, tag: Option[String] = None): VersionControl =
    VersionControl(
      browsableRepository = Some(s"https://gitlab.com/$owner/$repo"),
      connection = Some(VersionControlConnection.gitGit("gitlab.com", s"$owner/$repo.git")),
      developerConnection = Some(VersionControlConnection.gitSsh("gitlab.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)
}