Feeds:
Posts
Comments

Posts Tagged ‘CoffeeScript’

Recently I have published a number of posts about how to run CoffeeScript compiler on Ubuntu Linux, and on Windows (with Cygwin and without it).

As Stefan pointed out to in comments to my post, there are other interesting developments underway with regard of using CoffeeScript compiler. Examples he mentioned are:

Actually, there are more to it. Look, for example at django-coffeescript  python package. You can likely find more examples if you spend time searching.

What it means for me is that I’m far from being alone in my interest in CoffeeScript. There is a substantial (and growing) momentum behind CoffeeScript.  And this is very important. No matter how smart certain technology is, industry support is critical for its success.

Seeing all those developments happening make me more confident in future of CoffeeScript and I will use it for my developments.

I’m not going to explore myself any more ways to run CoffeeScript compiler. I have enough for my needs. I will explore other aspects of working with CoffeeScript in my future posts.

Advertisements

Read Full Post »

RecentlyI have posted instructions on how to install Node.js and CoffeeScript on Windows using Cygwin.  It results in fully-functional Node.js and CoffeeScript, but the process is rather cumbersome.

There is another way to get CoffeeScript working on Windows suggested by alisay and briefly described here, which is based on usage of the experimental Windows version of Node.js. While the way is not recommended as default by both CoffeeScript author (Jeremy Ashkenas) and Node.js team, it works more or less for me.  There are things (e.g. CoffeeScript  –watch option) that don’t work, there are things (e.g. Cake build task) that I managed to make working after doing certain tweaks in CoffeeScript files. But if you need just basic function – compilation – it works fine as well as a number of other functions.

Still, If you look for fully-functional Node.js as well, you need to install it over Cygwin as described in the previous post.

So here is how to make CoffeeScript working on Windows in a simple way.

Download Windows version of Node.js from here and download CoffeeScript from here (look for “Latest Version” link).

Put node.exe file into any directory you find it appropriate (e.g. I have used c:\tools\node). Unzip CoffeeScript in any other directory (I have used c:\dev). Go to the directory where you put node.exe and create coffee.cmd file.

In my case coffee.cmd looks like this:

@echo off
node.exe C:\dev\jashkenas-coffee-script-1a652a9\bin\coffee %*

Here C:\dev\jashkenas-coffee-script-1a652a9\bin\ is a path to coffee file which is located in bin directory of CoffeeScript dirstro. Substitute it with the actual path you have in your case.

Add the folder where you put coffee.cmd to Windows PATH (see instructions here), restart your computer.

Now you are ready to test your CoffeeScript compiler. Create test.coffee file in any directory, put “2+2” expression in it. Start Windows command prompt, go to the directory where you put the file and run command coffee –compile test.coffee. As a result of this a new test.js file will appear in the directory. The file should contain following JavaScript code:

(function() {
2 + 2;
}).call(this);

This means you have installed CoffeeScript successfully and you can compile CoffeeScript files.

However this does not mean that each and every option of CoffeeScript compiler works on Windows   in this setup as well. For example, I could not make working –watch option (maybe you will be more lucky).

CoffeeScript comes with its own build tool named Cake (see here instructions to it). Let’s make it working as well. First create cake.cmd file in the same folder where you have coffee.cmd and put this in the file:

@echo off
node.exe C:\dev\jashkenas-coffee-script-1a652a9\bin\cake %*

Now let’s test it. Open Windows command prompt. Go to the root folder of the unzipped CoffeeScript distro (there should be file named Cakefile). Run cake command. The output should show you a list of all build tasks in the Cakefile:

cake install              # install CoffeeScript into /usr/local (or --prefix)
cake build                # build the CoffeeScript language from source
cake build:full           # rebuild the source twice, and run the tests
cake build:parser         # rebuild the Jison parser (run build first)
cake build:ultraviolet    # build and install the Ultraviolet syntax highlighter
cake build:browser        # rebuild the merged script for inclusion in the browser
cake doc:site             # watch and continually rebuild the documentation for the website
cake doc:source           # rebuild the internal documentation
cake doc:underscore       # rebuild the Underscore.coffee documentation page
cake bench                # quick benchmark of compilation time
cake loc                  # count the lines of source code in the CoffeeScript compiler
cake test                 # run the CoffeeScript language test suite
cake test:browser         # run the test suite against the merged browser script
-p, --prefix       set the installation prefix for `cake install`

If you see such output, it means you can run Cake.

Unfortunately not all tasks of regular Cake build files will work on Windows with our setup. To make them working we have to tweak them a bit. Let’s use Cakefile  we have with CoffeeScript distro.  For example, if you try to execute build task by typing cake build you will get this error message:

CreateProcessW: %1 is not a valid Win32 application.

This happens since at some point Cake tries to execute certain Linux/Unix commands that obviously don’t work on Windows. To fix this particular problem open Cakefile in a text editor. Find following line:

proc =         spawn 'bin/coffee', args

Replace it with this:

proc =         spawn 'coffee.cmd', args

Run Cake build again. To verify that it worked fine, take a look at lib directory. There should be JavaScript files with timestamp corresponding to the time when you run the build process. If you see them, congratulations – you just built CoffeeScript.

This is not the only place in Cakefile which won’t work on Windows. Look for all uses of spawn and  exec functions and adjust them properly. For example this exec function call won’t work on Windows:

exec 'sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax'

since there is no sudo command on Windows etc.

Read Full Post »

Today we’ll be installing Node.js and CoffeeScript on Windows (I have described earlier how to do it on Ubuntu Linux). My Node.js installation routine is loosely based on this document.

Here is one catch I have to explain first. A version of Node.js for Windows does exist . However it is described by its authors as experimental one and it is not recommended for running CoffeeScript compiler. What’s more, IMHO it will likely always lag behind Linux/Unix version of Node.js.  The reason is following.  Authors of Node.js  aim at creating a very fast technology for networking applications. Naturally, to achieve this they rely heavily on low-level OS capabilities and they have chosen to use POSIX API for this purpose. POSIX stands for Portable Operating System Interface for Unix; all versions of UNIX and most versions of Linux implement it; Mac OS X is POSIX-compliant as well and as you can see here Ryan Dahl (“father” of Node.js) uses Mac laptop for development.   There are other POSIX-compliant OS as e.g. AIX.

However Microsoft decided not to make Windows POSIX-compliant. Yes, one can figure out how to do the same things (more or less) using Windows API, but still this requires major changes in the code. Maintaining two widely different code bases ( e.g. of Node.js) and evolving them in parallel is quite difficult and expensive. So do not count on “native” Windows version of Node.js, at least for now.

[Update on Sep 27, 2011. If you just want to compile CoffeeScript on Windows and do not need latest and greatest Node.js for other purposes, you can do it in much simpler way than I describe below. See details here. But a lot of advance functions of CoffeeScript compiler won’t work or would require tweaking to make them working. If you want to use latest and greatest Node.js on Windows and/or fully functional CoffeeScript compiler and Cake build tool, follow the instructions below].

We’ll use an existing work around of the problem to install and to run POSIX version of Node.js on Windows. Obviously, Node.js is by far not the first application which face a problem due to Windows being not POSIX-compliant. There is Cygwin which is a combination of DLL providing emulation of POSIX API on Windows and a set of POSIX-compliant software like e.g. Bash shell etc. Being installed on Windows Cygwin provides a sort of POSIX environment for other applications that need it to run.

So we’ll run Node.js on top of Cygwin. To do this we need to install Cygwin first. Here are original Cygwin installation instructions if you want to follow them. Otherwise just do what I describe.

Download Cygwin setup.exe and run it. It launches Cygwin installation wizard. On the second screen select “Install from Internet”.

Click “Next”. Accept all default values on screen #3:

Click “Next”. Enter c:\cygtemp ( or whatever temporary directory you like) as Local Package Directory on screen #4:

Click “Next” and agree on creating the directory. Screen #5 asks about details on your Internet connection:

If you are doing this at home, you almost certainly don’t have a proxy, so select “Direct Connection”. If you are within LAN (e.g. a corporate network), then you the most likely have a proxy and you can try “Use Internet Explorer Settings” or just enter proxy URL and port.  Click “Next”.

Screen #6 offers you to choose a URL to download binaries from:

Select any HTTP URL which you like (certain networks block FTP) and click “Next”. Click OK to setup alert dialog. The next screen suggest you to select packages you want to install:

  • Expand “Devel” section and select:
    •  gcc (the one which is labeled as “gcc: C compiler upgrade helper”),
    • openssl-devel
    • make
    • zlib-devel
  • Expand “Net” section and select:
    •  curl
    • openssl
  • Expand “Python” section and select python.

Click “Next”. The next screen will show you what additional packages will be downloaded as dependencies to the ones you selected:

and the download will start:

At this moment you can go and make coffee for yourself. The installation will take time to compete.

Next you will see this screen:

Select both checkboxes and click “Finish”. Congratulations, we have installed Cygwin.

Now go to Start->All Programs->Cygwin. Click Cygwin Bash Shell. This will launch Cygwin terminal window with Bash Shell:

First we have to make DNS resolution working for Cygwin. To do this we have to add DNS servers  to Cygwin resolv.conf file. Type this:

echo 'nameserver 8.8.8.8'  >> /etc/resolv.conf
echo 'nameserver 8.8.4.4'  >> /etc/resolv.conf

This adds Google Public DNS servers to your Cygwin configuration.

Re-start Cygwin terminal window. Type which gcc. If you see /usr/bin/gcc as a response, we are done with installing Cygwin.

If you want to just copy and paste the Bash commands I will be describing below into Cygwin terminal window, you can do it this way: right-click on Cygwin terminal window title bar and select Edit from the context menu:

Click Paste and the text you have copied previously will be pasted in Cygwin window.

Make sure you are in the home directory. Type this:

cd ~

Create a directory where Node.js will be installed:

mkdir  nodejs

Add the directory to Cygwin PATH:

echo 'export PATH=$HOME/nodejs/bin:$PATH' >> ~/.bashrc

Download and unzip Node.js source. To do this check URL for downloading the latest stable Node.js source. You can find it here. At the moment when I’m writing this post the URL is http://nodejs.org/dist/node-v0.4.11.tar.gz . Substitute your URL into the command below and type it into Cygwin terminal window:

curl http://nodejs.org/dist/node-v0.4.11.tar.gz | tar xz

This will create a directory with Node.js source in it. Check the name of the directory. Type ls node*. You are going to see something like this:

node-v0.4.11  nodejs

This means the source directory name is node-v0.4.11 (nodejs is the directory you have created earlier).

Go to this directory:

cd node-v0.4.11

Configure make build by typing:

./configure --prefix=~/nodejs

You should see at the end of output:

'configure' finished successfully (10.953s)

If the attempt failed, check whether you installed all additional Cygwin components I have mentioned above. For this just run Cygwin setup.exe and go through the steps described above. This won’t destroy your existing Cygwin, but it will add whatever you forgotten to add. Re-start Cygwin, and go to the source directory by typing cd ~/node-v0.4.11. Run the configuration command again.

After you executed the configuration command successfully, execute Node.js build:

make install

When the build ends you should see something like this:

'install' finished successfully (5m54.802s)

Now install NPM (Node.js Package Manager):

curl http://npmjs.org/install.sh | sh

The script will ask you if you want to do cleaning. Answer yes. If all goes OK you will see the message:

It worked

Now let’s verify whether Node.js works. Type node. You should see > as a prompt. Type 2+2. If the reply is 4, Node.js works.

It’s time to install CoffeScript.  Type

npm install -g coffee-script

in the terminal window. You should see a message like this at the end of the output:

coffee-script@1.1.2 /home/vlad/nodejs/lib/node_modules/coffee-script

which means CoffeeScript has been installed.

Now let’s verify that CoffeeScript works. Type this:

echo "square = (x) -> x * x" > test.coffee
coffee --compile test.coffee
cat test.js

You should see this:

(function() {
    var square;
    square = function(x) {
    return x * x;
  };
}).call(this);

Congratulations! Node.js and CoffeeScript are installed and ready to be used.

Now let’s have some fun with Node.js after all this hard work 🙂

Create (using Notepad editor or any other text editor) a new JavaScript file named webserver.js and put this code in it:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');

Save the file in Cygwin home directory (in my case it is c:\cygwin\home\vlad; substitute your user name). Then type in the terminal window:

node ~/webserver.js

You will see this message:

Server running at http://127.0.0.1:8080/

Open your browser and point it to http://127.0.0.1:8080/. You should see Hello World message in the browser. Congratulations! You have just written a web server in JavaScript using Node.js. Isn’t that cool?

Advice on how to use CoffeeScript on Windows.

Windows files can be accessed from Cygwin. Let’s suppose you put your test.coffee file which you want to compile into c:\dev\coffee Windows folder. The corresponding Cygwin file will be /cygdrive/c/dev/coffee/test.coffee. This means to compile it you should type in Cygwin terminal window:

coffee --compile /cygdrive/c/dev/coffee/test.coffee

The result of the compilation will be placed in the same folder where test.coffee is located.

See more details on accessing Windows files from Cygwin here.

To avoid typing long paths you can create a symlink which will be pointing to a folder where you keep your CoffeeScript files. Let’s suppose again that your CoffeeScript files are in c:\dev\coffee folder. Type this in Cygwin terminal window:

ln -s /cygdrive/c/dev/coffe cf

Of course, you can use any valid directory name instead of cf to name the symlink. Now you can access your CoffeeScript files using the symlink  with much shorter path:

coffee --compile cf/test.coffee

That’s all for today.

Read Full Post »

Today I want to install Node.js. It is quite remarkable piece of software which can do a lot of things that I want to try. But today my primary goal is to install it so that to be able to use CoffeeScript compiler for JavaScript development.

I’m using Ubuntu 11.04 on my laptop and it has Node.js installation package available in one of its repositories. Unfortunately it is version 0.2.6 while today (Aug 22, 2011) the newest stable Node.js version is 0.4.11 and the difference seems to be too much to justify using the old one. So I will attempt to use an alternative installation method to get the latest version installed. Actually, there is just one alternative method available: simply to build it from source. That’s what we are going to do. I will be using these published instructions.

I’m using Linux, so I should go directly to step 3 of the instructions. It suggests checking whether I have gcc (Gnu Compiler Collection) installed by typing which gcc in a terminal window. Well, I do have gcc in /usr/bin/gcc. If you don’t have it, you can install it using either Synaptic Package Manager (System->Administration-> Synaptic Package Manager) or apt.

Now we are ready to install Node.js and npm (Node Package Manager). I decided to follow the first option described here with some changes. First I want to use the latest stable version of Node.js (0.4.11) and not just the latest. Second, I want to place everything in ~/tools/nodejs directory. My modified script (node-install.sh) looks like this:

#!/bin/bash
echo 'export PATH=$HOME/tools/nodejs/bin:$PATH' >> ~/.bashrc #Adding Node.js to PATH
. ~/.bashrc
mkdir ~/tools/nodejs #This is the directory where Node.js will be installed
mkdir ~/tools/node0411 #This is a temporary directory used by the build
cd ~/tools/node0411
curl http://nodejs.org/dist/node-v0.4.11.tar.gz | tar xz –strip-components=1 #Download and unzip
./configure --prefix=~/tools/node js
make install # ok, fine, this step probably takes more than 30 seconds...
curl http://npmjs.org/install.sh | sh

You should substitute the version of Node.js you are going to install and the directories that you want to use into the script. The URL pointing to the latest stable source could be found here.

Create the file, set proper permissions on it (chmod 744 node-install.sh) and run it by typing ./ node-install.sh. Along the way the script will ask your permission to do cleaning. Answer “yes”. Execution of the script takes some time (around 10 minutes on my laptop), so be patient. At the end you should see “It worked” message.

Now let’s test if Node.js works. Type node in the terminal window. It should show > as a prompt. Type 2+2. Node.js should return 4. This means you installed it right.

Now it’s time to install CoffeScript. Just type npm install -g coffee-script in the terminal window. You should see a message like this:

coffee-script@1.1.2 /home/vlad/tools/nodejs/lib/node_modules/coffee-script  

which means CoffeeScript has been installed.

Now let’s verify that CoffeeScript works. Type this:

echo "square = (x) -> x * x" > test.coffee
coffee --compile test.coffee
cat test.js

You should see this:

(function() {
  var square;
  square = function(x) {
    return x * x;
  };
}).call(this);

Congratulations! Node.js and CoffeeScript are installed and ready to be used.

Now let’s have some fun with Node.js after all this hard work 🙂

Create a new JavaScript file named webserver.js and put this code in it:

var http = require('http');

http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8080);

console.log('Server running at http://127.0.0.1:8080/');

Then type in the terminal window:

node webserver.js

You will see this message:

Server running at http://127.0.0.1:8080/

Open your browser and point it to http://127.0.0.1:8080/. You should see Hello World message in the browser. Congratulations! You have just written a web server in JavaScript using Node.js. Isn’t that cool? 🙂

Next post will be about installing Node.js and CoffeeScript on Windows.

Read Full Post »