Skip to content

Instantly share code, notes, and snippets.

@foca
Created July 7, 2012 02:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save foca/3063978 to your computer and use it in GitHub Desktop.
Save foca/3063978 to your computer and use it in GitHub Desktop.
new_rails_app is a script to quickly create pre-configured rails applications out of a template app.

new_rails_app

This script is my way of creating rails apps without much trouble. It works better for me than Rails' templates, so this is what I use.

Basically, it clones a template app and then changes the name of the app.

It's a very simple solution that I've been using for a while now.

Installation

curl https://raw.github.com/gist/3063978/17a42cb07222147dad7caa830bf22a2e902b2347/new_rails_app.sh > new_rails_app
chmod +x new_rails_app

After that, make sure the new_rails_app script is somewhere in your $PATH.

Usage

new_rails_app TheAppsName

Configuration

There's none :)

If you want to change anything, you're more than welcome to create your own blank slate app that reflects your preferences and changing the script's line where $source is defined to point to your own repository.

#!/usr/bin/env bash
# Convert ThisIsACamelizedWord to this_is_a_camelized_word
#
# Usage:
# underscore ThisIsACamelizedWord
underscore() {
echo "$@" | sed -e 's/\([A-Z\d]\)\([A-Z][a-z]\)/\1_\2/g' -e 's/\([a-z\d]\)\([A-Z]\)/\1_\2/g' | tr "A-Z-" "a-z_";
}
# Rename the Rails app in the current directory. Both the new and old names must
# be provided in CamelCase. The script will find CamelCase and unders_scored
# words to replace.
#
# Usage:
# rename_rails_app "OldName" "NewName"
rename_rails_app() {
if [ ${#@} -ne 2 ]; then
echo "Usage:" 1>&2
echo " rename_rails_app \"OldName\" \"NewName\"" 1>&2
return 1;
fi
local old_name="$1";
local new_name="$2";
local underscored_old_name=$(underscore $old_name);
local underscored_new_name=$(underscore $new_name);
find_and_replace_app_name() {
for file in $(find .); do
sed -i "" -e "s/$1/$2/g" $file 2>/dev/null;
done
}
find_and_replace_app_name $old_name $new_name;
find_and_replace_app_name $underscored_old_name $underscored_new_name;
return 0;
}
# Print this program's help and exit.
usage() {
script=$(basename $0)
(
echo "Usage:";
echo " $script AppName"
echo
echo "Make sure the name you provide is CamelCased"
) 1>&2
exit 1
}
if [ ${#@} -ne 1 ]; then
usage
fi
case $1 in
-h | --help)
usage
;;
*)
name="$1"
dir=$(underscore $name)
;;
esac
source="http://github.com/foca/blank_slate_rails_app"
echo "- Cloning the blank slate app"
git clone "$source" "$dir" &>/dev/null
cd "$dir" &>/dev/null
rm -rf .git
echo "- Updating the javascript libraries"
script/update_javascripts &>/dev/null
rm script/update_javascripts
echo "- Renaming the app to \"${name}\""
rename_rails_app "BlankSlate" "$name"
echo "- Initializing the git repository"
(git init; git add .; git commit -m "Initial commit") &>/dev/null
echo
echo "==> Everything is ready, get to work!"
@Florent2
Copy link

Thanks for this gist, very nice solution!

In my fork I've added some code to make sure a new secret is generated on every app creation, see the method change_rails_app_secret.

@foca
Copy link
Author

foca commented Jul 25, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment