Skip to content

Instantly share code, notes, and snippets.

@kungfoo
Last active August 29, 2015 14:05
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 kungfoo/dac9a5433678f4bfb792 to your computer and use it in GitHub Desktop.
Save kungfoo/dac9a5433678f4bfb792 to your computer and use it in GitHub Desktop.
Creating and updating the eclipse line break and encoding in Gradle build
class SpecialEclipsePlugin extends SpecialPlugin {
@Override
public void apply(Project project) {
eggExtension(project).create("eclipse", EclipseExtension)
project.apply(plugin: 'eclipse')
project.afterEvaluate {
configureEclipseClasspath(it)
configureLineBreak(it)
configureFileEncoding(it)
}
}
// some methods left out for brevity's sake.
private void configureLineBreak(Project project) {
project.eclipse.project.file.whenMerged {
eclipse(project).configureLineBreak
}
}
protected boolean matchProperty(String line, String key) {
return line.matches('^(\\s*)' + key + '(\\s*)=.*$')
}
protected void createOrUpdate(File file, String property, value) {
if (file.exists()) {
// Update file
File tmpFile = File.createTempFile('settings','.tmp')
file.eachLine {String line ->
if (matchProperty(line, property)) {
tmpFile.append("${property}=${value}\n")
} else {
tmpFile.append("${line}\n")
}
}
tmpFile.renameTo(file)
} else {
// Create new file
file.write("${ECLIPSE_SETTINGS_VERSION}\n")
file.append("${property}=${value}\n")
}
}
}
class SpecialPlugin implements Plugin<Project> {
// yadda, yadda
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment