Gravatar Gmail
Octocat
Twitter_bird-over
Followers

Remove All Emacs temp ~ files


rm `find |grep "~"$`

See it here

CoffeeScript to JSON

Starting a new node package. coffee2json

Requirements


  • Take in coffeescript
  • compile javascript json objects
  • Strip out extra javascript tags and stuff
  • Run through JSON lint

Sounds good lets go.

Suse Node.js

I have tried these instructions on a brand new 11.4 Suse VM

sudo zypper ar http://download.opensuse.org/repositories/devel:/languages:/nodejs/openSUSE_11.4/ NodeJSBuildService
output:

Adding repository ‘NodeJSBuildService’ [done]
Repository ‘NodeJSBuildService’ successfully added
Enabled: Yes
Autorefresh: No
URI: http://download.opensuse.org/repositories/devel:/languages:/nodejs/openSUSE_11.4/

sudo zypper in nodejs nodejs-devel
output:

New repository or package signing key received:
Key ID: 01A61473270E2386
Key Name: devel:languages:nodejs OBS Project <devel:languages:nodejs@build.opensuse.org>
Key Fingerprint: 587F4144A9361A72F77AD50001A61473270E2386
Key Created: Thu 19 May 2011 11:51:03 AM EDT
Key Expires: Sat 27 Jul 2013 11:51:03 AM EDT
Repository: NodeJSBuildService

Do you want to reject the key, trust temporarily, or trust always? [r/t/a/?]®: a
Building repository ‘NodeJSBuildService’ cache [done]
Loading repository data…
Reading installed packages…
Resolving package dependencies…

The following NEW packages are going to be installed:
binutils-gold gcc gcc45 gcc45-c++ gcc-c++ glibc-devel libstdc++45-devel
libv8-3 linux-glibc-devel nodejs nodejs-devel

11 new packages to install.
Overall download size: 18.6 MiB. After the operation, additional 77.8 MiB
will be used.
Continue? [y/n/?] (y):y
Retrieving package linux-glibc-devel-2.6.36-3.1.noarch (1/11), 720.0 KiB (2.9 MiB unpacked)
Retrieving: linux-glibc-devel-2.6.36-3.1.noarch.rpm [done (0 B/s)]
Retrieving package binutils-gold-2.21-13.1.i586 (2/11), 699.0 KiB (2.8 MiB unpacked)
Retrieving: binutils-gold-2.21-13.1.i586.rpm [done (971.2 KiB/s)]
Retrieving package libv8-3-3.9.13.0-34.1.i586 (3/11), 1.4 MiB (5.3 MiB unpacked)
Retrieving: libv8-3-3.9.13.0-34.1.i586.rpm [done (668.6 KiB/s)]
Retrieving package glibc-devel-2.11.3-12.21.1.i686 (4/11), 4.0 MiB (22.2 MiB unpacked)
Retrieving: glibc-devel-2.11.3-12.21.1.i686.rpm [done (1.1 MiB/s)]
Retrieving package nodejs-0.6.10-1.3.i586 (5/11), 1.1 MiB (3.4 MiB unpacked)
Retrieving: nodejs-0.6.10-1.3.i586.rpm [done (489.5 KiB/s)]
Retrieving package libstdc++45-devel-4.5.1_20101208-9.8.i586 (6/11), 2.1 MiB (15.1 MiB unpacked)
Retrieving: libstdc++45-devel-4.5.1_20101208-9.8.i586.rpm [done (1.5 MiB/s)]
Retrieving package gcc45-4.5.1_20101208-9.8.i586 (7/11), 5.0 MiB (15.4 MiB unpacked)
Retrieving: gcc45-4.5.1_20101208-9.8.i586.rpm [done (1.6 MiB/s)]
Retrieving package gcc45-c++-4.5.1_20101208-9.8.i586 (8/11), 3.4 MiB (10.2 MiB unpacked)
Retrieving: gcc45-c++-4.5.1_20101208-9.8.i586.rpm [done (1.5 MiB/s)]
Retrieving package gcc-4.5-19.1.i586 (9/11), 4.0 KiB (0 B unpacked)
Retrieving: gcc-4.5-19.1.i586.rpm [done]
Retrieving package gcc-c++-4.5-19.1.i586 (10/11), 4.0 KiB (0 B unpacked)
Retrieving: gcc-c++-4.5-19.1.i586.rpm [done]
Retrieving package nodejs-devel-0.6.10-1.3.i586 (11/11), 133.0 KiB (580.0 KiB unpacked)
Retrieving: nodejs-devel-0.6.10-1.3.i586.rpm [done (313.2 KiB/s)]
Installing: linux-glibc-devel-2.6.36-3.1 [done]
Installing: binutils-gold-2.21-13.1 [done]
Installing: libv8-3-3.9.13.0-34.1 [done]
Installing: glibc-devel-2.11.3-12.21.1 [done]
Installing: nodejs-0.6.10-1.3 [done]
Installing: libstdc++45-devel-4.5.1_20101208-9.8 [done]
Installing: gcc45-4.5.1_20101208-9.8 [done]
Installing: gcc45-c++-4.5.1_20101208-9.8 [done]
Installing: gcc-4.5-19.1 [done]
Installing: gcc-c++-4.5-19.1 [done]
Installing: nodejs-devel-0.6.10-1.3 [done]

This should install three important binaries:
node, node-waf and npm.

In order for V8, the javascript runtime engine, to work correctly, I needed to install the development headers via:

sudo zypper in v8-devel

Enjoy your node 0.6.12 on suse.

Coffee!!



Coffee Script is the is shit. No matter how technical you consider yourself as a programmer, writing less code is always better. Although some might not consider JavaScript, node.js, commonjs, to be the most performance oriented approaches to development, I cannot stress the importance of clear concise code. These posts are dedicated to the advancement of simplistic linguistic languages such as Coffee Script.


My experience with CoffeeScript begins with writing Titanium Appcelerator apps in JavaScript. Although I was able to write js in an object oriented manner, it was extremely laborious task. We soon searched for alternatives and stumbled upon CoffeeScript. The first challenge was setting up a productive file structure.

Directory Structure

In our base project directory, we created a ‘coffee’ folder that contains our different directories that contain ui, network, and utility components. The structure will be compiled where you specify in your coffee compile command. If you need to translate current JavaScript into coffee you can use my version of js2coffee, a fork of rstacruz’s. I added a batch mode which can be invoked like this.

js2coffee —batch —recursive —output mynewfolder/ rootfolders/

Once we had all of our coffee files, we run a background coffee compiler that watches files for changes. I have adopted the practice of creating a ‘caffeine.sh’ shell script to execute these commands.

#!/bin/bash
DIR=`grep ‘path’ .gitmodules | awk ‘{print $3 }’`
coffee -o Resources/lib/ -cw “$DIR/coffee/” &
coffee -o Resources -cw coffee/ &
cake jsonconvert &

I also have a separate Cake file that converts my appinfo.coffee into appinfo.json which we use for many project configurations, strings, and file locations etc.

fs = require 'fs'

{print} = require 'sys'
{spawn} = require 'child_process'

task 'jsonconvert', 'convert appinfo.coffee into valid json', ->
  t = spawn 'coffee', ['-o', 'Resources/assetnest/', '-cwb',  './appinfo.coffee']
  t.stderr.on 'data', (data) ->
    console.log data.toString()
  t.stdout.on 'data', (data) ->
    console.log data.toString()
    s = spawn './coffeetojson', ['Resources/assetnest/appinfo.js', 'Resources/assetnest/appinfo.json']
    s.stdout.on 'data', (data) ->
      console.log data.toString()

The coffeetojson script can be found here, a fork of Aral Balkan’s.

Writing some Coffee

Now lets see some code

WIP

Remote Printing

When I want to print documents from a remote location to my home printer, I typically just ssh into my box at home and use the command line. Yesterday I decided to just “open” my home printer directly by forwarding port 9100 to the IP address of the printer from my home router. This exposes the printer’s socket to the outside world. I then setup a new printer configuration using my router ip and port 9100. Now I can print as if it was on my local network. Yes I realize that this also allows anyone to print to my printer and I will probably soon awake to hundreds of pages printing and wasting my ink. I also realized that if I curl ed my printer, it will print out the http request headers.

Developing Appcelerator Titanium Apps with CoffeeScript

Titanium Studio is a great way to produce apps for multiple platforms. But lets face it, the code you are using is sloppy as fuck and pollutes your global namespace faster and faster with each new feature.

OK, maybe its not that bad, but there is a great way to produce JavaScript in a cleaner and easier fashion. CoffeeScript is a little language that compiles down to JavaScript. Each CoffeeScript file compiles into a JavaScript file but wraps it in a anonymous function. It is a great approach, but you have to learn how to write code without all the ‘cool’ global vars in your current code base.

The standards of CommonJS should be followed and used when writing. This means we have to divide our code into ‘modules’ and use the require function to get at our objects and functions. The first step to writing good coffee for Titanium is having app.js down to one line. Anything in app.js is forever in the global namespace, so the less code here, the better. I typically just require my ‘home’ module.

Shareaholic for Rails

Hello Rails Developers,

I recently added the Shareaholic share buttons and they are very cool! The instructions found on their website are great for standard websites and Wordpress blogs, but I needed a slightly different approach for this rails blog.

First step was to create a partial that I could render where I like.

%div.shr_class.shareaholic-show-on-load
  :javascript
    var SHRSB_Settings = {"shr_class":{"src":"/assets/","link":"","service":"5,7,2,52,38,201,88,74",
    "apikey":"6ffe2cbf142c45bd4cd03b01ec46b8658","localize":true,"shortener":"google",
    "shortener_key":"","designer_toolTips":true,"twitter_template":"${title} - ${short_link} via
    @Shareaholic"}};
  :javascript
    (function() {
    var sb = document.createElement("script"); sb.type = "text/javascript";sb.async = true;
    sb.src = ("https:" == document.location.protocol ? "https://dtym7iokkjlif.cloudfront.net" :
    "http://cdn.shareaholic.com") + "/media/js/jquery.shareaholic-publishers-sb.min.js";
    var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(sb, s);
    })();

The required class name for Shareaholic is “shr_class shareaholic-show-on-load”. In HAML, concat ‘words’ with ‘.’ to have a space in the div’s class name.

I placed this in a file at ‘/railsroot/app/views/layouts/_shareaholic.html.haml’.

In SHRSB_Settings, there is the “src” attribute and I set mine to “/assets/” . Make sure you download the custom shareaholic_sexybookmarks.zip and unzip it to the app/assets folder if you are using the assets pipeline. You can move ‘sprite.png’ to images/ and ‘sprite.css’ to stylesheets.

You can also put it wherever you like, just make sure you change the “src” attribute to the correct path.

The next step is to place,

= render 'layouts/shareaholic'

wherever you would like your share buttons rendered. I placed them on the individual posts page so Shareaholic could correctly consume my blog post information. I had the buttons render on my posts index page, but the Shareaholic buttons were only reacting to the first post on the page.

If you are looking for a quick simply social media share solution, Shareaholic is a great tool. I really like the numbers that appear on the Facebook and Twitter buttons that show the number of hits.

shareaholic buttons

Learning on Linux

All computer science students should learn how to use *nix. Why? I believe Windows is a great OS, perfect for many users. However I do not think it is the ideal platform for computer scientists or developers. Windows hides many details from the user, ones that are important to understanding how programs compile and/or run. Being a good programmer is one thing, but If you are tethered to an IDE or windows you might not ever
y learn the underlying details of the computer. I believe it is easier to get started with a *nix system.

If you are looking for a change from Windows I would recommend Wubi, a Windows Ubuntu installer. It is also a simple process to install an Ubuntu partition to your existing hard drive. Read the details on Ubuntu.com

TUX :)

Building Titanium Mobile SDK 1.8.0.1 from source

The following tutorial is building Titanium Mobile SDK 1.8.0.1 from source. Appcelerator is suppose to release this in a few weeks but it is easy enough to build if you are eager to use the V8 javascript runtime capabilities. I am using Ubuntu Linux 10.04.3 32-bit.

This build is based on git commit id 8f6f25b9bad464edea4ecf823f28d7d2a74f032d

Dependencies

You will want to have Android SDK and NDK installed and know their path. If you do not have these installed look at this for the SDK and and this for the NDK.

sudo apt-get install build-essential ruby rubygems libzip-ruby1.9.1 scons libxml2-dev libgtk2.0-dev python-dev ruby-dev libdbus-glib-1-dev libnotify-dev libgstreamer0.10-dev libxss-dev libcurl4-openssl-dev git-core libsoup2.4-1 libsoup2.4-dev libsoup-gnome2.4-dev libicu-dev libgnutls-dev libjpeg8-dev libcurl4-openssl-dev libenchant-de gperf

sudo apt-get install build-essential scons gperf

sudo easy_install pyyaml

*Note: the first set of dependencies were taken from here. These were the dependencies necessary to build the titanium desktop. Not all of there are necessary but most are. The second and third dependencies are ones that I needed to install on a fairly bone stock Ubuntu install.

Now clone the Appcelerator titanium_mobile repo.

So once you clone it where you like

cd titanium_mobile

Building

Now you need to build it. Your welcome to try my little bash script I wrote that sets the necessary environment variables and runs scons to install. This way if you get errors from missing dependencies you can easily run it again. build.sh

or:


wget https://s3.amazonaws.com/wlaurance_6793/titanium_build/build.sh

Make sure you open it up and replace with the correct values for your SDK, NDK, and java-sun locations.

Now run the script:


sh build.sh

The compilation should take approximately 5 minutes.

If you get an error in the build.xml in the android directory, you might have to create a symlink in the prebuilt/linux-x86/bin directory in the NDK. It gave me an error concerning awk and told me to point it to gawk or nawk. Run the following command if you get this error.


ln -s `which gawk` $PATH_TO_NDK/prebuilt/linux-x86/bin/awk 

Replace $PATH_TO_NDK to the root directory path wherever your NDK is located. This will create a symlink to your gawk binary called awk.

If everything went according to plan, you should have a file name mobilesdk-1.8.0.1-linux.zip in the dist/ directory. My file is 16743472 bytes. Check to make sure yours is close. Feel free to ask questions in the comments and please let me know if there are other dependencies I should list. If you are unable to build it yourself you are welcome to my version.

sha1sum:

c91c1566677bd0bf1f8a00d48f84b40002cc2b81 mobilesdk-1.8.0.1-linux.zip

Installing

Now you can open Titanium Studio and go to Help => “Install Titanium Studio SDK from URL…”. In the popup put “file:///path/to/the/zip/file/you/just/made”. Click finish and it should be installed!

Install Titanium SDK from URL

Installing Titanium SDK from URL

Installing Titanium SDK from URL

The Importance of learning new programming languages and frameworks from the bottom up

IDE’s hide a lot of information from the programmer. So much so it really prevents one from understanding the lower level ideas of a particular programming language or framework. Lets take Java for example. Eclipse is a wonderful IDE that lets you preform many tasks easily and efficiently and to create large scale applications. Eclipse is great when working with api because of the popup methods and javadoc information. When first learning Java, one should not turn to such devices. If you have only used Eclipse or another IDE, chances are that you might not know how a package in Java truly works or how to compile your project.

Instead I recommend using the pure and simple power of the command line. So today we are going to build the hello world program. This mini tutorial is for Ubuntu but can most likely be applied to all *nix systems. If you are reading this tutorial and are using Windows, please read my page on learning in a linux environment .

sudo apt-get install openjdk-6-jdk

There are many more dependencies but the apt should list them and you should download them.

Now lets make a new directory for our test project.

mkdir test_project; cd test_project

Lets say we want a to make a package to store our useful classes inside of. Lets call it com.our.pack.name

 mkdir --parents com/our/pack/name

This will make all of the parent directories. So a package in java is no more than directories. Next “touch” a file in com/our/pack/name/OurTestClass.java and also ./Main.java in our root project directory. You should end up with the following tree structure:


.
|-- com
|   `-- our
|       `-- pack
|           `-- name
|               `-- OurTestClass.java
`-- Main.java

Tree is a great command line tool to quickly see your directory structure. It is in the repos.

sudo apt-get install tree

Alrighty, so now we are ready to write some code. Lets start with our class, OurTestClass.java. My text editor of choice is Emacs, but Gedit, Vi, Vim, Nano are all fine. Which ever you would like to use. (Just not a huge IDE :) ).

Here is the code for OurTestClass.java

package com.our.pack.name;

public class OurTestClass {
    
    private String name;
    private int id;

    public OurTestClass(String name, int id){
        this.name = name;
        this.id = id;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setId(int id){
        this.id = id;
    }
    
    public String toString(){
        return "Our name is " + this.name + " and our ID is " + this.id;
    }

}

The first statement is the package declaration. This tells the class what package it belongs too. The remainder of the code is fairly simple and should be straight forward.

Next lets write the code for Main.java
Version 1

import com.our.pack.name.OurTestClass;
public class Main {
    public static void main(String args[]){
        OurTestClass otc = new OurTestClass("Hello", 123);
        System.out.println(otc);
    }
}

The first line will import OurTestClass.java from com.our.pack.name package. This is a good time for me to note that packages imports are not required. They are a naming convention designed for convenience. The code for main could have easily looked like this:

Version 2

public class Main {
    public static void main(String args[]){
        com.our.pack.name.OurTestClass otc = new com.our.pack.nameOurTestClass("Hello", 123);
        System.out.println(otc);
    }
}

Importing packages allows us to not write the full package name each time we want to instantiate a class. This is also why two classes cannot be named the same thing when using imports. The compiler would not know which one to use. However you can have the same class name if you explicitly write the package path like the version of main.

Now we are ready to compile our code. I am using Version 1 of main to compile the code.

We are going to compile using:

javac Main.java

If you get any errors please do not be afraid to ask in the comments!

This will produce two “class” files compiled into Java bytecode. Looking at our tree

.
|-- com
|   `-- our
|       `-- pack
|           `-- name
|               |-- OurTestClass.class
|               `-- OurTestClass.java
|-- Main.class
|-- Main.java

The java compiler looked for the import statement to find OurTestClass. The rest of the libraries are loaded from the jdk. If you want to see what the compiler is doing do :

rm Main.class com/our/pack/name/OurTestClass.class #to remove the class files so we can see full compilation
javac -verbose Main.java

The output should be similar to this:

[parsing started Main.java]
[parsing completed 19ms]
[search path for source files: .]
[search path for class files: 
/usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/sunrsasign.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/jsse.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/netx.jar,/usr/lib/jvm/java-6-openjdk/jre/lib/plugin.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/modules/jdk.boot.jar,
/usr/lib/jvm/java-6-openjdk/jre/classes,
/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunpkcs11.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/ext/pulse-java.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/ext/gnome-java-bridge.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunjce_provider.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/ext/localedata.jar,
/usr/lib/jvm/java-6-openjdk/jre/lib/ext/dnsns.jar,.]
[loading ./com/our/pack/name/OurTestClass.java]
[parsing started ./com/our/pack/name/OurTestClass.java]
[parsing completed 1ms]
[loading java/lang/Object.class(java/lang:Object.class)]
[loading java/lang/String.class(java/lang:String.class)]
[checking Main]
[loading java/lang/System.class(java/lang:System.class)]
[loading java/io/PrintStream.class(java/io:PrintStream.class)]
[loading java/io/FilterOutputStream.class(java/io:FilterOutputStream.class)]
[loading java/io/OutputStream.class(java/io:OutputStream.class)]
[wrote Main.class]
[checking com.our.pack.name.OurTestClass]
[loading java/lang/StringBuilder.class(java/lang:StringBuilder.class)]
[loading java/lang/AbstractStringBuilder.class(java/lang:AbstractStringBuilder.class)]
[loading java/lang/CharSequence.class(java/lang:CharSequence.class)]
[loading java/io/Serializable.class(java/io:Serializable.class)]
[loading java/lang/Comparable.class(java/lang:Comparable.class)]
[loading java/lang/StringBuffer.class(java/lang:StringBuffer.class)]
[wrote ./com/our/pack/name/OurTestClass.class]
[total 468ms]

To run the Main we have just created do:

java Main

The output:

Our name is Hello and our ID is 123

And there is a simple java project written entirely from a text editor and compiled on the command line. In this next java mini tutorial I will show you how to take this project and allow for command line arguments and also how you can have command line arguments in Eclipse.

If you would like to fork this project on Github

Google Docs Scripting

Just found out that Google Docs allows you to write scripts to make tasks easier for spreadsheets. Here is small snippet I just wrote for a letter grade calculator:

function gradeIT(value) {
  if (value >= 91)
    return "A";
  else if (value >= 88)
    return "A-";
  else if (value >= 85)
    return "B+";
  else if (value >= 81)
    return "B";
  else if (value >= 78)
    return "B-";
  else if (value >= 75)
    return "C+";
  else if (value >= 71)
    return "C";
  else if (value >= 68)
    return "C-";
  else if (value >= 65)
    return "D+";
  else if (value >= 61)
    return "D";
  else if (value >= 58)
    return "D-";
  else
    return "F";
}

You can create, manage, and edit scripts by going to Tools => Script *

Once you have created or installed a script from the gallery you can use it in your spreadsheet like any other
function such as SUM or COUNT. In this example if I said “= gradeIT(B10)” and B10 was 80, the gradeIT function would return “B-”.

Welcome to my new blog

I am excited to get my enki blog up and running. This is my first production rails app and being new to rails I am having a blast! Learning so much about different gems, the ruby community, and the open source community. It really is amazing.