Skip to content

Instantly share code, notes, and snippets.

@denvazh
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denvazh/9824119 to your computer and use it in GitHub Desktop.
Save denvazh/9824119 to your computer and use it in GitHub Desktop.
Create new Rails application without installing Rails itself

Why?

This is just a small script to bootstrap Rails new application generation process.

Reason, why I don't use rails new as it is, because I don't want Rails to be installed at all :) What I really wanted to have is a way to create Rails application without installing Rails system-wide.

Typical configuration is done with rbenv ( to manage and install ruby ) and bundler ( which is the only gem I need to install manually system-wide ).

If the command below works, then it is possible to use the script.

$: gem list --local | grep bundler
bundler (1.3.5)

How to install

Typically I have my own scripts at $HOME/bin:

$: curl --output $HOME/bin/create_rails_app "https://gist.githubusercontent.com/denvazh/9824119/raw/63e857a8eae056834f6a1194335ca794935d29d9/create_rails_app"

How to use

Create new Rails application in current directory

$: create_rails_app MyApp

Create new Rails application with specific version

$: create_rails_app MyApp 3.0.0
#!/usr/bin/env bash
usage(){
printf "Usage: $(basename $0) RailsAppName\n"
}
if [ -z $1 ]; then
printf "Please provide application name for Rails\n"
usage
exit 1
else
if [ -d $1 ]; then
printf "Directory '%s' already exist!\n" $1
usage
exit 1
fi
fi
PATTERN=
MATCH_PATTERN=$(printf '^# gem[ a-zA-Z0-9\\"]*$')
RAILS_GEM_PATTERN=$(printf 'gem %srails%s' "\\'" "\\'")
if [ ! -z $2 ]; then
RAILS_VERSION=$2
printf "Using Rails version %s\n" $RAILS_VERSION
RAILS_VERSION_PATTERN=$(printf ", %s'%s%s'" "\\" $RAILS_VERSION "\\")
PATTERN=$(printf 's/%s/%s%s/' "$MATCH_PATTERN" "$RAILS_GEM_PATTERN" "$RAILS_VERSION_PATTERN")
else
PATTERN=$(printf 's/%s/%s/' "$MATCH_PATTERN" "$RAILS_GEM_PATTERN")
fi
APP=$1
mkdir -p $APP
pushd $APP >/dev/null
bundle init
sed -i.bak "$PATTERN" Gemfile
rm Gemfile.bak
bundle install
bundle exec rails new . -f --skip-bundle
popd >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment