Skip to content

Commit

Permalink
Merge pull request #3 from Liftric/feat/support_gradle_init_scripts
Browse files Browse the repository at this point in the history
CodeArtifactRepositoryPlugin: support gradle init scripts
  • Loading branch information
Ingwersaft authored Aug 8, 2023
2 parents b675d5d + d061d93 commit 6a1463e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,49 @@ You can also just get the token and endpoint, if you wan't to configure somethin
val token = codeArtifactToken(domain = "my_domain")
val uri = codeArtifactUri(domain = "my_domain", repository = "my_repository")
```

## Use from gradle init script
The plugin can also be used during gradle init time.

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
in a private code artifact repository.

**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.

```
import com.liftric.code.artifact.repository.codeArtifact
import com.liftric.code.artifact.repository.codeArtifactRepository
import software.amazon.awssdk.regions.Region.*
initscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("com.liftric.code.artifact.repository:code-artifact-repository-plugin:<latest>")
}
}
apply<com.liftric.code.artifact.repository.CodeArtifactRepositoryPlugin>()
codeArtifactRepository {
region.set(EU_CENTRAL_1)
// use profile credentials provider, otherwise the default credentials chain of aws will be used
if (System.getenv("CI") == null) {
profile.set("liftric")
}
// Determines how long the generated authentication token is valid in seconds
tokenExpiresIn.set(1_800)
}
settingsEvaluated {
pluginManagement {
repositories {
codeArtifact("<private-domain>", "fargate-gradle-plugin")
google()
mavenCentral()
gradlePluginPortal()
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle
import org.gradle.configurationcache.extensions.capitalized
import org.gradle.kotlin.dsl.getByName
import java.net.URI
Expand All @@ -21,14 +22,20 @@ abstract class CodeArtifactRepositoryPlugin : Plugin<Any> {
CodeArtifactRepositoryExtension.store[""] = it
}
}

is Project -> {
scope.extensions.create(extensionName, CodeArtifactRepositoryExtension::class.java, scope.extensions)
.also {
CodeArtifactRepositoryExtension.store[""] = it
}
}

is Gradle -> {
scope.beforeSettings {
extensions.create(extensionName, CodeArtifactRepositoryExtension::class.java, extensions)
.also {
CodeArtifactRepositoryExtension.store[""] = it
}
}
}
else -> {
throw GradleException("Should only get applied on Settings or Project")
}
Expand All @@ -48,6 +55,12 @@ inline fun Project.codeArtifactRepository(configure: CodeArtifactRepositoryExten
extensions.getByName<CodeArtifactRepositoryExtension>(CodeArtifactRepositoryPlugin.extensionName).configure()
}

inline fun Gradle.codeArtifactRepository(crossinline configure: CodeArtifactRepositoryExtension.() -> Unit) {
settingsEvaluated {
extensions.getByName<CodeArtifactRepositoryExtension>(CodeArtifactRepositoryPlugin.extensionName).configure()
}
}

/**
* Use the default CodeArtifact config (and therefore extension)
*/
Expand Down

0 comments on commit 6a1463e

Please sign in to comment.