Browse Source

Moved functions out of the bootstrap script.

master
Drew Short 9 years ago
parent
commit
8175ff6cd1
  1. 2
      .gitignore
  2. 1
      Changelog
  3. 103
      scripts/bootstrap.sh
  4. 103
      scripts/tools.sh

2
.gitignore

@ -0,0 +1,2 @@
# Ignore all emacs temp files
*~

1
Changelog

@ -0,0 +1 @@
Adding Initial Change Log Entry

103
scripts/bootstrap.sh

@ -9,107 +9,10 @@
# so as to update the system cleanly to new versions of the dotfiles repository.
#
# Helper Functions
# Make a backup of the existing file if it exists
# Then link to our supplied dotfile
link() {
# Only make a backup if an existing file is there and is not a link
if [ -f "$1/$2" ] && [ ! -L "$1/$2" ]; then
cp "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
fi
ln -sf "$3" "$1/$2"
}
copy() {
# Only make a backup if an existing directory is there and is not a link
# Allow passing true as third param to do backups
backup=true
if [ ! -z "$4" ]; then
backup=$4
fi
overwrite=false
if [ ! -z "$5" ]; then
overwrite=$5
fi
if [ -f "$1/$2" ] || [ -L "$1/$2" ]; then
if $backup; then
mv "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
else
if $overwrite; then
rm -r "$1/$2"
echo "removed original $2"
fi
fi
fi
# Should probably only do this is the source is newer
# Remove the old and copy the new
if $overwrite; then
cp -r "$3" "$1/$2"
fi
}
link_dir() {
# Only make a backup if an existing directory is there and is not a link
if [ -d "$1/$2" ] && [ ! -L "$1/$2" ]; then
mv "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
fi
BASEDIR=$(dirname $0)
# Don't overwrite links
if [ ! -L "$1/$2" ]; then
echo "ln -sf $3 $1/$2"
ln -sf "$3" "$1/$2"
fi
}
copy_dir() {
# Only make a backup if an existing directory is there and is not a link
if [ -d "$1/$2" ] || [ -L "$1/$2" ]; then
# Allow passing true as third param to do backups
backup=true
if [ ! -z "$3" ]; then
backup=$4
fi
if $backup; then
mv "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
else
rm -r "$1/$2"
echo "removed original $2"
fi
fi
# Should probably only do this is the source is newer
# Remove the old and copy the new
cp -r "$3" "$1/$2"
}
# Escape the path so we can use it in sed
escape_path() {
local safe="$(echo $1 | sed 's/\//\\\//g')"
echo $safe
}
# Clone a git repository into a location
clone_git_repo() {
local target=$1
local repo=$2
if [ ! -d "$target" ]; then
mkdir -p "$target"
echo "Cloning [$repo] into [$target]"
git clone "$repo" "$target"
else
# Update the repo otherwise
echo "Updating [$repo]"
cur_dir=$PWD
cd "$target"
git checkout .
git pull
cd $cur_dir
fi
}
# Source the tools script
. ${BASEDIR}/tools.sh
set -x
set -e

103
scripts/tools.sh

@ -0,0 +1,103 @@
#!/usr/bin/env bash
# Tools for the scripts
# Make a backup of the existing file if it exists
# Then link to our supplied dotfile
link() {
# Only make a backup if an existing file is there and is not a link
if [ -f "$1/$2" ] && [ ! -L "$1/$2" ]; then
cp "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
fi
ln -sf "$3" "$1/$2"
}
copy() {
# Only make a backup if an existing directory is there and is not a link
# Allow passing true as third param to do backups
backup=true
if [ ! -z "$4" ]; then
backup=$4
fi
overwrite=false
if [ ! -z "$5" ]; then
overwrite=$5
fi
if [ -f "$1/$2" ] || [ -L "$1/$2" ]; then
if $backup; then
mv "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
else
if $overwrite; then
rm -r "$1/$2"
echo "removed original $2"
fi
fi
fi
# Should probably only do this is the source is newer
# Remove the old and copy the new
if $overwrite; then
cp -r "$3" "$1/$2"
fi
}
link_dir() {
# Only make a backup if an existing directory is there and is not a link
if [ -d "$1/$2" ] && [ ! -L "$1/$2" ]; then
mv "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
fi
# Don't overwrite links
if [ ! -L "$1/$2" ]; then
echo "ln -sf $3 $1/$2"
ln -sf "$3" "$1/$2"
fi
}
copy_dir() {
# Only make a backup if an existing directory is there and is not a link
if [ -d "$1/$2" ] || [ -L "$1/$2" ]; then
# Allow passing true as third param to do backups
backup=true
if [ ! -z "$3" ]; then
backup=$4
fi
if $backup; then
mv "$1/$2" "$1/$2.old"
echo "Backed up original $2 to $1/$2.old"
else
rm -r "$1/$2"
echo "removed original $2"
fi
fi
# Should probably only do this is the source is newer
# Remove the old and copy the new
cp -r "$3" "$1/$2"
}
# Escape the path so we can use it in sed
escape_path() {
local safe="$(echo $1 | sed 's/\//\\\//g')"
echo $safe
}
# Clone a git repository into a location
clone_git_repo() {
local target=$1
local repo=$2
if [ ! -d "$target" ]; then
mkdir -p "$target"
echo "Cloning [$repo] into [$target]"
git clone "$repo" "$target"
else
# Update the repo otherwise
echo "Updating [$repo]"
cur_dir=$PWD
cd "$target"
git checkout .
git pull
cd $cur_dir
fi
}
Loading…
Cancel
Save