1- /*
2- * Copyright 2022 the GradleX team.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
16-
1+ // SPDX-License-Identifier: Apache-2.0
172package org .gradlex .buildparameters ;
183
19- import org .gradle . api . provider . Property ;
4+ import static org .gradlex . buildparameters . PluginCodeGeneration . escapeEnumValue ;
205
216import java .util .function .Function ;
22-
23- import static org .gradlex .buildparameters .PluginCodeGeneration .escapeEnumValue ;
7+ import org .gradle .api .provider .Property ;
248
259interface CodeGeneratingBuildParameter {
2610
@@ -37,10 +21,15 @@ static CodeGeneratingBuildParameter from(BuildParameter<?> parameter, BuildParam
3721 if (parameter instanceof IntegerBuildParameter ) {
3822 type = new ParameterType ("int" , "Integer" , ".map(Integer::parseInt)" , Function .identity ());
3923 } else if (parameter instanceof BooleanBuildParameter ) {
40- type = new ParameterType ("boolean" , "Boolean" , ".map(" + containingGroup .id .toSimpleTypeName () + "::parse" + parameter .id .toSimpleTypeName () + ")" , Function .identity ());
24+ type = new ParameterType (
25+ "boolean" ,
26+ "Boolean" ,
27+ ".map(" + containingGroup .id .toSimpleTypeName () + "::parse" + parameter .id .toSimpleTypeName () + ")" ,
28+ Function .identity ());
4129 } else if (parameter instanceof EnumBuildParameter ) {
4230 String typeName = parameter .id .toFullQualifiedTypeName ();
43- type = new ParameterType (typeName , typeName , ".map(" + typeName + "::parse)" , s -> typeName + "." + escapeEnumValue (s ));
31+ type = new ParameterType (
32+ typeName , typeName , ".map(" + typeName + "::parse)" , s -> typeName + "." + escapeEnumValue (s ));
4433 } else {
4534 type = new ParameterType ("String" , "String" , "" , s -> "\" " + s + "\" " );
4635 }
@@ -70,13 +59,17 @@ public String getType() {
7059 public String getValue () {
7160 if (parameter .getEnvironmentVariableName ().isPresent ()) {
7261 String envName = parameter .getEnvironmentVariableName ().get ();
73- return "providers.gradleProperty(\" " + parameter .id .toPropertyPath () + "\" ).orElse(providers.environmentVariable(\" " + envName + "\" ))" + type .transformation + ".getOrElse(" + getDefaultValue () + ")" ;
62+ return "providers.gradleProperty(\" " + parameter .id .toPropertyPath ()
63+ + "\" ).orElse(providers.environmentVariable(\" " + envName + "\" ))" + type .transformation
64+ + ".getOrElse(" + getDefaultValue () + ")" ;
7465 }
75- return "providers.gradleProperty(\" " + parameter .id .toPropertyPath () + "\" )" + type .transformation + ".getOrElse(" + getDefaultValue () + ")" ;
66+ return "providers.gradleProperty(\" " + parameter .id .toPropertyPath () + "\" )" + type .transformation
67+ + ".getOrElse(" + getDefaultValue () + ")" ;
7668 }
7769
7870 private String getDefaultValue () {
79- return type .defaultValueTransformation .apply (parameter .getDefaultValue ().get ().toString ());
71+ return type .defaultValueTransformation .apply (
72+ parameter .getDefaultValue ().get ().toString ());
8073 }
8174
8275 @ Override
@@ -108,9 +101,12 @@ public String getType() {
108101 public String getValue () {
109102 if (parameter .getEnvironmentVariableName ().isPresent ()) {
110103 String envName = parameter .getEnvironmentVariableName ().get ();
111- return "providers.gradleProperty(\" " + parameter .id .toPropertyPath () + "\" ).orElse(providers.environmentVariable(\" " + envName + "\" ))" + errorIfMandatory () + type .transformation ;
104+ return "providers.gradleProperty(\" " + parameter .id .toPropertyPath ()
105+ + "\" ).orElse(providers.environmentVariable(\" " + envName + "\" ))" + errorIfMandatory ()
106+ + type .transformation ;
112107 } else {
113- return "providers.gradleProperty(\" " + parameter .id .toPropertyPath () + "\" )" + errorIfMandatory () + type .transformation ;
108+ return "providers.gradleProperty(\" " + parameter .id .toPropertyPath () + "\" )" + errorIfMandatory ()
109+ + type .transformation ;
114110 }
115111 }
116112
@@ -130,16 +126,17 @@ private String errorIfMandatory() {
130126 }
131127
132128 String description = parameter .getDescription ().isPresent ()
133- ? " (" + parameter .getDescription ().get () + ")" : "" ;
129+ ? " (" + parameter .getDescription ().get () + ")"
130+ : "" ;
134131 String envVariable = parameter .getEnvironmentVariableName ().isPresent ()
135- ? " " + parameter .getEnvironmentVariableName ().get () + "=value (environment variable)\\ n" : "" ;
136-
137- return ".orElse(providers.provider(() -> { throw new RuntimeException( \" " +
138- "Build parameter " + parameter . id . toPropertyPath () + description + " not set. Use one of the following: \\ n" +
139- " -P" + parameter .id .toPropertyPath () + "=value (command line) \\ n" +
140- " " + parameter .id .toPropertyPath () + "=value (in 'gradle.properties' file )\\ n" +
141- envVariable +
142- "\" ); }))" ;
132+ ? " " + parameter .getEnvironmentVariableName ().get () + "=value (environment variable)\\ n"
133+ : "" ;
134+
135+ return ".orElse(providers.provider(() -> { throw new RuntimeException( \" " + "Build parameter "
136+ + parameter .id .toPropertyPath () + description + " not set. Use one of the following: \\ n" + " -P"
137+ + parameter .id .toPropertyPath () + "=value (command line )\\ n" + " "
138+ + parameter . id . toPropertyPath () + "=value (in 'gradle.properties' file) \\ n" + envVariable
139+ + "\" ); }))" ;
143140 }
144141 }
145142
@@ -150,7 +147,11 @@ class ParameterType {
150147 final String transformation ;
151148 final Function <String , String > defaultValueTransformation ;
152149
153- ParameterType (String name , String typeParameter , String transformation , Function <String , String > defaultValueTransformation ) {
150+ ParameterType (
151+ String name ,
152+ String typeParameter ,
153+ String transformation ,
154+ Function <String , String > defaultValueTransformation ) {
154155 this .name = name ;
155156 this .typeParameter = typeParameter ;
156157 this .transformation = transformation ;
0 commit comments