Installing Node.js on Ubuntu 10.4 LTS

Installing Node.js on Ubuntu 10.4 LTS is pretty straight forward.

You will want a Node.js versioning manager. Node.js has a quick release cycle, point releases happen quite frequently. A Node.js versioning manager will help you keep all of your versions isolated from each other.

As it stands today, there are four Node.js version managers. They are:

  1. NVM – NVM works like RVM. It must be sourced in your ~./bashrc or ~./profile file. Some people don’t like this. It’s my understanding that some find this to be a bit of hackery.
  2. Nave – Nave doesn’t need to be sourced or loaded up into your bash profile. But, when you use Nave it executes commands into a subshell. It’s my understanding that if any process in a subshell modifies the environment then these changes won’t persist to the parent process. It’s not entirely clear these changes persist or not. But the rhetoric from some regarding using subshells for version management was enough to drive me away.
  3. n – I love the simplicity of ‘n’. It doesn’t use subshells and it doesn’t require that you modify your bash profile. I would use ‘n’ if it installed NPM (Node.js package manager) with each release, and it doesn’t.
  4. nodeenv - I never seriously considered this one as it requires Python to be installed. I haven’t read about anyone using this. But I wanted to list it so that you’d be informed about its existence.

Use NVM. Seriously, it just works.

On your clean Ubuntu machine, make sure that Git is installed:

sudo apt-get install git-core

Then install NVM:

git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh # <------ be sure to add this line to the end of your ~./profile or ~./bashrc file

Now install all of the packages need to build Node.js:

sudo apt-get install build-essential openssl libssl-dev pkg-config

Now install the latest version of Node.js, at the time of this writing it’s v0.6.9

nvm install v0.6.9

You now have a Node.js environment on your machine! Just run node on the command line to experiment with the Node.js REPL. You can also run npm to install Node.js packages. Read more about NPM here.

Do you use Git? If so, checkout Gitpilot to make using Git mindless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Comparing Two Javascript Objects

Recently, I was faced with a problem where I needed to compare two Javascript objects. My initial strategy was to convert them to JSON and compare the JSON strings.

Sort of like this:

var a = JSON.stringify(person1);//'{"firstName":"JP","lastName":"Richardson"}'
var b = JSON.stringify(person2);//'{"firstName":"JP","lastName":"Richardson"}'

assert(a === b);

Simple enough, right?

Not so fast. I encountered a case like this:

var a = JSON.stringify(person1);//'{"firstName":"JP","lastName":"Richardson"}'
var b = JSON.stringify(person2);//'{"lastName":"Richardson","firstName":"JP"}'

assert(a === b);

The data is the same, but the string is different. Fortunately, Stackoverflow had a nice Javascript object comparison algorithm to dump into my app.

Object.prototype.equals = function(x)
{
  var p;
  for(p in this) {
      if(typeof(x[p])=='undefined') {return false;}
  }

  for(p in this) {
      if (this[p]) {
          switch(typeof(this[p])) {
              case 'object':
                  if (!this[p].equals(x[p])) { return false; } break;
              case 'function':
                  if (typeof(x[p])=='undefined' ||
                      (p != 'equals' && this[p].toString() != x[p].toString()))
                      return false;
                  break;
              default:
                  if (this[p] != x[p]) { return false; }
          }
      } else {
          if (x[p])
              return false;
      }
  }

  for(p in x) {
      if(typeof(this[p])=='undefined') {return false;}
  }

  return true;
}

Test passed. I eventually hit a situation where I had some code with an Object that had a Person prototype and some data that came from JSON. Kinda like this:

var person1 = new Person('JP', 'Richardson');
var person2 = JSON.parse('{"firstName":"JP","lastName":"Richardson"}');

//deepEquals is code snippet above ^
person1.deepEquals(person2); // <--- THIS FAILS

I only cared about comparing the data. The methods associated with the object (Prototype) didn’t matter. Let’s modify the above algorithm. I use CoffeeScript. Here’s the modification:

Object::jsonEquals = (x) ->
  #we do this because two objects may have the same data fields and data but different prototypes
  x1 = JSON.parse(JSON.stringify(this))
  x2 = JSON.parse(JSON.stringify(x))

  p = null
  for p of x1
    return false if typeof (x2[p]) is 'undefined'
  for p of x1
    if x1[p]
      switch typeof (x1[p])
        when 'object'
          return false unless x1[p].jsonEquals(x2[p])
        when 'function'
          return false if typeof (x2[p]) is 'undefined' or (p isnt 'equals' and x1[p].toString() isnt x2[p].toString())
        else
          return false  unless x1[p] is x2[p]
    else
      return false if x2[p]
  for p of x2
    return false if typeof (x1[p]) is 'undefined'
  true

This causes the situation like I described above to pass. Essentially convert to JSON to remove the prototype. I suppose you could make this more efficient my just manually setting the prototype to Object before doing the comparison, but oh well this works for the time being.

Do you use Git? If so, checkout Gitpilot to make project management and collaborating on projects seamless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Node.js Exec Like Ruby Exec and Writing a Node.js Native Add On Module

Recently, I was faced with a problem that required my Node.js programs process to execute another process and have the procoess that’s passed to the exec function completely replace the Node.js process. In short, I wanted an ‘exec’ function like Ruby’s ‘exec’ function. Unfortunately, out of the box, Node.js doesn’t support this functionality. I asked on Stackoverflow.com, and someone had a response that I should use the POSIX exec functions to solve my problem and to consider writing a native Node.js extension.


npm install kexec

You can then use it like:

var kexec = require('kexec');
kexec('top'); //you can pass any process that you want here

Here is the C++ source for Node Kexec:


#include <v8.h>
#include <node.h>
#include <cstdio>

//#ifdef __POSIX__
#include <unistd.h>
/*#else
#include <process.h>
#endif*/

using namespace node;
using namespace v8;

static Handle<Value> kexec(const Arguments& args) {
    String::Utf8Value v8str(args[0]);
    char* argv2[] = {"", "-c", *v8str, NULL};

    execvp("/bin/sh", argv2);      
    return Undefined();
}

extern "C" {
    static void init (Handle<Object> target) {
        NODE_SET_METHOD(target, "kexec", kexec);
    }

    NODE_MODULE(kexec, init);
}

As you can see, writing a C++ add on in Node.js isn’t too difficult. You can use it in your Node.js Javascript like so:

var kexec;

try {
  kexec = require("./build/default/kexec.node"); //Node.js v0.4
} catch(e) {
  kexec = require("./build/Release/kexec.node"); //Node.js v0.6
}

module.exports = kexec.kexec; //function of kexec module is named kexec

Don’t forget your wscript file, which ironically is Python code:

def set_options(opt):
  opt.tool_options("compiler_cxx")

def configure(conf):
  conf.check_tool("compiler_cxx")
  conf.check_tool("node_addon")

def build(bld):
  obj = bld.new_task_gen("cxx", "shlib", "node_addon") 
  obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE","-Wall"]
  obj.target = "kexec"
  obj.source = "src/node_kexec.cpp"

In your package.json, include this bit:

"scripts": { "install": "node-waf configure build" }

Github Sourcecode: Node.js kernel exec

I’ve also included other resources for writing a Node.js Native Add On Module:

  1. Google V8 Engine Getting Started
  2. Google V8 Embedder’s Guide
  3. How to Roll Your Own Javascript API with V8
  4. How to Write Your Own Native Node.js Extension
  5. Writing Node.js Native Extensions
  6. Node.js Native Extension with Hammer and a Prayer
  7. Mastering Node; Add Ons
  8. Node.js Documentation: Add Ons
  9. Postgres Node.js Module
  10. V8 Sample: shell.cc
  11. V8 Objects
  12. There’s C in My JavaScript
  13. Converting V8 Arguments to C++ Types

Do you use Git? If so, checkout Gitpilot to make using Git easy.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Using OCMock with Mac OS X Lion, Xcode 4, to Mock and Unit Test Cocoa Desktop Apps

If you’re trying to learn how to use OCMock, you’ll encounter a number of articles dedicated to using it with iOS. You won’t find very many related to writing tests, mocks, and stubs for your Cocoa desktop applications for OS X Lion. If you’re writing your apps to exclusively target OS X Lion (10.7), then this article will be of use to you. I’m not sure if this technique will work for Snow Leopard apps or not. But, since OS X Lion 10.7 is a 64 bit OS the distributable library in the downloadable package (1.77) will not work. Hence, the reason for writing this article.

Here are the steps that you need to follow, you will create a brand new demo application that will demo using OCMock. You should be able to apply part of these instructions to your own project.

I should preface these instructions by stating that I am not an Xcode or Objective C expert. Also, I’m using Xcode version 4.1 despite version 4.2 being available. I just haven’t upgraded yet.

Building 64-bit OCMock Library

This is the first necessary task. As stated earlier, the libOCMock.a file found in the downloadable package is 32 bit only.

  1. Download the latest OCMock package. At the time of this writing, it’s version 1.77. It’s conceivable that later versions will include the 64 bit version and you’ll be able to skip these steps entirely.
  2. Mount the package and extract the Source and Release directories.
  3. Navigate to Source/ocmock-1.77 and open up OCMock.xcodeproj
  4. When Xcode opens up, click the root node “5 targets, multiple platforms” of the project navigator. The project’s build settings will show up. Observe that there are 5 targets: OCMock, OCMockTests, OCMockPhoneSim, OCMockPhoneDevice, OCMockLib
  5. Notice how there are 4 projects schemes: OCMockPhoneSim, OCMockPhoneDevice, OCMock, and OCMockLib
  6. Notice how there are four products: OCMock.framework, OCMockTests.octest, libOCMock.a, and libOCMock.a
  7. Recall, that we are most interested in a 64 bit libOCMock.a
  8. Delete the target OCMockPhoneSim. Select it. Right click and hit ‘Delete’ You should notice that one of the libOCMock.a products disappears, leaving us with one left.
  9. You should still be in the OCMockPhoneSim target with “My Mac 64-bit” Select the target OCMockPhoneDevice. Change the Base SDK to Mac OS X 10.7 on every dropdown that you can.
  10. Change Architecture to 64-bit on every drop down that you can.
  11. Remove i386 from Valid Architecture.
  12. Change scheme to “OCMockLib – My Mac 64-bit”
  13. Click ‘OCMockLib’ target. Click ‘Build Phases” and then expand the ‘Run Script’, remove the following text:
    # combine lib files for device and simulator platforms into one
    
    lipo -create "${BUILD_DIR}/${BUILD_STYLE}-iphoneos/libOCMock.a" "${BUILD_DIR}/${BUILD_STYLE}-iphonesimulator/libOCMock.a" -output "${TARGET_BUILD_DIR}/Library/libOCMock.a"
    
    &nbsp;
    
    # copy the headers (we could have used a copy files build phase, too)
    
    cp -R "${BUILD_DIR}/${BUILD_STYLE}-iphoneos/Headers" "${TARGET_BUILD_DIR}/Library"
    
  14. Click ‘Build’ from the ‘Product’ menu. 64 bit libOCMock.a should be built now. Right click libOCMock.a in the project navigator under the ‘Products’ group. Click ‘Show in Finder’. Copy libOCMock.a and the directory OCMock founder in the Headers directory that is located in the same directory as libOCMock.a. Copy these two items to a location that you can find them later.

Adding OCMock to the Demo Project

Now we’ll add the library and header to a demo project.

  1. Open Xcode. Click File…New Project. Select Cocoa Application.
  2. Name it whatever you want. Make sure that you click “Include Unit Tests”
  3. Verify that the default test is working… click Product…Test You should get a test error in the testing file. If so, works as expected.
  4. Navigate to your project directory. Create a directory in it called ‘TestLibraries’
  5. Copy libOCMock.a and the folder ‘OCMock’ containing the header files into the ‘TestLibraries’ directory.
  6. You’ll have a group (folder) that is named like so: (YOUR_PROJECT_NAME)Tests. We’ll refer to this as the testing group. Right click it and click ‘Add Files to..’
  7. Select the folder in your project directory that you created: ‘TestLibraries’ Make sure that “Copy items into designations group’s folder” is NOT checked since these files already exist at the project root. Select ‘Create groups for any added folders’. Uncheck your project target and make sure that your testing target is checked.
  8. Go to the Test target build settings. Make sure that: “Library Search Paths” has “TestLibraries” in it. If not, you’ll need to add the following string WITH THE QUOTES: “${SRCROOT}/TestLibraries”
  9. In the Test target build settings, make sure that “Header Search Paths” has “TestLibraries” with recursive selected. If not, add the following string WITH THE QUOTES:  ”${SRCROOT}/TestLibraries” Select the ‘recursive’ option.
  10. In the Test target build settings, locate ‘Other Linker Flags’, add: -ObjC -all_load
  11. In your implementation test file, locate the textExample method. Put this snippet in its place:
    #include <OCMock/OCMock.h> //put this at the top
    id mockString = [OCMockObject mockForClass:[NSString class]];
    
    [[[mockString stub] andReturn:@"MOCKS UP IN"] lowercaseString];
    
    STAssertEqualObjects([mockString lowercaseString], @"MOCKS UP IN", nil);
    
  12. Click “Product” menu and “Build For Testing”
  13. Then click “Product” and “Test” All should pass.

If you get the following error: ‘unrecognized selector sent to instance’ then you didn’t at the ‘Other Linker Flags’

Hope this helps. References:

Do you use Git? If so, checkout Gitpilot to make using Git easy.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Synchronous File Copy in Node.js

Sometimes, asynchronous operations can be a burden. Especially when you’re writing small console utilities like to batch process files.

There are many asynchronous ways to copy a file. Here is a synchronous version (CoffeeScript):

copyFileSync = (srcFile, destFile) ->
  BUF_LENGTH = 64*1024
  buff = new Buffer(BUF_LENGTH)
  fdr = fs.openSync(srcFile, 'r')
  fdw = fs.openSync(destFile, 'w')
  bytesRead = 1
  pos = 0
  while bytesRead > 0
    bytesRead = fs.readSync(fdr, buff, 0, BUF_LENGTH, pos)
    fs.writeSync(fdw,buff,0,bytesRead)
    pos += bytesRead
  fs.closeSync(fdr)
  fs.closeSync(fdw)

You can view the converted version in JavaScript.

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Buzz: A Node.js Command Line Program to Keep Your App Running Indefinitely; Like the Program Forever

Buzz is a command line program that can kill your app routinely and restart it.
It’ll will also restart your app if it dies. It’s a lot like the other Node.js
program Forever.

It’s much simpler than Forever. Approximately 50 lines of CoffeeScript code.
It displays your apps output to STDOUT and also displays any of your apps
STDERR output in red.

Usage

Install it via npm:

npm install buzz

Then run:

buzz 240 your_cool_app param1 param2

The first parameter to buzz is the time in seconds that it’ll be killed and
restarted. So, `your_cool_app` would be killed and restarted after four minutes.

If you don’t want buzz to kill your app, but you want it to bring it back to
life if it dies, run:

buzz your_cool_app param1 param2

You can test buzz by running his the app `buzz_test`:

buzz_test

`buzz_test` runs the app `smarty_pants` that spews out random facts to you and
taunts you. Occasionally `smarty_pants` will commit suicide, but buzz will
bring him back to life.

`buzz_test` ends up actualy just running the following command:

buzz 10 smarty_pants 2000 0.15

Which will kill smarty pants every 10 seconds and bring him back to life. Also,
every two seconds, smarty pants will spit out a random fact. Approximately, every
13 seconds smarty pants will take his own life, but Buzz will bring him back.

Motivation

I have a command line app that is nasty to debug. It’s working fine for the first
five minutes or so. Thus, Buzz was born. Instead of fixing the bug, I wanted
to make this. =)

But really, it’s utility is that it’s a much simpler Forever.

The name comes from Buzz Lightyear in the movie Toy Story. His popular phrase was: To infinity and beyond!

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

A Node.js Experiment: Thinking Asynchronously, Using Recursion to Calculate the Total File Size in a Directory

I recently picked up Node.js/CoffeeScript; I figured that since JavaScript can run on about every modern computing device, it’s about time that I accept JavaScript instead of side-stepping it by using dying technologies such as GWT and Silverlight.

I’ve always felt that the best way to learn a new language/platform is to start by writing a simple program that solves a simple problem.

My problem involved traversing the filesystem and performing some tasks. For the sake of this blog post and for the sake of your attention span, the problem can be reduced to a simple algorithm that computes the total space that a directory and its contents use.

Let’s start by creating a simple synchronous version:

fs = require('fs')
path = require('path')

du = (dir) ->
  total = 0
  try 
    stat = fs.lstatSync(dir)
    if stat.isFile()
      total += stat.size
    else if stat.isDirectory()
      files = fs.readdirSync(dir)
      for file in files
        total += du(path.join(dir, file))
  catch e
  
  total

DIR = '/'
total_bytes = du(DIR)
total_kb = total_bytes / 1024.0
total_mb = total_kb / 1024.0

console.log("#{DIR}: #{total_mb.toFixed(3)} MB")

This code works fine and as expected. It displays the total size of your entire directory in MiB. Ya, I know, I wrote “MB”.

But… we are using Node.js here. The asynchronous nature should be embraced. Let’s rewrite this algorithm in an asynchronous form.

fs = require('fs')
path = require('path')

duAsync = (dir, cb) ->
  total = 0
  fs.lstat dir, (err, stat) ->
    if err then return
    if stat.isFile()
      total += stat.size
    else if stat.isDirectory()
      fs.readdir dir, (err, files) ->
        if err then return
        for file in files
          duAsync path.join(dir,file), cb
    cb(null,total)

DIR = '/'
duAsync DIR, (err, total_bytes) ->
  total_kb = total_bytes / 1000.0
  total_mb = total_kb / 1000.0

  console.log("#{DIR}: #{total_mb.toFixed(3)} MB")

Hmm, this doesn’t output the correct values. I’m not passing the totals up the callback chain.

Also, from here on out, I’m only going to show the algorithm.

Let’s take advantage of closures and modify this a bit. If we could remove the recursion, that may simplify things a bit.

duAsync2 = (dir,cb) ->
  total = 0
  files = []
  all_files.push(dir)

  while all_files.length > 0
    current_dir = files.pop
    fs.lstat current_dir, (err,stat) ->
      if err then return
      if stat.isFile()
        total += stat.size
      else if stat.isDirectory()
        fs.readdir current_dir, (err,files) ->
          if err then return
          for file in files
            all_files.push(path.join(current_dir, file))
      cb(null,total)

On the surface, this looks fairly simple. We have removed the recursive aspect to simplify it a bit. The code in the while block will always see ‘total’ so we don’t run into the same problem as the last implementation.

One major problem though, this doesn’t work. This exits almost right away. Ah yes… we are doing an asynchronous implementation. The all_files array is empty by the time the while loop goes to the second iteration.

Maybe recursion is unavoidable? Let’s still leverage closures though.

This version is very similar to the last, I’ve just managed to use recursion within a function. The ‘again’ function is called recursively.

duAsync3 = (dir,cb) ->
  total = 0

  again = (current_dir) ->
    fs.lstat current_dir, (err, stat) ->
      if err then return
      if stat.isFile()
        total += stat.size
      else if stat.isDirectory()
        fs.readdir current_dir, (err,files) ->
          if err then return
          for file in files
            again(path.join(current_dir, file))
      cb(null, total)

  again(dir)

It works! Consider this: what if you only want the results at the very end? That is, you only want the callback to occur once, and at the end… then what do you do?

This was a dilemma that I faced for a bit. For this particular problem, it might not really matter much. Especially considering that this is a console utility. However, I considered figuring this out, a right of passage as a Node.js/JavaScript noob. So I didn’t want to use any utilities such as Async.js, Seq, etc.

I started doing research, fortunately I stumbled upon two great articles:

  1. “Asynchronous JavaScript: The Tale of Harry”
  2. Currying the Callback the Essence of Futures

The first article seemed to have almost an identical problem. Except, that the author didn’t impose the additional constraint of only executing the callback upon the finished. The solution in that article works as expected, but seems a bit more complex than necessary.

I kept researching. Found an article Deriving the Y-Combinator in 7 Easy Steps (JavaScript). My mind was exploding learning some of these functional programming concepts!

But, I still wasn’t closer to a solution. I finally made my way into #node.js on freenode (IRC). Fortunately, AvianFlu was able to lend me a tip. He suggested the following:

  • Create three variables: started, finished, running
  • At the beginning of the callback, increment started and running.
  • At the end, decrement running and increment finished.
  • When (started === finished) && (running === 0) You should be done.

I experimented with this for awhile. Sometimes, it felt that I was close. But it never quite worked. Then I thought about it a bit more and kept the concept of a ‘running’ variable and added a variable to denote the number of files left to process.

duAsync4 = (dir,cb) ->
  total = 0
  file_counter = 1 #starts at one because of the initial directory
  async_running = 0

  again = (current_dir) ->
    fs.lstat current_dir, (err, stat) ->
      if err then file_counter--; return
      if stat.isFile()
        file_counter--
        total += stat.size
      else if stat.isDirectory()
        file_counter--
        async_running++
        fs.readdir current_dir, (err,files) ->
          async_running--
          if err then return #console.log err.message
          file_counter += files.length
          for file in files
            again path.join(current_dir, file)
      else
        file_counter--
      if file_counter is 0 and async_running is 0
        cb(null, total)

  again dir

This works. What’s important to note is that there are many ways to solve problems using Node.js. On my Quad-Core MBP 8 GB Ram, this is almost twice as fast as the synchronous version!

Try it out and let me know your results. Also, can you think of any other ways to solve this problem?

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

A for-loop conversion from JavaScript to CoffeeScript

I was stumbling along StackOverflow when I ran into this question:

How can I convert a Javascript for-loop to Coffee? With this example:

for (i = 0; i < 10; i++) {
    doStuff();
}

The answer: (I’ve since edited and updated the Stackoverflow answer)

doStuff() for i in [0 .. 9]

http://jashkenas.github.com/coffee-script/#loops

This answer works for this contrived case.

What happens when you have a case like this:

for (var x = 0; x < myArray.length; ++x)
  alert(x);

What’s the answer smarty pants?
If you answered:

alert(i) for i in [0 .. myArray.length-1]

You get a pass if myArray has anything in it. What will happen if myArray is an empty (not null) array?
Go ahead and execute it. Go here to the CoffeeScript page, and paste this code in the “Try CoffeeScript”, I’ll wait.

myArray = []
alert(i) for i in [0 .. myArray.length-1]

What’s that? You say it returned “0″ and “-1″???

The secret is in the ".." which implies "=>" or "<="
and the "..." implies "&lt" or "&gt".

So this is probably what you expect:

myArray = []
alert(i) for i in [0 ... myArray.length]

It’s too bad the loops section at this time doesn’t mention this.

CoffeeScript is a great tool. Here is another tool that allows you to convert JavaScript to CoffeeScript. Try our examples here and see what the tool generates. Hint: it isn’t always optimized.

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Modifying $NODE_PATH for Node.js/NPM/NVM

I’ve been hacking around with Node.js lately. If you develop with Node.js then you should be using both NPM (package manager) and NVM (node version manager, like RVM).

For some reason, NVM doesn’t seem to set $NODE_PATH when you switch Node versions. $NODE_PATH needs to be set so that when you call ‘require’ from within your Node programs, it can find the relevant modules.

If you don’t set $NODE_PATH, that’s OK too. But you’ll find that in your project directory you’ll need to do a lot of linking. You can do this like so:

npm install -g express #the '-g' flag installs the module to the npm node_module directory
npm link express

Doing this is OK, but is a bit annoying at times because some modules still won’t work if you link with them, so you’re left installing it locally to your project’s directory.

I hacked up some bash commands to set my Node environment how I like it. This also allows me to make my own Node modules.

My ~/.bash_profile on Mac OS X Lion

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
PROJ=~/Dropbox/Projects
. ~/.nvm/nvm.sh

JP_NODE_PATH=/Users/jprichardson/Dropbox/Projects/Personal/js/node_modules
JP_NODE_BIN_PATH="${JP_NODE_PATH}/.bin"

NP=$(which node) 
BP=${NP%bin/node} #this replaces the string '/bin/node'
LP="${BP}lib/node_modules"

export PATH="$PATH:$JP_NODE_BIN_PATH"
export NODE_PATH="$JP_NODE_PATH:$LP"

When I run `which node` this is capturing the output from my default Node version. You set this like:
nvm alias default 0.4 which will use the latest 0.4, which is 0.4.12.

Again, I’m not a bash hacker by any means. I didn’t even know why you need to use ‘export’. I didn’t know how to concatenate strings in a bash variable. Or, even how to run a command and capture the output in a variable. I also didn’t know how to modify strings in bash. What kind of hacker am I? ::sobs:: Hehehe.

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Automating the Generation of iOS Push Notification Certificates

I have a large number (200+) of iOS applications. I needed to generate push notification certificates for each of them. If you’ve ever gone through the process, it can be a huge pain to do just a few. I needed to write a script
to automate this process.

First, I created a ruby gem to access the Mac OS X Keychain App.

Next, I leveraged watir to actually login to the iOS provisioning portal and upload/download the necessary files.

First, make sure that you’re using Mac OS X 10.7 (Lion) and that you have Chrome and Ruby 1.9.2 installed.

  1. Download chromedriver http://code.google.com/p/chromium/downloads/list
  2. Move the binary to /usr/local

Next, run these commands:

git clone git://github.com/jprichardson/GeneratePushCerts.git
gem install 'keychain_manager'
gem install 'watir-webdriver'
cd ./GeneratePushCerts

Modify the ‘config.example.yml’ file with your iOS developer/provisioning portal credentials. Rename the file
to ‘config.yml’.

You’ll want to edit ‘app.rb’ and modify the END_WITH variable and set it to ”. I should have created a regular expression around line 60.

Run the app ‘ruby app.rb’.

Let’s take a look at some watir code from the file:

  browser.checkbox(id: 'enablePush').click() #enable configure buttons
  browser.button(id: 'aps-assistant-btn-prod-en').click() #configure button

  Watir::Wait.until { browser.body.text.include?('Generate a Certificate Signing Request') }

  browser.button(id: 'ext-gen59').click() #on lightbox overlay, click continue

  Watir::Wait.until { browser.body.text.include?('Submit Certificate Signing Request') }

  browser.file_field(name: 'upload').set(CERT_REQUEST_FILE)
  browser.execute_script("callFileValidate();")
  #browser.file_field(name: 'upload').click() #calls some local javascript to validate the file and enable continue button, unfortunately File Browse dialog shows up

  browser.button(id: 'ext-gen75').click()

  Watir::Wait.until(WAIT_TO) { browser.body.text.include?('Your APNs SSL Certificate has been generated.') }

  browser.button(id: 'ext-gen59').click() #continue

  Watir::Wait.until { browser.body.text.include?('Step 1: Download') }

  File.delete(DOWNLOADED_CERT_FILE) if File.exists?(DOWNLOADED_CERT_FILE)

  browser.button(alt: 'Download').click() #download cert

  puts('Checking for existence of downloaded certificate file...')
  while !File.exists?(DOWNLOADED_CERT_FILE)
    sleep 1
  end

  Watir::Wait.until { browser.body.text.include?("Download & Install Your Apple Push Notification service SSL Certificate") }

  browser.button(id: 'ext-gen91').click()
  browser.goto(APP_IDS_URL)

You can browse the entire source code on Github.

I think watir is pretty intuitive and is a swiss army knife for automating the interaction with web pages. It’s pretty slow as it’s meant for testing, so I wouldn’t use it for screen scraping, but it’s served me well for this task.

Do you use Git? If so, checkout Gitpilot to make using Git thoughtless.

Follow me on Twitter: @jprichardson and read my blog on entrepreneurship: Techneur.

-JP Richardson

Follow

Get every new post delivered to your Inbox.