Asgard(netflixから)と統合するgradleプラグイン(groovyを知らず、gradleを使い始めたばかり)に取り組んでいます。拡張オブジェクトをユーザーに次のように見せたい:
asgard {
url = "http://asgard"
regions {
"us-east-1" {
autoScaling {
{
devPhase = "test"
min = 3
max = 6
availabilityZones = ["us-east-1a", "us-east-1b", "us-east-1c"]
ami = "Base AMI 2013-07-11"
instanceType = "m3.xlarge"
securityGroups = ["base", "test-application"]
}
}
}
}
}
またはそれに近い何か。autoScaling をリストにすることでこれに近づくことができましたが、そのクラスからプロパティを取得しようとすると、動的プロパティが返され、値が返されないようです。以下のスタータープラグインは次のとおりです。
import org.gradle.internal.reflect.Instantiator
class AsgardPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("asgard", AsgardPluginExtension, project)
project.asgard.extensions.regions = project.container(AsgardRegion) {String name ->
AsgardRegion region = project.gradle.services.get(Instantiator).newInstance(AsgardRegion, name)
assert region instanceof ExtensionAware
region.extensions.add("autoScaling", project.container(AsgardAutoScaling))
return region
}
project.task('displayConfigs') << {
if(project.asgard.applicationName == null) project.asgard.applicationName = project.name
println "Asgard URL $project.asgard.url"
println "Application name $project.asgard.applicationName"
println "Runs on regions..."
project.asgard.regions.each() {region ->
println "\tRegion $region.name"
println "\tAutoScaling groups..."
region.autoScaling.each() {asg ->
println "\t\tdevPhase $asg"
println "\t\tdevPhase $asg.devPhase"
println "\t\tdevPhase $asg.get('devPhase')"
}
}
}
}
}
class AsgardPluginExtension {
String url = "http://asgard.sisa.samsung.com"
String applicationName
AsgardPluginExtension(Project project) {
applicationName = project.name
}
}
class AsgardRegion {
def String name
List<AsgardAutoScaling> autoScaling
AsgardRegion(String name) { this.name = name }
}
class AsgardAutoScaling {
String devPhase
int min
int max
List<String> availabilityZones
String ami
String instanceType
String sshKey
List<String> securityGroups
}
タスクを実行すると、ログに次のように表示されます。
$ ./gradlew displayConfigs
:displayConfigs
Asgard URL http://asgard
Application name gradle-asgard-plugin
Runs on regions...
Region us-east-1
AutoScaling groups...
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "devPhase" on "AsgardRegion_Decorated@65087be0", value: "test".
Deprecated dynamic property: "min" on "AsgardRegion_Decorated@65087be0", value: "3".
Deprecated dynamic property: "max" on "AsgardRegion_Decorated@65087be0", value: "6".
Deprecated dynamic property: "availabilityZones" on "AsgardRegion_Decorated@65087be0", value: "[us-east-1a, us-east-1...".
Deprecated dynamic property: "ami" on "AsgardRegion_Decorated@65087be0", value: "Base AMI 2013-07-11".
Deprecated dynamic property: "instanceType" on "AsgardRegion_Decorated@65087be0", value: "m3.xlarge".
Deprecated dynamic property: "securityGroups" on "AsgardRegion_Decorated@65087be0", value: "[base, test-application]".
devPhase
devPhase test
devPhase AsgardRegion_Decorated@14860315('devPhase')
BUILD SUCCESSFUL
Total time: 1.996 secs
私はこれについてすべて間違っていますか?そうでない場合、どうすれば動的プロパティから値を取得できますか (get と value を試しましたが、うまくいかないようです)。
これを読んでくれてありがとう。
@peter-niederwieserのフィードバックに基づく編集
ヒントをくれたピーター・ニーダーヴィーザーに感謝します。あなたのアドバイスを受けて、クロージャーがどのように機能するかを調べた後、私が試したかったことを実行する方法をよりよく理解できたと思います.
現在のプラグインの構文は次のとおりです。
asgard {
url = "http://asgard"
regions {
"us-east-1" {
autoScaling {
devPhase = "test"
min = 3
max = 6
availabilityZones = ["a", "b", "c"]
ami = "Base AMI 2013-07-11"
instanceType = "m3.xlarge"
securityGroups = ["base", "test-application"]
}
autoScaling {
devPhase = "stage"
}
}
"us-west-1" {
autoScaling {
devPhase = "test"
}
autoScaling {
devPhase = "stage"
}
}
}
}
これをサポートするコードは次の場所にあります。
import org.gradle.internal.reflect.Instantiator
class AsgardPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("asgard", AsgardPluginExtension, project)
project.asgard.extensions.regions = project.container(AsgardRegion) {String name ->
AsgardRegion region = project.gradle.services.get(Instantiator).newInstance(AsgardRegion, name, project)
assert region instanceof ExtensionAware
return region
}
project.task('displayConfigs') << {
if(project.asgard.applicationName == null) project.asgard.applicationName = project.name
println "Asgard URL $project.asgard.url"
println "Application name $project.asgard.applicationName"
println "Runs on regions..."
project.asgard.regions.each() {region ->
println "\tRegion $region.name"
println "\tAutoScaling groups..."
region.autoScaling.each() {asg ->
println "\t\tdevPhase $asg.devPhase"
}
}
}
}
}
class AsgardPluginExtension {
String urlgg
String applicationName
AsgardPluginExtension(Project project) {
applicationName = project.name
}
}
class AsgardRegion {
String name
Project project
List<AsgardAutoScaling> autoScaling = []
AsgardRegion(String name, Project project) {
this.name = name
this.project = project
}
void autoScaling(Closure closure) {
def asg = new AsgardAutoScaling()
project.configure(asg, closure)
autoScaling.add(asg)
}
}
class AsgardAutoScaling {
String devPhase
int min
int max
List<String> availabilityZones
String ami
String instanceType
String sshKey
List<String> securityGroups
}
実行したときに表示される出力は次のとおりです。
$ ./gradlew displayConfigs
:displayConfigs
Asgard URL http://asgard
Application name gradle-asgard-plugin
Runs on regions...
Region us-east-1
AutoScaling groups...
devPhase test
devPhase stage
Region us-west-1
AutoScaling groups...
devPhase test
devPhase stage
BUILD SUCCESSFUL
Total time: 1.929 secs