Is it possible to edit a file while it is in-flight as part of a copy?

This reminded me of a problem I had to solve some time ago: https://discuss.gradle.org/t/generating-multiple-source-files-from-a-template/14800.

In your case it might be better to perform the base64 first as a separate task (or a copy step). You cannot use a line filter, so you might want to try

eachFile { fcd ->
  String content = yourBase64Method(fcd.file.text)  
  fcd.file.text = content
}

Obviously that is a bit naive - if you have large files you might run out of memory.

Then you just need to use second copy which combines an Ant ConcatFilter & ReplaceTokens filter

eachFile { fcd ->
  // I've not tested this line - you'll need to do some reading on 
  // FileCopyDetails & RelativePath classes from the Gradle API.
  String relativePathName = fcd.relativeSourcePath.pathString

  // Add your header
  fcd.filter ConcatFilter prepend : '/path/to/your/header/template'

 // Now have the tokens in the header replaced. 
  fcd.filter ReplaceTokens, tokens : [  PATH : "${relativePathName}" ]
}
1 Like