.bash_profile and handy scripts

2 Comments

Just a copy of my .bash_profile in case I ever forget it.

Misc unix commands
> du -sh *

.bash_profile

export PATH=$PATH:~/scripts
export JAVA_HOME=/Library/Java/Home
alias nutshell='cd /Users/jrasmusson/Developer/agilenutshell'
alias reduce='sips --resampleWidth 200 a.png --out b.png'
alias ios='cd /Users/jrasmusson/Developer/iosbyexample'
alias gs='git status'
alias test='bundle exec rspec spec'

My scripts directory:

ci.sh easy git checkin

#!/bin/bash
echo "Checking in..." 

git add .

if [ -z "$1" ]
then
 git commit -a -m "cleanup"
else
 git commit -a -m "$1"
fi

git push
echo "Done!"

reset.sh resets my git repository

!/bin/bash
echo "Reseting git repository"
git reset --hard HEAD^
git clean -f
git merge origin/master

chmod 711 ci.sh reset.sh

Something that handles ssh

#!/bin/sh

echo "ssh mount..."
ssh -t -t root@192.168.1.107 <<EOF
mount -o remount,rw /system
exit
EOF

echo "copying..."
scp libspotify.so root@192.168.1.107:/system/chrome/plugins/libspotify.so
echo "ssh chmod"
ssh root@192.168.1.107 <<EOF
chmod 0744 /system/chrome/plugins/libspotify.so
reboot
exit
EOF
Advertisements

How to drop, create, populate, and reset your rails database

Leave a comment

Getting frustrated with all the commands I needed to type I created the following scripts and tools to simplify destroying and recreating my rails database.

Step 1: Create an ‘all’ task.

lib/tasks/db.rake

namespace :db do

  task :all => [:environment, :drop, :create, :migrate, :populate] do
  end

end

The basic commands for recreating and populating your database from the command line often require some variation of:

Rebuild db
$ rake db:drop
$ rake db:create
$ rake db:migrate
$ rake db:fixtures:load RAILS_ENV=test
$ rake db:test:prepare

This rake task does most of this for us. To get data into the database I create a second task shown above ‘populate’.

Step 2: Create a populate task.


require 'faker'

namespace :db do

  desc "Fill database with sample data"
  task :populate => :environment do

    puts "----------------"
    puts "RAILS_ENV is #{RAILS_ENV}"
    puts "----------------"

    puts "----------------"
    puts "--- populate ---"
    puts "----------------"

    make_cities
    make_users
    make_categories
    make_listings
  end

end

def make_cities

  puts "----------------"
  puts "--- cities   ---"
  puts "----------------"

  City.create!(:name => "Calgary")
  City.create!(:name => "San Francisco")
end

def make_users

  puts "----------------"
  puts "---- users  ----"
  puts "----------------"


  admin = User.create!(:name => "aaa",
                       :email => "aaa@aaa.com",
                       :password => "foobar",
                       :city_id => 1)
  admin.toggle!(:admin)
  2.times do |n|
    name = Faker::Name.name
    email = "example-#{n+1}@railstutorial.org"
    password = "password"
    User.create!(:name => name,
                 :email => email,
                 :password => password,
                 :city_id => 1)
  end
end

This task uses the fake gem to create and load test data into which ever environment we pass in via ‘RAILS_ENV’. It won’t work by itself yet, but we can now run this using our ‘all’ task by going:

> rake db.all
> rake db.all RAILS_ENV=test

Step 3: Put it together in a bash script

db.sh

#!/bin/bash
echo "Reseting database"
rake db:all
rake db:all RAILS_ENV=test

Now when ever I would to scrap and start all over, I rollback my git repository, and rebuild my db.

Voila. Clean slate and I can what ever it was I was trying to do again.

Simple bash script to reset your git repository

1 Comment

Here’s a simple script I use whenever I screw up (often) and need to reset my git local git repository to a clean slate.

reset.sh


#!/bin/bash
echo "Reseting git repository"
git reset --hard HEAD^
git clean -f
git merge origin/master 

Simple bash script for checking into git

Leave a comment

Having gotten tired of typing the same commands over and over again I created a simple script to conveniently check into my git repositories.

ci.sh


!/bin/bash
echo "Checking in..."

git add .

if [ -z "$1" ]
then
 git commit -a -m "cleanup"
else
 git commit -a -m "$1"
fi

git push
echo "Done!"

%d bloggers like this: