|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Mike Penz |
| 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 | +import java.text.SimpleDateFormat |
| 17 | + |
| 18 | +/** |
| 19 | + * retrieves a value from the local properties |
| 20 | + */ |
| 21 | +ext.getLocalProps = { |
| 22 | + Properties localProperties = new Properties() |
| 23 | + localProperties.load(new File('local.properties').newDataInputStream()) |
| 24 | + return localProperties |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * helper method to retrieve a value either from a local property, or via the global defined properties |
| 29 | + */ |
| 30 | +ext.getLocalOrGlobalProperty = { key -> |
| 31 | + try { |
| 32 | + def val = getLocalProps().getProperty("$key") |
| 33 | + if (val == null) { |
| 34 | + val = getProperty("$key") |
| 35 | + } |
| 36 | + return val |
| 37 | + } catch (ignored) { |
| 38 | + return null |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * retrieves a value from the properties or falls back to the default |
| 44 | + */ |
| 45 | +ext.getProperyOrDefault = { key, defau -> |
| 46 | + def val = null |
| 47 | + try { |
| 48 | + val = project.hasProperty(key) ? project.getProperty(key) : null |
| 49 | + } catch (Exception ex) { |
| 50 | + } |
| 51 | + if (val != null && !val.toString().equals("")) { |
| 52 | + return val |
| 53 | + } |
| 54 | + val = getLocalOrGlobalProperty(key) |
| 55 | + if (val == null || val.toString().equals("")) { |
| 56 | + return defau |
| 57 | + } else { |
| 58 | + return val |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * used to sign the app in release mode |
| 64 | + * Further information can be found in the README.md |
| 65 | + */ |
| 66 | +ext.getSigningFile = { |
| 67 | + return getLocalOrGlobalProperty("openSource.signing.file") |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * retrieves the current git hash |
| 72 | + */ |
| 73 | +ext.gitHash = { -> |
| 74 | + try { |
| 75 | + def outstream = new ByteArrayOutputStream() |
| 76 | + exec { |
| 77 | + commandLine 'git', 'rev-parse', '--short', 'HEAD' |
| 78 | + standardOutput = outstream |
| 79 | + } |
| 80 | + return outstream.toString().trim() |
| 81 | + } catch (Exception ex) { |
| 82 | + logger.warn('The build was not able to resolve the git branch, this project seems to be not within a git project') |
| 83 | + return "" |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * retrieves the current git branch |
| 89 | + */ |
| 90 | +ext.gitBranch = { -> |
| 91 | + try { |
| 92 | + def outstream = new ByteArrayOutputStream() |
| 93 | + exec { |
| 94 | + commandLine 'git', 'name-rev', '--name-only', 'HEAD' |
| 95 | + standardOutput = outstream |
| 96 | + } |
| 97 | + return outstream.toString().trim() |
| 98 | + } catch (Exception ex) { |
| 99 | + logger.warn('The build was not able to resolve the git branch, this project seems to be not within a git project') |
| 100 | + return "" |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * retrieves the current timestamp during the build |
| 106 | + */ |
| 107 | +ext.buildDate = { -> |
| 108 | + return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(new Date()) |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Checks if this build is from an IDE |
| 113 | + */ |
| 114 | +ext.isIdeBuild = { -> |
| 115 | + return project.properties['android.injected.invoked.from.ide'] == 'true' |
| 116 | +} |