Skip to content

Instantly share code, notes, and snippets.

View mikehearn's full-sized avatar

Mike Hearn mikehearn

View GitHub Profile
@mikehearn
mikehearn / KotlinDuckTyping.kt
Created December 18, 2015 13:46
Kotlin duck typing / type classing fiddle
class A {
fun shout() = println("go team A!")
}
class B {
fun shout() = println("go team B!")
}
interface Shoutable {
fun shout()
@mikehearn
mikehearn / Scripts
Last active December 4, 2023 11:45
systemd unit monitoring setup
----------------------------------------------------
#!/usr/bin/env bash
# place in a file called email-unit-status.sh on the PATH
# requires you to have set /etc/admin-email to contain the address to receive the messages
# requires install of ansi2html
#
set -e
@mikehearn
mikehearn / plugin.xml
Created May 1, 2023 16:31
IntelliJ plugin xml for a kotlin scripting plugin
<idea-plugin>
<id>dev.hydraulic.hshell</id>
<name>Hydraulic Shell</name>
<vendor email="contact@hydraulic.software" url="https://hshell.hydraulic.dev">Hydraulic</vendor>
<description><![CDATA[
Hydraulic Shell is a replacement for shell scripting that uses Kotlin.
]]></description>
<depends>com.intellij.modules.platform</depends>
@mikehearn
mikehearn / gist:fdcb2c3ef30c6a2970d089fd8f860216
Created May 1, 2023 16:26
IntelliJ plugin to register a custom Kotlin scripting host
package hydraulic.hshell.intellij
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.diagnostic.logger
import java.io.File
import java.io.IOException
import kotlin.script.experimental.intellij.ScriptDefinitionsProvider
private val LOG = logger<HShellDefinitionProvider>()
@mikehearn
mikehearn / Locker.kt
Created April 6, 2023 08:25
A class that makes it easy to wrap state such that it's only accessible when locked.
package hydraulic.kotlin.utils
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
/**
* A wrapper class that makes it harder to forget to take a lock before accessing some shared state.
*
* Simply define an anonymous object to hold the data that must be grouped under the same lock, and then pass it
#!/usr/bin/env hshell
// -------------------- Dependencies and imports ------------------------------------------------
//
// You can depend on Maven coordinates, they will be downloaded and cached in ~/.m2 automatically.
@file:DependsOn("com.github.ricksbrown:cowsay:1.0.0")
import com.github.ajalt.mordant.table.table
import com.github.ricksbrown.cowsay.Cowsay
import hydraulic.utils.os.runningOnWindows
@mikehearn
mikehearn / conveyor.conf
Created December 4, 2022 13:19
Config for GH Desktop (using GH Actions to prepare the resources dir)
// A config for Conveyor (https://conveyor.hydraulic.dev/) that creates a download site for GitHub Desktop.
// Shows how to package a real-world and sophisticated Electron app.
include required("/stdlib/electron/electron.conf")
// Import package.json files so we can avoid duplicating config. There are two in this project.
package-json {
include required("app/package.json")
}
@mikehearn
mikehearn / Kryo Kotlin class serialiser.kt
Created December 17, 2015 10:53
A Kryo serialiser that lets you serialise immutable classes like "class Foo(val a: Int, val b: Bar)" without bypassing the c'tor.
interface SerializeableWithKryo
class ImmutableClassSerializer<T : SerializeableWithKryo>(val klass: KClass<T>) : Serializer<T>() {
val props = klass.memberProperties.sortedBy { it.name }
val propsByName = props.toMapBy { it.name }
val constructor = klass.primaryConstructor!!
init {
// Verify that this class is immutable (all properties are final)
assert(props.none { it is KMutableProperty<*> })
// A server config for Conveyor: https://conveyor.hydraulic.dev/
// Import config from a Java build system (but you can get it from anywhere).
include required("#!gradle -q printConveyorConfig")
app {
vendor = Hydraulic
fsname = sample-server
machines = [linux.amd64.glibc] // Don't want packages for other platforms.
@mikehearn
mikehearn / teamcity.service
Created June 10, 2022 15:45
An example systemd service for TeamCity
[Unit]
After=postgresql.service
Description=JetBrains TeamCity
Requires=postgresql.service
Wants=network-online.target
# Can be included to allow a server to write to its own files.
[Service]
CacheDirectory=jetbrains/teamcity
ConfigurationDirectory=jetbrains/teamcity