@@ -43,6 +43,7 @@ import org.gradle.api.file.RegularFileProperty
4343import org.gradle.api.provider.Property
4444import org.gradle.api.tasks.*
4545import org.gradle.api.tasks.options.Option
46+ import org.intellij.lang.annotations.Language
4647import org.openrewrite.InMemoryExecutionContext
4748import writeLF
4849
@@ -99,7 +100,7 @@ abstract class RebuildFilePatches : BaseTask() {
99100 git(" checkout" , " file" ).executeSilently(silenceErr = true )
100101
101102 // handle AT
102- var addedNewAts = false
103+ val filesWithNewAts = mutableListOf< String >()
103104 baseDir.walk()
104105 .map { it.relativeTo(baseDir).toString().replace(" \\ " , " /" ) }
105106 .filter {
@@ -110,8 +111,10 @@ abstract class RebuildFilePatches : BaseTask() {
110111 val source = inputDir.resolve(it)
111112 val decomp = baseDir.resolve(it)
112113 val className = it.replace(" .java" , " " )
113- addedNewAts = handleATInSource(source, ats, className) || addedNewAts
114- handleATInBase(decomp, ats, baseDir)
114+ if (handleATInSource(source, ats, className)) {
115+ handleATInBase(decomp, ats, baseDir)
116+ filesWithNewAts.add(it)
117+ }
115118 oldAts.merge(ats)
116119 }
117120
@@ -123,7 +126,8 @@ abstract class RebuildFilePatches : BaseTask() {
123126 )
124127 }
125128
126- if (addedNewAts) {
129+ if (filesWithNewAts.isNotEmpty()) {
130+ // we removed the comment, we need to commit this
127131 git(" add" , " ." ).executeSilently(silenceErr = true )
128132 git(" commit" , " --amend" , " --no-edit" ).executeSilently(silenceErr = true )
129133 }
@@ -148,9 +152,13 @@ abstract class RebuildFilePatches : BaseTask() {
148152 .operate()
149153
150154 git(" switch" , " -" ).executeSilently(silenceErr = true )
151- if (addedNewAts ) {
155+ if (filesWithNewAts.isNotEmpty() ) {
152156 try {
153- git(" rebase" , " file" ).executeSilently()
157+ // todo detect if sed is not present (windows) and switch out sed for something else
158+ @Language(" Shell Script" )
159+ val sequenceEditor = " sed -i -e 0,/pick/{s/pick/drop/}"
160+ val execs = filesWithNewAts.map { it -> " sed -i -e 's|// Paper-AT:.*||g' $it && ((git add $it && git commit --amend --no-edit) || true)" }.flatMap { listOf (" --exec" , it) }.toTypedArray()
161+ git.withEnv(mapOf (" GIT_SEQUENCE_EDITOR" to sequenceEditor))(" rebase" , " -i" , " file" , " --strategy-option=theirs" , * execs).executeSilently()
154162 } catch (e: Exception ) {
155163 // TODO better message to inform the user on what to do
156164 throw RuntimeException (" Encountered conflicts while rebuilding file patches." , e)
@@ -193,8 +201,10 @@ abstract class RebuildFilePatches : BaseTask() {
193201 private fun handleATInSource (source : Path , newAts : AccessTransformSet , className : String ): Boolean {
194202 val sourceLines = source.readLines()
195203 var foundNew = false
204+ val fixedLines = ArrayList <String >(sourceLines.size)
196205 sourceLines.forEach { line ->
197206 if (! line.contains(" // Paper-AT: " )) {
207+ fixedLines.add(line)
198208 return @forEach
199209 }
200210
@@ -213,6 +223,12 @@ abstract class RebuildFilePatches : BaseTask() {
213223 atClass.mergeMethod(MethodSignature .of(name.substring(0 , index), name.substring(index)), accessTransform)
214224 }
215225 logger.lifecycle(" Found new AT in $className : $at -> $accessTransform " )
226+
227+ fixedLines.add(split[0 ])
228+ }
229+
230+ if (foundNew) {
231+ source.writeText(fixedLines.joinToString(" \n " , postfix = " \n " ), Charsets .UTF_8 )
216232 }
217233
218234 return foundNew
0 commit comments