PropertyUtils.groovy
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2019-2022 Andres Almiray.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package org.kordamp.gradle
019 
020 import groovy.transform.CompileStatic
021 import org.gradle.api.Project
022 import org.gradle.api.file.Directory
023 import org.gradle.api.file.DirectoryProperty
024 import org.gradle.api.file.RegularFile
025 import org.gradle.api.file.RegularFileProperty
026 import org.gradle.api.provider.Property
027 import org.gradle.api.provider.Provider
028 import org.kordamp.gradle.property.PathAware
029 
030 import java.nio.file.Paths
031 
032 import static org.kordamp.gradle.util.StringUtils.isBlank
033 import static org.kordamp.gradle.util.StringUtils.isNotBlank
034 
035 /**
036  @author Andres Almiray
037  @since 0.2.0
038  */
039 @CompileStatic
040 class PropertyUtils {
041     private static String normalizePath(String path, String delimiter) {
042         if (':' == path) {
043             return ''
044         }
045         return path[1..-1].replace(':', delimiter).replace(' ''_'+ delimiter
046     }
047 
048     static String resolveValue(String envKey,
049                                String propertyKey,
050                                PathAware pathAware,
051                                String projectName) {
052         projectName = projectName.replace(' ''_').replace('-''_')
053         String value = System.getenv(normalizePath(pathAware.path, '_').toUpperCase() + envKey)
054         if (isBlank(value)) value = System.getProperty(normalizePath(pathAware.path, '.'+ propertyKey)
055         if (isBlank(value)) value = System.getenv(projectName.toUpperCase() '_' + envKey)
056         if (isBlank(value)) value = System.getProperty(projectName + '.' + propertyKey)
057         if (isBlank(value)) value = System.getenv(envKey)
058         if (isBlank(value)) value = System.getProperty(propertyKey)
059         value
060     }
061 
062     static Provider<String> stringProvider(String envKey,
063                                            String propertyKey,
064                                            Property<String> property,
065                                            Project project,
066                                            PathAware pathAware) {
067         project.providers.provider {
068             String value = resolveValue(envKey, propertyKey, pathAware, project.name)
069             isNotBlank(value? value : property.orNull
070         }
071     }
072 
073     static Provider<Boolean> booleanProvider(String envKey,
074                                              String propertyKey,
075                                              Provider<Boolean> property,
076                                              Project project,
077                                              PathAware pathAware) {
078         project.providers.provider {
079             String value = resolveValue(envKey, propertyKey, pathAware, project.name)
080             if (isNotBlank(value)) {
081                 return Boolean.parseBoolean(value)
082             }
083             property.getOrElse(false)
084         }
085     }
086 
087     static Provider<Integer> integerProvider(String envKey,
088                                              String propertyKey,
089                                              Provider<Integer> property,
090                                              Project project,
091                                              PathAware pathAware) {
092         project.providers.provider {
093             String value = resolveValue(envKey, propertyKey, pathAware, project.name)
094             if (isNotBlank(value)) {
095                 return Integer.parseInt(value)
096             }
097             property.getOrElse(0)
098         }
099     }
100 
101     static Provider<RegularFile> fileProvider(String envKey,
102                                               String propertyKey,
103                                               RegularFileProperty property,
104                                               Project project,
105                                               PathAware pathAware) {
106         project.providers.provider {
107             String value = resolveValue(envKey, propertyKey, pathAware, project.name)
108             if (isNotBlank(value)) {
109                 RegularFileProperty p = project.objects.fileProperty()
110                 p.set(Paths.get(value).toFile())
111                 return p.get()
112             }
113             property.orNull
114         }
115     }
116 
117     static Provider<Directory> directoryProvider(String envKey,
118                                                  String propertyKey,
119                                                  DirectoryProperty property,
120                                                  Project project,
121                                                  PathAware pathAware) {
122         project.providers.provider {
123             String value = resolveValue(envKey, propertyKey, pathAware, project.name)
124             if (isNotBlank(value)) {
125                 DirectoryProperty p = project.objects.directoryProperty()
126                 p.set(Paths.get(value).toFile())
127                 return p.get()
128             }
129             property.orNull
130         }
131     }
132 }