Philipp Küng

Guy behind @bitfondue

Use Swiss keyboard shortcuts for line commenting in Sonic Pi

Every now and then after a day at work I enjoy toying around with Sonic Pi, an amazing application for making music with code.

There are a lot of great talks by Sam Aaron the creator of Sonic Pi on Youtube but I like this one quite a bit.

So while I was sitting there with my glass of Port working through the Sonic Pi Tutorial I had the urge to gain back my text editing fluency I’m used to from my day job. Luckily Sonic Pi supports a lot of keyboard shortcuts however as of now they’re optimised for a keyboard layout of the english language.

After a bit of tinkering and the help of Karabiner-Elements I managed to re-map my line-commenting shortcut of CMD+Shift+7 (I’m working on a MacBook with a Swiss German keyboard) to the shortcut defined by Sonic Pi M-/.

The configuration you’ll want is:

{
  "title": "Remap CH comment combo to EN comment combo",
  "rules": [
    {
      "description": "Remap CH comment combo to EN comment combo",
      "manipulators": [
        {
          "conditions": [
            {
              "bundle_identifiers": [
                "net\\.sonic\\-pi\\.app"
              ],
              "type": "frontmost_application_if"
            }
          ],
          "from": {
            "key_code": "7",
            "modifiers": {
              "mandatory": [
                "left_command",
                "left_shift"
              ]
            }
          },
          "to": {
            "key_code": "keypad_slash",
            "modifiers": [
              "left_command"
            ]
          },
          "type": "basic"
        }
      ]
    }
  ]
}

If you also got this need then following these steps should set you up.

  1. Install Karabiner-Elements
  2. In your terminal go to ~/.config/karabiner/assets/complex_modifications
  3. In there create a file remap_ch_comment_combo_to_en_comment_combo.json and fill it with the configuration above.
  4. Then start Karabiner Elements, go to the Complex Modifications tab Complex Modifications tab and there click the Enable button for the new configuration Enable button.

Deploy a Phoenix Application on Heroku

Since I heard of Phoenix, the Elixir framework on The Changelog Podcast, I’ve spent some time getting familiar with the framework. However when time came to deploy the application onto Heroku there were a couple of things that weren’t initially obvious to me, like how to run brunch to compile the assets or how to parse the DATABASE_URL environment variable.

Buildpacks

First we have to change the currently used buildpack to the multi buildpack which makes it possible to run the Node.js besides the Elixir buildpack.

heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git

Then add the file .buildpacks with the contents below which will pull in the buildpacks and run their compile script. The buildpack listed last will hereby be used to run the application.

# .buildpacks
https://github.com/heroku/heroku-buildpack-nodejs.git#34cffc9b6397bc1ce97a4b5e911fa771fc4e7907
https://github.com/HashNuke/heroku-buildpack-elixir.git#36f2ff22d0236589256d9044091b950b7cc565d2

Compile the assets

Now that we have multiple buildpacks we need to tell the Node.js one to run the postinstall hook after all the dependencies are installed. Just add the scripts part to your package.json and you’re all set for Heroku to run the brunch build command.

# package.json
{
  "dependencies": {
    ...
  },
  "engines": {
    "node": "~ 0.12.1"
  },
  "scripts": {
    "postinstall": "node_modules/.bin/brunch build"  
  }
}

Parse the environment variable

When deploying the application to Heroku, the configuration variables will be exposed to the application via environment variables. For the database that means there will be a single String from which the username, password, host, etc. will have to be extracted. You could theoretically do that by hand defining those variables individually, however what happens if your database provider has an issue and suddenly decides to change the value behind your environment variable? - in order to avoid that, just put the code below in your prod.secret.exs file to help you split the configuration variable for the database.

# config/prod.secret.exs
defmodule Heroku do
  def database_config(uri) do
    parsed_uri = URI.parse(uri)
    [username, password] = parsed_uri.userinfo
                           |> String.split(":")
    [_, database] = parsed_uri.path
                    |> String.split("/")

    [{:username, username},
     {:password, password},
     {:hostname, parsed_uri.host},
     {:database, database},
     {:port, parsed_uri.port},
     {:adapter, Ecto.Adapters.Postgres}]
  end
end

Then instead of defining the arguments for your app separately, call the database_config function and you’re all set.

# config/prod.secret.exs
config :yourapplication, Yourapplication.Repo,
  "DATABASE_URL"
  |> System.get_env
  |> Heroku.database_config

Run the database migrations

heroku run mix ecto.migrate

Since I’m pretty new to Elixir and even newer to the Phoenix Framework, this tutorial might lack some best practices, in which case please let me know so this post can be updated to reflect them.

Automatically deploy to Heroku from Github

While starting on a new project the other day I decided to automate as much as possible. One of those tasks was deploying to Heroku when pushing the master branch to Github. It took me some time to figure certain parts out, even though the documentation is quite extensive; guess it being a beta feature didn’t help.

  1. Start by going into the Settings > Webhooks & Services view and add the HerokuBeta Service.
  2. Create the app on Heroku via the command line heroku create myapp
  3. Fill in myapp as the Name in the HerokuBeta settings view and leave Github api url empty.
  4. Then for the Heroku token, you’ll need your Heroku api token, which you get via heroku auth:token (eg. token123) and your email address you’re using to login to Heroku (eg. hi@email.com). Then convert the two into a base64 hash for the Authorization header by issuing this command on a Unix system echo "hi@email.com:token123" | base64 (eg. base64-123).

     curl -X POST https://api.heroku.com/oauth/authorizations \
     -H "Accept: application/vnd.heroku+json; version=3" \
     -H "Authorization: Basic base64-123" \
     -H "Content-Type: application/json" \
     -d "{\"description\":\"direct token description (preferably meaningful)\"}"
    
  5. For the Github token follow the instructions and head to https://github.com/settings/applications where you can click Create new token, then give it a name and leave the default settings before hitting Generate token. Then copy this token and fill it into the form.
  6. Save the configuration with Add service.
  7. I first thought the hook is already working, however it’s only working with Github deployment events, which you can add by adding another service called Github Auto-Deployment.
  8. Repeat step 5 to acquire a new Github token and add it there, save and from now on your automatic Heroku deployment should be fully working.