summary refs log tree commit diff
path: root/pkgs/development/mobile/xcodeenv/simulate-app.nix
blob: 5f71b5994080c83af10cfc4debffa9cdea4a6082 (plain) (blame)
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
{stdenv, xcodewrapper}:
{name, bundleId, app}:

stdenv.mkDerivation {
  name = stdenv.lib.replaceChars [" "] [""] name;
  buildCommand = ''
    mkdir -p $out/bin
    cat > $out/bin/run-test-simulator << "EOF"
    #! ${stdenv.shell} -e
    
    if [ "$1" = "" ]
    then
        # Show the user the possibile UDIDs and let him pick one, if none is provided as a command-line parameter
        xcrun simctl list
        
        echo "Please provide a UDID of a simulator:"
        read udid
    else
        # If a parameter has been provided, consider that a device UDID an use that
        udid="$1"
    fi
    
    # Open the simulator instance
    open -a "$(readlink "${xcodewrapper}/bin/Simulator")" --args -CurrentDeviceUDID $udid
    
    # Copy the app and restore the write permissions
    appTmpDir=$(mktemp -d -t appTmpDir)
    cp -r "$(echo ${app}/*.app)" "$appTmpDir"
    chmod -R 755 "$(echo $appTmpDir/*.app)"
    
    # Wait for the simulator to start
    echo "Press enter when the simulator is started..."
    read
    
    # Install the app
    xcrun simctl install "$udid" "$(echo $appTmpDir/*.app)"
    
    # Remove the app tempdir
    rm -Rf $appTmpDir
    
    # Launch the app in the simulator
    xcrun simctl launch $udid "${bundleId}"
    EOF

    chmod +x $out/bin/run-test-simulator
  '';
}