forked from steveworley/commit-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·165 lines (139 loc) · 4.86 KB
/
setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
#
# Pre-commit Tools for Acquia Cloud projects.
#
# Downloads and configures tools that help standardise developer workflow. This
# script should be added to the root directory of a project and should be
# committed to the project to allow developers to quickly get setup.
#
# @see https://github.com/steveworley/commit-tools
#
# CONFIGURAITON:
# -------------------------------------------
UPSTREAM=master
TASKS=(code-review.sh tests.sh)
PREFIX=PROJ # Used for commit-msg.
LENGTH=15 # Used for commit-msg.
# -------------------------------------------
# Determine the root directory of the git repository.
ROOT="$( cd "$( git rev-parse --show-cdup )" && pwd )"
# Test if the setup.lock file exists and abort.
if [ -f "$ROOT/setup.lock" ]; then
echo "Setup has been run"
exit 0
fi
# Fetch composer.json from a GIST.
function fetch_composer_json() {
echo "Fetching composer.json"
wget -O $1/composer.json https://raw.githubusercontent.com/steveworley/commit-tools/master/composer.json
}
# Update configuration when setup is run.
#
# First run of this program when command line arguments are passed in should
# update the configuration variables such that subsequent runs of setup for a
# project should not need to know the configurations for the project.
#
# @param $1 PATTERN
# sed regex compatible string / delimiters
# @param $2 REPLACE
# sed regex compatible string / delimiters
# @param $3 LINE
# Line number to be used to limit the replacement.
function update_config() {
file="$( cd "$( git rev-parse --show-cdup )" && pwd )/$( basename $0)"
sed -i '' -e "${3}s/$1/$2/g" "$file"
}
# Test to see if the directory structure matches the Acquia Cloud expectation.
if [ ! -d "$ROOT/docroot" ]; then
echo "Unable to locate Drupal. This needs to be run from an Acquia directory configuration."
exit 1
fi
# UPSTREAM has been passed into the program
if [ $1 ] && [ "$UPSTREAM" == "master" ]; then
UPSTREAM=$1
update_config "UPSTREAM=master" "UPSTREAM=$UPSTREAM" 15
fi
# LENGTH has been passed into the program
if [ $2 ] && [ "$PREFIX" == 'PROJ' ]; then
PREFIX=$2
update_config "PREFIX=PROJ" "PREFIX=$PREFIX" 17
fi
# LENGTH has been passed into the program
if [ $3 ] && [ "$LENGTH" == 15 ]; then
LENGTH=$3
update_config "LENGTH=15" "LENGTH=$LENGTH" 18
fi
# Test php is available (should be if terminal is opened with DD)
php -v > /dev/null 2>&1
PHP_IS_INSTALLED=$?
if [ $PHP_IS_INSTALLED -ne 0 ]; then
echo "Unable to find PHP. Please run from terminal with php on path"
exit 1
fi
# Test to see if composer.json has been downloaded.
if [ ! -f "$ROOT/composer.json" ] || [ ! -s "$ROOT/composer.json" ]; then
fetch_composer_json $ROOT
fi
# Update the gitignore.
IGNORE_VENDOR="vendor"
IGNORE_COMPOSER="composer.phar"
IGNORE_LOCK="setup.lock"
echo "" >> "$ROOT/.gitignore"
echo "$IGNORE_VENDOR" >> "$ROOT/.gitignore"
echo "$IGNORE_COMPOSER" >> "$ROOT/.gitignore"
echo "$IGNORE_LOCK" >> "$ROOT/.gitignore"
# Test if Composer is installed
if [ ! -f "$ROOT/composer.phar" ]; then
echo "Installing composer..."
curl -sS https://getcomposer.org/installer | php;
fi
# Test if Composer is installed
$ROOT/composer.phar -v > /dev/null 2>&1
COMPOSER_IS_INSTALLED=$?
if [ $COMPOSER_IS_INSTALLED -ne 0 ]; then
# Composer could not be installed... prompt to install manually and retry.
echo "Unable to install Composer. Make sure composer.phar is available from project root."
exit 1
fi
# Test to see if dependencies have been installed.
if [ ! -d "$ROOT/vendor" ]; then
echo "Installing dependencies..."
$ROOT/composer.phar install
# Register Drupal CS standards.
PHPCS="$ROOT/vendor/bin/phpcs"
$PHPCS --config-set installed_paths $ROOT/vendor/drupal/coder/coder_sniffer
fi
# Precommit hook.
PRECOMMIT=$ROOT/.git/hooks/pre-commit
if [ ! -f $PRECOMMIT ]; then
echo "Setting up git hooks..."
# Fetch the pre-commit bootstrap script and set permissions.
wget -O $PRECOMMIT https://raw.githubusercontent.com/steveworley/commit-tools/master/pre-commit
sed -i '' -e "s|UPSTREAM=master|UPSTREAM=$UPSTREAM|g" "$PRECOMMIT"
chmod +x $PRECOMMIT
fi
# Precommit tasks.
TASKDIR=$ROOT/.git/hooks/tasks
if [ ! -d $TASKDIR ]; then
mkdir $TASKDIR
# List of commands that we want to include as hooks to run for this project
GIT_URL=https://raw.githubusercontent.com/steveworley/commit-tools/master/tasks
for task in ${TASKS[@]}
do
wget -O "$TASKDIR/$task" "$GIT_URL/$task"
done
fi
# Add commit message validation.
COMMITMSG=$ROOT/.git/hooks/commit-msg
if [ ! -f $COMMITMSG ]; then
# Download the commit message hook.
wget -O $COMMITMSG https://raw.githubusercontent.com/steveworley/commit-tools/master/commit-msg
# Update with project settings.
sed -i '' -e "s|LENGTH=#replace#|LENGTH=$LENGTH|g" "$COMMITMSG"
sed -i '' -e "s|PREFIX=#replace#|PREFIX=$PREFIX|g" "$COMMITMSG"
chmod +x $COMMITMSG
fi
# Complete!
echo "Setup has finished."
touch $ROOT/setup.lock
exit 0