Fixing Rails Scripts

Windows Line Endings Considered Harmful

While I prefer and tend to use Ubuntu for most of my work, I occasionally have to do bits in OS X and Windows. Windows is a nightmare of an operating system at the best of times, but what often get caught out by is the fact that Windows line endings are CR LF based, which really upsets shell scripts in Linux/Unix. Whats more is that the errors you tend to get are not very descriptive, whether it be a script run from the console, or through web gateway interface (i.e. public/dispatch.*).

Executable Permission Bit

Another thing I often find that gets lost in translation is the executable permission bit if things get archived, or put on FAT/NTFS drives (i.e. flash USB disks). Subversion has an explicit svn:executable property, however git will just inherit the permission from the underlying operating system. Both have systems have advantages and disadvantages to their approach.

The Rake Task

In case you need to reset these for some odd reason here is a rake task I hacked up that deals with this. You will want to make sure you have dos2unix installed. Normally it is in a package called tofrodos in Debian systems, and can be installed with sudo apt-get install tofordos

Add to lib/tasks/fix_scripts.rake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace :install do
  desc "Fixes any issues created from Windows new line termination and sets scripts as executable"
  task :fix_scripts do
    unless `which chmod`.empty?
      # Having executable on anything that shouldn't is annoying as some GUI's run the script instead of open it
      `find . -type f -print0 | xargs -0 chmod -x`
      # These need to be executable
      `chmod -R +x ./script`
      `chmod -R +x ./public/dispatch*.*`
    else
      STDERR << "WARNING: chmod command not installed. You probably are not running a *nix"
    end
    # Remove any DOS line endings coz they screw up shebang lines
    unless `which dos2unix`.empty?
      for file in Dir['./script/**/*'] + Dir['./public/dispatch*.*']
        `dos2unix -d #{file}` unless File.directory? file
      end
    else
      STDERR << "WARNING: dos2unix command not installed. No attempt to resolve line ends has been made"
    end
  end
end

Listening to Lounge / Trip hop:
Shoalin Satellite - Thievery Corporation

Free Designs

Below are a handful of open source / free web designs