Skip to content

Instantly share code, notes, and snippets.

View melix's full-sized avatar

Cédric Champeau melix

View GitHub Profile
@melix
melix / build.gradle
Created May 29, 2013 14:53
Apply local tweaks to a Gradle build if user.gradle is present
// here goes the conf
...
// append this
if (file('user.gradle').exists()) {
apply from: 'user.gradle' // if a user file is present, tweak the build (add tasks, ...). This file should not be in VCS.
}
@melix
melix / sonar.gradle
Created June 13, 2013 09:10
Workaround for Sonar working only with leaf projects
apply plugin: "sonar-runner"
sonarRunner {
sonarProperties {
property "sonar.sourceEncoding", "UTF-8"
// must choose one and only one language, Sonar doesn't know about polyglot projects!
property "sonar.language", "java"
}
}
@melix
melix / build.gradle
Created June 19, 2013 12:41
Apply javadoc fix tool to your Gradle build
// Copy JavadocFixTool.java into buildSrc/src/main/java
// this will apply the javadoc fix tool to all generated javadocs
// we use it to make sure that the javadocs are not vulnerable independently of the JDK used to build
allprojects {
tasks.withType(Javadoc).all {
doLast {
def javadocFix = new JavadocFixTool()
javadocFix.recursive = true
javadocFix.doPatch = true
class Top {
int overload() { 0 }
}
class Bottom {
int overload(String... args) { 1+args.length }
}
@CompileStatic
void boom() {
def b = new Bottom()
@melix
melix / convert.groovy
Created July 17, 2013 12:57
Convert Confluence HTML export into asciidoc
@Grab('net.sourceforge.htmlcleaner:htmlcleaner:2.4')
import org.htmlcleaner.*
def src = new File('html').toPath()
def dst = new File('asciidoc').toPath()
def cleaner = new HtmlCleaner()
def props = cleaner.properties
props.translateSpecialEntities = false
def serializer = new SimpleHtmlSerializer(props)
@melix
melix / imnewco.groovy
Created July 29, 2013 14:07
Immutable+Newify+CompileStatic
import groovy.transform.*
@Immutable class Coordinates {
double latitude, longitude
}
@Immutable class Path {
Coordinates[] coordinates
}
@melix
melix / fibbench.groovy
Created August 2, 2013 14:09
Benchmark for Fibonacci in Groovy
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1')
import groovy.transform.CompileStatic
import groovy.transform.Memoized
def untypedFib(n) {
n<2?n:untypedFib(n-1)+untypedFib(n-2)
}
int typedFib(int n) {
n<2?n:typedFib(n-1)+typedFib(n-2)
}
@melix
melix / GroovyActivity.groovy
Created October 24, 2013 20:25
Simple Groovy activity
package me.champeau.groovydroid
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.View
import android.widget.EditText
import com.android.dx.dex.DexFormat
import com.android.dx.dex.DexOptions
@melix
melix / StringGrep.groovy
Created November 5, 2013 18:15
Grep method for String
String.metaClass.grep = { pattern, cl ->
def matcher = delegate =~ pattern
if (matcher.find()) {
cl.doCall(*matcher.collect { it })
}
}
def address = 'Beverly Hills, 90210'.grep(/([\w\s]+),\s*(\d+)/) { match, city, zip ->
[city: city, zip: zip]
}
assert address == [city: 'Beverly Hills', zip: '90210']
@melix
melix / ast_aliasing.groovy
Created December 12, 2013 14:11
Using an existing annotation as to trigger AST transformations. This sample compiles a class which is annotated with @entity, and we want to transparently apply @tostring(includeNames=true) to it.
import org.codehaus.groovy.control.CompilerConfiguration
import groovy.transform.ToString
import static org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder.*
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
def cc = new CompilerConfiguration()
withConfig(cc) {
inline(phase: 'CONVERSION') { source, context, classNode ->
if (classNode.annotations.any { it.classNode.name == 'Entity' }) {
new ASTTransformationCustomizer(ToString, includeNames:true).call(source, context, classNode)