Welcome Guest, Not a member yet? Register   Sign In
Generate very basic CI project from the command line
#1

[eluser]AzizLight[/eluser]
Hi everybody,
I just started using CI and I wanted to have a way of generating a very basic CI project from the command line. What I mean by that is a lot simpler and minimalistic than it sounds: a fresh unzipped CI folder is copied to the current folder, renamed and then the user is given the choice to move the application folder outside the system folder. I created a very simple bash script that does exactly that.
Before you continue reading, please be warned that I litteraly learned bash programming while writting this script so the code is definitely rudimentary and perfectible. Aditonally, the code is quite poorly documented, I'm sorry about that. However it's enough for me.
I only tested this script in OS X 10.5 but I'm pretty sure it will work perfectly in any Unix-based/Linux Distribution that has bash installed and probably under Windows with Cygwin
Code:
#!/bin/bash
# This is where the CodeIgniter main folder is
ci_dir="/Users/aziz/Documents/Scripts/php/CodeIgniter_1.7.1"

# The current date used for the default project name if no project name is submited
date=`date +"%Y-%m-%d_%H-%M-%S"`

# Create the main project folder
if [ -z $1 ]
then
    read -p "Choose a name for your project: [Default: ci-$date]" project_name
    if [ -z $project_name ]
    then
        \cp -R $ci_dir ./ci-$date
        project_name=ci-$date
    else
        \cp -R $ci_dir ./$project_name
        project_name=$project_name
    fi
else
    let "folder_exists= 1"
    while [ $folder_exists -eq 1 ]
    do
        if [ -d $1 ]
        then
            echo 'This directory already exists'
            read -p "Choose a name for your project: [Default: ci-$date]" project_name
            if [ -z $project_name ]
            then
                let "folder_exists= 0"
                \cp -R $ci_dir ./ci-$date
                project_name=ci-$date
            else
                if [ -d $project_name ]
                then
                    let "folder_exists= 1"
                else
                    let "folder_exists= 0"
                    \cp -R $ci_dir ./$project_name
                    project_name=$project_name
                fi
            fi
        else
            let "folder_exists= 0"
            \cp -R $ci_dir $1
            project_name=$1
        fi
    done
fi

# Ask the user if he wants to move the application folder outside the system folder
dir=`pwd`
echo "Project folder created in $dir/"
read -p "Do you want to move the application? [Default: y]"$'\n'"[y/n] " -n 1 choice
if [ -z $choice ] || [ $choice = 'y' ]
then
    cd "$project_name"
    mv system/application application
    cd -
    echo 'application folder moved outside the system folder'
fi
echo -e "\nProject generated successfully in $dir/$project_name"

To use this script, copy the code in a blank file, name it ci (or whatever you want). Replace the path in line 3 with the path of the CI folder in you computer, the one that will be copied to the new location to create your new project, and make it exectable:
Code:
chmod a+x ci

then move the file to the folder of you choice and add that folder to the $PATH. I chose the ~/bin/ folder for the exemple:
Code:
mv ci ~/bin
To add that folder to the path, the easiest way is to open ~/.bash_profile or ~/.bashrc (under OS X it's .bash_profile, under Linux it's .bashrc - create the appropriate file if it doesn't exist) and add that line at the bottom of the file:
Code:
export PATH=/Users/aziz/bin:$PATH
Notice: if the file already exists and the $PATH is already exported, you can just append the folder path to the value of the path, ie:
Code:
export PATH=/opt/local/bin:/opt/local/sbin:/opt/subversion:/Users/aziz/bin:$PATH
Also don't forget to replace /Users/aziz/bin with the correct path..

I release this code to the public domain, do whatever you want with it Wink And feel free to criticize as much as you want Smile (just try not to be mean please)
Enjoy!




Theme © iAndrew 2016 - Forum software by © MyBB