makedist-web.sh (8430B)
1 #!/bin/bash 2 3 # makedist-web.sh builds a Web version of an iPlug2 project using emscripten 4 # it copies a template folder from the iPlug2 tree and does a find and replace on various JavaScript and HTML files 5 # arguments: 6 # 1st argument : either "on", "off" or "ws" - this specifies whether $EMRUN is used to launch a server and browser after compilation. "ws" builds the project in websocket mode, without the WAM stuff 7 # 2nd argument : site origin - 8 # 3rd argument : browser - either "chrome", "safari", "firefox" - if you want to launch a browser other than chrome, you must specify the correct origin for argument #2 9 10 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 11 IPLUG2_ROOT=../../iPlug2 12 PROJECT_ROOT=$SCRIPT_DIR/.. 13 IPLUG2_ROOT=$SCRIPT_DIR/$IPLUG2_ROOT 14 FILE_PACKAGER=$EMSDK/upstream/emscripten/tools/file_packager.py 15 EMRUN="python3 ${IPLUG2_ROOT}/Scripts/emrun/emrun.py" 16 17 PROJECT_NAME=TemplateProject 18 BUILD_DSP=1 19 BUILD_EDITOR=1 20 WEBSOCKET_MODE=0 21 EMRUN_BROWSER=chrome 22 LAUNCH_EMRUN=1 23 EMRUN_SERVER=1 24 EMRUN_SERVER_PORT=8001 25 EMRUN_CONTAINER=0 26 SITE_ORIGIN="/" 27 28 cd $PROJECT_ROOT 29 30 if [ "$1" = "ws" ]; then 31 LAUNCH_EMRUN=0 32 BUILD_DSP=0 33 WEBSOCKET_MODE=1 34 elif [ "$1" = "off" ]; then 35 LAUNCH_EMRUN=0 36 elif [ "$1" = "container" ]; then 37 EMRUN_CONTAINER=1 38 fi 39 40 if [ "$#" -eq 2 ]; then 41 SITE_ORIGIN=${2} 42 fi 43 44 if [ "$#" -eq 3 ]; then 45 EMRUN_BROWSER=${3} 46 fi 47 48 # check to see if the build web folder has its own git repo 49 if [ -d build-web/.git ] 50 then 51 # if so trash only the scripts 52 if [ -d build-web/scripts ]; then 53 if [ "$BUILD_DSP" -eq "1" ]; then 54 rm build-web/scripts/*-wam.js 55 fi 56 57 if [ "$BUILD_EDITOR" -eq "1" ]; then 58 rm build-web/scripts/*-web.* 59 fi 60 fi 61 else 62 # otherwise trash the whole build-web folder 63 if [ -d build-web ]; then 64 rm -r build-web 65 fi 66 67 mkdir build-web 68 fi 69 70 mkdir build-web/scripts 71 72 echo BUNDLING RESOURCES ----------------------------- 73 74 if [ -f ./build-web/imgs.js ]; then rm ./build-web/imgs.js; fi 75 if [ -f ./build-web/[email protected] ]; then rm ./build-web/[email protected]; fi 76 if [ -f ./build-web/svgs.js ]; then rm ./build-web/svgs.js; fi 77 if [ -f ./build-web/fonts.js ]; then rm ./build-web/fonts.js; fi 78 79 FILE_PACKAGER=$EMSDK/upstream/emscripten/tools/file_packager.py 80 #package fonts 81 FOUND_FONTS=0 82 if [ "$(ls -A ./resources/fonts/*.ttf)" ]; then 83 FOUND_FONTS=1 84 python3 $FILE_PACKAGER fonts.data --preload ./resources/fonts/ --exclude *DS_Store --js-output=./fonts.js 85 fi 86 87 #package svgs 88 FOUND_SVGS=0 89 if [ "$(ls -A ./resources/img/*.svg)" ]; then 90 FOUND_SVGS=1 91 python3 $FILE_PACKAGER svgs.data --preload ./resources/img/ --exclude *.png --exclude *DS_Store --js-output=./svgs.js 92 fi 93 94 #package @1x pngs 95 FOUND_PNGS=0 96 if [ "$(ls -A ./resources/img/*.png)" ]; then 97 FOUND_PNGS=1 98 python3 $FILE_PACKAGER imgs.data --use-preload-plugins --preload ./resources/img/ --use-preload-cache --indexedDB-name="/$PROJECT_NAME_pkg" --exclude *DS_Store --exclude *@2x.png --exclude *.svg >> ./imgs.js 99 fi 100 101 # package @2x pngs into separate .data file 102 FOUND_2XPNGS=0 103 if [ "$(ls -A ./resources/img/*@2x*.png)" ]; then 104 FOUND_2XPNGS=1 105 mkdir ./build-web/2x/ 106 cp ./resources/img/*@2x* ./build-web/2x 107 python3 $FILE_PACKAGER [email protected] --use-preload-plugins --preload ./2x@/resources/img/ --use-preload-cache --indexedDB-name="/$PROJECT_NAME_pkg" --exclude *DS_Store >> ./[email protected] 108 rm -r ./build-web/2x 109 fi 110 111 if [ -f ./imgs.js ]; then mv ./imgs.js ./build-web/imgs.js; fi 112 if [ -f ./[email protected] ]; then mv ./[email protected] ./build-web/[email protected]; fi 113 if [ -f ./svgs.js ]; then mv ./svgs.js ./build-web/svgs.js; fi 114 if [ -f ./fonts.js ]; then mv ./fonts.js ./build-web/fonts.js; fi 115 116 if [ -f ./imgs.data ]; then mv ./imgs.data ./build-web/imgs.data; fi 117 if [ -f ./[email protected] ]; then mv ./[email protected] ./build-web/[email protected]; fi 118 if [ -f ./svgs.data ]; then mv ./svgs.data ./build-web/svgs.data; fi 119 if [ -f ./fonts.data ]; then mv ./fonts.data ./build-web/fonts.data; fi 120 121 if [ "$BUILD_DSP" -eq "1" ]; then 122 echo MAKING - WAM WASM MODULE ----------------------------- 123 cd $PROJECT_ROOT/projects 124 emmake make --makefile $PROJECT_NAME-wam-processor.mk 125 126 if [ $? -ne "0" ]; then 127 echo IPlugWAM WASM compilation failed 128 exit 1 129 fi 130 131 cd $PROJECT_ROOT/build-web/scripts 132 133 # prefix the -wam.js script with scope 134 echo "AudioWorkletGlobalScope.WAM = AudioWorkletGlobalScope.WAM || {}; AudioWorkletGlobalScope.WAM.$PROJECT_NAME = { ENVIRONMENT: 'WEB' };" > $PROJECT_NAME-wam.tmp.js; 135 cat $PROJECT_NAME-wam.js >> $PROJECT_NAME-wam.tmp.js 136 mv $PROJECT_NAME-wam.tmp.js $PROJECT_NAME-wam.js 137 138 # copy in WAM SDK and AudioWorklet polyfill scripts 139 cp $IPLUG2_ROOT/Dependencies/IPlug/WAM_SDK/wamsdk/*.js . 140 cp $IPLUG2_ROOT/Dependencies/IPlug/WAM_AWP/*.js . 141 142 # copy in template scripts 143 cp $IPLUG2_ROOT/IPlug/WEB/Template/scripts/IPlugWAM-awn.js $PROJECT_NAME-awn.js 144 cp $IPLUG2_ROOT/IPlug/WEB/Template/scripts/IPlugWAM-awp.js $PROJECT_NAME-awp.js 145 146 # replace NAME_PLACEHOLDER in the template -awn.js and -awp.js scripts 147 sed -i.bak s/NAME_PLACEHOLDER/$PROJECT_NAME/g $PROJECT_NAME-awn.js 148 sed -i.bak s/NAME_PLACEHOLDER/$PROJECT_NAME/g $PROJECT_NAME-awp.js 149 150 # replace ORIGIN_PLACEHOLDER in the template -awn.js script 151 sed -i.bak s,ORIGIN_PLACEHOLDER,$SITE_ORIGIN,g $PROJECT_NAME-awn.js 152 153 rm *.bak 154 else 155 echo "WAM not being built, BUILD_DSP = 0" 156 fi 157 158 cd $PROJECT_ROOT/build-web 159 160 # copy in the template HTML - comment this out if you have customised the HTML 161 cp $IPLUG2_ROOT/IPlug/WEB/Template/index.html index.html 162 sed -i.bak s/NAME_PLACEHOLDER/$PROJECT_NAME/g index.html 163 164 if [ $FOUND_FONTS -eq "0" ]; then sed -i.bak s/'<script async src="fonts.js"><\/script>'/'<!--<script async src="fonts.js"><\/script>-->'/g index.html; fi 165 if [ $FOUND_SVGS -eq "0" ]; then sed -i.bak s/'<script async src="svgs.js"><\/script>'/'<!--<script async src="svgs.js"><\/script>-->'/g index.html; fi 166 if [ $FOUND_PNGS -eq "0" ]; then sed -i.bak s/'<script async src="imgs.js"><\/script>'/'<!--<script async src="imgs.js"><\/script>-->'/g index.html; fi 167 if [ $FOUND_2XPNGS -eq "0" ]; then sed -i.bak s/'<script async src="[email protected]"><\/script>'/'<!--<script async src="[email protected]"><\/script>-->'/g index.html; fi 168 if [ $WEBSOCKET_MODE -eq "1" ]; then 169 cp $IPLUG2_ROOT/Dependencies/IPlug/WAM_SDK/wamsdk/wam-controller.js scripts/wam-controller.js 170 cp $IPLUG2_ROOT/IPlug/WEB/Template/scripts/websocket.js scripts/websocket.js 171 sed -i.bak s/'<script src="scripts\/audioworklet.js"><\/script>'/'<!--<script src="scripts\/audioworklet.js"><\/script>-->'/g index.html; 172 sed -i.bak s/'let WEBSOCKET_MODE=false;'/'let WEBSOCKET_MODE=true;'/g index.html; 173 else 174 sed -i.bak s/'<script src="scripts\/websocket.js"><\/script>'/'<!--<script src="scripts\/websocket.js"><\/script>-->'/g index.html; 175 176 # update the i/o details for the AudioWorkletNodeOptions parameter, based on config.h channel io str 177 MAXNINPUTS=$(python3 $IPLUG2_ROOT/Scripts/parse_iostr.py "$PROJECT_ROOT" inputs) 178 MAXNOUTPUTS=$(python3 $IPLUG2_ROOT/Scripts/parse_iostr.py "$PROJECT_ROOT" outputs) 179 180 if [ $MAXNINPUTS -eq "0" ]; then 181 MAXNINPUTS=""; 182 sed -i.bak '181,203d' index.html; # hack to remove GetUserMedia() from code, and allow WKWebKitView usage for instruments 183 fi 184 sed -i.bak s/"MAXNINPUTS_PLACEHOLDER"/"$MAXNINPUTS"/g index.html; 185 sed -i.bak s/"MAXNOUTPUTS_PLACEHOLDER"/"$MAXNOUTPUTS"/g index.html; 186 fi 187 188 rm *.bak 189 190 # copy the style & WAM favicon 191 mkdir styles 192 cp $IPLUG2_ROOT/IPlug/WEB/Template/styles/style.css styles/style.css 193 cp $IPLUG2_ROOT/IPlug/WEB/Template/favicon.ico favicon.ico 194 195 echo MAKING - WEB WASM MODULE ----------------------------- 196 197 cd $PROJECT_ROOT/projects 198 199 emmake make --makefile $PROJECT_NAME-wam-controller.mk EXTRA_CFLAGS=-DWEBSOCKET_CLIENT=$WEBSOCKET_MODE 200 201 if [ $? -ne "0" ]; then 202 echo IPlugWEB WASM compilation failed 203 exit 1 204 fi 205 206 cd $PROJECT_ROOT/build-web 207 208 # print payload 209 echo payload: 210 find . -maxdepth 2 -mindepth 1 -name .git -type d \! -prune -o \! -name .DS_Store -type f -exec du -hs {} \; 211 212 # launch emrun 213 if [ "$LAUNCH_EMRUN" -eq "1" ]; then 214 mkcert 127.0.0.1 localhost 215 if [ "$EMRUN_CONTAINER" -eq "1" ]; then 216 $EMRUN --no_browser --serve_after_close --serve_after_exit --port=$EMRUN_SERVER_PORT --hostname=0.0.0.0 . 217 elif [ "$EMRUN_SERVER" -eq "0" ]; then 218 $EMRUN --browser $EMRUN_BROWSER --no_server --port=$EMRUN_SERVER_PORT index.html 219 else 220 $EMRUN --browser $EMRUN_BROWSER --no_emrun_detect index.html 221 fi 222 else 223 echo "Not running emrun" 224 fi