Last active
August 29, 2015 14:05
-
-
Save kungfoo/dac9a5433678f4bfb792 to your computer and use it in GitHub Desktop.
Creating and updating the eclipse line break and encoding in Gradle build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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