Skip to content

Instantly share code, notes, and snippets.

View mortenson's full-sized avatar
💧
Did you try to clear cache?

Samuel Mortenson mortenson

💧
Did you try to clear cache?
View GitHub Profile
@mortenson
mortenson / test-migrations.sh
Last active January 4, 2024 00:21
Example script that tests if migrations added in current working tree will break tests when applied to master. Good for PRs but can be used locally "safely"
#! /bin/bash
# Assumes migrations are stored in ./db/migrations as single files.
# Also assumes migrations are in (alphanumeric) order.
set -e
rm -rf /tmp/test-migrations
mkdir /tmp/test-migrations
cp -r db/migrations/* /tmp/test-migrations/
@mortenson
mortenson / wait-for-render.sh
Created January 2, 2024 00:33
Bash script to wait for the given Render services to finish deploying. Useful for triggering deploy hooks sequentially, or tracking deploy status in GitHub actions.
#! /bin/bash
# RENDER_SERVICE_NAMES should be a newline-separated list of service IDs (ex: srv-...)
# RENDER_API_TOKEN is your personal Render API token
max_attempts=200
while IFS= read -r RENDER_SERVICE_NAME; do
echo "Checking $RENDER_SERVICE_NAME ..."
attempt_counter=0
@mortenson
mortenson / create_migration.go
Last active June 19, 2023 13:31
Create sqlc migrations automatically using pg-schema-diff
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
@mortenson
mortenson / block.js
Last active December 18, 2023 19:15
Very simple and stupid chrome extension to block arbitrary sites
document.body.innerHTML = 'get back to work'
@mortenson
mortenson / autosave.js
Created January 14, 2023 18:45
Minimal form autosave functionality using vanilla JS
document.querySelectorAll("form").forEach((formEl) => {
const key = location.pathname + formEl.id;
const json = localStorage.getItem(key);
if (json) {
const data = JSON.parse(json);
Object.keys(data).forEach((inputId) => {
formEl.elements[inputId].value = data[inputId];
});
} else {
localStorage.setItem(
@mortenson
mortenson / secure_vendor_unserialize.php
Last active June 29, 2022 17:20
An idea about how to secure unserialize() calls in PHP dependencies that aren't using allowed_classes.
<?php
namespace Some\Namespace\I\Dont\Trust;
function unserialize($data, array $options = []) {
if (empty($options)) {
$options = [
'allowed_classes' => [
\DateTime::class, // Set to classes used in namespace.
],
@mortenson
mortenson / drupal_autowire.php
Last active May 7, 2022 17:55
Stupid auto wiring for any function/method in Drupal
<?php
// You shouldn't call this at runtime - do it when something builds (ex: plugins) and store the args somewhere.
function get_autowire_args(callable $callback, \Drupal\Component\DependencyInjection\Container $container) {
$cache = &drupal_static(__FUNCTION__);
$args = [];
$reflection = new \ReflectionFunction($callback);
foreach ($reflection->getParameters() as $i => $param) {
$type_obj = $param->getType();
if (!$type_obj || ($type_obj instanceof \ReflectionNamedType && $type_obj->isBuiltin())) {
@mortenson
mortenson / sams_portland_stuff.md
Last active May 3, 2024 20:33
Sam's fun things to do in Portland

Portland recommendations by Sam

Sorted by location-ish. I tried to reduce the list to places I think are good for visitors vs. my absolute favorites, but I put a section at the bottom for places that may not be for everyone.

Breakfast / Brunch

Toki Restaurant (Downtown)

580 SW 12th Ave

My order: Grilled mackerel breakfast with a bloody mary. Their Dalgona coffee is quite good too.

@mortenson
mortenson / assets.sh
Last active September 28, 2020 11:37
Watch mode for a game I'm working on
#! /bin/bash
function processSprite {
file=$1
echo "Creating sprite sheet for $file..."
filename=$(basename $file .aseprite)
aseprite -b "$file" --sheet "assets/$filename-Sheet.png" > /dev/null
}
function processTilemap {
@mortenson
mortenson / ddrush.sh
Last active March 20, 2022 09:51
What I currently use for local Drupal/Tome dev
#! /bin/bash
# This runs a new Docker container, mounts the current directory (Drupal root),
# and runs an arbitrary drush command, in case you don't want to run it in dsession.
sudo docker run --rm -it --init -v "$(pwd)":/var/www/tome mortenson/tome drush "$@"