Run Android Emulator from WebStorm

When developing React Native apps, Android Emulator is essential to testing the Android part, but it is tedious to start Android Studio each time just to run the Emulator.

Fortunately, Android Emulator has a command line tool which can be configured to start the emulator with specific AVD right from your favorite IDE.

In my case this is WebStorm, but it took some tinkering to get the configuration right, because WebStorm Bash runner expects a script name to run, but the emulator tool expects argument -avd AVD_Name as option and WebStorm put that in quotes so it did not work right away.

The working configuration is to set it up as seen in the screenshot:

img-alternative-text

  • the AVD name as Script
  • full path to emulator as the Interpreter path
  • -avd as Interpreter Arguments

Now it runs the command

emulator -avd "AVD_name"

which successfully starts the correct image.

Titles and Descriptions in Photos.app

featured-content-sync2-icon_2x.jpg

Adding Captions and Comments to photos in macOS’ Photos.app is easy after you have shared it via iCloud photo sharing. But how to add titles or descriptions to photos you don’t plan on sharing right away, such as just cataloguing old pictures for easier searching?

 

Get Info

The Photos UI is rater limited and no obvious place to add titles is visible. But select one or more photos, hit ⌘+I (or pick Info from the Window menu) and this opens a dialog with information about selected items, among other things, Title, Description, Keywords and Location. This is especially nice if you pick multiple items, you can assign title or description to all of them in group.

photos_info.png

In the above examples I have picked two photos and assigned title, longer description and location to an old photo from 2001. All these fields are now searchable. I have used the description field to list people in the pictures, etc.

 

RubyMine high CPU, no background tasks

RubyMine, being an IDE and not light editor, is expected to hog some CPU. But after each RSpec run, the CPU usage jumps to 300 % for several minutes. If I do fast iteration on specs, then this high CPU usage is constant and heats up my Mac, causing fan noise and of course drains battery.

Screen Shot 2017-08-23 at 17.13.19.png

Googling around for solution did not find anything, especially as there are not background processes running. I even tried profiling the app to see if it gives any  ideas (hint: it did not).

One support ticket for WebStorm though tipped me off and turned out that the culprit are Inspections – those little yellow or red markers around right gutter that tell you when your code smells or is outright broken.

 

Handpick the Inspections

Screen Shot 2017-08-23 at 17.11.58.png

Navigate to Preferences > Editor > Inspections and go through the long list. Unfortunately RubyMine does not tell you which Inspection it currently runs, so it is trial and error to find out what causes the slowness.

In my case there were a lot of “Double quotes” as well as whitespace whitespace warnings, but growing tired of not finding any low hanging fruits, I decided to just disable almost all warnings and left only error-level Inspections.

Now, after running specs, RubyMine CPU usage jumps to around 100 % for only a little while and then returns to normal.

 

A note on tabs

RubyMine has default setting of allowing only a handful of tabs open at the same time. Increasing this limit affects, among other things, the Inspections performance too as these are run for all tabs after every spec run. (Don’t ask me why JetBrains thinks this is necessary.)

Resolving xip.io DNS with OpenWRT

I use MAMP for local PHP development server and it has a very nice option to automatically set up ServerAlias using xip.io so that the virtual host will become accessible from LAN. This is especially nice to allow communication between tablet and my virtual host.

Screen Shot 2017-07-13 at 11.25.37.png

This worked well in the office, but my home OpenWRT router seemed to block such usage. Turns out that it has a security feature to filter out DNS results that point to private networks, otherwise known as RFC1918 responses.

Allow RFC1918 Private Network DNS responses

To allow OpenWRT to forward such responses, open the web interface, and navigate to DNS settings.

openwrt_dns_settings.png

In the General Settings tab, scroll down to find Rebind Protection and add xip.io as whitelisted exception.openwrt_rfc1918_exception.png

Now try to access your xip.io domain from your LAN and it should work as intended.

Google Translator Service

MacOS has very handy shortcut ⌃⌘D to look up translations in the built-in dictionary. Unfortunately this does not always help if the word in question is either not in English or the definition is equally puzzling.

Automator to the rescue

Combining sources like this question in apple.stackexchange and this blog post in blog.fosketts I created an Automator Service that takes selected text from any application and then opens Google Translate with Auto->ET translation of the selection. Also added a shortcut for it: ⌃⇧⌘D

The AppleScript

Open Automator and create a New > Service, then search for Run AppleScript and drop that into the workflow. Then paste in this AppleScript from below.

on run {input, parameters}
	set phrase to input as string
	set myBrowser to "Google Chrome"
	set toLang to "et"

	tell application myBrowser
		open location "https://translate.google.com/#auto/" & toLang & "/" & phrase
	end tell
end run

Change myBrowser to your preferred browser and also toLang to desired destination language and then save the service. I chose name Translate to Estonian for obvious reasons.

The global shortcut

img-alternative-text

Next open System Preferences > Keyboard > Shortcuts and scroll down to your new service. Then add a convinient shortcut. I chose ⌃⇧⌘D because that is similar to the official Look up shortcut.

Now go forth and translate! 🖖

MacOS Estonian Development keyboard layout

My previous post about creating custom Estonian layout using Ukelele has gone seriously outdated.

Nowadays customising layouts is a lot simpler, as is sharing your custom layouts.

My new customised EstonianDev layout has the following modifications compared to stock layout:

  • Option+ä will result in ^ (caret symbol)
  • Key next to Backspace is now correctly producing   acute accent (´) and grave accent/backtick (`)

 

Installing

  1. Download  the EstonianDev.dmg.zip file (sorry for the .zip – WP does not allow .dmg files to be uploaded directly)
  2. Unzip and mount the image
  3. Drag the EstonianDev.bundle to the Keyboard Layouts folder linked into the DMG
  4. Add the new layout from Input Sources
  5. Enjoy!

Atom New Window (via AppleScript)

Again, building on my previous scripts it was dead to make a script that creates new window for Atom editor.

This is best used with Spotlight or Alfred to quickly create new window in current space (even if another window was open in some other space it will not switch there).

Alfred activating Atom New Window script
Alfred activating Atom New Window script

 

Download: Atom New Window v1

 

Installation

  1. Unzip the archive and place it in Applications folder. Right-click the Atom New Window icon and select Open and then allow OS X to open this (as it is downloaded from internet).
  2. Newer OS X versions require you to enable Assistive Access for the app. Go to System Preferences -> Security -> Accessibility and then enable this app.
Go to System Preferences > Security > Accessibility to enable Atom New Window to control application menus.
Go to System Preferences > Security > Accessibility to enable Atom New Window to control application menus.

Of course, you are advised to explore the script yourself: right click the app icon and select Show Package Contents and then open the file Contents/Resources/Scripts/main.scpt with Apple Script Editor to view it’s contents.

PS. In related posts section you can find similar scripts for other apps.

Faster RubyMine RSpec with Vagrant

I use RubyMine and Vagrant for my Rails development and run RSpec specs from RubyMine for convenience. The problem is that every time I start rspec, it takes a few seconds to connect to the box, before actually doing anything. This is especially annoying when running single spec which should be fast.

vagrant ssh is slow to connect

% time vagrant ssh -c whoami
vagrant
Connection to 10.211.55.45 closed.
2,57s user 0,73s system 82% cpu 3,984 total

That’s probably because vagrant ssh-config takes most of that time to sort things out:

% time vagrant ssh-config > /dev/null
2,53s user 0,79s system 77% cpu 4,269 total

When researching this issue, some people mention DNS or other Vagrant provider specific issues, but they complain of an order of magnitude slower (30-40 sec) connection. Might just be that I have everything set up correctly and it just takes that much time to get the configuration.

But what this means is that if you run RSpecs from RubyMine, they are slow to start, because RubyMine executes vagrant ssh-config every time (RUBY-16186).

Direct SSH to the rescue

Fortunately, direct SSH with keypair authentication is blazing fast:

time ssh vagrant@10.211.55.45 -i ~/.ssh/parallels_key whoami
vagrant
0,01s user 0,01s system 21% cpu 0,114 total

So, to take advantage of that you need to:

  1. set up SSH configuration
  2. configure RubyMine remote ruby over SSH, not Vagrant
  3. Add RubyMine helpers to RUBYLIB to be able to start persistent spring instance.

Set up SSH configuration

Vagrant might be using their vagrant_insecure_private_key for the SSH or, in my case, Parallels custom keypair, as I don’t use VirtualBox. So run this command to find out which key is in use:

% vagrant ssh-config
Host default
  HostName 10.211.55.45
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/laas/project/.vagrant/machines/default/parallels/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Now we know the IP address of the box and what keyfile is used. For easier access, I symlinked the key file to my .ssh folder:

ln -s IdentityFile /Users/laas/project/.vagrant/machines/default/parallels/private_key ~/.ssh/parallels_key

You can try out SSH connection:

ssh vagrant@10.211.55.45 -i ~/.ssh/parallels_key whoami
vagrant

Configure RubyMine to use remote ruby over SSH

Next step is to configure new Ruby SDK for the project. Open Preferences and navigate Language & Frameworks > Ruby SDK and Gems, click + button and pick New remote…. Then fill in the data as shown in the screenshot and when closing dialog, ensure that the newly added ruby is selected as active.

Configure Remote Ruby Interpreter

Now specs should still run (but take several seconds every time to boot up Rails, because spring is shut down after every connection).

Note: I actually use landrush to manage development boxes names and so I can configure remote ruby with a domain name and not worry about IP address changes when recreating the devbox.

Add RubyMine helpers to RUBYLIB

The problem above is that spring closes itself down every time SSH connection is closed. One could start a long-running spring from terminal SSH, but that would result in RubyMine complaining that it can not load teamcity formatter:

cannot load such file -- teamcity/spec/runner/formatter/teamcity/formatter (LoadError)

This is because RubyMine injects a special formatter into rspec so that it can parse the output better. Thanks to Oleg at Jetbrains (RUBY-16324) I discovered that the required formatters are present in the vagrant box and all I need is to add them to ruby load path, before starting spring.

So, I added following lines to my /home/vagrant/.bashrc:

# RubyMine-RUBYLIB
if [[ -d "$HOME/.rubymine_helpers" ]]
then
  export RUBYLIB=$HOME/.rubymine_helpers/rb/testing/patch/common/:$HOME/.rubymine_helpers/rb/testing/patch/bdd/:$RUBYLIB
fi

This sets up RUBYLIB variable with RubyMine helpers. I skipped over testunit folder, as I do not use that, but feel free to add that also if you need it.

Let the specs fly

Now, just SSH into your Vagrant box, start Spring, e.g. by running:

bin/rspec --help

And then RubyMine can re-use the already running Spring server to speed itself up.

GIT merge-svn

How to use GIT to merge two SVN branches

TL;DR: Download the git-merge-svn script here

I’ve been using git for years now but had to start using SVN for some projects. I found that GIT is good enough Subversion client too, especially as I retain the ability to commit often and rebase my work on top of commits from other devs (on the SVN side).

The only question arose – can I merge two SVN branches so that GIT log will show the merge?

The git-svn manual states that one should avoid all git clone|merge|pull|push activity when using git-svn.

But git log does show merge history that was created in Subversion – how does it do that?

svn:mergeinfo

Subversion does not support actual merge of branches (more like cherry-picking), but since version 1.5 Subversion supports the svn:mergeinfo property that is used to track what has been merged into this folder previously.

Digging some more into the matter, I found out that GIT supports setting svn:mergeinfo property on the SVN branch when dcommit‘ing:

git svn dcommit --mergeinfo "/branches/somebranch:1-3"

NB! the svn:mergeinfo is overwritten with whatever is given on the command-line, so be careful to list previous merges too.

While more recent git version added the config parameter to automatically set this property:

config key: svn.pushmergeinfo

I had some troubles with the automatic mergeinfo – for one reason or the other GIT calculated it wrong and I couldn’t get it to work.

SOLUTION: git-merge-svn

To automate the process, I wrote a shell script git merge-svn which can be used to merge two SVN branches with correct svn:mergeinfo set on the dcommit.

The script handles both situations:

  • the branch is not merged in git – will do git merge beforehand
  • the branches have been already merged in git (but not in SVN) – will traverse until previous ancestor for the merged commit revisions.

UPDATE: Thanks theantway and haraldreingruber for patches – the script now:

  • always does full merge (no fast-forward) so that SVN can fully understand and
  • does not die on first merge (no previous mergeinfo)

Download the git-merge-svn script here

Example usage

With this script I was able to produce these merges solely on git-side and retain the merge info so that GIT graph shows the log nicely:

git-merge-svn result

  1. Make some commits on devel6
  2. dcommit devel6 to SVN (required to get SVN revision numbers for the commits)
  3. check out testtunk6 – yes, I know I made a typo in the name 😉
  4. git merge-svn devel6

The last commant outputs:

% git merge-svn devel6
About to do an SVN merge: devel6 -> testtunk6

* NEW MERGE COMMIT
|
| * devel6 [7b71187] (r102)
* | testtunk6 [0682a45] (r101)
 |
  * [273d6d6] (r100)


STEP 1: GIT merge
Executing:
  git merge --no-ff devel6

Continue? (y/n) [n]: y
Merge made by the 'recursive' strategy.
 testfile | 1 +
 1 file changed, 1 insertion(+)

STEP 2: SVN dcommit

executing:
git svn dcommit --mergeinfo
/idp/branches/devel:9-32,35-41 /idp/branches/devel6:89 /idp/branches/devel6:94 /idp/branches/devel6:93 /idp/branches/devel6:96 /idp/branches/devel6:97 /idp/branches/devel6:99 /idp/branches/devel6:100 /idp/branches/devel6:102

Continue? (y/n) [n]: y
Committing to https://my.svn.host/svn/auth/idp/branches/testtunk6 ...
  M testfile
Committed r103
  M testfile
Found merge parent (svn:mergeinfo prop): 7b71187fc371d3f86658c5850009e63be88157ac
r103 = 87759323cbadd38bac78087d57b6870a926287e7 (refs/remotes/svn/testtunk6)
No changes between 3fb2168cfbbe605fbd810d76513443203a85a549 and refs/remotes/svn/testtunk6
Resetting to the latest refs/remotes/svn/testtunk6

Safari New Window

Safari New Window icon

Download: Safari New Window

Previously I have written AppleScripts to open new windows for TextMate, Sublime Text 2 and also Safari. But the latter was somehow corrupt when using with Mac OS X 10.8.3, so I rewrote it based on previous two, adding features in the process:

  1. Checks if Safari is already running and creates new window (without switching spaces)
  2. Activates Safari if it’s not running yet.

The full source code is here:

-- quickly check if app is running.
-- Thanks https://discussions.apple.com/message/13184433#13184433
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

property appName : "Safari"

-- Open new windows or If app is not already running, start it
if appIsRunning(appName) then
    tell application appName
        make new document
        activate
    end tell
else
    tell application appName to activate
end if