Commit 9ee7c30c5035b5081d06665c24211b560d72ed83
Exists in
yxb_dev
and in
2 other branches
Merge branch 'developer' into yxb_dev
# Conflicts: # app/libs/processor.jar
Showing
8 changed files
with
134 additions
and
17 deletions
Show diff stats
.gitignore
| @@ -7,6 +7,7 @@ | @@ -7,6 +7,7 @@ | ||
| 7 | /build | 7 | /build |
| 8 | /captures | 8 | /captures |
| 9 | .externalNativeBuild | 9 | .externalNativeBuild |
| 10 | +app/libs/processor.jar | ||
| 10 | app/release/app-release.apk | 11 | app/release/app-release.apk |
| 11 | app/src/main/java/com/shunzhi/parent/DataBaseTestActivity.java | 12 | app/src/main/java/com/shunzhi/parent/DataBaseTestActivity.java |
| 12 | app/src/main/res/layout/activity_data_base_test.xml | 13 | app/src/main/res/layout/activity_data_base_test.xml |
.idea/modules.xml
| @@ -9,6 +9,7 @@ | @@ -9,6 +9,7 @@ | ||
| 9 | <module fileurl="file://$PROJECT_DIR$/parentwork.iml" filepath="$PROJECT_DIR$/parentwork.iml" /> | 9 | <module fileurl="file://$PROJECT_DIR$/parentwork.iml" filepath="$PROJECT_DIR$/parentwork.iml" /> |
| 10 | <module fileurl="file://F:\parentWorkHolper\parentwork.iml" filepath="F:\parentWorkHolper\parentwork.iml" /> | 10 | <module fileurl="file://F:\parentWorkHolper\parentwork.iml" filepath="F:\parentWorkHolper\parentwork.iml" /> |
| 11 | <module fileurl="file://$PROJECT_DIR$/processor/processor.iml" filepath="$PROJECT_DIR$/processor/processor.iml" /> | 11 | <module fileurl="file://$PROJECT_DIR$/processor/processor.iml" filepath="$PROJECT_DIR$/processor/processor.iml" /> |
| 12 | + <module fileurl="file://E:\parentwork\processor\processor.iml" filepath="E:\parentwork\processor\processor.iml" /> | ||
| 12 | <module fileurl="file://$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" filepath="$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" /> | 13 | <module fileurl="file://$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" filepath="$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" /> |
| 13 | </modules> | 14 | </modules> |
| 14 | </component> | 15 | </component> |
app/build.gradle
| @@ -45,7 +45,36 @@ android { | @@ -45,7 +45,36 @@ android { | ||
| 45 | task processorTask(type: Copy) { | 45 | task processorTask(type: Copy) { |
| 46 | from '../processor/build/libs/processor.jar' into 'libs/' | 46 | from '../processor/build/libs/processor.jar' into 'libs/' |
| 47 | } | 47 | } |
| 48 | + | ||
| 49 | +//region 如果需要注释,注释掉可以合并起来的这一段全部 | ||
| 50 | +task fileCheckTask() { | ||
| 51 | + //delete('/app/src/main/assets/migrations') | ||
| 52 | + //此方法执行以后会删除所有的version.sql文件,不能主动添加sql语句 | ||
| 53 | + //如果已经定义、实现的注解不能满足更新要求,请自己添加sql语句,并且将下面删除文件的语句注释 | ||
| 54 | + //注意 注释以后不要打开(写这个task是为了试验) | ||
| 55 | + doLast { | ||
| 56 | + File file = new File(rootDir.getAbsolutePath() + "\\app\\src\\main\\assets\\migrations") | ||
| 57 | + File[] files = file.listFiles() | ||
| 58 | + for (int i = 0; i < files.length; i++) { | ||
| 59 | + File file1 = files[i] | ||
| 60 | + println("delete : "+file1.getAbsolutePath()) | ||
| 61 | + file1.delete() | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +afterEvaluate { | ||
| 67 | + tasks.matching { | ||
| 68 | + it.name.startsWith('process') && (it.name.endsWith('ReleaseJavaRes') || it.name.endsWith | ||
| 69 | + ('DebugJavaRes')) | ||
| 70 | + }.each { tk -> | ||
| 71 | + tk.dependsOn(fileCheckTask) | ||
| 72 | + } | ||
| 73 | +} | ||
| 74 | +//endregion | ||
| 75 | + | ||
| 48 | processorTask.dependsOn(':processor:build') | 76 | processorTask.dependsOn(':processor:build') |
| 77 | + | ||
| 49 | preBuild.dependsOn(processorTask) | 78 | preBuild.dependsOn(processorTask) |
| 50 | greendao { | 79 | greendao { |
| 51 | schemaVersion 1//数据库版本升级 | 80 | schemaVersion 1//数据库版本升级 |
| @@ -66,4 +95,4 @@ dependencies { | @@ -66,4 +95,4 @@ dependencies { | ||
| 66 | compile 'me.leolin:ShortcutBadger:1.1.19@aar' | 95 | compile 'me.leolin:ShortcutBadger:1.1.19@aar' |
| 67 | annotationProcessor 'com.google.dagger:dagger-compiler:2.12' | 96 | annotationProcessor 'com.google.dagger:dagger-compiler:2.12' |
| 68 | compile files('libs/processor.jar') | 97 | compile files('libs/processor.jar') |
| 69 | -} | 98 | -} |
| 99 | +} | ||
| 70 | \ No newline at end of file | 100 | \ No newline at end of file |
app/libs/processor.jar
No preview for this file type
app/src/main/assets/migrations/1.sql
app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java
| @@ -2,6 +2,11 @@ package com.shunzhi.parent.annotation; | @@ -2,6 +2,11 @@ package com.shunzhi.parent.annotation; | ||
| 2 | 2 | ||
| 3 | import org.shunzhi.processorlibs.AddColumn; | 3 | import org.shunzhi.processorlibs.AddColumn; |
| 4 | 4 | ||
| 5 | +/* | ||
| 6 | +Error:groovy.lang.MissingMethodException: No signature of method: | ||
| 7 | +java.io.File.plus() is applicable for argument types: (java.lang.String) values: [\app\src\main\assets\migrations] | ||
| 8 | + */ | ||
| 9 | + | ||
| 5 | public class VersionTest { | 10 | public class VersionTest { |
| 6 | @AddColumn(version = 3, tableName = "test", type = "String") | 11 | @AddColumn(version = 3, tableName = "test", type = "String") |
| 7 | private String name; | 12 | private String name; |
processor/src/main/java/org/shunzhi/processorlibs/Update.java
0 → 100644
| @@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
| 1 | +package org.shunzhi.processorlibs; | ||
| 2 | + | ||
| 3 | +import java.lang.annotation.ElementType; | ||
| 4 | +import java.lang.annotation.Retention; | ||
| 5 | +import java.lang.annotation.RetentionPolicy; | ||
| 6 | +import java.lang.annotation.Target; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 可能会使用到,只定义了注解,逻辑处理需要在VersionProcessor类中实现<p/> | ||
| 10 | + * 需要使用的时候自己实现 | ||
| 11 | + */ | ||
| 12 | +@Target(ElementType.FIELD) | ||
| 13 | +@Retention(RetentionPolicy.SOURCE) | ||
| 14 | +public @interface Update { | ||
| 15 | + String columnName(); | ||
| 16 | + | ||
| 17 | + String tableName(); | ||
| 18 | + | ||
| 19 | + int version(); | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 请设置类型 | ||
| 23 | + * {@link Update#valueType()} | ||
| 24 | + */ | ||
| 25 | + String strValue() default ""; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 请设置类型 | ||
| 29 | + * {@link Update#valueType()} | ||
| 30 | + */ | ||
| 31 | + int intValue() default -1; | ||
| 32 | + | ||
| 33 | + /** | ||
| 34 | + * 类型需要与值对应,不对应可能会导致更新数据库失败 | ||
| 35 | + */ | ||
| 36 | + String valueType(); | ||
| 37 | + | ||
| 38 | + boolean boolValue() default false; | ||
| 39 | +} |
processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java
| @@ -8,8 +8,11 @@ import java.io.FileWriter; | @@ -8,8 +8,11 @@ import java.io.FileWriter; | ||
| 8 | import java.io.IOException; | 8 | import java.io.IOException; |
| 9 | import java.io.OutputStreamWriter; | 9 | import java.io.OutputStreamWriter; |
| 10 | import java.io.PrintStream; | 10 | import java.io.PrintStream; |
| 11 | +import java.text.SimpleDateFormat; | ||
| 12 | +import java.util.Date; | ||
| 11 | import java.util.HashMap; | 13 | import java.util.HashMap; |
| 12 | import java.util.HashSet; | 14 | import java.util.HashSet; |
| 15 | +import java.util.Locale; | ||
| 13 | import java.util.Set; | 16 | import java.util.Set; |
| 14 | 17 | ||
| 15 | import javax.annotation.processing.AbstractProcessor; | 18 | import javax.annotation.processing.AbstractProcessor; |
| @@ -31,24 +34,31 @@ public class VersionProcessor extends AbstractProcessor { | @@ -31,24 +34,31 @@ public class VersionProcessor extends AbstractProcessor { | ||
| 31 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | 34 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 32 | // File file = new File("test"); | 35 | // File file = new File("test"); |
| 33 | // if (!file.exists())file.mkdir(); | 36 | // if (!file.exists())file.mkdir(); |
| 34 | -// File countFile = new File("测试数据.txt"); | ||
| 35 | -// try { | ||
| 36 | -// BufferedWriter writer = new BufferedWriter(new FileWriter(countFile)); | ||
| 37 | -// writer.write(System.currentTimeMillis() + ""); | ||
| 38 | -// writer.newLine(); | ||
| 39 | -// writer.flush();writer.close(); | ||
| 40 | -// } catch (IOException e) { | ||
| 41 | -// e.printStackTrace(); | ||
| 42 | -// } | 37 | + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.CHINA); |
| 38 | + File countFile = new File("buildData.txt"); | ||
| 39 | + BufferedWriter writer = null; | ||
| 40 | + try { | ||
| 41 | + writer = new BufferedWriter(new FileWriter(countFile)); | ||
| 42 | + writer.write(format.format(new Date())); | ||
| 43 | + writer.newLine(); | ||
| 44 | + writer.flush(); | ||
| 45 | + //writer.close(); | ||
| 46 | + } catch (IOException e) { | ||
| 47 | + e.printStackTrace(); | ||
| 48 | + } | ||
| 43 | 49 | ||
| 44 | BufferedWriter testBw = null; | 50 | BufferedWriter testBw = null; |
| 45 | 51 | ||
| 46 | HashMap<Integer, Set<String>> map = new HashMap<>(); | 52 | HashMap<Integer, Set<String>> map = new HashMap<>(); |
| 47 | 53 | ||
| 48 | try { | 54 | try { |
| 55 | + File file = new File("sql.txt"); | ||
| 56 | + //if (!file.exists()) file.createNewFile(); | ||
| 57 | + Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(AddColumn.class); | ||
| 58 | + writer.write("annotations size = " + elements.size()); | ||
| 49 | for (Element element : roundEnv.getElementsAnnotatedWith(AddColumn.class)) { | 59 | for (Element element : roundEnv.getElementsAnnotatedWith(AddColumn.class)) { |
| 50 | - File file = new File("sql.txt"); | ||
| 51 | - if (!file.exists()) file.createNewFile(); | 60 | + writer.write(element.getSimpleName().toString()); |
| 61 | + writer.newLine(); | ||
| 52 | testBw = new BufferedWriter(new OutputStreamWriter(new PrintStream(file))); | 62 | testBw = new BufferedWriter(new OutputStreamWriter(new PrintStream(file))); |
| 53 | AddColumn addColumn = element.getAnnotation(AddColumn.class); | 63 | AddColumn addColumn = element.getAnnotation(AddColumn.class); |
| 54 | int version = addColumn.version(); | 64 | int version = addColumn.version(); |
| @@ -72,6 +82,8 @@ public class VersionProcessor extends AbstractProcessor { | @@ -72,6 +82,8 @@ public class VersionProcessor extends AbstractProcessor { | ||
| 72 | if (!isDebug) | 82 | if (!isDebug) |
| 73 | set.add(sql); | 83 | set.add(sql); |
| 74 | } | 84 | } |
| 85 | + writer.write(sql); | ||
| 86 | + writer.newLine(); | ||
| 75 | if (isDebug) { | 87 | if (isDebug) { |
| 76 | testBw.write(version + " "); | 88 | testBw.write(version + " "); |
| 77 | testBw.write(sql); | 89 | testBw.write(sql); |
| @@ -91,27 +103,52 @@ public class VersionProcessor extends AbstractProcessor { | @@ -91,27 +103,52 @@ public class VersionProcessor extends AbstractProcessor { | ||
| 91 | } | 103 | } |
| 92 | } | 104 | } |
| 93 | File tmp = new File("tmp"); | 105 | File tmp = new File("tmp"); |
| 94 | -// File migrations = new File(tmp.getParentFile(), "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator)); | 106 | + File migrations = new File(tmp.getParentFile(), "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator)); |
| 107 | + try { | ||
| 108 | + writer.newLine(); | ||
| 109 | + writer.write(map.toString()); | ||
| 110 | + writer.newLine(); | ||
| 111 | + } catch (IOException e) { | ||
| 112 | + e.printStackTrace(); | ||
| 113 | + } | ||
| 95 | // if (migrations.exists()) { | 114 | // if (migrations.exists()) { |
| 96 | // File[] files = migrations.listFiles(); | 115 | // File[] files = migrations.listFiles(); |
| 97 | -// if (files != null) | 116 | +// if (files != null) { |
| 98 | // for (File f : files) { | 117 | // for (File f : files) { |
| 99 | // try { | 118 | // try { |
| 119 | +// writer.write("exist files" + f.getName()); | ||
| 120 | +// writer.newLine(); | ||
| 121 | +// writer.flush(); | ||
| 100 | // Integer existVersion = Integer.parseInt(f.getName().replace(".sql", "")); | 122 | // Integer existVersion = Integer.parseInt(f.getName().replace(".sql", "")); |
| 101 | // if (existVersion < 2) continue; | 123 | // if (existVersion < 2) continue; |
| 102 | // if (!map.containsKey(existVersion)) { | 124 | // if (!map.containsKey(existVersion)) { |
| 125 | +// writer.write("delete file" + f.getName()); | ||
| 126 | +// writer.newLine(); | ||
| 127 | +// writer.flush(); | ||
| 103 | // f.delete(); | 128 | // f.delete(); |
| 104 | // } | 129 | // } |
| 105 | -// } catch (NumberFormatException e) { | 130 | +// } catch (Exception e) { |
| 106 | // e.printStackTrace(); | 131 | // e.printStackTrace(); |
| 107 | // } | 132 | // } |
| 108 | // } | 133 | // } |
| 134 | +// } | ||
| 109 | // } | 135 | // } |
| 136 | + | ||
| 137 | + try { | ||
| 138 | + writer.write("will create sql file"); | ||
| 139 | + writer.newLine(); | ||
| 140 | + writer.write(map.toString()); | ||
| 141 | + } catch (IOException e) { | ||
| 142 | + e.printStackTrace(); | ||
| 143 | + } | ||
| 110 | for (Integer version : map.keySet()) { | 144 | for (Integer version : map.keySet()) { |
| 111 | try { | 145 | try { |
| 112 | String path = "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator) + version + ".sql"; | 146 | String path = "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator) + version + ".sql"; |
| 113 | File file = new File(tmp.getParentFile(), path); | 147 | File file = new File(tmp.getParentFile(), path); |
| 114 | Set<String> set = map.get(version); | 148 | Set<String> set = map.get(version); |
| 149 | + writer.write("create file" + version + ".sql"); | ||
| 150 | + writer.newLine(); | ||
| 151 | + writer.flush(); | ||
| 115 | if (file.exists()) { | 152 | if (file.exists()) { |
| 116 | BufferedReader br = new BufferedReader(new FileReader(file)); | 153 | BufferedReader br = new BufferedReader(new FileReader(file)); |
| 117 | String buffer; | 154 | String buffer; |
| @@ -119,6 +156,8 @@ public class VersionProcessor extends AbstractProcessor { | @@ -119,6 +156,8 @@ public class VersionProcessor extends AbstractProcessor { | ||
| 119 | set.add(buffer); | 156 | set.add(buffer); |
| 120 | } | 157 | } |
| 121 | br.close(); | 158 | br.close(); |
| 159 | + } else { | ||
| 160 | + file.createNewFile(); | ||
| 122 | } | 161 | } |
| 123 | BufferedWriter bw = new BufferedWriter(new FileWriter(file)); | 162 | BufferedWriter bw = new BufferedWriter(new FileWriter(file)); |
| 124 | StringBuilder builder = new StringBuilder(); | 163 | StringBuilder builder = new StringBuilder(); |
| @@ -133,6 +172,11 @@ public class VersionProcessor extends AbstractProcessor { | @@ -133,6 +172,11 @@ public class VersionProcessor extends AbstractProcessor { | ||
| 133 | e.printStackTrace(); | 172 | e.printStackTrace(); |
| 134 | } | 173 | } |
| 135 | } | 174 | } |
| 175 | + try { | ||
| 176 | + writer.close(); | ||
| 177 | + } catch (IOException e) { | ||
| 178 | + e.printStackTrace(); | ||
| 179 | + } | ||
| 136 | return true; | 180 | return true; |
| 137 | } | 181 | } |
| 138 | } | 182 | } |
| 139 | \ No newline at end of file | 183 | \ No newline at end of file |