diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 241901f..9b37ab2 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -10,6 +10,7 @@ diff --git a/.idea/modules.xml b/.idea/modules.xml index 45d1910..889474f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -7,6 +7,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index a49cac9..2dda60c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,6 +8,7 @@ android { keyPassword '123456' storeFile file('../app/parent.jks') storePassword '123456' + } } compileSdkVersion 26 @@ -18,6 +19,11 @@ android { versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + javaCompileOptions { + annotationProcessorOptions { + includeCompileClasspath = true + } + } } buildTypes { release { @@ -31,7 +37,16 @@ android { } productFlavors { } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_7 + targetCompatibility JavaVersion.VERSION_1_7 + } +} +task processorTask(type: Copy) { + from '../processor/build/libs/processor.jar' into 'libs/' } +processorTask.dependsOn(':processor:build') +preBuild.dependsOn(processorTask) greendao { schemaVersion 1//数据库版本升级 } @@ -46,6 +61,10 @@ dependencies { implementation project(':mvpsdk') implementation project(':roundedimageview-2.2.1') implementation files('libs/AMap_Location_V3.8.0_20180201.jar') - implementation 'org.greenrobot:greendao:3.2.2' // add library - compile "me.leolin:ShortcutBadger:1.1.19@aar" + implementation 'org.greenrobot:greendao:3.2.2' + // add library + compile 'me.leolin:ShortcutBadger:1.1.19@aar' + annotationProcessor 'com.google.dagger:dagger-compiler:2.12' + //implementation project(':processor') + compile files('libs/processor.jar') } diff --git a/app/libs/processor.jar b/app/libs/processor.jar new file mode 100644 index 0000000..3ae4550 Binary files /dev/null and b/app/libs/processor.jar differ diff --git a/app/src/main/assets/migrations/2.sql b/app/src/main/assets/migrations/2.sql new file mode 100644 index 0000000..d0d505f --- /dev/null +++ b/app/src/main/assets/migrations/2.sql @@ -0,0 +1 @@ +ALTER table test INSERT ADD COLUMN age INTEGER diff --git a/app/src/main/assets/migrations/3.sql b/app/src/main/assets/migrations/3.sql new file mode 100644 index 0000000..468cdbf --- /dev/null +++ b/app/src/main/assets/migrations/3.sql @@ -0,0 +1 @@ +ALTER table test INSERT ADD COLUMN name TEXT diff --git a/app/src/main/java/com/shunzhi/parent/annotation/AddColumn.java b/app/src/main/java/com/shunzhi/parent/annotation/AddColumn.java deleted file mode 100644 index 7c1de6b..0000000 --- a/app/src/main/java/com/shunzhi/parent/annotation/AddColumn.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.shunzhi.parent.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by 10501 on 2018/3/28.
- * 升级数据库版本使用,在新加字段时添加此注解
- * 只写了int类型和String类型的,其他需要的话自己写吧
- * 如果我没写完,谁想用谁写吧 - * - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -public @interface AddColumn { - int version(); -} diff --git a/app/src/main/java/com/shunzhi/parent/annotation/VersionHelper.java b/app/src/main/java/com/shunzhi/parent/annotation/VersionHelper.java deleted file mode 100644 index 187b0cf..0000000 --- a/app/src/main/java/com/shunzhi/parent/annotation/VersionHelper.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shunzhi.parent.annotation; - -import android.text.TextUtils; -import android.util.SparseArray; - -import org.greenrobot.greendao.annotation.Entity; - -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.List; - -import timber.log.Timber; - -/** - * Created by 10501 on 2018/3/28. - */ - -public class VersionHelper { - private static final String sql_insert = "ALTER table %s INSERT ADD COLUMN %s %s"; - private static final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'"; - - public static SparseArray> init(Class clazz,String dbName) { - SparseArray> array = new SparseArray<>(); - Field[] fields = clazz.getDeclaredFields(); - for (Field field : fields) { - AddColumn column = field.getAnnotation(AddColumn.class); - if (column != null) { - Timber.i("---==== :fieldName= %s version :%s fieldClass : %s", field.getName() - , column.version(), field.getType()); - Integer version = column.version(); - List sqlList = array.get(version); - if (sqlList == null) { - sqlList = new ArrayList<>(); - array.put(version, sqlList); - } - if (field.getType().getSimpleName().equalsIgnoreCase("string")) { - String sql = String.format(sql_insert, dbName, field.getName(), "TEXT"); - sqlList.add(sql); - } else if ("integer".equalsIgnoreCase(field.getType().getSimpleName())) { - String sql = String.format(sql_insert, dbName, field.getName(), "INTEGER"); - sqlList.add(sql); - } else { - //其他类型使用到的时候再写 - Timber.i("类型检测失败"); - } - } - } - return array; - } -} diff --git a/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java b/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java index f2eb7e9..4f7f8a6 100644 --- a/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java +++ b/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java @@ -1,14 +1,15 @@ package com.shunzhi.parent.annotation; +import org.shunzhi.processorlibs.AddColumn; + /** * Created by 10501 on 2018/3/28. */ - public class VersionTest { - @AddColumn(version = 1) + @AddColumn(version = 3,tableName = "test",type = "String",isDebug = false) private String name; - @AddColumn(version = 2) + @AddColumn(version = 3,tableName = "test",type = "Integer",isDebug = false) Integer age; String area; diff --git a/processor/.gitignore b/processor/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/processor/.gitignore @@ -0,0 +1 @@ +/build diff --git a/processor/build.gradle b/processor/build.gradle new file mode 100644 index 0000000..c2ba5ae --- /dev/null +++ b/processor/build.gradle @@ -0,0 +1,9 @@ +apply plugin: 'java-library' + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'org.greenrobot:greendao:3.2.2' +} + +sourceCompatibility = "1.7" +targetCompatibility = "1.7" diff --git a/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java b/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java new file mode 100644 index 0000000..fa11f55 --- /dev/null +++ b/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java @@ -0,0 +1,19 @@ +package org.shunzhi.processorlibs; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Retention(RetentionPolicy.SOURCE) +@Target({ElementType.FIELD}) +public @interface AddColumn { + int version(); + + String tableName(); + + String type(); + + boolean isDebug() default true; +} diff --git a/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java b/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java new file mode 100644 index 0000000..cfba19d --- /dev/null +++ b/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java @@ -0,0 +1,96 @@ +package org.shunzhi.processorlibs; + +import org.greenrobot.greendao.annotation.Entity; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; + +@SupportedAnnotationTypes({"org.shunzhi.processorlibs.AddColumn"}) +@SupportedSourceVersion(SourceVersion.RELEASE_7) +public class VersionProcessor extends AbstractProcessor { + private final String sql_insert = "ALTER table %s INSERT ADD COLUMN %s %s"; + private final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'"; + + String path = "app\\src\\main\\assets\\migrations".replace("\\", File.separator); + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { +// File file = new File("test"); +// if (!file.exists())file.mkdir(); + int i = 0; + BufferedWriter testBw = null; + + HashMap> map = new HashMap<>(); + + try { + for (Element element : roundEnv.getElementsAnnotatedWith(AddColumn.class)) { + File file = new File("sql.txt"); + if (!file.exists()) file.createNewFile(); + testBw = new BufferedWriter(new OutputStreamWriter(new PrintStream(file))); + AddColumn addColumn = element.getAnnotation(AddColumn.class); + int version = addColumn.version(); + String tableName = addColumn.tableName(); + String type = addColumn.type(); + List list = map.get(version); + boolean isDebug = addColumn.isDebug(); + if (list == null) list = new ArrayList<>(); + String sql = "null"; + if (type.equalsIgnoreCase("String")) { + sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "TEXT"); + if (!isDebug) list.add(sql); + } else if (type.equalsIgnoreCase("Integer")) { + sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "INTEGER"); + if (!isDebug) list.add(sql); + } + testBw.write(version + " "); + testBw.write(sql); + testBw.newLine(); + testBw.flush(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (testBw != null) testBw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + for (Integer version : map.keySet()) { + try { + File tmp = new File("tmp"); + String path = "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator) + version + ".sql"; + File file = new File(tmp.getParentFile(), path); + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + for (String str : map.get(version)) { + bw.write(str); + bw.newLine(); + bw.flush(); + } + bw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return true; + } +} diff --git a/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor new file mode 100644 index 0000000..9839235 --- /dev/null +++ b/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -0,0 +1 @@ +org.shunzhi.processorlibs.VersionProcessor \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 4bd36fb..74f0e2f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':mvpsdk', ':roundedimageview-2.2.1' +include ':app', ':mvpsdk', ':roundedimageview-2.2.1', ':processor' diff --git a/sql.txt b/sql.txt new file mode 100644 index 0000000..89e0026 --- /dev/null +++ b/sql.txt @@ -0,0 +1 @@ +3 ALTER table test INSERT ADD COLUMN age INTEGER -- libgit2 0.21.0