1#!/bin/bash
2###############################################################################
3# Copyright 2015 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7###############################################################################
8#
9# ios_setup.sh: Sets environment variables used by other iOS scripts.
10
11# File system location where we mount the ios devices.
12IOS_MOUNT_POINT="/tmp/mnt_iosdevice"
13
14# Location on the ios device where all data are stored. This is
15# relative to the mount point.
16IOS_DOCS_DIR="Documents"
17
18# Temporary location to assemble the app into an .ipa package.
19IOS_PCKG_DIR="/tmp/ios_pckg"
20
21# Bundle id of the app that runs the tests.
22TEST_RUNNER_BUNDLE_ID="com.google.iOSShell"
23
24# Directory with the Skia source.
25SKIA_SRC_DIR=$(cd "${SCRIPT_DIR}/../../.."; pwd)
26
27# Provisioning profile - this needs to be set up on the local machine.
28PROVISIONING_PROFILE="9e88090d-abed-4e89-b106-3eff3512d31f"
29
30# Code Signing identity - this needs to be set up on the local machine.
31CODE_SIGN_IDENTITY="iPhone Developer: Google Development (3F4Y5873JF)"
32
33IOS_BUNDLE_ID="com.google.iOSShell"
34
35IOS_RESULTS_DIR="results"
36
37# BUILDTYPE is 'Debug' by default.
38if [[ -z "$BUILDTYPE" ]]; then
39  BUILDTYPE="Debug"
40fi
41
42ios_uninstall_app() {
43  ideviceinstaller -U "$IOS_BUNDLE_ID"
44}
45
46ios_install_app() {
47  rm -rf $IOS_PCKG_DIR
48  mkdir -p $IOS_PCKG_DIR/Payload  # this directory must be named 'Payload'
49  cp -rf "${SKIA_SRC_DIR}/xcodebuild/${BUILDTYPE}-iphoneos/iOSShell.app" "${IOS_PCKG_DIR}/Payload/"
50  local RET_DIR=`pwd`
51  cd $IOS_PCKG_DIR
52  zip -r iOSShell.ipa Payload
53  ideviceinstaller -i ./iOSShell.ipa
54  cd $RET_DIR
55}
56
57ios_rm() {
58  local TARGET="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
59
60  ios_mount
61  rm -rf "$TARGET"
62  ios_umount
63}
64
65ios_mkdir() {
66  local TARGET="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
67  ios_mount
68  mkdir -p "$TARGET"
69  ios_umount
70}
71
72ios_cat() {
73  local TARGET="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
74  ios_mount
75  RET="$(cat $TARGET)"
76  ios_umount
77  echo -e "$RET"
78}
79
80# ios_mount: mounts the iOS device for reading or writing.
81ios_mount() {
82  # If this is already mounted we unmount it.
83  if $(mount | grep --quiet "$IOS_MOUNT_POINT"); then
84    >&2 echo "Device already mounted at: $IOS_MOUNT_POINT - Unmounting."
85    ios_umount
86  fi
87
88  # Ensure there is a mount directory.
89  if [[ ! -d "$IOS_MOUNT_POINT" ]]; then
90    mkdir -p $IOS_MOUNT_POINT
91  fi
92  ifuse --container $TEST_RUNNER_BUNDLE_ID $IOS_MOUNT_POINT
93  sleep 1
94  >&2 echo "Successfully mounted device."
95}
96
97# ios_umount: unmounts the ios device.
98ios_umount() {
99  umount $IOS_MOUNT_POINT
100  sleep 1
101}
102
103# ios_restart: restarts the iOS device.
104ios_restart() {
105  idevicediagnostics restart
106}
107
108# ios_pull(ios_src, host_dst): Copies the content of ios_src to host_dst.
109# The path is relative to the 'Documents' folder on the device.
110ios_pull() {
111  # read input params
112  local IOS_SRC="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
113  local HOST_DST="$2"
114
115  ios_mount
116  if [[ -d "${HOST_DST}" ]]; then
117    cp -r "$IOS_SRC/" "$HOST_DST"
118  else
119    cp -r "$IOS_SRC" "$HOST_DST"
120  fi
121  ios_umount
122}
123
124# ios_push(host_src, ios_dst)
125ios_push() {
126  # read input params
127  local HOST_SRC="$1"
128  local IOS_DST="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$2"
129
130  ios_mount
131  rm -rf $IOS_DST
132  mkdir -p "$(dirname $IOS_DST)"
133  cp -r "$HOST_SRC" "$IOS_DST"
134  ios_umount
135}
136
137ios_path_exists() {
138  local TARGET_PATH="$IOS_MOUNT_POINT/$IOS_DOCS_DIR/$1"
139  local RET=1
140  ios_mount
141  if [[ -e $TARGET_PATH ]]; then
142    RET=0
143  fi
144  ios_umount
145  return $RET
146}
147
148#   ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
149#   HOST_LS=`ls -ld $HOST_SRC`
150#   if [ "${ANDROID_LS:0:1}" == "d" -a "${HOST_LS:0:1}" == "-" ];
151#   then
152#     ANDROID_DST="${ANDROID_DST}/$(basename ${HOST_SRC})"
153#   fi
154
155
156#   ANDROID_LS=`$ADB $DEVICE_SERIAL shell ls -ld $ANDROID_DST`
157#   if [ "${ANDROID_LS:0:1}" == "-" ]; then
158#     #get the MD5 for dst and src
159#     ANDROID_MD5=`$ADB $DEVICE_SERIAL shell md5 $ANDROID_DST`
160#     if [ $(uname) == "Darwin" ]; then
161#       HOST_MD5=`md5 -q $HOST_SRC`
162#     else
163#       HOST_MD5=`md5sum $HOST_SRC`
164#     fi
165
166#     if [ "${ANDROID_MD5:0:32}" != "${HOST_MD5:0:32}" ]; then
167#       echo -n "$ANDROID_DST "
168#       $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
169#     fi
170#   elif [ "${ANDROID_LS:0:1}" == "d" ]; then
171#     for FILE_ITEM in `ls $HOST_SRC`; do
172#       adb_push_if_needed "${HOST_SRC}/${FILE_ITEM}" "${ANDROID_DST}/${FILE_ITEM}"
173#     done
174#   else
175#     HOST_LS=`ls -ld $HOST_SRC`
176#     if [ "${HOST_LS:0:1}" == "d" ]; then
177#       $ADB $DEVICE_SERIAL shell mkdir -p $ANDROID_DST
178#       adb_push_if_needed $HOST_SRC $ANDROID_DST
179#     else
180#       echo -n "$ANDROID_DST "
181#       $ADB $DEVICE_SERIAL shell mkdir -p "$(dirname "$ANDROID_DST")"
182#       $ADB $DEVICE_SERIAL push $HOST_SRC $ANDROID_DST
183#     fi
184#   fi
185# }
186
187# setup_device "${DEVICE_ID}"
188