Skip to content

Commit 6a1463e

Browse files
authored
Merge pull request #3 from Liftric/feat/support_gradle_init_scripts
CodeArtifactRepositoryPlugin: support gradle init scripts
2 parents b675d5d + d061d93 commit 6a1463e

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,49 @@ You can also just get the token and endpoint, if you wan't to configure somethin
5656
val token = codeArtifactToken(domain = "my_domain")
5757
val uri = codeArtifactUri(domain = "my_domain", repository = "my_repository")
5858
```
59+
60+
## Use from gradle init script
61+
The plugin can also be used during gradle init time.
62+
63+
The example (the `init.gradle.kts` file) configures a custom plugin repository, in our case used for a custom and private fargate plugin which is hosted
64+
in a private code artifact repository.
65+
66+
**Note:** We have to use a gradle init script, no other way to apply the CodeArtifactRepositoryPlugin before the pluginManagement block inside a `settings.gradle.kts` file.
67+
68+
```
69+
import com.liftric.code.artifact.repository.codeArtifact
70+
import com.liftric.code.artifact.repository.codeArtifactRepository
71+
import software.amazon.awssdk.regions.Region.*
72+
73+
initscript {
74+
repositories {
75+
maven {
76+
url = uri("https://plugins.gradle.org/m2/")
77+
}
78+
}
79+
dependencies {
80+
classpath("com.liftric.code.artifact.repository:code-artifact-repository-plugin:<latest>")
81+
}
82+
}
83+
apply<com.liftric.code.artifact.repository.CodeArtifactRepositoryPlugin>()
84+
codeArtifactRepository {
85+
region.set(EU_CENTRAL_1)
86+
// use profile credentials provider, otherwise the default credentials chain of aws will be used
87+
if (System.getenv("CI") == null) {
88+
profile.set("liftric")
89+
}
90+
// Determines how long the generated authentication token is valid in seconds
91+
tokenExpiresIn.set(1_800)
92+
}
93+
settingsEvaluated {
94+
pluginManagement {
95+
repositories {
96+
codeArtifact("<private-domain>", "fargate-gradle-plugin")
97+
google()
98+
mavenCentral()
99+
gradlePluginPortal()
100+
}
101+
}
102+
}
103+
104+
```

src/main/kotlin/com/liftric/code/artifact/repository/CodeArtifactRepositoryPlugin.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.gradle.api.Project
88
import org.gradle.api.artifacts.dsl.RepositoryHandler
99
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
1010
import org.gradle.api.initialization.Settings
11+
import org.gradle.api.invocation.Gradle
1112
import org.gradle.configurationcache.extensions.capitalized
1213
import org.gradle.kotlin.dsl.getByName
1314
import java.net.URI
@@ -21,14 +22,20 @@ abstract class CodeArtifactRepositoryPlugin : Plugin<Any> {
2122
CodeArtifactRepositoryExtension.store[""] = it
2223
}
2324
}
24-
2525
is Project -> {
2626
scope.extensions.create(extensionName, CodeArtifactRepositoryExtension::class.java, scope.extensions)
2727
.also {
2828
CodeArtifactRepositoryExtension.store[""] = it
2929
}
3030
}
31-
31+
is Gradle -> {
32+
scope.beforeSettings {
33+
extensions.create(extensionName, CodeArtifactRepositoryExtension::class.java, extensions)
34+
.also {
35+
CodeArtifactRepositoryExtension.store[""] = it
36+
}
37+
}
38+
}
3239
else -> {
3340
throw GradleException("Should only get applied on Settings or Project")
3441
}
@@ -48,6 +55,12 @@ inline fun Project.codeArtifactRepository(configure: CodeArtifactRepositoryExten
4855
extensions.getByName<CodeArtifactRepositoryExtension>(CodeArtifactRepositoryPlugin.extensionName).configure()
4956
}
5057

58+
inline fun Gradle.codeArtifactRepository(crossinline configure: CodeArtifactRepositoryExtension.() -> Unit) {
59+
settingsEvaluated {
60+
extensions.getByName<CodeArtifactRepositoryExtension>(CodeArtifactRepositoryPlugin.extensionName).configure()
61+
}
62+
}
63+
5164
/**
5265
* Use the default CodeArtifact config (and therefore extension)
5366
*/

0 commit comments

Comments
 (0)