Skip to content

Instantly share code, notes, and snippets.

@shad1w
Last active April 23, 2021 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shad1w/c81e0470deed507e6630be52eccd59ff to your computer and use it in GitHub Desktop.
Save shad1w/c81e0470deed507e6630be52eccd59ff to your computer and use it in GitHub Desktop.
Gradle change line endings

Line endings changer for gradle

USE THIS AT YOUR OWN RISK! I AM NOT RESPONSIBLE FOR ANYTHING YOU DO BY USING THIS! IF ANY DAMAGE IS DONE BY FOLLOWING THE INSTRUCTIONS HERE OR BY USING MY CODE, IT IS ENTIRELY YOUR RESPONSIBILITY AND I MAY NOT BE HELD LIABLE FOR IT!

This is a simple gradle task for changing line endings in files.

How to use

Copy the source code of this task to your_project_dir/buildSrc/src/main/groovy/ChangeLineEndings.groovy

Usage example:

task fixLineEndings(type: ChangeLineEndings) {
	changeTo LF
	forFile "$buildDir/artifacts/setup.sh"
	forFile "$buildDir/artifacts/run.sh"
}

changeTo accepts one parameter, which has to be either CRLF, CR or LF.

Restrictions

For now it's not possible to use wildcards in file paths supplied to forFile.

License

Licensed under the MIT license

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class ChangeLineEndings extends DefaultTask {
static final String CRLF = "\r\n"
static final String LF = "\n"
static final String CR = "\r"
private filePaths = []
private String newEnding = LF
@TaskAction
def changeLineEndings() {
def changeToEnding = newEnding
filePaths.each { path ->
File file = new File(path)
String fileContents = file.text
file.write(fileContents.replaceAll(/\r\n|\n|\r/, changeToEnding))
}
}
def changeTo(String newEnding) {
this.newEnding = newEnding
}
def forFile(String path) {
this.filePaths << path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment