summary refs log tree commit diff
path: root/pkgs/development/mobile/titaniumenv/build-app.nix
blob: cf699c34e2be585c0e0fb1651f5fc6d063f883c0 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{stdenv, androidsdk, titaniumsdk, titanium, alloy, xcodewrapper, jdk, python, nodejs, which, xcodeBaseDir}:
{ name, src, target, androidPlatformVersions ? [ "23" ], androidAbiVersions ? [ "armeabi" "armeabi-v7a" ], tiVersion ? null
, release ? false, androidKeyStore ? null, androidKeyAlias ? null, androidKeyStorePassword ? null
, iosMobileProvisioningProfile ? null, iosCertificateName ? null, iosCertificate ? null, iosCertificatePassword ? null, iosVersion ? "10.2"
, enableWirelessDistribution ? false, installURL ? null
}:

assert (release && target == "android") -> androidKeyStore != null && androidKeyAlias != null && androidKeyStorePassword != null;
assert (release && target == "iphone") -> iosMobileProvisioningProfile != null && iosCertificateName != null && iosCertificate != null && iosCertificatePassword != null;
assert enableWirelessDistribution -> installURL != null;

let
  androidsdkComposition = androidsdk {
    platformVersions = androidPlatformVersions;
    abiVersions = androidAbiVersions;
    useGoogleAPIs = true;
  };
  
  deleteKeychain = ''
    security default-keychain -s login.keychain
    security delete-keychain $keychainName
  '';
  
  # On Mac OS X, the java executable shows an -unoffical postfix in the version
  # number. This confuses the build script's version detector.
  # We fix this by creating a wrapper that strips it out of the output.
  
  javaVersionFixWrapper = stdenv.mkDerivation {
    name = "javaVersionFixWrapper";
    buildCommand = ''
      mkdir -p $out/bin
      cat > $out/bin/javac <<EOF
      #! ${stdenv.shell} -e
      
      if [ "\$1" = "-version" ]
      then
          ${jdk}/bin/javac "\$@" 2>&1 | sed "s|-unofficial||" | sed "s|-u60|_60|" >&2
      else
          exec ${jdk}/bin/javac "\$@"
      fi
      EOF
      chmod +x $out/bin/javac
    '';
  };
in
stdenv.mkDerivation {
  name = stdenv.lib.replaceChars [" "] [""] name;
  inherit src;
  
  buildInputs = [ nodejs titanium alloy jdk python which ] ++ stdenv.lib.optional (stdenv.system == "x86_64-darwin") xcodewrapper;
  
  buildPhase = ''
    export HOME=$TMPDIR
    
    ${stdenv.lib.optionalString (tiVersion != null) ''
      # Replace titanium version by the provided one
      sed -i -e "s|<sdk-version>[0-9a-zA-Z\.]*</sdk-version>|<sdk-version>${tiVersion}</sdk-version>|" tiapp.xml
    ''}
    
    # Simulate a login
    mkdir -p $HOME/.titanium
    cat > $HOME/.titanium/auth_session.json <<EOF
    { "loggedIn": true }
    EOF
    
    echo "{}" > $TMPDIR/config.json
    titanium --config-file $TMPDIR/config.json --no-colors config sdk.defaultInstallLocation ${titaniumsdk}
    titanium --config-file $TMPDIR/config.json --no-colors config paths.modules ${titaniumsdk}
    
    mkdir -p $out
    
    ${if target == "android" then
        ''
          ${stdenv.lib.optionalString (stdenv.system == "x86_64-darwin") ''
            # Hack to make version detection work with OpenJDK on Mac OS X
            export PATH=${javaVersionFixWrapper}/bin:$PATH
            export JAVA_HOME=${javaVersionFixWrapper}
            javac -version
          ''}
          
          titanium config --config-file $TMPDIR/config.json --no-colors android.sdk ${androidsdkComposition}/libexec
          
          export PATH=$(echo ${androidsdkComposition}/libexec/tools):$(echo ${androidsdkComposition}/libexec/build-tools/android-*):$PATH
          
          ${if release then
            ''titanium build --config-file $TMPDIR/config.json --no-colors --force --platform android --target dist-playstore --keystore ${androidKeyStore} --alias ${androidKeyAlias} --store-password ${androidKeyStorePassword} --output-dir $out''
          else
            ''titanium build --config-file $TMPDIR/config.json --no-colors --force --platform android --target emulator --build-only -B foo --output $out''}
        ''
      else if target == "iphone" then
        ''
          ${if release then
            ''
              export HOME=/Users/$(whoami)
              export keychainName=$(basename $out)
            
              # Create a keychain with the component hash name (should always be unique)
              security create-keychain -p "" $keychainName
              security default-keychain -s $keychainName
              security unlock-keychain -p "" $keychainName
              security import ${iosCertificate} -k $keychainName -P "${iosCertificatePassword}" -A
              security set-key-partition-list -S apple-tool:,apple: -s -k "" $keychainName
              provisioningId=$(grep UUID -A1 -a ${iosMobileProvisioningProfile} | grep -o "[-A-Za-z0-9]\{36\}")
   
              # Ensure that the requested provisioning profile can be found
        
              if [ ! -f "$HOME/Library/MobileDevice/Provisioning Profiles/$provisioningId.mobileprovision" ]
              then
                  mkdir -p "$HOME/Library/MobileDevice/Provisioning Profiles"
                  cp ${iosMobileProvisioningProfile} "$HOME/Library/MobileDevice/Provisioning Profiles/$provisioningId.mobileprovision"
              fi
            
              # Simulate a login
              mkdir -p $HOME/.titanium
              cat > $HOME/.titanium/auth_session.json <<EOF
              { "loggedIn": true }
              EOF
              
              # Configure the path to Xcode
              titanium --config-file $TMPDIR/config.json --no-colors config paths.xcode ${xcodeBaseDir}
              
              # Make plutil available
              mkdir -p $TMPDIR/bin
              ln -s /usr/bin/plutil $TMPDIR/bin
              export PATH=$TMPDIR/bin:$PATH
              
              # Link the modules folder
              if [ ! -e modules ]
              then
                  ln -s ${titaniumsdk}/modules modules
              fi
              
              # Do the actual build
              titanium build --config-file $TMPDIR/config.json --force --no-colors --platform ios --target dist-adhoc --pp-uuid $provisioningId --distribution-name "${iosCertificateName}" --keychain $HOME/Library/Keychains/$keychainName-db --device-family universal --ios-version ${iosVersion} --output-dir $out
            
              # Remove our generated keychain
              ${deleteKeychain}
            ''
          else
            ''
              # Copy all sources to the output store directory.
              # Why? Debug application include *.js files, which are symlinked into their
              # sources. If they are not copied, we have dangling references to the
              # temp folder.
            
              cp -av * $out
              cd $out
              
              # We need to consult a real home directory to find the available simulators
              export HOME=/Users/$(whoami)
              
              # Configure the path to Xcode
              titanium --config-file $TMPDIR/config.json --no-colors config paths.xcode ${xcodeBaseDir}
              
              # Link the modules folder
              if [ ! -e modules ]
              then
                  ln -s ${titaniumsdk}/modules modules
                  createdModulesSymlink=1
              fi
              
              # Execute the build
              titanium build --config-file $TMPDIR/config.json --force --no-colors --platform ios --target simulator --build-only --device-family universal --ios-version ${iosVersion} --output-dir $out
              
              # Remove the modules symlink
              if [ "$createdModulesSymlink" = "1" ]
              then
                  rm $out/modules
              fi
          ''}
        ''

      else throw "Target: ${target} is not supported!"}
  '';
  
  installPhase = ''
    mkdir -p $out
    
    ${if target == "android" && release then ""
      else
        if target == "android" then
          ''cp "$(ls build/android/bin/*.apk | grep -v '\-unsigned.apk')" $out''
        else if target == "iphone" && release then
           ''
             cp -av build/iphone/build/* $out
             mkdir -p $out/nix-support
             echo "file binary-dist \"$(echo $out/Products/Release-iphoneos/*.ipa)\"" > $out/nix-support/hydra-build-products
             
             ${stdenv.lib.optionalString enableWirelessDistribution ''
               appname=$(basename $out/Products/Release-iphoneos/*.ipa .ipa)
               bundleId=$(grep '<id>[a-zA-Z0-9.]*</id>' tiapp.xml | sed -e 's|<id>||' -e 's|</id>||' -e 's/ //g')
               version=$(grep '<version>[a-zA-Z0-9.]*</version>' tiapp.xml | sed -e 's|<version>||' -e 's|</version>||' -e 's/ //g')
               
               sed -e "s|@INSTALL_URL@|${installURL}?bundleId=$bundleId\&amp;version=$version\&amp;title=$appname|" ${../xcodeenv/install.html.template} > "$out/$appname.html"
               echo "doc install \"$out/$appname.html\"" >> $out/nix-support/hydra-build-products
             ''}
           ''
        else if target == "iphone" then ""
        else throw "Target: ${target} is not supported!"}
    
    ${if target == "android" then ''
        mkdir -p $out/nix-support
        echo "file binary-dist \"$(ls $out/*.apk)\"" > $out/nix-support/hydra-build-products
    '' else ""}
  '';
  
  failureHook = stdenv.lib.optionalString (release && target == "iphone") deleteKeychain;
}