use of groovy.lang.Closure in project atlas by alibaba.
the class DiffResExtractor method extractDiff.
/**
* assets : Compare apk directly
* res : Through diffResFiles , Go to apk validation
*
* @param appVariantContext
* @param diffResFiles
* @param currentApk
* @param baseApk
* @param fullResDir
* @param destDir
* @throws IOException
*/
public static void extractDiff(AppVariantContext appVariantContext, Set<String> diffResFiles, File currentApk, File baseApk, File fullResDir, File destDir, boolean fullValues) throws IOException {
if (!currentApk.exists() || !baseApk.exists() || !fullResDir.exists()) {
return;
}
FileUtils.deleteDirectory(destDir);
destDir.mkdirs();
File tmpFolder = new File(destDir.getParentFile(), "tmp-diffRes");
FileUtils.deleteDirectory(tmpFolder);
tmpFolder.mkdirs();
File apkDir = new File(tmpFolder, "newApkDir");
File baseApkDir = new File(tmpFolder, "baseApkDir");
ZipUtils.unzip(currentApk, apkDir.getAbsolutePath());
ZipUtils.unzip(baseApk, baseApkDir.getAbsolutePath());
// compare res and assets
Collection<File> files = FileUtils.listFiles(apkDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
int basePathLength = apkDir.getAbsolutePath().length();
// Calculate the assets
for (File file : files) {
String relativePath = file.getAbsolutePath().substring(basePathLength);
if (!relativePath.startsWith("/assets/")) {
continue;
}
File baseFile = new File(baseApkDir, relativePath);
if (!baseFile.exists() || !MD5Util.getFileMD5(file).equals(MD5Util.getFileMD5(baseFile))) {
FileUtils.copyFile(file, new File(destDir, relativePath));
}
}
// Calculate the res
for (String diffFile : diffResFiles) {
File baseFile = new File(baseApkDir, diffFile);
File currentFile = new File(apkDir, diffFile);
if (baseFile.exists() && currentFile.exists() && MD5Util.getFileMD5(baseFile).equals(MD5Util.getFileMD5(currentFile))) {
continue;
}
// copy file
File rawFile = new File(fullResDir, diffFile);
if (rawFile.exists()) {
FileUtils.copyFile(rawFile, new File(destDir, diffFile));
}
}
// //Resource. Arsc must be generated
File resDir = new File(destDir, "res");
File valuesDir = new File(resDir, "values");
if (fullValues) {
FileUtils.forceMkdir(valuesDir);
appVariantContext.getProject().copy(new Closure(DiffResExtractor.class) {
public Object doCall(CopySpec cs) {
cs.from(fullResDir);
cs.into(destDir);
cs.include("res/values*/**");
return cs;
}
});
// FileUtils.copyFile(new File(fullResDir, "res/values/values.xml"),
// new File(destDir, "res/values/values.xml"));
} else {
if (!resDir.exists()) {
FileUtils.forceMkdir(valuesDir);
File stringsFile = new File(valuesDir, "strings.xml");
UUID uuid = UUID.randomUUID();
FileUtils.writeStringToFile(stringsFile, String.format("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n <string " + "name=\"%s\">%s</string>\n</resources>\n", uuid, uuid), "UTF-8", false);
}
}
// XML Settings values.
File valuesXml = new File(resDir, "values/values.xml");
AtlasBuildContext.sBuilderAdapter.apkInjectInfoCreator.injectTpatchValuesRes(appVariantContext, valuesXml);
try {
removeStringValue(valuesXml, "ttid");
removeStringValue(valuesXml, "config_channel");
} catch (Exception e) {
throw new RuntimeException(e);
}
final Pattern densityOnlyPattern = Pattern.compile("[a-zA-Z]+-[a-zA-Z]+dpi");
if (resDir.exists()) {
File[] resDirs = resDir.listFiles();
if (resDirs != null) {
for (File file : resDirs) {
Matcher m = densityOnlyPattern.matcher(file.getName());
if (m.matches()) {
FileUtils.moveDirectory(file, new File(file.getAbsolutePath() + "-v3"));
}
}
}
}
}
use of groovy.lang.Closure in project groovy by apache.
the class StaticTypeCheckingVisitor method ensureValidSetter.
/**
* Given a binary expression corresponding to an assignment, will check that
* the type of the RHS matches one of the possible setters and if not, throw
* a type checking error.
*
* @param expression the assignment expression
* @param leftExpression left expression of the assignment
* @param rightExpression right expression of the assignment
* @param setterInfo possible setters
* @return {@code false} if valid setter found or {@code true} if type checking error created
*/
private boolean ensureValidSetter(final Expression expression, final Expression leftExpression, final Expression rightExpression, final SetterInfo setterInfo) {
// for expressions like foo = { ... }
// we know that the RHS type is a closure
// but we must check if the binary expression is an assignment
// because we need to check if a setter uses @DelegatesTo
VariableExpression receiver = varX("%", setterInfo.receiverType);
// for "x op= y" expression, find type as if it was "x = x op y"
Expression valueExpression = rightExpression;
if (isCompoundAssignment(expression)) {
Token op = ((BinaryExpression) expression).getOperation();
if (op.getType() == ELVIS_EQUAL) {
// GROOVY-10419: "x ?= y"
valueExpression = elvisX(leftExpression, rightExpression);
} else {
op = Token.newSymbol(TokenUtil.removeAssignment(op.getType()), op.getStartLine(), op.getStartColumn());
valueExpression = binX(leftExpression, op, rightExpression);
}
}
Function<Expression, MethodNode> setterCall = right -> {
MethodCallExpression call = callX(receiver, setterInfo.name, right);
call.setImplicitThis(false);
visitMethodCallExpression(call);
return call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
};
Function<MethodNode, ClassNode> setterType = setter -> {
ClassNode type = setter.getParameters()[0].getOriginType();
if (!setter.isStatic() && !(setter instanceof ExtensionMethodNode) && GenericsUtils.hasUnresolvedGenerics(type)) {
type = applyGenericsContext(extractPlaceHolders(setterInfo.receiverType, setter.getDeclaringClass()), type);
}
return type;
};
MethodNode methodTarget = setterCall.apply(valueExpression);
if (methodTarget == null && !isCompoundAssignment(expression)) {
// if no direct match, try implicit conversion
for (MethodNode setter : setterInfo.setters) {
ClassNode lType = setterType.apply(setter);
ClassNode rType = getDeclaredOrInferredType(valueExpression);
if (checkCompatibleAssignmentTypes(lType, rType, valueExpression, false)) {
methodTarget = setterCall.apply(castX(lType, valueExpression));
if (methodTarget != null) {
break;
}
}
}
}
if (methodTarget != null) {
for (MethodNode setter : setterInfo.setters) {
if (setter == methodTarget) {
leftExpression.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, methodTarget);
// clear assumption
leftExpression.removeNodeMetaData(INFERRED_TYPE);
storeType(leftExpression, setterType.apply(methodTarget));
break;
}
}
return false;
} else {
ClassNode firstSetterType = setterType.apply(setterInfo.setters.get(0));
addAssignmentError(firstSetterType, getType(valueExpression), expression);
return true;
}
}
use of groovy.lang.Closure in project groovy by apache.
the class AbstractTypeCheckingExtension method newScope.
public TypeCheckingScope newScope(Closure code) {
TypeCheckingScope scope = newScope();
Closure callback = code.rehydrate(scope, this, this);
callback.call();
return scope;
}
use of groovy.lang.Closure in project groovy by apache.
the class GroovyTypeCheckingExtensionSupport method beforeMethodCall.
@Override
public boolean beforeMethodCall(final MethodCall call) {
setHandled(false);
List<Closure> onMethodSelection = eventHandlers.get("beforeMethodCall");
if (onMethodSelection != null) {
for (Closure closure : onMethodSelection) {
safeCall(closure, call);
}
}
return handled;
}
use of groovy.lang.Closure in project groovy by apache.
the class GroovyTypeCheckingExtensionSupport method handleAmbiguousMethods.
@Override
@SuppressWarnings("unchecked")
public List<MethodNode> handleAmbiguousMethods(final List<MethodNode> nodes, final Expression origin) {
List<Closure> onMethodSelection = eventHandlers.get("handleAmbiguousMethods");
List<MethodNode> methodList = nodes;
if (onMethodSelection != null) {
Iterator<Closure> iterator = onMethodSelection.iterator();
while (methodList.size() > 1 && iterator.hasNext()) {
final Closure closure = iterator.next();
Object result = safeCall(closure, methodList, origin);
if (result != null) {
if (result instanceof MethodNode) {
methodList = Collections.singletonList((MethodNode) result);
} else if (result instanceof Collection) {
methodList = new LinkedList<MethodNode>((Collection<? extends MethodNode>) result);
} else {
throw new GroovyBugError("Type checking extension returned unexpected method list: " + result);
}
}
}
}
return methodList;
}
Aggregations