Skip to content

Commit

Permalink
BATM-410 - proper Gradle dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanak-generalbytes committed Jan 10, 2018
1 parent 45b4f4e commit ce3980e
Show file tree
Hide file tree
Showing 40 changed files with 436 additions and 335 deletions.
27 changes: 0 additions & 27 deletions bin/create-maven-repos.sh

This file was deleted.

50 changes: 0 additions & 50 deletions bin/install-lib.sh

This file was deleted.

9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ buildscript {
}
}

final File repoDir = project.file('libs')
allprojects {
repositories {
jcenter()
}
repositories {
//bitcoin-json-rpc-client-1.0.jar isn't part of any well known maven repo
//so we search the libs dir; gradle generates (guesses) metadata except dependencies.
//Artifacts from repos with real metadata take precedence.
flatDir {
dirs repoDir
}
}
}
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)
}
}
}
}
}
}
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)
}
}
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);
}
}
}
}
}
}
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";
}
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
}
}
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
}
}
Loading

0 comments on commit ce3980e

Please sign in to comment.