1
+ @file:Suppress(" unused" )
2
+
1
3
package com.liftric.code.artifact.repository
2
4
3
5
import org.gradle.api.GradleException
@@ -7,96 +9,93 @@ import org.gradle.api.artifacts.dsl.RepositoryHandler
7
9
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
8
10
import org.gradle.api.initialization.Settings
9
11
import org.gradle.configurationcache.extensions.capitalized
10
- import org.gradle.kotlin.dsl.create
11
- import org.gradle.kotlin.dsl.getByType
12
- import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider
13
- import software.amazon.awssdk.services.codeartifact.CodeartifactClient
14
- import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenResponse
15
- import software.amazon.awssdk.services.codeartifact.model.GetRepositoryEndpointResponse
16
- import software.amazon.awssdk.services.sts.StsClient
17
-
18
- private const val extensionName = " CodeArtifactRepository"
19
-
20
- private lateinit var codeArtifact: CodeArtifact
21
- private lateinit var extension: CodeArtifactRepositoryExtension
12
+ import org.gradle.kotlin.dsl.getByName
13
+ import java.net.URI
22
14
23
15
abstract class CodeArtifactRepositoryPlugin : Plugin <Any > {
24
16
override fun apply (scope : Any ) {
25
17
when (scope) {
26
18
is Settings -> {
27
- extension = scope.extensions.create(extensionName)
28
- codeArtifact = CodeArtifact (extension)
19
+ scope.extensions.create(extensionName, CodeArtifactRepositoryExtension ::class .java, scope.extensions)
29
20
}
21
+
30
22
is Project -> {
31
- extension = scope.extensions.create(extensionName)
32
- codeArtifact = CodeArtifact (extension)
23
+ scope.extensions.create(extensionName, CodeArtifactRepositoryExtension ::class .java, scope.extensions)
33
24
}
25
+
34
26
else -> {
35
27
throw GradleException (" Should only get applied on Settings or Project" )
36
28
}
37
29
}
38
30
}
39
- }
40
-
41
- class CodeArtifact (private val extension : CodeArtifactRepositoryExtension ) {
42
- private val account: String
43
- get() = stsClient.getCallerIdentity {}.account()
44
31
45
- private val stsClient by lazy {
46
- StsClient .builder().apply {
47
- region(extension.region.get())
48
- if (! extension.shouldResolveCredentialsByEnvironment.getOrElse(true )) {
49
- credentialsProvider {
50
- ProfileCredentialsProvider .create(extension.profile.get()).resolveCredentials()
51
- }
52
- }
53
- }.build()
54
- }
55
-
56
- private val codeArtifactClient by lazy {
57
- CodeartifactClient .builder().apply {
58
- region(extension.region.get())
59
- if (! extension.shouldResolveCredentialsByEnvironment.getOrElse(true )) {
60
- credentialsProvider {
61
- ProfileCredentialsProvider .create(extension.profile.get()).resolveCredentials()
62
- }
63
- }
64
- }.build()
65
- }
66
-
67
- fun authorizationTokenRepsponse (): GetAuthorizationTokenResponse {
68
- return codeArtifactClient.getAuthorizationToken {
69
- it.domain(extension.domain.get())
70
- it.domainOwner(account)
71
- it.durationSeconds(extension.tokenExpiresIn.getOrElse(1_800 ))
72
- }
73
- }
74
-
75
- fun repositoryEndpointResponse (repository : String , format : String = "maven"): GetRepositoryEndpointResponse {
76
- return codeArtifactClient.getRepositoryEndpoint {
77
- it.domain(extension.domain.get())
78
- it.domainOwner(account)
79
- it.repository(repository)
80
- it.format(format)
81
- }
32
+ companion object {
33
+ const val extensionName = " CodeArtifactRepository"
82
34
}
83
35
}
84
36
85
37
inline fun Settings.codeArtifactRepository (configure : CodeArtifactRepositoryExtension .() -> Unit ) {
86
- extensions.getByType <CodeArtifactRepositoryExtension >().configure()
38
+ extensions.getByName <CodeArtifactRepositoryExtension >(CodeArtifactRepositoryPlugin .extensionName ).configure()
87
39
}
88
40
89
41
inline fun Project.codeArtifactRepository (configure : CodeArtifactRepositoryExtension .() -> Unit ) {
90
- extensions.getByType <CodeArtifactRepositoryExtension >().configure()
42
+ extensions.getByName <CodeArtifactRepositoryExtension >(CodeArtifactRepositoryPlugin .extensionName ).configure()
91
43
}
92
44
93
- fun RepositoryHandler.codeArtifact (repository : String ): MavenArtifactRepository = codeArtifact(extension.domain.get(), repository)
45
+ /* *
46
+ * Use the default CodeArtifact config (and therefore extension)
47
+ */
48
+ fun RepositoryHandler.codeArtifact (domain : String , repository : String ): MavenArtifactRepository =
49
+ codeArtifact(" " , domain, repository)
94
50
95
- fun RepositoryHandler.codeArtifact (domain : String , repository : String ): MavenArtifactRepository = maven {
96
- setName(listOf (" CodeArtifact" , domain, repository).joinToString(" " ) { it.capitalized() })
97
- setUrl(codeArtifact.repositoryEndpointResponse(repository).repositoryEndpoint())
98
- credentials {
99
- username = " aws"
100
- password = codeArtifact.authorizationTokenRepsponse().authorizationToken()
101
- }
51
+ /* *
52
+ * Use CodeArtifact by additional name
53
+ */
54
+ fun RepositoryHandler.codeArtifact (additionalName : String , domain : String , repository : String ) = maven {
55
+ val extensionName = " $additionalName${CodeArtifactRepositoryPlugin .extensionName} "
56
+ CodeArtifactRepositoryExtension .additional[extensionName]?.let {
57
+ name = listOf (extensionName, domain, repository).joinToString(" " ) { it.capitalized() }
58
+ url = URI .create(it.repositoryEndpointResponse(domain, repository).repositoryEndpoint())
59
+ credentials {
60
+ username = " aws"
61
+ password = it.authorizationTokenResponse(domain).authorizationToken()
62
+ }
63
+ } ? : throw GradleException (" didn't find CodeArtifactRepositoryExtension with the name: $" )
64
+ }
65
+
66
+ /* *
67
+ * If you need the plain token
68
+ */
69
+ fun codeArtifactToken (domain : String ): String = codeArtifactToken(" " , domain)
70
+
71
+ /* *
72
+ * If you need the plain endpoint uri
73
+ */
74
+ fun codeArtifactUri (domain : String , repository : String , format : String ): URI =
75
+ codeArtifactUri(" " , domain, repository, format)
76
+
77
+ /* *
78
+ * If you need the plain token
79
+ *
80
+ * @param additionalName this is the name (prefix) of the codeArtifactRepository configuration. Use an empty string to use
81
+ * the default extension
82
+ */
83
+ fun codeArtifactToken (additionalName : String , domain : String ): String {
84
+ val extensionName = " $additionalName${CodeArtifactRepositoryPlugin .extensionName} "
85
+ val settings = CodeArtifactRepositoryExtension .additional[extensionName]
86
+ ? : throw GradleException (" didn't find CodeArtifactRepositoryExtension with the name: $" )
87
+ return settings.authorizationTokenResponse(domain).authorizationToken()
88
+ }
89
+
90
+ /* *
91
+ * If you need the plain endpoint uri
92
+ *
93
+ * @param additionalName this is the name (prefix) of the codeArtifactRepository configuration. Use an empty string to use
94
+ * the default extension
95
+ */
96
+ fun codeArtifactUri (additionalName : String , domain : String , repository : String , format : String ): URI {
97
+ val extensionName = " $additionalName${CodeArtifactRepositoryPlugin .extensionName} "
98
+ val settings = CodeArtifactRepositoryExtension .additional[extensionName]
99
+ ? : throw GradleException (" didn't find CodeArtifactRepositoryExtension with the name: $" )
100
+ return settings.repositoryEndpointResponse(domain, repository, format).repositoryEndpoint().let { URI .create(it) }
102
101
}
0 commit comments