]> gitweb.fluxo.info Git - scripts.git/commitdiff
Sandbox: more modular design
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 20 Jun 2015 14:46:26 +0000 (11:46 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 20 Jun 2015 14:46:26 +0000 (11:46 -0300)
TODO.md
sandbox

diff --git a/TODO.md b/TODO.md
index dd245e4d88fd6d3d674e98814316a19268272b14..9c779a26313e66bfca92687cf3dc6814e6c7080c 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -2,6 +2,7 @@ TODO
 ====
 
 * Better sandbox template
+  * Split `git init` from `git flow`.
   * Should work for more use cases.
   * Should be indepotent.
   * Asking whether to setup:
diff --git a/sandbox b/sandbox
index 88f8fca2d960b89e5ae32809fb0b720fa4cd87a8..bfb00504c79f5efff5729143d2314dd747094c5c 100755 (executable)
--- a/sandbox
+++ b/sandbox
@@ -5,22 +5,48 @@
 
 # Parameters
 BASENAME="`basename $0`"
-CODE="$HOME/code"
 PROJECT="$1"
 REPO="$2"
 BOOTSTRAP="git://git.sarava.org/puppet-bootstrap.git"
 TEMPLATES="git://git.sarava.org/templates.git"
 
+# Read a parameter from user
+function sandbox_ask {
+  local input
+  local function="$1"
+  local default="n"
+  shift 2
+
+  read -rep "Setup $function? (defaults to $default): " input
+
+  if [ "$input" == "y" ]; then
+    sandbox_$function
+  fi
+}
+
+# Checkout to develop branch if available
+function sandbox_checkout_develop {
+  (
+    cd $PROJECT
+
+    if git branch --list develop | grep -q develop; then
+      git checkout develop
+    fi
+  )
+}
+
 # Initialize project
 function sandbox_init {
-  echo "Initializing $PROJECT..."
-  mkdir -p $CODE/$PROJECT
+  if [ ! -d "$PROJECT" ]; then
+    echo "Initializing $PROJECT..."
+    mkdir -p $PROJECT
+  fi
 }
 
 # Git integration
 function sandbox_git {
   (
-    cd $CODE/$PROJECT
+    cd $PROJECT
     touch .gitignore
 
     echo "$PROJECT"                                     > README.md
@@ -42,13 +68,22 @@ function sandbox_git {
       echo "Installing hooks..."
       git hooks --install
     fi
+  )
+}
 
-    git branch develop
+# Setup git-flow integration
+function sandbox_git_flow {
+  (
+    cd $PROJECT
 
-    if [ -e "/usr/lib/git-core/git-flow" ]; then
-      echo ""
-      echo "Setting up git-flow..."
-      git flow init -d
+    if ! git branch --list develop | grep -q develop; then
+      git branch develop
+
+      if [ -e "/usr/lib/git-core/git-flow" ]; then
+        echo ""
+        echo "Setting up git-flow..."
+        git flow init -d
+      fi
     fi
   )
 }
@@ -61,12 +96,12 @@ function sandbox_ikiwiki {
     else
       echo ""
       echo "Setting up ikiwiki integration..."
-      cd $CODE/$PROJECT
-      git checkout develop
+      cd $PROJECT
+      sandbox_checkout_develop
 
       if [ ! -e ".gitignore" ]; then
         cp $HOME/file/templates/ikiwiki/.gitignore .
-      elif ! grep -q -f $HOME/file/templates/ikiwiki/.gitignore; then
+      elif ! grep -q -f $HOME/file/templates/ikiwiki/.gitignore .gitignore; then
         cat $HOME/file/templates/ikiwiki/.gitignore >> .gitignore
       fi
 
@@ -81,11 +116,11 @@ function sandbox_ikiwiki {
       fi
 
       if [ ! -d "templates" ]; then
-        cp r $HOME/file/templates/ikiwiki/templates .
+        cp -r $HOME/file/templates/ikiwiki/templates .
       fi
 
       if [ ! -d "bootstrap" ]; then
-        cp r $HOME/file/bootstrap/ikiwiki/bootstrap .
+        cp -r $HOME/file/templates/ikiwiki/bootstrap .
       fi
 
       git add .
@@ -99,13 +134,13 @@ function sandbox_vagrant {
   (
     echo ""
     echo "Setting up vagrant integration..."
-    cd $CODE/$PROJECT
-    git checkout develop
+    cd $PROJECT
+    sandbox_checkout_develop
     echo '.vagrant' >> .gitignore
     git commit -a -m "Adds vagrant support"
 
     # Use the best approach
-    #git clone $BOOSTRAP $CODE/$PROJECT/puppet
+    #git clone $BOOSTRAP $PROJECT/puppet
     #git submodule add $BOOSTRAP puppet
     git remote add puppet $BOOTSTRAP
     git subtree add --prefix puppet $BOOTSTRAP master --squash
@@ -120,13 +155,14 @@ fi
 
 # Clone or initialize
 if [ ! -z "$REPO" ]; then
-  git clone $URL $CODE/$PROJECT
+  git clone $URL $PROJECT
 else
   sandbox_init
-  sandbox_git
-  sandbox_ikiwiki
-  sandbox_vagrant
+  sandbox_ask git
+  sandbox_ask git_flow
+  sandbox_ask ikiwiki
+  sandbox_ask vagrant
 fi
 
 # Teardown
-echo "Welcome to your new project :)"
+echo "Done sandboxing :)"