Install Haml and Sass Textmate Bundles

In one of my last projects I had the opportunity to try Haml and Sass. I’m quite happy with it: the code is neat and clear, forcing the developers to maintain it correctly nested (something less usual as it should be), plus some other advantages like the CSS variables or output styles.

To highlight properly in Textmate install the Bundles:

cd ~/Library/Application\ Support/TextMate/Bundles
svn co "http://macromates.com/svn/Bundles/trunk/Bundles/Ruby Haml.tmbundle"
svn co "http://macromates.com/svn/Bundles/trunk/Review/Bundles/Ruby Sass.tmbundle"

Reload bundles (Bundles->Bundle Editor->Reload Bundles)

Comments (1)

Install Git Textmate bundle

cd ~/Library/Application\ Support/TextMate/Bundles
git clone git://gitorious.org/git-tmbundle/mainline.git Git.tmbundle

In the TextMate preferences, advanced tab, shell variables, set the TM_GIT variable to point to /opt/local/bin/git (if installed via MacPorts).

Reload bundles (Bundles->Bundle Editor->Reload Bundles).

Now it works as the SVN bundle, but pressing Ctrl+Shift+G.

Comments (0)

Update SVN user and password, through https

If you have a svn+ssh repository and you change the username or password, you’ll need to authenticate again checking out the code:

svn co svn+ssh://user@server.com/path/to/project project

But if the service is through https, you’ll need a special option in the command:

svn co --username user https://server.com/path/to/project project

Comments (0)

Deploy Sinatra applications in Dreamhost

Dreamhost is a great hosting, and now that they use Phusion Passenger (mod_rails), we can host Rack based applications, like Sinatra.

Adding the great repository hosting by github, we can have a professional deploying system for our home made projects.

  • Create the folder structure:
    myapp/
      public/
      tmp/
      views/

  • Create your sinatra app, myapp/mywebapp.rb
  • Create the Passenger rackup file, myapp/config.ru

  • In panel.dreamhost.com go to Manage Domains -> Add Domain/Sub Domain.
  • Fill the domain/subdomain to host the application, enable mod_rails (Passenger), and add /public to the web directory.
  • Install sinatra:

    Connect to your host via SSH

    ssh youruser@yourhost.com

    gem install sinatra

    This will install sinatra for your user in ~/.gem

Now we could upload via FTP the application to the new domain generated.

If we need to restart the app, just create a restart.txt file in the tmp folder

touch tmp/restart.txt

But we want to use git as repository of the code, and capistrano for the deployment instead of upload manually. This are the steps:

  • Go to github.com and create the new repository.
  • Go to your project folder initialize git, commit and push to our new repository:
    cd myapp
    git init
    git add .
    git commit -m'first import'
    git remote add origin git@github.com:user/project.git
    git push origin master
  • Configure capistrano;
    cd myapp
    mkdir config
    capify .

    Edit config/deploy.rb

  • Be sure that you have empty the domain folder in the server, and setup the capistrano structure:
    cap deploy:setup
  • In Dreamhost edit your domain and add /current/public to the web directory.
  • Now you can deploy:
    cap deploy
  • After this steps, Passenger couldn’t find the sinatra gem again, but was solved installing it again. Weird…

    Now you can also restart the app with:
    cap deploy:restart

Comments (0)

First aids restoring a Rails app after a crash

The very first steps to restore a Rails application after a crash:

  1. Check the logs:
    tail -f myapp/log/production.log

    Hopefully here will appear the error that generates the crash.

    If nothing appear, even after refreshing, is because the application is not running, so usually is a fail on the mongrels. If in doubt is not a bad idea restart them anyway.

  2. Check for the mongrels for our app and kill/restart them.
    ps aux | grep mongrel
    rm log/mongrel.88*.pid
    mongrel_rails cluster::start
  3. If is not the mongrels, probably something related to the web server. Restart it.
    nginx -t -c /etc/nginx/nginx.conf
    sudo /etc/init.d/nginx restart
Comments (0)

Check free ports in a server

If you are going to deploy a new Rails application to a server with already another applications, is good to be sure that the ports where the new mongrels will run are free:

netstat -tavpne | grep LISTEN

Comments (0)

Delete SVN folders

I’m migrating some projects from SVN to Git, so I want to get rid of the .svn folders recursively for the project:

find d . -name .svn -exec rm -rf '{}' \; -print

Comments (1)

First notes using Git

If you don’t know, Git is a version control system, like SVN, but better. I’ll try not to stop in their differences and go directly to the Git way.

Install git:
sudo port install git-core

Check out a project:
git clone user@server:project.git

Add a remote repository. We can use whatever name but usually is origin:
git remote add origin user@server:project.git

Create a new project:
mkdir myproject
cd myproject
git init

See the changes of the local copy:
git status

We can ignore files creating a file called .gitignore and add the files:
.DS_Store
thumbs.db

Branches are different versions of our code that we don’t want to mix. With Git we will work in the same files, and change the branch what we are working on from git. The files will be updated. The main branch is called master. We can have several branches in our local machine, but only commit the master to the git server.

See the branches we have in our machine and which one is active:
git branch

See the remote branches:
git branch -r

Create branch:
git branch mybranch

Delete branch
git branch -d mybranch

Switch branch:
git checkout mybranch

Update the current branch with changes from the server:
git pull

Add all new files to our local branch:
git add .

Commit all files changed to our local branch:
git commit -a -m'my message'

Commit only modified files, not untracked:
git commit . -m'my message'

Update the server with your commits across all common branches:
git push

Update the server with our new local branch:
git push origin mybranch

See the commits in our local branch:
git log

See the differences between your working files and the last commit
git diff

See the differences between one commit and the current version (HEAD)
git diff 129fc3d1e9f70a1e4c2e38fdbf3acd576a63b314 HEAD

Undo changes since last commit:
git reset --hard

Undo last commit:
git reset --hard HEAD^
To undo more than one commit add carets:
HEAD^^^ -> 3 commits, or HEAD~3

Revert a specific commit. Next commits will remain:
git revert 5998a70e469f323a3386355fcd09323576abaf93

Extract specific files as they were in another commit:
git checkout 5998a70e469f323a3386355fcd09323576abaf93 -- path/to.file

Merge remote branch into the current branch:
git pull origin remotebranch

Merge local branch into the current branch:
git pull . myotherbranch

Usually the workflow will be:
git pull
-- make changes --
git pull
git add .
git commit -a -m "my message"
git push

To have a more detailed cheat sheet visit http://cheat.errtheblog.com/s/git/, and for a more detailed explanation on how git works visit http://lwn.net/Articles/210045/

Comments (0)

Communication between ActionScript 3 and JavaScript

With AS3 is extremely easy to communicate ActionScript with JavaScript:

In the AS code:

In the HTML code:

For a more detailed example check the example at Adobe.

The files download doesn’t work (no externalinterface_files.zip nor ExternalInterfaceExample.zip), so I compiled the example. You can download it.

Comments (3)

install mysql gem in leopard

I thought I had the gem mysql installed for months, until recently working in a project with Sinatra and Sequel I discover it wasn’t working. That is because Rails uses another adapter if that one is not available.

So to install it properly in Leopard is not so easy as other gems:

If you did
sudo gem install mysql

Must be
sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

(make sure that is your mysql path)

If mysql.bundle looks for the dylib file in the wrong place. To change it:

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

Again make sure that are your paths to MySQL and Ruby. I used Macports to install Ruby, that’s why I have it in /opt/. You could have it in /urs/.

Comments (0)