Skip to content

Commit 7bbfcf1

Browse files
authored
Adds docs for resolvers (#2084)
* Clean up parsing repositories for publishing * Add documentation for resolvers
1 parent 051bf24 commit 7bbfcf1

File tree

10 files changed

+98
-57
lines changed

10 files changed

+98
-57
lines changed

modules/cli/src/main/scala/scala/cli/commands/publish/RepoParams.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,12 @@ object RepoParams {
6868
}
6969
RepoParams.gitHubRepoFor(org, name)
7070
case repoStr =>
71-
val repo0 = RepositoryParser.repositoryOpt(repoStr)
72-
.collect {
73-
case m: MavenRepository =>
74-
m
75-
}
76-
.getOrElse {
77-
val url =
78-
if (repoStr.contains("://")) repoStr
79-
else os.Path(repoStr, os.pwd).toNIO.toUri.toASCIIString
80-
MavenRepository(url)
81-
}
71+
val repo0 = RepositoryParser.repositoryOpt(repoStr).getOrElse {
72+
val url =
73+
if (repoStr.contains("://")) repoStr
74+
else os.Path(repoStr, os.pwd).toNIO.toUri.toASCIIString
75+
MavenRepository(url)
76+
}
8277

8378
RepoParams(
8479
PublishRepository.Simple(repo0),

modules/cli/src/main/scala/scala/cli/commands/publish/RepositoryParser.scala

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package scala.cli.commands.publish
33
// from coursier.internal.SharedRepositoryParser
44
// delete when coursier.internal.SharedRepositoryParser.repositoryOpt is available for us
55

6-
import coursier.Repositories
76
import coursier.core.Repository
87
import coursier.ivy.IvyRepository
98
import coursier.maven.MavenRepository
9+
import coursier.{LocalRepositories, Repositories}
1010

1111
object RepositoryParser {
1212

13-
def repositoryOpt(s: String): Option[Repository] =
13+
def repositoryOpt(s: String): Option[MavenRepository] =
1414
if (s == "central")
1515
Some(Repositories.central)
1616
else if (s.startsWith("sonatype:"))
@@ -23,16 +23,10 @@ object RepositoryParser {
2323

2424
Some(Repositories.bintray(id))
2525
}
26-
else if (s.startsWith("bintray-ivy:"))
27-
Some(Repositories.bintrayIvy(s.stripPrefix("bintray-ivy:")))
28-
else if (s.startsWith("typesafe:ivy-"))
29-
Some(Repositories.typesafeIvy(s.stripPrefix("typesafe:ivy-")))
3026
else if (s.startsWith("typesafe:"))
3127
Some(Repositories.typesafe(s.stripPrefix("typesafe:")))
3228
else if (s.startsWith("sbt-maven:"))
3329
Some(Repositories.sbtMaven(s.stripPrefix("sbt-maven:")))
34-
else if (s.startsWith("sbt-plugin:"))
35-
Some(Repositories.sbtPlugin(s.stripPrefix("sbt-plugin:")))
3630
else if (s == "scala-integration" || s == "scala-nightlies")
3731
Some(Repositories.scalaIntegration)
3832
else if (s == "jitpack")
@@ -53,24 +47,4 @@ object RepositoryParser {
5347
Some(Repositories.apache(s.stripPrefix("apache:")))
5448
else
5549
None
56-
57-
def repository(s: String): Either[String, Repository] =
58-
repositoryOpt(s) match {
59-
case Some(repo) => Right(repo)
60-
case None =>
61-
if (s.startsWith("ivy:")) {
62-
val s0 = s.stripPrefix("ivy:")
63-
val sepIdx = s0.indexOf('|')
64-
if (sepIdx < 0)
65-
IvyRepository.parse(s0)
66-
else {
67-
val mainPart = s0.substring(0, sepIdx)
68-
val metadataPart = s0.substring(sepIdx + 1)
69-
IvyRepository.parse(mainPart, Some(metadataPart))
70-
}
71-
}
72-
else
73-
Right(MavenRepository(s))
74-
}
75-
7650
}

modules/cli/src/main/scala/scala/cli/commands/shared/SharedDependencyOptions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import caseapp.*
44
import com.github.plokhotnyuk.jsoniter_scala.core.*
55
import com.github.plokhotnyuk.jsoniter_scala.macros.*
66

7+
import scala.build.preprocessing.directives.Repository
78
import scala.cli.commands.tags
89

910
// format: off
@@ -17,7 +18,7 @@ final case class SharedDependencyOptions(
1718
@Group(HelpGroup.Dependency.toString)
1819
@Tag(tags.should)
1920
@Tag(tags.inShortHelp)
20-
@HelpMessage("Add repositories")
21+
@HelpMessage(Repository.usageMsg)
2122
@Name("repo")
2223
@Name("r")
2324
repository: List[String] = Nil,

modules/directives/src/main/scala/scala/build/preprocessing/directives/Repository.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import scala.cli.commands.SpecificationLevel
99
@DirectiveGroupName("Repository")
1010
@DirectiveExamples("//> using repository jitpack")
1111
@DirectiveExamples("//> using repository sonatype:snapshots")
12+
@DirectiveExamples("//> using repository m2Local")
1213
@DirectiveExamples(
1314
"//> using repository https://maven-central.storage-download.googleapis.com/maven2"
1415
)
1516
@DirectiveUsage(
1617
"//> using repository _repository_",
1718
"`//> using repository `_repository_"
1819
)
19-
@DirectiveDescription("Add a repository for dependency resolution")
20+
@DirectiveDescription(Repository.usageMsg)
2021
@DirectiveLevel(SpecificationLevel.SHOULD)
2122
// format: off
2223
final case class Repository(
@@ -36,4 +37,9 @@ final case class Repository(
3637

3738
object Repository {
3839
val handler: DirectiveHandler[Repository] = DirectiveHandler.derive
40+
41+
val usageMsg =
42+
"""Add repositories for dependency resolution.
43+
|
44+
|Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository""".stripMargin
3945
}

website/docs/guides/dependencies.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ For example:
3737
org.postgresql:postgresql:42.2.8
3838
```
3939

40+
### Repositories
41+
42+
Sometimes dependencies are published into non-standard repositories, like nightly builds published to Sonatype Snapshots. Scala CLI can use additional maven and ivy repositories with the `repository` directive or `--repository` command line options:
43+
44+
```scala
45+
//> using repository sonatype:snapshots
46+
```
47+
48+
or
49+
50+
```bash ignore
51+
scala-cli --repository "https://maven-central.storage-download.googleapis.com/maven2"
52+
```
53+
54+
Both directive and command line option accept predefined repository definitions (see below) or a URL of the root of Maven repository.
55+
56+
Repositories can also be resolved from the `COURSIER_REPOSITORIES` environment variable, but this is not recommended (more in [Coursier documentation](https://get-coursier.io/docs/other-repositories)).
57+
58+
#### Predefined repositories
59+
60+
61+
| predefined repository | kind | description |
62+
| ---------- | ---- | --- |
63+
| central | Maven [(root)](https://repo1.maven.org/maven2) | Used by default, default repository for most Scala libraries |
64+
| sonatype:snapshots | Maven [(root)](https://oss.sonatype.org/content/repositories/snapshots) | Repositories where most Scala libraries publish its snapshots / nightly builds. Used when `X.nightly` is used as Scala version e.g. `3.1.nightly`. |
65+
| ivy2local | Ivy | Local ivy repository, used to publish things locally (e.g. by `publishLocal`). Localized in `<ivy-home>/local`, usually `<user-home>/.ivy/local`. |
66+
| m2Local | Maven | Local maven repository, localized in `<user-home>/.m2/repository` |
67+
68+
Scala CLI delegates parsing of predefined repositories to Coursier and full details can be obtains from Coursier source code ([here](https://github.com/coursier/coursier/blob/2444eebcc151e0f6927e269137e8737c1f31cbe2/modules/coursier/jvm/src/main/scala/coursier/LocalRepositories.scala) and [here](https://github.com/coursier/coursier/blob/2444eebcc151e0f6927e269137e8737c1f31cbe2/modules/coursier/shared/src/main/scala/coursier/internal/SharedRepositoryParser.scala))
69+
70+
71+
72+
73+
74+
4075
### Excluding Transitive Dependencies
4176

4277
To exclude a transitive dependency from a Scala CLI project use the `exclude` parameter:
@@ -98,7 +133,7 @@ It is possible to declare dependencies limited to the test scope with the `using
98133
`````
99134

100135
More details can be found in
101-
the [`using` directives guide](./using-directives.md#directives-with-a-test-scope-equivalent).
136+
the [`using` directives guide](./using-directives#directives-with-a-test-scope-equivalent).
102137

103138
## Specifying dependencies from the command line
104139

@@ -121,11 +156,11 @@ scala-cli compile Sample.sc \
121156
```
122157

123158
Note that `--dependency` is only meant as a convenience. You should favor adding dependencies in the sources themselves
124-
via [using directives](/docs/guides/configuration.md#special-imports). However, the `--dependency` CLI option takes
159+
via [using directives](./configuration#special-imports). However, the `--dependency` CLI option takes
125160
precedence over `using` directives, so it can be used to override a `using` directive, such as when you want to work
126161
with a different dependency version.
127162

128-
You can also add repositories on the command-line, via `--repository`:
163+
You can also add repositories on the command-line, via `--repository` or `//> using repos`
129164

130165
```bash ignore
131166
scala-cli compile Sample.sc \

website/docs/reference/cli-options.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ Add dependencies
257257

258258
Aliases: `-r`, `--repo`
259259

260-
Add repositories
260+
Add repositories for dependency resolution.
261+
262+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
261263

262264
### `--compiler-plugin`
263265

website/docs/reference/directives.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ Enable Python support
235235

236236
### Repository
237237

238-
Add a repository for dependency resolution
238+
Add repositories for dependency resolution.
239+
240+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
239241

240242
`//> using repository `_repository_
241243

@@ -244,6 +246,8 @@ Add a repository for dependency resolution
244246

245247
`//> using repository sonatype:snapshots`
246248

249+
`//> using repository m2Local`
250+
247251
`//> using repository https://maven-central.storage-download.googleapis.com/maven2`
248252

249253
### Resource directories

website/docs/reference/scala-command/cli-options.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ Aliases: `-r`, `--repo`
226226

227227
`SHOULD have` per Scala Runner specification
228228

229-
Add repositories
229+
Add repositories for dependency resolution.
230+
231+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
230232

231233
### `--compiler-plugin`
232234

website/docs/reference/scala-command/directives.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ Set the default platform to Scala.js or Scala Native
180180

181181
### Repository
182182

183-
Add a repository for dependency resolution
183+
Add repositories for dependency resolution.
184+
185+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
184186

185187
`//> using repository `_repository_
186188

@@ -189,6 +191,8 @@ Add a repository for dependency resolution
189191

190192
`//> using repository sonatype:snapshots`
191193

194+
`//> using repository m2Local`
195+
192196
`//> using repository https://maven-central.storage-download.googleapis.com/maven2`
193197

194198
### Resource directories

website/docs/reference/scala-command/runner-specification.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
186186

187187
**--repository**
188188

189-
Add repositories
189+
Add repositories for dependency resolution.
190+
191+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
190192

191193
Aliases: `-r` ,`--repo`
192194

@@ -900,7 +902,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
900902

901903
**--repository**
902904

903-
Add repositories
905+
Add repositories for dependency resolution.
906+
907+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
904908

905909
Aliases: `-r` ,`--repo`
906910

@@ -1431,7 +1435,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
14311435

14321436
**--repository**
14331437

1434-
Add repositories
1438+
Add repositories for dependency resolution.
1439+
1440+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
14351441

14361442
Aliases: `-r` ,`--repo`
14371443

@@ -1988,7 +1994,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
19881994

19891995
**--repository**
19901996

1991-
Add repositories
1997+
Add repositories for dependency resolution.
1998+
1999+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
19922000

19932001
Aliases: `-r` ,`--repo`
19942002

@@ -2564,7 +2572,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
25642572

25652573
**--repository**
25662574

2567-
Add repositories
2575+
Add repositories for dependency resolution.
2576+
2577+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
25682578

25692579
Aliases: `-r` ,`--repo`
25702580

@@ -3116,7 +3126,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
31163126

31173127
**--repository**
31183128

3119-
Add repositories
3129+
Add repositories for dependency resolution.
3130+
3131+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
31203132

31213133
Aliases: `-r` ,`--repo`
31223134

@@ -3705,7 +3717,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
37053717

37063718
**--repository**
37073719

3708-
Add repositories
3720+
Add repositories for dependency resolution.
3721+
3722+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
37093723

37103724
Aliases: `-r` ,`--repo`
37113725

@@ -4339,7 +4353,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
43394353

43404354
**--repository**
43414355

4342-
Add repositories
4356+
Add repositories for dependency resolution.
4357+
4358+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
43434359

43444360
Aliases: `-r` ,`--repo`
43454361

@@ -5186,7 +5202,9 @@ Embed resources into the Scala Native binary (can be read with the Java resource
51865202

51875203
**--repository**
51885204

5189-
Add repositories
5205+
Add repositories for dependency resolution.
5206+
5207+
Accepts predefined repositories supported by Coursier (like `sonatype:snapshots` or `m2Local`) or a URL of the root of Maven repository
51905208

51915209
Aliases: `-r` ,`--repo`
51925210

0 commit comments

Comments
 (0)