My attempt to beautify Ruby code in TextMate

Posted by kain Sat, 13 Sep 2008 12:26:00 GMT

This is based on Paul Lutus work, version 2.4

#!/usr/bin/env ruby

=begin
/***************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
=end

PVERSION = "Version 2.4, 08/25/2008"

# Modified to work as a TextMate command by Claudio Poli (http://www.icoretech.org)

$tabSize = 2
$tabStr = " "

# indent regexp tests

$indentExp = [
  /^module\b/,
  /^class\b/,
  /^if\b/,
  /(=\s*|^)until\b/,
  /(=\s*|^)for\b/,
  /^unless\b/,
  /(=\s*|^)while\b/,
  /(=\s*|^)begin\b/,
  /(^| )case\b/,
  /\bthen\b/,
  /^rescue\b/,
  /^def\b/,
  /\bdo\b/,
  /^else\b/,
  /^elsif\b/,
  /^ensure\b/,
  /\bwhen\b/,
  /\{[^\}]*$/,
  /\[[^\]]*$/
]

# outdent regexp tests

$outdentExp = [
  /^rescue\b/,
  /^ensure\b/,
  /^elsif\b/,
  /^end\b/,
  /^else\b/,
  /\bwhen\b/,
  /^[^\{]*\}/,
  /^[^\[]*\]/
]

def makeTab(tab)
  return (tab < 0) ? "" : $tabStr * $tabSize * tab
end

def addLine(line,tab)
  line.strip!
  line = makeTab(tab)+line if line.length > 0
  return line + "\n"
end

def beautifyRuby

  comment_block = false
  program_end = false
  multiLine_array = []
  multiLine_str = ""
  tab = 0
  source = STDIN.read
  dest = ""
  source.split("\n").each do |line|
    if(!program_end)
      # detect program end mark
      if(line =~ /^__END__$/)
        program_end = true
      else
        # combine continuing lines
        if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
          multiLine_array.push line
          multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
          next
        end

        # add final line
        if(multiLine_str.length > 0)
          multiLine_array.push line
          multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
        end

        tline = ((multiLine_str.length > 0) ? multiLine_str:line).strip
        if(tline =~ /^=begin/)
          comment_block = true
        end
      end
    end
    if(comment_block || program_end)
      # add the line unchanged
      dest += line + "\n"
    else
      comment_line = (tline =~ /^#/)
      if(!comment_line)
        # throw out sequences that will
        # only sow confusion
        while tline.gsub!(/\{[^\{]*?\}/,"")
        end
        while tline.gsub!(/\[[^\[]*?\]/,"")
        end
        while tline.gsub!(/'.*?'/,"")
        end
        while tline.gsub!(/".*?"/,"")
        end
        while tline.gsub!(/\`.*?\`/,"")
        end
        while tline.gsub!(/\([^\(]*?\)/,"")
        end
        while tline.gsub!(/\/.*?\//,"")
        end
        while tline.gsub!(/%r(.).*?\1/,"")
        end
        # delete end-of-line comments
        tline.sub!(/#[^\"]+$/,"")
        # convert quotes
        tline.gsub!(/\\\"/,"'")
        $outdentExp.each do |re|
          if(tline =~ re)
            tab -= 1
            break
          end
        end
      end
      if (multiLine_array.length > 0)
        multiLine_array.each do |ml|
          dest += add_line(ml,tab)
        end
        multiLine_array.clear
        multiLine_str = ""
      else
        dest += addLine(line,tab)
      end
      if(!comment_line)
        $indentExp.each do |re|
          if(tline =~ re && !(tline =~ /\s+end\s*$/))
            tab += 1
            break
          end
        end
      end
    end
    if(tline =~ /^=end/)
      comment_block = false
    end
  end

  STDOUT.write(dest)
  # uncomment this to complain about mismatched blocks
  # if(tab != 0)
  #   STDERR.puts "#{path}: Indentation error: #{tab}"
  # end
end

beautifyRuby

Auto vacuum on mail.app database

Posted by kain Wed, 03 Sep 2008 10:42:00 GMT

As you probably know (this is an old trick) it’s possibile to speed up mail.app by vacuuming it’s database, which is created with sqlite.

There’s also a trick to perform an autovacuum on it, here’s how:

Quit mail.app then open a terminal and issue the following:

sqlite3 ~/Library/Mail/Envelope\ Index ".dump" > EnvelopeIndex.sql

Add

pragma auto_vacuum=1;

to the top of EnvelopeIndex.sql

rm ~/Library/Mail/Envelope\ Index
sqlite3 ~/Library/Mail/Envelope\ Index ".read EnvelopeIndex.sql"

Cannot delete directories on fat32 filesystem under leopard

Posted by kain Sun, 24 Aug 2008 15:19:00 GMT

FAT32 formatted drives can not handle unix permissions at all.

You can not do chown, chmod or any of that stuff. In particular, if a file was locked in windows, it will be read as having an immutable flag in OS X. The only way to change it is by mounting the drive in windows.

You must open a dos window from inside windows and type:

attrib -r -s c:\foldername

and hit enter.

If you have long folder names or ones with spaces in the name it gets a little tricky guessing at the dos name equivilant ~1 etc. If you can’t get the dos name on any folders then rename the folder to a 8 letter name first then run the attrib command. Afterwards when you boot back into leopard, the locked check marks will be gone.

From pperret http://discussions.apple.com/thread.jspa?messageID=7090349

my rails .gitignore 2

Posted by kain Thu, 24 Jul 2008 18:28:00 GMT

config/database.yml
*~
*.cache
*.log
*.pid
tmp/**/*
.DS\_Store
db/cstore/**
doc/api
doc/app
doc/plugins
coverage/*
db/*.sqlite3
*.tmproj
Capfile

usually I leave a config/database.yml.example, you can extend this list also to ignore the schema.rb.

edge rails should be able to recreate missing directory, because as you know git won’t follow empty directories, if that’s the case, just touch a .gitignore in tmp and log directories.

extra trick: to always ignore those files globally:

git config --global core.excludesfile /path/to/.gitignore

update:

suggested by schram:

doc/*.dot

to ignore railroad files

also for somebody is useful to have the sqlite production database versioned, so remove db/*.sqlite3.

another update:

ignored vim swap files

*.sw?

Troubles with ssh public key authentication

Posted by kain Sat, 19 Jul 2008 09:38:00 GMT

1) Always read your secure(.log) on your server

2) Do this in your server:

chmod go-w ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Chances are that you know what I’m talking about.

keep an eye into your plesk's qmail logs

Posted by kain Fri, 18 Jul 2008 11:02:00 GMT

Create a file check_maillog in /usr/local/sbin or where do you prefer, make it executable and wrote in it:

tail -f /usr/local/psa/var/log/maillog /var/spool/qscan/qmail-queue.log /var/spool/qscan/quarantine.log /var/log/clamav/clamd.log /var/log/clamav/freshclam.log

Remove every file that a .pkg provides

Posted by kain Fri, 18 Jul 2008 09:57:00 GMT

lsbom  -f -l -s /Library/Receipts/package_here.pkg/Contents/Archive.bom  | (cd /; sudo xargs rm)

Passenger (aka mod_rails) PATH on Leopard 1

Posted by kain Sat, 12 Jul 2008 17:48:00 GMT

 Passenger uses a little subset of your PATH.

So if you have installed ruby, gem and misc binaries in other paths you cannot access them via your passenger rails application.

Here comes a solution, create an executable script ruby_with_env, I wrote it in my /usr/local/bin:

#!/bin/bash
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/graphviz/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin:/usr/local/git/bin/
:$PATH"
/usr/local/bin/ruby $*

 Now point the file to your PassengerRuby directive in your apache configuration file and restart.

uuencode never worked for me..

Posted by kain Thu, 03 Jul 2008 22:22:00 GMT

 Maybe I’m just stupid, however I just wanted a basic cron job to send a compressed postgresql backup file to a gmail account.

Mutt to the rescue.

cd /tmp && pg_dump maxgests -U postgres | gzip --best -c > database.gz && (mutt -a database.gz email@gmail.com < /dev/null)