-
Notifications
You must be signed in to change notification settings - Fork 47
/
build.sh
executable file
·143 lines (136 loc) · 5.45 KB
/
build.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
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
#!/usr/bin/env bash
# Script to build the waifu2x-mac project (more) easily
# Works with Xcode 10.1 and waifu2x-mac as of commit ca0088b (2018-11-17)
# Place this script at the root of the waifu2x-mac folder, then
# chmod +x build.sh; ./build.sh;
# Define variables for coloured text
BOLD=$(tput bold); LINE=$(tput smul); LOFF=$(tput rmul); RESET=$(tput sgr0)
RED=$(tput setaf 1); GREEN=$(tput setaf 2); YELLOW=$(tput setaf 3)
WARNING="${BOLD}${YELLOW}${LINE}Warning:${RESET}${BOLD}"
ERROR="${BOLD}${RED}${LINE}Error:${RESET}${BOLD}"
INFO="${BOLD}${GREEN}${LINE}Info:${RESET}${BOLD}"
# Change the working directory to the directory containing the script
cd $(cd -P -- "$(dirname -- "$0")" && pwd -P)
# Check if the script is in the same directory as the Xcode project
if [[ $(find . -name 'waifu2x-mac.xcodeproj' -d -maxdepth 1) ]]
then
# Reset sudo timestamp (to avoid automatic sudoing in the script)
sudo -K
# Clean the base directory of previous build attempts
UNTRACKED=$(git clean -dffnx 2> /dev/null) # dry run
if [[ ${UNTRACKED} ]]
then
echo -e "${WARNING} The ${PWD##*/} directory will be cleaned of all" \
"untracked files.${RESET}"
echo "${UNTRACKED}" # list of files to delete
read -n 1 -r -p "${BOLD}${LINE}Are you sure?${LOFF} (y/n) ${RESET}" YN
case ${YN} in
[Yy]* )
echo >&2
git clean -dffx # delete ALL untracked files and folders
;;
* ) # anything but Y cancels the operation
echo >&2
echo "${ERROR} Build cancelled.${RESET}" >&2
exit 1
;;
esac
fi
# Check to make sure Xcode.app is the active developer directory
# N.B.: Command Line Tools are NOT sufficient
ATTEMPT=0
until [[ $(xcrun xcode-select -p 2> /dev/null | grep "\.app") ]]
do # Try to find Xcode.app (or Xcode-beta.app)
((++ATTEMPT))
if [[ ${ATTEMPT} -eq 3 ]]
then
echo "${ERROR} Xcode couldn't be selected as the active developer" \
"directory.${RESET}" >&2
echo "${BOLD}Please download and install Xcode from" \
"${LINE}https://developer.apple.com/download/${RESET}" >&2
echo "${BOLD}If Xcode is installed, try the command:${RESET}" \
"sudo xcode-select -s /Applications/Xcode.app/Contents/Developer" >&2
exit 1 # Fails because Xcode.app couldn't be selected
fi
XCODEAPP=$(find /Applications -name 'Xcode*.app' -d -maxdepth 1 | head -n1)
if [[ ${XCODEAPP} ]] # Xcode installed but not selected
then # try to select Xcode
echo "${WARNING} Selecting Xcode for command line tools (requires" \
"admin privileges).${RESET}"
# Set Xcode.app as the active developer directory
sudo xcrun xcode-select -s ${XCODEAPP}/Contents/Developer 2> /dev/null
sudo xcrun xcodebuild -license accept 2> /dev/null
continue # recheck if Xcode selected
else # Xcode not installed
if ! [[ $(type mas 2> /dev/null) ]] # check for Mac App Store CLI
then
if ! [[ $(type brew 2> /dev/null) ]] # check for Homebrew pkg manager
then
echo "${WARNING} Installing Homebrew.${RESET}"
/usr/bin/ruby -e "$(curl -fsSL \
https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
echo "${WARNING} Installing Mac App Store CLI package manager.${RESET}"
brew install mas # Mac App Store CLI package manager
fi
XCODEMAS=$(mas search Xcode | \
sed -E "s/^ +([0-9]+) +Xcode +\(.+\)$/\1/" | grep -E "^[0-9]+$")
XCODESIZE=$(mas info $XCODEMAS | grep "Size:")
echo "${WARNING} Downloading Xcode. (${XCODESIZE})${RESET}"
read -n 1 -r -p "${BOLD}${LINE}Are you sure?${LOFF} (y/n) ${RESET}" YN
case ${YN} in
[Yy]* )
echo >&2
mas install ${XCODEMAS} # installs Xcode
;;
* ) # anything but Y cancels the operation
echo >&2
echo "${ERROR} Xcode installation cancelled.${RESET}" >&2
exit 1
;;
esac
continue # recheck if Xcode installed
fi
done
# Agreeing to the Xcode license
if [[ $(xcodebuild -help 2>&1 | grep "Agreeing") ]]
then
echo "${WARNING} Agreeing to the Xcode license (requires admin" \
"privileges).${RESET}"
echo "${BOLD}You can read the license at" \
"${LINE}https://www.apple.com/legal/sla/docs/xcode.pdf${LOFF}${RESET}"
sudo xcrun xcodebuild -license accept
fi
# Building with Xcode
echo "${BOLD}${LINE}Building...${RESET}"
xcodebuild clean build -quiet -scheme waifu2x-mac-app CODE_SIGN_IDENTITY="" \
CODE_SIGNING_REQUIRED=NO -derivedDataPath DerivedData > /dev/null
EXITCODE=$?
if [[ ${EXITCODE} -eq '0' ]] # success
then # move the build to a more convenient directory
mkdir build
mv ./DerivedData/Build/Products/Release/waifu2x-mac-app.app \
./build/waifu2x-mac-app.app
rm -rf ./DerivedData
BUILT=$(find build -name 'waifu2x-mac-app.app' -d -maxdepth 1)
if [[ ${BUILT} ]]
then
echo "${INFO} Build success.${RESET}"
echo "${BUILT}" # show where the built macOS app is
exit 0
else
echo "${ERROR} Build failed.${RESET}" >&2
git clean -dffx >&2 # delete ALL untracked files and folders
exit 1
fi
else
echo "${ERROR} Build failed.${RESET}" >&2
git clean -dffx >&2 # delete ALL untracked files and folders
exit ${EXITCODE}
fi
else # the script is not in the waifu2x-mac folder
echo "${ERROR}This script must be placed in the same directory as" \
"\"waifu2x-mac.xcodeproj\".${RESET}" >&2
exit 1
fi
exit 1