Perform a command line operation on a file being copied

I’m not sure how to do this

I’m copying a set of files from Windows to a Linux mounted share. In the process, I’d like to perform a dos2unix on those files.

My code is like this :

def dos2unixExecutable = path/to/dos2unix.exe
 copy {
  from sourceSets.main.allSource
  into file("$destFolder")
  eachFile { fileCopyDetails ->
    //apply dos2unix if from windows
            commandLine dos2unixExecutable,"$destFolder/${fileCopyDetails.relativePath}"

This doesn’t work because the fileCopyDetails.relativePath is the path of the destionation file, but it doesn"t exist yet. I don’t want to perform the exec task on the source file because I don’t want to modify the source file.

Of course I could perform the copy of all files, and then perform dos2unix once the copy is over. But I really want to perform the dos2unix “on the fly”, to avoid looping two times over the files.

If you are going to use the native dos2unix method I would probably suggest splitting this into two separate tasks. Yes, like you said, you would have to iterate over the files twice, but that is very trivial, and I would assume there to be negligible, if any performance overhead.

You might also want to consider a pure Java implementation as well, for portability reasons. Perhaps implementing this as a ‘FilterReader’ or using the Ant FixCRLF task.

I like the ant task way. I never think about using regular ant from my gradle builds…

Thanks for your help