Wednesday, October 7, 2009

Grails development environment

Install Java

Install using apt-get:

sudo apt-get install sun-java6-jdk sun-java6-plugin

The following command is used to ensure the correct version is used:

sudo update-java-alternatives -s java-6-sun

To add JAVA_HOME to path for all users, edit /etc/bash.bashrc

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

If you want to set JAVA_HOME for a single user, check this article: http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/.

Install Grails

Download Grails at http://grails.org/, and unzip it to /usr/lib/grails-1.1.1 (assume the version is 1.1.1, yours might be different).

Add GRAILS_HOME variable to /etc/bash.bashrc

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export GRAILS_HOME=/usr/lib/grails-1.1.1
export PATH=$PATH:$JAVA_HOME/bin:$GRAILS_HOME/bin

To test the installation, type:

grails --help


Install Netbeans

Download Netbeans at http://www.netbeans.org/downloads/index.html. Select the Java bundle which includes Groovy support.

The downloaded file is a sh script. The current version is 6.7.1. Make it executable:

sudo chmod +x netbeans-6.7.1-ml-java-linux.sh

Run it to install Netbeans:

sudo ./netbeans-6.7.1-ml-java-linux.sh

This will launch the installer. Keep the default settings.

After the installation, you should have a "NetBeans IDE 6.7.1" menu item in the Ubuntu Programming menu. If you cannot find it, open System > Preferences > Main Menu, and enable NetBeans in the Programming menu.

Launch NetBeans. You might need to set GRAILS_HOME to /usr/lib/grails-1.1.1 in the Groovy tab at Tools > Options > Miscellaneous.

Configure MySQL

Grails comes pre-packaged with HSQLDB, but since we're using MySQL, we have a few quick steps to tell Grails how to talk to our database. First, download the Java MySQL driver from http://www.mysql.com/products/connector/j/.

Open the zip(or tar.gz) file and extract the mysql-connector-java-5.1.10-bin.jar file into the lib directory of your Grails application. Let's say the grails application is called 'kw'.

ls /home/david/workspace/kw/lib/

mysql-connector-java-5.1.10-bin.jar

Create an empty database, e.g. kw_dev, and grant all privileges to your database developer

create database kw_dev;
grant all on kw_dev.* to 'your_db_dev_username'@'localhost';

Edit DataSource.groovy under kw/grails-app/conf/

dataSource {
  pooled = true
  driverClassName = "com.mysql.jdbc.Driver"
  username = "" // your database username
  password = "" // your password
}
hibernate {
  cache.use_second_level_cache=true
  cache.use_query_cache=true
  cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
  development {
    dataSource {
      dbCreate = "create-drop" // one of 'create', 'create-drop','update'
      url = "jdbc:mysql://localhost/kw_dev"
    }
  }
  test {
    dataSource {
      dbCreate = "create-drop"
      url = "jdbc:mysql://localhost/kw_dev"
    }
  }
  production {
    dataSource {
      dbCreate = "update"
      url = "jdbc:mysql://localhost/kw_dev"
    }
  }
}

Sunday, September 13, 2009

.vimrc for ruby and python

vi ~/.vimrc

set nocompatible              " Use vim defaults
"set ls=2                      " Always show status line
set showcmd                   " Show incomplete commands
set scrolloff=3               " Keep 3 lines when scrolling
set ruler                     " Show the cursor position all the time
set title                     " Show title in console title bar
set hid                       " Change buffer without saving
set showmatch                 " Show matching bracets

set ts=2                      " Numbers of spaces of tab character
set sw=2                      " Numbers of spaces to (auto)indent
set et                        " Tabs are converted to spaces, use only when required
set sts=2                     " Soft tab stop
set smartindent               " Smart indent
set autoindent
set nocindent
set wrap

set hlsearch                  " Highlight searches
set incsearch                 " Do incremental searching

if has("autocmd")
"au FileType cpp,c,java,sh,pl,php,python,ruby set autoindent
"au FileType cpp,c,java,sh,pl,php,py,rb set smartindent
au FileType cpp,c,java,sh,pl,php set cindent
au BufRead *.py set cinwords=if,elif,else,for,while,try,except,finally,def,class
au BufRead *.rb set cinwords=if,elsif,else,unless,for,while,begin,rescue,def,class,module
"au BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
"au BufRead *.rb set smartindent cinwords=if,elsif,else,unless,for,while,begin,rescue,def,class,module
endif

syntax on
"set background=dark
"hi Normal ctermfg=grey ctermbg=darkgrey
hi PreProc ctermfg=magenta
hi Statement ctermfg=darkYellow
hi Type ctermfg=blue
hi Function ctermfg=blue
hi Identifier ctermfg=darkBlue
hi Special ctermfg=darkCyan
hi Constant ctermfg=darkCyan
hi Comment ctermfg=darkGreen
au BufRead,BufNewFile *.rb hi rubySymbol ctermfg=green


For more information about syntax highlight in vi, search for the README.txt file in
/usr/share/vim/vim72/colors (the 72 could be different)

To see the list of colors for all groups, type :highlight in vi


References:

- Using Vim: Syntax Highlighting

Saturday, August 29, 2009

Syntax highlight in blogspot

In the "Layout" -> "Edit HTML" page, add the following code after <!-- end outer-wrapper -->, and save the template.

<!-- SyntaxHighlighter -->
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shCore.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushBash.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCss.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJScript.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushPlain.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushPython.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushSql.js'></script>
<script type='text/javascript' 
src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushXml.js'></script>

<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shCore.css' 
rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shThemeDefault.css' 
rel='stylesheet' type='text/css'/>

<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
<!-- end SyntaxHighlighter -->

The SyntaxHighlighter library searches all <pre> tags and apply highlight styles according to their class names.

<pre class="brush: js">
var foobar = function() {
  var el = document.getElementById('something');
  if (el) {
    el.innerHTML = 'Found you!';
  }
}
</pre>

Here is the highlighted code:

var foobar = function() {
  var el = document.getElementById('something');
  if (el) {
    el.innerHTML = 'Found you!';
  }
}


References

- SyntaxHighlighter API
- Using SyntaxHighlighter on BLOGGER
- How to add syntax highlight to Blogger