Open up terminal and type in the command
which bash in order to see the location of the bash compiler. Next we are going to include what's called the bash "shabang." What that is is basically the call to the location of the BASH (Bourne-Again Shell) compiler. You see it at the top of every shell script, and it looks like
Now let's create a new file, save it where you want- save it to the Desktop, and title it whatever you want, say start-project.sh (note the shell extension). Now open up that file, and follow along with the script.
let's start with the "shabang,"
Next we send an
echomessage and an
printfmessage which allows prefomatted text, in our case the
\nfor new line.
echo "Hello, what sort of project do you want to start?" printf " 1) wp (WordPress) \n 2) ang (Angular) \n 3) angExp (Angular w/ Express) \n 4) exp (Express Seed) \n 5) exp-basic (Express basic server) \n 6) html \n:" read PROJECT_TYPE
We use the
read command to retrieve the user response and set it to a variable
PROJECT_TYPE which always needs to be in caps.
Let's get a few more details from the user
echo "Where would you like this project folder placed? (ie. sites, Documents)" read ROOT_PATH echo "Folder name?" read FOLDER_NAME echo "Ok I will create you a folder at" /Users/daviddavis/$ROOT_PATH/$FOLDER_NAME ##replace this folder structure with your folder structure
Here we've asked the user where to start a project, and what folder to create it in and record the response.
Now let's start creating the project
cd /Users/daviddavis/$ROOT_PATH/ #go to the directory mkdir $FOLDER_NAME #make the folder cd $FOLDER_NAME #go into the folder
We then navigate to the root path recorded as $ROOT_PATH. Note the $ in front of the variable which you have to use when utilizing the variable. We use the
cd command to traverse the directory. Then we use the
mkdir command to create a new folder with the recorded name, and then we go into the folder structure.
Now for the last bit of code for our script, I have a series of
seed or boilerplate projects that either I created or are out there on the web, and we will retrieve them from github or somewhere else on the web and place them into the user's newly created project folder. You should modify the types of projects and sources to fit your needs.
Retrieve seed projects out on the web.
case $PROJECT_TYPE in "wp"|"1") curl -O https://wordpress.org/latest.zip unzip latest.zip mv wordpress site rm latest.zip cd site rm -rf wp-content git init git pull https://github.com/david-j-davis/blank_wp_dystrick_theme.git atom . ;; "ang"|"2") git init git pull https://github.com/angular/angular-seed.git atom . ;; "angExp"|"3") git init git pull https://github.com/btford/angular-express-seed.git atom . ;; "exp"|"4") git init git pull https://github.com/meanie/express-seed.git atom . ;; "exp-basic"|"5") git init git pull https://github.com/david-j-davis/Express-Basic-Server.git atom . ;; "html"|"6") git init git pull https://github.com/david-j-davis/html5-project-boiler-pug-bootstrap-node-sass.git atom . ;; esac
If you are familiar with a switch statement then the above structure should make sense- if not look it up. For each PROJECT_TYPE choice that the user might have entered, we run a switch statement checking whether the user entered the name of the project type or (denoted by the "|" character) the project number. Then we either curl a boilerplate in or pull from github.
If you are a WordPress user you'll like the first option here where we curl in the latest WordPress build then move the folder structure and then I pull in a popular boilerplate theme. Lastly you see the
atom . command. If you use Sublime, Brackets, etc. you will want to replace that line of code with whatever command automates opening your editor. Essentially the 'atom .' opens up my Atom editor at that current location. Best of luck!
If you would like to see how to automate this script to run in the terminal with an alias command, I've posted how to do it here.