1
+ #! /usr/bin/env bash
2
+
3
+ { # this ensures the entire script is downloaded #
4
+
5
+ dbsync_install_dir () {
6
+ printf %s " ${HOME} /.dbsync"
7
+ }
8
+
9
+ dbsync_latest_version () {
10
+ echo " v1.0.0"
11
+ }
12
+
13
+ #
14
+ # Outputs the location to DB Sync depending on:
15
+ # * The availability of $GIT_SOURCE
16
+ #
17
+ dbsync_source () {
18
+ local DBSYNC_METHOD
19
+ DBSYNC_METHOD=" $1 "
20
+ local DBSYNC_SOURCE_URL
21
+ DBSYNC_SOURCE_URL=" https://github.com/clivewalkden/bash-magento2-db-sync.git"
22
+ echo " $DBSYNC_SOURCE_URL "
23
+ }
24
+
25
+ do_install () {
26
+ local INSTALL_DIR
27
+ INSTALL_DIR=" $( dbsync_install_dir) "
28
+
29
+ # Downloading to $INSTALL_DIR
30
+ mkdir -p " $INSTALL_DIR "
31
+ if [ -f " $INSTALL_DIR /db-sync.sh" ]; then
32
+ echo " => db-sync is already installed in $INSTALL_DIR , trying to update the script"
33
+ else
34
+ echo " => Downloading db-sync as script to '$INSTALL_DIR '"
35
+ fi
36
+
37
+ if [ -d " $INSTALL_DIR /.git" ]; then
38
+ echo " => db-sync is already installed in $INSTALL_DIR , trying to update using git"
39
+ command printf ' \r=> '
40
+ command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " fetch origin tag " $( dbsync_latest_version) " --depth=1 2> /dev/null || {
41
+ echo >&2 " Failed to update db-sync, run 'git fetch' in $INSTALL_DIR yourself."
42
+ exit 1
43
+ }
44
+ else
45
+ # Cloning to $INSTALL_DIR
46
+ echo " => Downloading db-sync from git to '$INSTALL_DIR '"
47
+ command printf ' \r=> '
48
+ mkdir -p " ${INSTALL_DIR} "
49
+ if [ " $( ls -A " ${INSTALL_DIR} " ) " ]; then
50
+ command git init " ${INSTALL_DIR} " || {
51
+ echo >&2 ' Failed to initialize db-sync repo. Please report this!'
52
+ exit 2
53
+ }
54
+ command git --git-dir=" ${INSTALL_DIR} /.git" remote add origin " $( dbsync_source) " 2> /dev/null \
55
+ || command git --git-dir=" ${INSTALL_DIR} /.git" remote set-url origin " $( dbsync_source) " || {
56
+ echo >&2 ' Failed to add remote "origin" (or set the URL). Please report this!'
57
+ exit 2
58
+ }
59
+ command git --git-dir=" ${INSTALL_DIR} /.git" fetch origin tag " $( dbsync_latest_version) " --depth=1 || {
60
+ echo >&2 ' Failed to fetch origin with tags. Please report this!'
61
+ exit 2
62
+ }
63
+ else
64
+ command git -c advice.detachedHead=false clone " $( dbsync_source) " -b " $( dbsync_latest_version) " --depth=1 " ${INSTALL_DIR} " || {
65
+ echo >&2 ' Failed to clone dbsync repo. Please report this!'
66
+ exit 2
67
+ }
68
+ fi
69
+ fi
70
+ command git -c advice.detachedHead=false --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " checkout -f --quiet " $( dbsync_latest_version) "
71
+ if [ -n " $( command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " show-ref refs/heads/master) " ]; then
72
+ if command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch --quiet 2> /dev/null; then
73
+ command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch --quiet -D master > /dev/null 2>&1
74
+ else
75
+ echo >&2 " Your version of git is out of date. Please update it!"
76
+ command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch -D master > /dev/null 2>&1
77
+ fi
78
+ fi
79
+
80
+ echo " => Compressing and cleaning up git repository"
81
+ if ! command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " reflog expire --expire=now --all; then
82
+ echo >&2 " Your version of git is out of date. Please update it!"
83
+ fi
84
+ if ! command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " gc --auto --aggressive --prune=now ; then
85
+ echo >&2 " Your version of git is out of date. Please update it!"
86
+ fi
87
+
88
+ local PROFILE_INSTALL_DIR
89
+ PROFILE_INSTALL_DIR=" $( dbsync_install_dir | command sed " s:^$HOME :\$ HOME:" ) "
90
+ SOURCE_STR=" \\ nexport DBSYNC_DIR=\" ${PROFILE_INSTALL_DIR} \"\\ nif [ -f \"\$ DBSYNC_DIR/db-sync.sh\" ]; then\\ n\t export PATH=\" $INSTALL_DIR :\$ PATH\"\\ nfi # This loads db-sync\\ n"
91
+ local DBSYNC_PROFILE
92
+ DBSYNC_PROFILE=" ${HOME} /.bashrc"
93
+
94
+ if ! command grep -qc ' /db-sync.sh' " ${DBSYNC_PROFILE} " ; then
95
+ echo " => Appending db-sync source string to ${DBSYNC_PROFILE} "
96
+ command printf " ${SOURCE_STR} " >> " ${DBSYNC_PROFILE} "
97
+ else
98
+ echo " => db-sync source string already in ${DBSYNC_PROFILE} "
99
+ fi
100
+
101
+ \. " ${DBSYNC_PROFILE} "
102
+
103
+ dbsync_reset
104
+
105
+ echo " => Close and reopen your terminal to start using db-sync.sh"
106
+ }
107
+
108
+ #
109
+ # Unsets the various functions defined
110
+ # during the execution of the install script
111
+ #
112
+ dbsync_reset () {
113
+ unset -f dbsync_install_dir dbsync_latest_version dbsync_source do_install
114
+ }
115
+
116
+ do_install
117
+
118
+ } # this ensures the entire script is downloaded #
0 commit comments