Skip to content

Commit 0b9a455

Browse files
Merge pull request #39 from bow-swift/nefc_cached_dependencies
Flag for compile using cached dependencies and project
2 parents 7e56905 + e1895a6 commit 0b9a455

File tree

2 files changed

+114
-39
lines changed

2 files changed

+114
-39
lines changed

bin/nef

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ printHelpClean() {
7575

7676
printHelpCompileClean() {
7777
echo ""
78-
echo "${normal}nef ${green}${bold}$COMPILE${normal}${reset} | ${green}${bold}$CLEAN${normal}${reset} <path>"
78+
echo "${normal}nef ${green}${bold}$COMPILE${normal}${reset} | ${green}${bold}$CLEAN${normal}${reset} <path> <options>"
7979
echo ""
8080
echo " ${required}${bold}<path>${reset}${normal} path to the folder where the project and playgrounds are located"
81+
echo " ${optional}${bold}--use-cache${reset}${normal} is an option for 'compile' command. Use cached dependencies if it is possible. In another case, it will download them ${optional}[optional]${reset}"
8182
echo ""
8283
}
8384

@@ -135,6 +136,8 @@ jekyll() {
135136
--bow-branch ) printHelpPlayground; exit 1 ;;
136137
--podfile ) printHelpPlayground; exit 1 ;;
137138

139+
--use-cache) printHelpCompile; exit 1 ;;
140+
138141
$JEKYLL ) ;;
139142
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
140143
$COMPILE ) printHelpCompile; exit 1 ;;
@@ -172,6 +175,8 @@ playground() {
172175
--bow-branch ) shift; branch=$1 ;;
173176
--podfile ) shift; podfile=$1 ;;
174177

178+
--use-cache) printHelpCompile; exit 1 ;;
179+
175180
$JEKYLL ) printHelpJekyll; exit 1 ;;
176181
$PLAYGROUND ) ;;
177182
$COMPILE ) printHelpCompile; exit 1 ;;
@@ -198,6 +203,7 @@ playground() {
198203
##
199204
compile() {
200205
projectFolder=""
206+
flag=""
201207

202208
while [ "$1" != "" ]; do
203209
case $1 in
@@ -210,6 +216,8 @@ compile() {
210216
--bow-branch ) printHelpPlayground; exit 1 ;;
211217
--podfile ) printHelpPlayground; exit 1 ;;
212218

219+
--use-cache) flag="--use-cache" ;;
220+
213221
$JEKYLL ) printHelpJekyll; exit 1 ;;
214222
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
215223
$COMPILE ) shift; projectFolder=$1 ;;
@@ -220,7 +228,7 @@ compile() {
220228
done
221229

222230
if [ "${#projectFolder}" -gt 0 ]; then
223-
nefc compile "$projectFolder"
231+
nefc compile "$projectFolder" "$flag"
224232
else
225233
printHelpCompile; echo "${bold}[!] ${normal}${red}error:${reset} command format."; exit 1
226234
fi
@@ -246,6 +254,8 @@ clean() {
246254
--bow-branch ) printHelpPlayground; exit 1 ;;
247255
--podfile ) printHelpPlayground; exit 1 ;;
248256

257+
--use-cache) ;;
258+
249259
$JEKYLL ) printHelpJekyll; exit 1 ;;
250260
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
251261
$COMPILE ) printHelpCompile; exit 1 ;;

bin/nefc

Lines changed: 102 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
COMPILE="compile"
55
CLEAN="clean"
66
DEPENDENCIES="install"
7+
COMPILE_CACHED_DEPENDENCIES="--use-cache"
8+
79
DERIVED_DATA_DIR="nef/DerivedData"
810

911
#: terminal setup
@@ -12,21 +14,26 @@ normal=$(tput sgr0)
1214

1315
red=$(tput setaf 1)
1416
green=$(tput setaf 2)
17+
required=$(tput setaf 222)
18+
optional=$(tput setaf 230)
1519
reset=$(tput sgr0)
1620

21+
1722
#: IN - Check Args
1823

1924
##
2025
# printWrongArguments()
2126
##
2227
printWrongArguments() {
2328
echo ""
24-
echo "${bold}nefc ${normal}[${bold}$COMPILE ${normal}| ${bold}$DEPENDENCIES ${normal}| ${bold}$CLEAN${normal}]${bold} ${normal}<project>"
29+
echo "${bold}nefc ${normal}[${bold}$COMPILE ${normal}| ${bold}$DEPENDENCIES ${normal}| ${bold}$CLEAN${normal}]${bold} ${normal}<project> <options>"
2530
echo ""
2631
echo " ${bold}$COMPILE${normal} compile playground's pages for the selected project"
2732
echo " ${bold}$DEPENDENCIES${normal} builds dependencies in selected project"
2833
echo " ${bold}$CLEAN${normal} clean builds in selected project"
2934
echo ""
35+
echo " ${optional}${bold}$COMPILE_CACHED_DEPENDENCIES${reset}${normal} is an option for '$COMPILE'. Use cached dependencies if it is possible. In another case, it will download and install them ${optional}[optional]${reset}"
36+
echo ""
3037
}
3138

3239
##
@@ -43,18 +50,32 @@ printWrongConfig() {
4350
# - Parameter `args`: list of arguments received from command line
4451
##
4552
checkArguments() {
46-
# $0 and $1 in `args` are ref. to first and second parameter from command line
47-
# $0 - command
48-
# $1 - path
53+
# $0 - `nefc`
54+
# $1 - command
55+
# $2 - path
56+
# $3 - flag param
4957

50-
if [ "$#" -ne 2 ]; then printWrongArguments $0; exit 1; fi
58+
local command="$1"
59+
local flag="$3"
60+
local isValidCommand=""
5161

52-
local config=("$COMPILE" "$CLEAN" "$DEPENDENCIES")
53-
for e in "${config[@]}"; do [ $1 = $e ] && return 0; done
62+
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then printWrongArguments $0; exit 1; fi
63+
64+
local validCommands=("$COMPILE" "$CLEAN" "$DEPENDENCIES")
65+
for valid in "${validCommands[@]}"; do
66+
if [ "$command" = "$valid" ]; then isValidCommand="$command"; fi
67+
done
5468

55-
printWrongConfig; exit 1
69+
if [[ "$isValidCommand" = "" ]]; then
70+
printWrongConfig; exit 1
71+
elif [[ "$command" = "$COMPILE" ]]; then
72+
if [[ "$flag" != "$COMPILE_CACHED_DEPENDENCIES" ]] && [[ "$flag" != "" ]]; then
73+
printWrongArguments $0; exit 1;
74+
fi
75+
fi
5676
}
5777

78+
5879
#: - Dependencies
5980

6081
##
@@ -70,23 +91,28 @@ dependencies() {
7091
#: - Compile
7192

7293
##
73-
# compile(String folder)
94+
# compile(String folder, String flag)
7495
# - Parameter `folder`: path to the project folder.
96+
# - parameter `flag`: configuration for building.
7597
##
7698
compile() {
77-
makeStructure "$1"
78-
buildDependencies "$1"
79-
buildProject "$1"
99+
local flag="$2"
100+
101+
makeStructure "$1" "$flag"
102+
buildDependencies "$1" "$flag"
103+
buildProject "$1" "$flag"
80104
copyFrameworks "$1"
81105
compilePlaygroundPages "$1"
82106
}
83107

84108
##
85-
# buildProject(String folder) throws
109+
# buildProject(String folder, String flag) throws
86110
# - Parameter `folder`: path to the project folder.
111+
# - parameter `flag`: configuration for building.
87112
##
88113
buildProject() {
89-
cd "$1" # parameter `folder`
114+
cd "$1" # parameter `folder`
115+
local flag="$2" # parameter `flag`
90116
local logPath="nef/log"
91117

92118
find . -name '*.pbxproj' -print0 | while IFS= read -r -d $'\0' project; do
@@ -105,18 +131,22 @@ buildProject() {
105131

106132
echo -ne "${reset}Building ${green}$workspaceName${normal} ($schemeName) ..."
107133

108-
set +e
109-
xcodebuild -workspace "$workspace" -sdk "$sdk" -scheme "$schemeName" -derivedDataPath "$DERIVED_DATA_DIR" -configuration Debug 1> "$log" 2>&1
110-
installed=`grep "BUILD SUCCEEDED" "$log"`
111-
set -e
134+
if [[ $(shouldBuildWorkspace "$workspace" "$flag") = "1" ]]; then
135+
set +e
136+
xcodebuild -workspace "$workspace" -sdk "$sdk" -scheme "$schemeName" -derivedDataPath "$DERIVED_DATA_DIR" -configuration Debug 1> "$log" 2>&1
137+
installed=`grep "BUILD SUCCEEDED" "$log"`
138+
set -e
139+
else
140+
installed="OK!"
141+
fi
112142

113143
if [ "${#installed}" -gt 0 ]; then
114-
echo ""
144+
echo ""
115145
else
116-
echo ""
117-
echo "${bold}${red}error: ${reset}${bold}building $workspaceName${normal}"
118-
cat "$log"
119-
exit 1
146+
echo ""
147+
echo "${bold}${red}error: ${reset}${bold}building $workspaceName${normal}"
148+
cat "$log"
149+
exit 1
120150
fi
121151
done
122152
}
@@ -180,13 +210,32 @@ isPlatfromIOSPlaygroundPage() {
180210
}
181211

182212
##
183-
# buildDependencies(String folder)
213+
# shouldBuildWorkspace(String workspace, String flag)
214+
# - Parameter `workspace`
215+
# - parameter `flag`: configuration for building.
216+
# - Return `shouldBuildWorkspace` 1 - yes (is not cached); 0 - no (cached)
217+
##
218+
shouldBuildWorkspace() {
219+
local workspace="$1" # parameter `workspace`
220+
local flag="$2" # parameter `flag`
221+
222+
local workspaceName=$(echo "$workspace" | rev | cut -d'/' -f 1 | rev | cut -d'.' -f 1)
223+
local workspaceFwPath="nef/build/fw/$workspaceName.framework"
224+
225+
[ -d "$DERIVED_DATA_DIR/build" ] && [ -d "$workspaceFwPath" ] && [ "$flag" = "$COMPILE_CACHED_DEPENDENCIES" ] && echo 0;
226+
echo 1
227+
}
228+
229+
##
230+
# buildDependencies(String folder, String flag)
184231
# - Parameter `folder`: path to the project folder.
232+
# - parameter `flag`: configuration for building.
185233
##
186234
buildDependencies() {
187235
local path="$1" # parameter `folder`
236+
local flag="$2" # parameter `flag`
188237

189-
buildPODS "$path"
238+
buildPODS "$path" "$flag"
190239
addPlaygroundReference "$path"
191240
}
192241

@@ -197,6 +246,7 @@ buildDependencies() {
197246
buildPODS() {
198247
cd "$1"
199248

249+
local flag="$2"
200250
local podfile="Podfile"
201251
local log="nef/log/pod-install.log"
202252

@@ -207,7 +257,12 @@ buildPODS() {
207257
cd "$path"
208258

209259
set +e
210-
pod install --repo-update 1> "$log" 2>&1
260+
if [[ "$flag" = "$COMPILE_CACHED_DEPENDENCIES" ]]; then
261+
pod install 1> "$log" 2>&1
262+
else
263+
pod install --repo-update 1> "$log" 2>&1
264+
fi
265+
211266
installed=`grep "Pod installation complete" "$log"`
212267
set -e
213268

@@ -269,16 +324,21 @@ playgroundForProjectPath() {
269324
}
270325

271326
##
272-
# makeStructure(String folder)
327+
# makeStructure(String folder, String flag)
273328
# - Parameter `folder`: path to the project folder.
329+
# - parameter `flag`: configuration for building.
274330
##
275331
makeStructure() {
276332
set +e
277-
local projectFolder=$1 # parameter `folder`
333+
local projectFolder="$1" # parameter `folder`
334+
local flag="$2" # parameter `flag`
278335

279336
cd "$projectFolder"
280337

281-
cleanStructure "$projectFolder"
338+
if [[ "$flag" != "$COMPILE_CACHED_DEPENDENCIES" ]]; then
339+
cleanStructure "$projectFolder"
340+
fi
341+
282342
mkdir -p nef/build/fw
283343
mkdir -p nef/build/output
284344
mkdir -p nef/log
@@ -310,7 +370,7 @@ copyFrameworks() {
310370
exit 1
311371
fi
312372

313-
cp -a $(find "$DERIVED_DATA_DIR/build" -name '*.framework') nef/build/fw
373+
cp -a $(find "$DERIVED_DATA_DIR/build" -name '*.framework') nef/build/fw 2>/dev/null
314374
echo "Copy ${green}frameworks${reset}"
315375
}
316376

@@ -396,26 +456,28 @@ compilePlaygroundPage() {
396456
local sources="$playgroundPage/../../Sources"
397457
local staticLib="$playgroundName"$(date '+_%H_%M_%S')
398458
local staticLibPath="nef/build/fw/$staticLib"
459+
local iOSFwPath=`xcode-select -p`"/Platforms/iPhoneOS.platform/Developer/Library/Frameworks"
460+
local macOSFwPath=`xcode-select -p`"/Platforms/MacOSX.platform/Developer/Library/Frameworks"
399461

400462
platformIOS=$(isPlatfromIOSPlaygroundPage "$playgroundPage")
401463
hasSourceFolderFiles=$(ls "$sources" 2> /dev/null)
402464

403465
# A. macOS paltform
404466
if [ "$platformIOS" -eq "0" ]; then
405467
if [ "${#hasSourceFolderFiles}" -gt 0 ]; then
406-
xcrun -k swiftc -D NOT_IN_PLAYGROUND -emit-module "$sources"/* -F "nef/build/fw" -o "$staticLibPath" 1> "$llog" 2>&1
407-
xcrun -k swiftc -D NOT_IN_PLAYGROUND -static-executable "$staticLibPath" -F "nef/build/fw" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
468+
xcrun -k swiftc -D NOT_IN_PLAYGROUND -emit-module "$sources"/* -F "nef/build/fw" -F "$macOSFwPath" -o "$staticLibPath" 1> "$llog" 2>&1
469+
xcrun -k swiftc -D NOT_IN_PLAYGROUND -static-executable "$staticLibPath" -F "nef/build/fw" -F "$macOSFwPath" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
408470
else
409-
xcrun -k swiftc -D NOT_IN_PLAYGROUND -F "nef/build/fw" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
471+
xcrun -k swiftc -D NOT_IN_PLAYGROUND -F "nef/build/fw" -F "$macOSFwPath" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
410472
fi
411473

412474
# B. iOS platform
413475
else
414476
if [ "${#hasSourceFolderFiles}" -gt 0 ]; then
415-
xcrun -k -sdk "iphonesimulator" swiftc -D NOT_IN_PLAYGROUND -target "x86_64-apple-ios12.1-simulator" -emit-module "$sources"/* -F "nef/build/fw" -o "$staticLibPath" 1> "$llog" 2>&1
416-
xcrun -k -sdk "iphonesimulator" swiftc -D NOT_IN_PLAYGROUND -target "x86_64-apple-ios12.1-simulator" -static-executable "$staticLibPath" -F "nef/build/fw" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
477+
xcrun -k -sdk "iphonesimulator" swiftc -D NOT_IN_PLAYGROUND -target "x86_64-apple-ios12.1-simulator" -emit-module "$sources"/* -F "nef/build/fw" -F "$iOSFwPath" -o "$staticLibPath" 1> "$llog" 2>&1
478+
xcrun -k -sdk "iphonesimulator" swiftc -D NOT_IN_PLAYGROUND -target "x86_64-apple-ios12.1-simulator" -static-executable "$staticLibPath" -F "nef/build/fw" -F "$iOSFwPath" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
417479
else
418-
xcrun -k -sdk "iphonesimulator" swiftc -D NOT_IN_PLAYGROUND -target "x86_64-apple-ios12.1-simulator" -F "nef/build/fw" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
480+
xcrun -k -sdk "iphonesimulator" swiftc -D NOT_IN_PLAYGROUND -target "x86_64-apple-ios12.1-simulator" -F "nef/build/fw" -F "$iOSFwPath" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
419481
fi
420482
fi
421483

@@ -427,6 +489,7 @@ compilePlaygroundPage() {
427489
exit 1
428490
}
429491

492+
430493
#: - Clean
431494

432495
##
@@ -463,6 +526,7 @@ cleanPODS() {
463526
rm -rf ./*.xcworkspace 1>/dev/null 2>/dev/null
464527
}
465528

529+
466530
#: MAIN
467531
set -e
468532
checkArguments $@
@@ -475,7 +539,8 @@ else
475539
fi
476540

477541
if [ $1 = "$COMPILE" ]; then
478-
compile "$projectPath"
542+
flag="$3"
543+
compile "$projectPath" "$flag"
479544
elif [ $1 = "$DEPENDENCIES" ]; then
480545
dependencies "$projectPath"
481546
else

0 commit comments

Comments
 (0)