|
| 1 | +package com.antfortune.freeline |
| 2 | + |
| 3 | +import groovy.json.JsonBuilder |
| 4 | +import org.gradle.api.Project; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by huangyong on 16/11/3. |
| 8 | + */ |
| 9 | +class FreelineAnnotationCollector { |
| 10 | + |
| 11 | + public static final def ANNOTATION_CLASSES = [ |
| 12 | + "Landroid/databinding/BindingAdapter;", |
| 13 | + "Landroid/databinding/BindingConversion;", |
| 14 | + "Landroid/databinding/Bindable;", |
| 15 | + ] |
| 16 | + |
| 17 | + public static final def ANNOTATION_TARGETS = [ |
| 18 | + "Landroid/databinding/BindingAdapter;": "BindingAdapter", |
| 19 | + "Landroid/databinding/BindingConversion;": "BindingConversion", |
| 20 | + "Landroid/databinding/Bindable;": "Bindable" |
| 21 | + ] |
| 22 | + |
| 23 | + private static def sAnnotationCollection = [:] |
| 24 | + |
| 25 | + public static void addNewAnno(String anno, String path, String className, String entry, boolean isJar) { |
| 26 | + String key = ANNOTATION_TARGETS[anno] |
| 27 | + if (!sAnnotationCollection.containsKey(key)) { |
| 28 | + sAnnotationCollection[key] = [] |
| 29 | + } |
| 30 | + |
| 31 | + sAnnotationCollection[key].add(['path': path, 'className': className, 'entry': entry, 'isJar': isJar]) |
| 32 | + } |
| 33 | + |
| 34 | + public static void saveCollections(Project project, String buildCacheDirPath, Map modules) { |
| 35 | + def description = FreelineUtils.readProjectDescription(project) |
| 36 | + sAnnotationCollection.keySet().each { key -> |
| 37 | + sAnnotationCollection[key].each { value -> |
| 38 | + if (value['isJar']) { |
| 39 | + modules.each { m, sep -> |
| 40 | + if (value['path'].contains(sep)) { |
| 41 | + value['module'] = m |
| 42 | + value['java_path'] = findJavaPath(description, m as String, value['className'] as String) |
| 43 | + return false |
| 44 | + } |
| 45 | + } |
| 46 | + } else { |
| 47 | + value['module'] = project.name |
| 48 | + value['java_path'] = findJavaPath(description, project.name, value['className'] as String) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + def json = new JsonBuilder(sAnnotationCollection).toPrettyString() |
| 54 | + println json |
| 55 | + FreelineUtils.saveJson(json, FreelineUtils.joinPath(buildCacheDirPath, "freeline_annotation_info.json"), true) |
| 56 | + |
| 57 | + sAnnotationCollection.clear() |
| 58 | + } |
| 59 | + |
| 60 | + private static String findJavaPath(def description, String module, String className) { |
| 61 | + if (description != null) { |
| 62 | + if (description['project_source_sets'].containsKey(module)) { |
| 63 | + def relatedPath = className.replace("/", File.separator).replace(".class", ".java") |
| 64 | + if (!relatedPath.endsWith(".java")) { |
| 65 | + relatedPath = relatedPath + ".java" |
| 66 | + } |
| 67 | + |
| 68 | + def javaPath = null |
| 69 | + description['project_source_sets'][module]['main_src_directory'].each { path -> |
| 70 | + File file = new File(FreelineUtils.joinPath(path as String, relatedPath)) |
| 71 | + if (file.exists()) { |
| 72 | + javaPath = file.absolutePath |
| 73 | + return false |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (javaPath != null) { |
| 78 | + return javaPath |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return null |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments