-
Notifications
You must be signed in to change notification settings - Fork 5
/
update.sh
executable file
·98 lines (79 loc) · 4.04 KB
/
update.sh
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
#!/bin/bash
{ # Braces protect against edits to this file whilst it is executing.
# Make sure that git is installed.
if ! command -v git &> /dev/null; then
echo "The git command could not be found."
echo " "
echo "Please install git to retrieve new updates of The Misinformation Game."
echo "Installers for git can be found at https://git-scm.com/downloads."
echo " "
exit
fi
# This update is destructive. Make sure the user knows that.
echo "=================================================================="
echo "Do you wish to lose all your local changes other than the config"
echo "files in ./config, so that you can retrieve new updates?"
echo "=================================================================="
echo " "
echo "WARNING: If you have made changes to the source code other than to"
echo "edit the files in ./config, then this command should not be used."
echo "It will delete all of your local changes as it force updates to the"
echo "code in the MisinformationGame repository on GitHub. If you have"
echo "made code changes, then you should use git manually instead."
echo " "
echo "Enter YES to confirm, or NO to cancel:"
printf "> "
read -r CONFIRMATION || exit 1
echo " "
if [ "$CONFIRMATION" != "YES" ]; then
echo "Cancelled update."
exit 1
fi
# If the current directory isn't a git repository, make it one.
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [ "$inside_git_repo" ]; then
echo "Source code is already a git repository."
else
echo "Converting your source code into a git repository..."
# Initialise this directory as a git repository.
git init || { echo "Unable to initialise this directory as a git repository!" ; exit 1; }
git checkout -b main || { echo "Unable to create main branch!" ; exit 1; }
git add --all || { echo "Unable to add initial contents of repository!" ; exit 1; }
git commit -m "Backup repository before setting up git" || { echo "Unable to backup repository!" ; exit 1; }
git remote add origin https://github.com/TheMisinformationGame/MisinformationGame || { echo "Unable to link this repository to The Misinformation Game's repository on GitHub!" ; exit 1; }
# Backup the current state of the repository.
git checkout -b git-setup-backup || { echo "Unable to backup repository!" ; exit 1; }
fi
# Create a temporary directory to save config files into.
if ! tmp="$(mktemp -d)"; then
echo "Unable to create temporary directory for maintaining config files."
exit 1
fi
# Copy the files we want to save into the temporary directory.
cp -f ./config/firebase-config.js "$tmp/firebase-config.js" || { echo "Unable to save your Firebase config from ./config/firebase-config.js!" ; exit 1; }
HAS_DEV_CONFIG=$( test -f ./config/development-config.js ; echo $? )
if (( "$HAS_DEV_CONFIG" == 0 )); then
cp -f ./config/development-config.js "$tmp/development-config.js" || { echo "Unable to save your development config from ./config/development-config.js!" ; exit 1; }
fi
# Make sure we are in the main branch.
git checkout main || { echo "Unable to checkout the main branch!" ; exit 1; }
# Fetch the latest version of the repository.
git fetch origin main || { echo "Unable to fetch new updates!" ; exit 1; }
# Reset to the latest version of the repository.
git reset --hard origin/main || { echo "Unable to apply updates to the git repository!" ; exit 1; }
# Copy back in the Firebase config.
cp -f "$tmp/firebase-config.js" ./config/firebase-config.js || { echo "Unable to restore your Firebase config to ./config/firebase-config.js!" ; exit 1; }
if (( "$HAS_DEV_CONFIG" == 0 )); then
cp -f "$tmp/development-config.js" ./config/development-config.js || { echo "Unable to restore your development config to ./config/development-config.js!" ; exit 1; }
fi
# Success!
echo " "
echo "=============================="
echo "Update completed successfully!"
echo "=============================="
echo " "
echo "Run ./deploy.sh again to update your instance of The Misinformation Game."
echo " "
# This exit and the braces protect against edits to this file while it is running.
exit $?
}