@@ -41,7 +41,7 @@ class RouterTransform extends Transform {
4141
4242 @Override
4343 boolean isIncremental () {
44- return false
44+ return true
4545 }
4646
4747 @Override
@@ -58,25 +58,87 @@ class RouterTransform extends Transform {
5858 throws TransformException , InterruptedException , IOException {
5959 long begin = System . currentTimeMillis()
6060 project. logger. info(" - router transform begin:" )
61-
61+ boolean isIncremental = transformInvocation. incremental
62+ if (! isIncremental) {
63+ transformInvocation. outputProvider. deleteAll()
64+ }
6265 Scanner . records = Scanner . getRecords(project. name)
6366
6467 transformInvocation. inputs. each { TransformInput input ->
6568 if (! input. jarInputs. empty) {
6669 project. logger. info(" -- jarInputs:" )
6770 input. jarInputs. each { JarInput jarInput ->
68- execute {
69- transformJar(transformInvocation, jarInput)
71+ File destFile = getJarDestFile(transformInvocation, jarInput)
72+ if (isIncremental) {
73+ Status status = jarInput. getStatus()
74+ switch (status) {
75+ case Status . NOTCHANGED :
76+ break
77+ case Status . ADDED :
78+ case Status . CHANGED :
79+ execute {
80+ transformJar(jarInput, destFile)
81+ }
82+ break
83+ case Status . REMOVED :
84+ execute {
85+ if (destFile. exists()) {
86+ FileUtils . forceDelete(destFile)
87+ }
88+ }
89+ break
90+ }
91+ } else {
92+ execute {
93+ transformJar(jarInput, destFile)
94+ }
7095 }
7196 }
7297 }
7398
7499 if (! input. directoryInputs. empty) {
75100 project. logger. info(" -- directoryInputs:" )
76101 input. directoryInputs. each { DirectoryInput directoryInput ->
77- execute {
78- transformDir(transformInvocation, directoryInput)
102+ File dest = transformInvocation. outputProvider. getContentLocation(
103+ directoryInput. name, directoryInput. contentTypes, directoryInput. scopes, Format . DIRECTORY )
104+ if (isIncremental) {
105+ String srcDirPath = directoryInput. getFile(). getAbsolutePath()
106+ String destDirPath = dest. getAbsolutePath()
107+ Map<File , Status > fileStatusMap = directoryInput. getChangedFiles()
108+ for (Map.Entry < File , Status > changedFile : fileStatusMap. entrySet()) {
109+ Status status = changedFile. getValue()
110+ File inputFile = changedFile. getKey()
111+ String destFilePath = inputFile. getAbsolutePath(). replace(srcDirPath, destDirPath)
112+ File destFile = new File (destFilePath)
113+ switch (status) {
114+ case Status . NOTCHANGED :
115+ break
116+ case Status . ADDED :
117+ case Status . CHANGED :
118+ execute {
119+ try {
120+ FileUtils . touch(destFile)
121+ } catch (IOException e) {
122+ Files . createParentDirs(destFile)
123+ }
124+ transformSingleFile(inputFile, destFile)
125+ }
126+ break
127+ case Status . REMOVED :
128+ execute {
129+ if (destFile. exists()) {
130+ FileUtils . deleteQuietly(destFile)
131+ }
132+ }
133+ break
134+ }
135+ }
136+ } else {
137+ execute {
138+ transformDir(directoryInput, dest)
139+ }
79140 }
141+
80142 }
81143 }
82144 }
@@ -94,9 +156,7 @@ class RouterTransform extends Transform {
94156 project. logger. info(" cost time: ${ (System.currentTimeMillis() - begin) / 1000.0f} s" )
95157 }
96158
97- void transformJar (TransformInvocation transformInvocation , JarInput jarInput ) {
98- // com.android.support:appcompat-v7:27.1.1 (/path/to/xxx.jar)
99- project. logger. info(" --- ${ jarInput.name} (${ jarInput.file.absolutePath} )" )
159+ File getJarDestFile (TransformInvocation transformInvocation , JarInput jarInput ) {
100160 String destName = jarInput. name
101161 if (destName. endsWith(" .jar" )) { // local jar
102162 // rename to avoid the same name, such as classes.jar
@@ -105,17 +165,30 @@ class RouterTransform extends Transform {
105165 }
106166 File destFile = transformInvocation. outputProvider. getContentLocation(
107167 destName, jarInput. contentTypes, jarInput. scopes, Format . JAR )
168+ return destFile
169+ }
170+
171+ void transformSingleFile (File inputFile , File destFile ) {
172+ if (inputFile. isFile() && Scanner . shouldScanClass(inputFile)) {
173+ project. logger. info(" --- ${ inputFile.absolutePath} " )
174+ Scanner . scanClass(inputFile)
175+ }
176+ FileUtils . copyFile(inputFile, destFile)
177+ }
178+
179+ void transformJar (JarInput jarInput , File destFile ) {
180+ // com.android.support:appcompat-v7:27.1.1 (/path/to/xxx.jar)
181+ project. logger. info(" --- ${ jarInput.name} (${ jarInput.file.absolutePath} )" )
108182 if (Scanner . shouldScanJar(jarInput)) {
109183 Scanner . scanJar(jarInput. file, destFile)
110184 }
111185
112186 FileUtils . copyFile(jarInput. file, destFile)
113187 }
114188
115- void transformDir (TransformInvocation transformInvocation , DirectoryInput directoryInput ) {
189+
190+ void transformDir (DirectoryInput directoryInput , File dest ) {
116191 project. logger. info(" -- directory: ${ directoryInput.name} (${ directoryInput.file.absolutePath} )" )
117- File dest = transformInvocation. outputProvider. getContentLocation(
118- directoryInput. name, directoryInput. contentTypes, directoryInput. scopes, Format . DIRECTORY )
119192 project. logger. info(" -- dest dir: ${ dest.absolutePath} " )
120193 directoryInput. file. eachFileRecurse { File file ->
121194 if (file. isFile() && Scanner . shouldScanClass(file)) {
0 commit comments