Skip to content

Instantly share code, notes, and snippets.

View EwanDawson's full-sized avatar

Ewan Dawson EwanDawson

  • Ping Identity
  • Edinburgh, UK
View GitHub Profile
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.your-company</groupId>
<artifactId>your-project</artifactId>
<version>0.1-SNAPSHOT</version>
<name>your-project</name>
void mapsAreEqual(Map a, Map b) {
if ( a == null || b == null ) { assert a == b; return }
assert a.keySet().sort()*.toString() == b.keySet().sort()*.toString()
a.each{ key, value -> assert "${b[key]}" == "$value" }
}
class Appointment implements Comparable {
LocalDateTime time
String description
String place
// Define a natural ordering of Appointments
// base on their times.
int compareTo(that) { this.date <=> that.date }
# add alias to get latest version of mongo cheatsheet
alias mongocheat='curl -s http://cheat.errtheblog.com/s/mongo | groovy -e "@Grab(group=/net.sourceforge.nekohtml/, module=/nekohtml/, version=/1.9.14/)import org.cyberneko.html.parsers.SAXParser;println new XmlSlurper(new SAXParser()).parseText(System.in.text).depthFirst().find{ it.@class == /sheet/}.text()"'
@EwanDawson
EwanDawson / gist:1001984
Created June 1, 2011 08:30
Groovy closure delegation test
import spock.lang.Specification
class ClassInstanceAccessFromWithinClosureSpec extends Specification {
def component = new ClassUnderTest()
def "class instance variable is accessible when called from within closure delegated to Object" (){
setup: def delegate = new Object()
expect: component.execute(delegate)
}
@EwanDawson
EwanDawson / BuildConfig.groovy
Created June 5, 2011 16:16
Grails BuildConfig for using logback instead of log4j
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
//grails.project.war.file = "target/${appName}-${appVersion}.war"
grails.project.dependency.resolution = {
inherits("global") {
excludes "slf4j-log4j12"
}
log "warn" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
repositories {
@EwanDawson
EwanDawson / logback.xml
Created June 5, 2011 17:41
Logback configuration for versions < 0.9.19
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</layout>
</appender>
<root level="warn">
<appender-ref ref="STDOUT" />
@EwanDawson
EwanDawson / gist:1629928
Created January 18, 2012 00:09
Debian: find all the files of a particular package that have been modified since the package was installed
# Actually, lists the installed files in the order they were updated, most recent first.
# All the unmodified files will have the same last modified date, so it should be pretty
# easy to spot where the updated files begin.
PACKAGE=jetty # Replace with your package
ls -lt $(ls -Fd1 $(dpkg -L $PACKAGE) | grep -v "[\/\@]$")
@EwanDawson
EwanDawson / regex-capture.groovy
Created April 17, 2012 16:09
Idiomatic regex group capturing in Groovy
// Using Matcher object returned by =~ operator
matcher = "Hello world v1.01" =~ /.* v(\S*)/
if (matcher.matches()) version = matcher[0][1]
assert version == "1.01"
// We can make this a little tidier using the 'with' method
version = ("Hello world v1.01" =~ /.* v(\S*)/).with { matches() ? it[0][1] : null }
assert version == "1.01"
@GrabResolver(name='javahg', root='https://oss.sonatype.org/content/repositories/snapshots/')
@Grab(group='com.aragost.javahg', module='javahg', version='0.3-SNAPSHOT')
import com.aragost.javahg.*
import com.aragost.javahg.commands.*
config = new RepositoryConfiguration()
config.hgrcPath = null // forces Mercurial to look in the usual places for .hgrc
rep = Repository.open(config, new File('.')) // assume the current directory is a repo
// Print the status of the repo