Skip to content

Commit

Permalink
Merge pull request #39 from bow-swift/nefc_cached_dependencies
Browse files Browse the repository at this point in the history
Flag for compile using cached dependencies and project
  • Loading branch information
miguelangel-dev authored Apr 24, 2019
2 parents 7e56905 + e1895a6 commit 0b9a455
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 39 deletions.
14 changes: 12 additions & 2 deletions bin/nef
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ printHelpClean() {

printHelpCompileClean() {
echo ""
echo "${normal}nef ${green}${bold}$COMPILE${normal}${reset} | ${green}${bold}$CLEAN${normal}${reset} <path>"
echo "${normal}nef ${green}${bold}$COMPILE${normal}${reset} | ${green}${bold}$CLEAN${normal}${reset} <path> <options>"
echo ""
echo " ${required}${bold}<path>${reset}${normal} path to the folder where the project and playgrounds are located"
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}"
echo ""
}

Expand Down Expand Up @@ -135,6 +136,8 @@ jekyll() {
--bow-branch ) printHelpPlayground; exit 1 ;;
--podfile ) printHelpPlayground; exit 1 ;;

--use-cache) printHelpCompile; exit 1 ;;

$JEKYLL ) ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
Expand Down Expand Up @@ -172,6 +175,8 @@ playground() {
--bow-branch ) shift; branch=$1 ;;
--podfile ) shift; podfile=$1 ;;

--use-cache) printHelpCompile; exit 1 ;;

$JEKYLL ) printHelpJekyll; exit 1 ;;
$PLAYGROUND ) ;;
$COMPILE ) printHelpCompile; exit 1 ;;
Expand All @@ -198,6 +203,7 @@ playground() {
##
compile() {
projectFolder=""
flag=""

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

--use-cache) flag="--use-cache" ;;

$JEKYLL ) printHelpJekyll; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$COMPILE ) shift; projectFolder=$1 ;;
Expand All @@ -220,7 +228,7 @@ compile() {
done

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

--use-cache) ;;

$JEKYLL ) printHelpJekyll; exit 1 ;;
$PLAYGROUND ) printHelpPlayground; exit 1 ;;
$COMPILE ) printHelpCompile; exit 1 ;;
Expand Down
139 changes: 102 additions & 37 deletions bin/nefc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
COMPILE="compile"
CLEAN="clean"
DEPENDENCIES="install"
COMPILE_CACHED_DEPENDENCIES="--use-cache"

DERIVED_DATA_DIR="nef/DerivedData"

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

red=$(tput setaf 1)
green=$(tput setaf 2)
required=$(tput setaf 222)
optional=$(tput setaf 230)
reset=$(tput sgr0)


#: IN - Check Args

##
# printWrongArguments()
##
printWrongArguments() {
echo ""
echo "${bold}nefc ${normal}[${bold}$COMPILE ${normal}| ${bold}$DEPENDENCIES ${normal}| ${bold}$CLEAN${normal}]${bold} ${normal}<project>"
echo "${bold}nefc ${normal}[${bold}$COMPILE ${normal}| ${bold}$DEPENDENCIES ${normal}| ${bold}$CLEAN${normal}]${bold} ${normal}<project> <options>"
echo ""
echo " ${bold}$COMPILE${normal} compile playground's pages for the selected project"
echo " ${bold}$DEPENDENCIES${normal} builds dependencies in selected project"
echo " ${bold}$CLEAN${normal} clean builds in selected project"
echo ""
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}"
echo ""
}

##
Expand All @@ -43,18 +50,32 @@ printWrongConfig() {
# - Parameter `args`: list of arguments received from command line
##
checkArguments() {
# $0 and $1 in `args` are ref. to first and second parameter from command line
# $0 - command
# $1 - path
# $0 - `nefc`
# $1 - command
# $2 - path
# $3 - flag param

if [ "$#" -ne 2 ]; then printWrongArguments $0; exit 1; fi
local command="$1"
local flag="$3"
local isValidCommand=""

local config=("$COMPILE" "$CLEAN" "$DEPENDENCIES")
for e in "${config[@]}"; do [ $1 = $e ] && return 0; done
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then printWrongArguments $0; exit 1; fi

local validCommands=("$COMPILE" "$CLEAN" "$DEPENDENCIES")
for valid in "${validCommands[@]}"; do
if [ "$command" = "$valid" ]; then isValidCommand="$command"; fi
done

printWrongConfig; exit 1
if [[ "$isValidCommand" = "" ]]; then
printWrongConfig; exit 1
elif [[ "$command" = "$COMPILE" ]]; then
if [[ "$flag" != "$COMPILE_CACHED_DEPENDENCIES" ]] && [[ "$flag" != "" ]]; then
printWrongArguments $0; exit 1;
fi
fi
}


#: - Dependencies

##
Expand All @@ -70,23 +91,28 @@ dependencies() {
#: - Compile

##
# compile(String folder)
# compile(String folder, String flag)
# - Parameter `folder`: path to the project folder.
# - parameter `flag`: configuration for building.
##
compile() {
makeStructure "$1"
buildDependencies "$1"
buildProject "$1"
local flag="$2"

makeStructure "$1" "$flag"
buildDependencies "$1" "$flag"
buildProject "$1" "$flag"
copyFrameworks "$1"
compilePlaygroundPages "$1"
}

##
# buildProject(String folder) throws
# buildProject(String folder, String flag) throws
# - Parameter `folder`: path to the project folder.
# - parameter `flag`: configuration for building.
##
buildProject() {
cd "$1" # parameter `folder`
cd "$1" # parameter `folder`
local flag="$2" # parameter `flag`
local logPath="nef/log"

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

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

set +e
xcodebuild -workspace "$workspace" -sdk "$sdk" -scheme "$schemeName" -derivedDataPath "$DERIVED_DATA_DIR" -configuration Debug 1> "$log" 2>&1
installed=`grep "BUILD SUCCEEDED" "$log"`
set -e
if [[ $(shouldBuildWorkspace "$workspace" "$flag") = "1" ]]; then
set +e
xcodebuild -workspace "$workspace" -sdk "$sdk" -scheme "$schemeName" -derivedDataPath "$DERIVED_DATA_DIR" -configuration Debug 1> "$log" 2>&1
installed=`grep "BUILD SUCCEEDED" "$log"`
set -e
else
installed="OK!"
fi

if [ "${#installed}" -gt 0 ]; then
echo ""
echo ""
else
echo ""
echo "${bold}${red}error: ${reset}${bold}building $workspaceName${normal}"
cat "$log"
exit 1
echo ""
echo "${bold}${red}error: ${reset}${bold}building $workspaceName${normal}"
cat "$log"
exit 1
fi
done
}
Expand Down Expand Up @@ -180,13 +210,32 @@ isPlatfromIOSPlaygroundPage() {
}

##
# buildDependencies(String folder)
# shouldBuildWorkspace(String workspace, String flag)
# - Parameter `workspace`
# - parameter `flag`: configuration for building.
# - Return `shouldBuildWorkspace` 1 - yes (is not cached); 0 - no (cached)
##
shouldBuildWorkspace() {
local workspace="$1" # parameter `workspace`
local flag="$2" # parameter `flag`

local workspaceName=$(echo "$workspace" | rev | cut -d'/' -f 1 | rev | cut -d'.' -f 1)
local workspaceFwPath="nef/build/fw/$workspaceName.framework"

[ -d "$DERIVED_DATA_DIR/build" ] && [ -d "$workspaceFwPath" ] && [ "$flag" = "$COMPILE_CACHED_DEPENDENCIES" ] && echo 0;
echo 1
}

##
# buildDependencies(String folder, String flag)
# - Parameter `folder`: path to the project folder.
# - parameter `flag`: configuration for building.
##
buildDependencies() {
local path="$1" # parameter `folder`
local flag="$2" # parameter `flag`

buildPODS "$path"
buildPODS "$path" "$flag"
addPlaygroundReference "$path"
}

Expand All @@ -197,6 +246,7 @@ buildDependencies() {
buildPODS() {
cd "$1"

local flag="$2"
local podfile="Podfile"
local log="nef/log/pod-install.log"

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

set +e
pod install --repo-update 1> "$log" 2>&1
if [[ "$flag" = "$COMPILE_CACHED_DEPENDENCIES" ]]; then
pod install 1> "$log" 2>&1
else
pod install --repo-update 1> "$log" 2>&1
fi

installed=`grep "Pod installation complete" "$log"`
set -e

Expand Down Expand Up @@ -269,16 +324,21 @@ playgroundForProjectPath() {
}

##
# makeStructure(String folder)
# makeStructure(String folder, String flag)
# - Parameter `folder`: path to the project folder.
# - parameter `flag`: configuration for building.
##
makeStructure() {
set +e
local projectFolder=$1 # parameter `folder`
local projectFolder="$1" # parameter `folder`
local flag="$2" # parameter `flag`

cd "$projectFolder"

cleanStructure "$projectFolder"
if [[ "$flag" != "$COMPILE_CACHED_DEPENDENCIES" ]]; then
cleanStructure "$projectFolder"
fi

mkdir -p nef/build/fw
mkdir -p nef/build/output
mkdir -p nef/log
Expand Down Expand Up @@ -310,7 +370,7 @@ copyFrameworks() {
exit 1
fi

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

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

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

# A. macOS paltform
if [ "$platformIOS" -eq "0" ]; then
if [ "${#hasSourceFolderFiles}" -gt 0 ]; then
xcrun -k swiftc -D NOT_IN_PLAYGROUND -emit-module "$sources"/* -F "nef/build/fw" -o "$staticLibPath" 1> "$llog" 2>&1
xcrun -k swiftc -D NOT_IN_PLAYGROUND -static-executable "$staticLibPath" -F "nef/build/fw" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
xcrun -k swiftc -D NOT_IN_PLAYGROUND -emit-module "$sources"/* -F "nef/build/fw" -F "$macOSFwPath" -o "$staticLibPath" 1> "$llog" 2>&1
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
else
xcrun -k swiftc -D NOT_IN_PLAYGROUND -F "nef/build/fw" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
xcrun -k swiftc -D NOT_IN_PLAYGROUND -F "nef/build/fw" -F "$macOSFwPath" "$file" -o "nef/build/output/$playgroundName" 1> "$log" 2>&1
fi

# B. iOS platform
else
if [ "${#hasSourceFolderFiles}" -gt 0 ]; then
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
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
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
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
else
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
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
fi
fi

Expand All @@ -427,6 +489,7 @@ compilePlaygroundPage() {
exit 1
}


#: - Clean

##
Expand Down Expand Up @@ -463,6 +526,7 @@ cleanPODS() {
rm -rf ./*.xcworkspace 1>/dev/null 2>/dev/null
}


#: MAIN
set -e
checkArguments $@
Expand All @@ -475,7 +539,8 @@ else
fi

if [ $1 = "$COMPILE" ]; then
compile "$projectPath"
flag="$3"
compile "$projectPath" "$flag"
elif [ $1 = "$DEPENDENCIES" ]; then
dependencies "$projectPath"
else
Expand Down

0 comments on commit 0b9a455

Please sign in to comment.