forked from GENERALBYTESCOM/batm_public
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BATM-410 - proper Gradle dependencies
- Loading branch information
1 parent
45b4f4e
commit ce3980e
Showing
40 changed files
with
436 additions
and
335 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
buildSrc/src/main/groovy/com.generalbytes.batm.gradle/DependencySubstitutionPlugin.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.generalbytes.batm.gradle | ||
|
||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.artifacts.DependencyResolveDetails | ||
import org.gradle.api.artifacts.UnknownConfigurationException | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
class DependencySubstitutionPlugin implements Plugin<Project> { | ||
private DependencySubstitutionPluginExtension extension | ||
private List<String> initialConfinedConfigurations = new LinkedList<>() | ||
private Logger logger = LoggerFactory.getLogger('gb-gradle') | ||
|
||
static void confine(Project project, String configurationName) { | ||
confine(project.getConfigurations().getByName(configurationName)) | ||
} | ||
|
||
static void confine(Configuration configuration) { | ||
configuration.transitive = false | ||
} | ||
|
||
void apply(Project project) { | ||
extension = project.extensions.create('dependencySubstitutions', DependencySubstitutionPluginExtension) | ||
initialConfinedConfigurations.addAll([ | ||
'compile', | ||
'compileOnly', | ||
'compileClasspath', | ||
'implementation', | ||
'providedCompile', | ||
|
||
'testCompile', | ||
'testCompileOnly', | ||
'testCompileClasspath', | ||
'testImplementation' | ||
]) | ||
|
||
project.afterEvaluate { | ||
configure(project) | ||
} | ||
} | ||
|
||
void configure(Project project) { | ||
initialConfinedConfigurations.each { | ||
try { | ||
confine(project.configurations[it]) | ||
} catch (UnknownConfigurationException ignored) { | ||
logger.info("Plugin-specified confined configuration $it not found, skipping...") | ||
} | ||
} | ||
extension.confinedConfigurations.each { | ||
switch (it) { | ||
case String: | ||
confine(project, (String) it) | ||
break | ||
case Configuration: | ||
confine((Configuration) it) | ||
break | ||
default: | ||
throw new IllegalStateException("Illegal specification for confined configuration: $it (${it.class})") | ||
} | ||
} | ||
|
||
if (extension.conflictFail) { | ||
project.configurations.all { | ||
resolutionStrategy { | ||
failOnVersionConflict() | ||
} | ||
} | ||
} | ||
|
||
project.configurations.all { | ||
resolutionStrategy { | ||
eachDependency { DependencyResolveDetails details -> | ||
final SimpleModuleVersionIdentifier from = new SimpleModuleVersionIdentifier(details.requested.group, details.requested.name, details.requested.version) | ||
final String toVersion = extension.substitutions.get(from) | ||
if (toVersion != null && toVersion.length() > 0) { | ||
details.useVersion(toVersion) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...src/main/groovy/com.generalbytes.batm.gradle/DependencySubstitutionPluginExtension.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.generalbytes.batm.gradle | ||
|
||
import org.gradle.api.artifacts.Configuration | ||
|
||
import java.util.regex.Matcher | ||
import java.util.regex.Pattern | ||
|
||
class DependencySubstitutionPluginExtension { | ||
List<Object> confinedConfigurations = new LinkedList<>() | ||
Map<SimpleModuleVersionIdentifier, String> substitutions = new HashMap<>() | ||
boolean conflictFail = true | ||
|
||
void substitute(File file) { | ||
final Pattern commentPattern = Pattern.compile('(?m)^\\p{Blank}*#.*$') | ||
final Pattern substitutionPattern = Pattern.compile('(?m)^substitute\\p{Blank}*from\\p{Blank}*:\\p{Blank}*\\\'([^\\\']*)\\\'\\p{Blank}*,\\p{Blank}*toVersion\\p{Blank}*:\\p{Blank}*\\\'([^\\\']*)\\\'\\p{Blank}*$') | ||
int lineNo = 0 | ||
file.eachLine { line -> | ||
lineNo++ | ||
Matcher substitutionMatcher = substitutionPattern.matcher(line) | ||
if (substitutionMatcher.matches()) { | ||
final String from = substitutionMatcher.group(1) | ||
final String toVersion = substitutionMatcher.group(2) | ||
substitute(from, toVersion) | ||
} else if (!line.matches(commentPattern)) { | ||
def msg = "Error on line $lineNo of file ${file.canonicalPath}: illegal line format." | ||
throw new IllegalStateException(msg) | ||
} | ||
} | ||
} | ||
|
||
void substitute(String from, String toVersion) { | ||
substitutions.put(new SimpleModuleVersionIdentifier(from), toVersion) | ||
} | ||
|
||
void substitute(Map attrs) { | ||
def from = attrs.from | ||
def toVersion = attrs.toVersion | ||
|
||
if (from == null) { | ||
def msg = "Missing required argument: 'from'." | ||
throw new IllegalArgumentException(msg) | ||
} | ||
if (toVersion == null) { | ||
def msg = "Missing required argument: 'toVersion'." | ||
throw new IllegalArgumentException(msg) | ||
} | ||
substitute(from, toVersion) | ||
} | ||
|
||
void confine(String cfgName) { | ||
confinedConfigurations.add(cfgName) | ||
} | ||
|
||
void confine(Configuration cfg) { | ||
confinedConfigurations.add(cfg) | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
buildSrc/src/main/groovy/com.generalbytes.batm.gradle/EncryptFile.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.generalbytes.batm.gradle | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
import javax.crypto.Cipher | ||
import javax.crypto.CipherOutputStream | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
class EncryptFile extends DefaultTask { | ||
public static final int REQUIRED_KEY_LENGTH = 32 | ||
private Object outputFile; | ||
private Object inputFile; | ||
private byte[] key = null; | ||
|
||
@OutputFile | ||
File getOutputFile() { | ||
return getProject().file(outputFile); | ||
} | ||
|
||
void setOutputFile(Object outputFile) { | ||
this.outputFile = outputFile; | ||
} | ||
|
||
@InputFile | ||
File getInputFile() { | ||
return getProject().file(inputFile) | ||
} | ||
|
||
void setInputFile(Object inputFile) { | ||
this.inputFile = inputFile | ||
} | ||
|
||
@Input | ||
byte[] getKey() { | ||
return key | ||
} | ||
|
||
void setKey(byte[] key) { | ||
if (key == null) { | ||
throw new IllegalArgumentException("Key can't be null.") | ||
} | ||
if (key.length != REQUIRED_KEY_LENGTH) { | ||
throw new IllegalArgumentException("Incorrect key length (${key.length}); should be ${REQUIRED_KEY_LENGTH}.") | ||
} | ||
this.key = key | ||
} | ||
|
||
@TaskAction | ||
void encrypt() throws IOException { | ||
if (key == null || key.length != REQUIRED_KEY_LENGTH) { | ||
throw new IllegalStateException("Invalid key: ${key}") | ||
} | ||
|
||
// Length is 16 byte | ||
final SecretKeySpec sks = new SecretKeySpec(key, "AES"); | ||
|
||
// Create cipher | ||
final Cipher cipher = Cipher.getInstance("AES"); | ||
cipher.init(Cipher.ENCRYPT_MODE, sks); | ||
|
||
getInputFile().withInputStream { is -> | ||
getOutputFile().withOutputStream { os -> | ||
// Wrap the output stream | ||
new CipherOutputStream(os, cipher).withStream { cos -> | ||
// Write bytes | ||
int b; | ||
byte[] d = new byte[8]; | ||
while((b = is.read(d)) != -1) { | ||
cos.write(d, 0, b); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
buildSrc/src/main/groovy/com.generalbytes.batm.gradle/HelperConstants.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.generalbytes.batm.gradle; | ||
|
||
public class HelperConstants { | ||
public static final String ANDROID_ARTIFACT_EXPORTING_CONFIGURATION_NAME = "exportReleaseApk"; | ||
} |
17 changes: 17 additions & 0 deletions
17
buildSrc/src/main/groovy/com.generalbytes.batm.gradle/SimpleModuleIdentifier.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.generalbytes.batm.gradle | ||
|
||
import groovy.transform.EqualsAndHashCode | ||
import groovy.transform.ToString | ||
import org.gradle.api.artifacts.ModuleIdentifier | ||
|
||
@EqualsAndHashCode | ||
@ToString | ||
class SimpleModuleIdentifier implements ModuleIdentifier{ | ||
final String group | ||
final String name | ||
|
||
SimpleModuleIdentifier(String group, String name) { | ||
this.group = group | ||
this.name = name | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
buildSrc/src/main/groovy/com.generalbytes.batm.gradle/SimpleModuleVersionIdentifier.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.generalbytes.batm.gradle | ||
|
||
import groovy.transform.EqualsAndHashCode | ||
import groovy.transform.ToString | ||
import org.gradle.api.artifacts.ModuleVersionIdentifier | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
|
||
import java.util.regex.Matcher | ||
import java.util.regex.Pattern | ||
|
||
@EqualsAndHashCode | ||
@ToString | ||
class SimpleModuleVersionIdentifier implements ModuleVersionIdentifier { | ||
private Logger logger = LoggerFactory.getLogger(DependencySubstitutionPluginExtension.class.simpleName) | ||
final SimpleModuleIdentifier module | ||
final String version | ||
|
||
SimpleModuleVersionIdentifier(String id) { | ||
final Matcher matcher = Pattern.compile('^([^:]*):([^:]*):([^:]*)$').matcher(id) | ||
if (!matcher.matches()) { | ||
def msg = "Module identifier '$id' has incorrect format." | ||
// logger.error(msg) | ||
throw new IllegalArgumentException(msg) | ||
} | ||
this.module = new SimpleModuleIdentifier(matcher.group(1), matcher.group(2)) | ||
this.version = matcher.group(3) | ||
} | ||
|
||
SimpleModuleVersionIdentifier(String group, String name, String version) { | ||
this.version = version | ||
this.module = new SimpleModuleIdentifier(group, name) | ||
} | ||
|
||
@Override | ||
String getGroup() { | ||
return module.group | ||
} | ||
|
||
@Override | ||
String getName() { | ||
return module.name | ||
} | ||
} |
Oops, something went wrong.