U
    |e]F                     @   st   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZ ddlmZ dZG dd	 d	eZd
d ZdS )z'
iOS target, based on kivy-ios project
    N)BuildozerCommandException)Target	no_config)joinbasename
expanduserrealpath)getpassa  
<?php
// credits goes to http://jeffreysambells.com/2010/06/22/ios-wireless-app-distribution

$ipas = glob('*.ipa');
$provisioningProfiles = glob('*.mobileprovision');
$plists = glob('*.plist');

$sr = stristr( $_SERVER['SCRIPT_URI'], '.php' ) === false ?
    $_SERVER['SCRIPT_URI'] : dirname($_SERVER['SCRIPT_URI']) . '/';
$provisioningProfile = $sr . $provisioningProfiles[0];
$ipa = $sr . $ipas[0];
$itmsUrl = urlencode( $sr . 'index.php?plist=' . str_replace( '.plist', '', $plists[0] ) );


if ($_GET['plist']) {
    $plist = file_get_contents( dirname(__FILE__)
        . DIRECTORY_SEPARATOR
        . preg_replace( '/![A-Za-z0-9-_]/i', '', $_GET['plist']) . '.plist' );
    $plist = str_replace('_URL_', $ipa, $plist);
    header('content-type: application/xml');
    echo $plist;
    die();
}


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Install {appname}</title>
<style type="text/css">
li { padding: 1em; }
</style>

</head>
<body>
<ul>
    <li><a href="<? echo $provisioningProfile; ?>">Install Team Provisioning File</a></li>
    <li><a href="itms-services://?action=download-manifest&url=<? echo $itmsUrl; ?>">
         Install Application</a></li>
</ul>
</body>
</html>
c                       s   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Ze	dd Z
e	dd Zdd Zdd Zdd Zdd Zdd Zdd Z fddZ fddZd d! Zd/d#d$Zd%d& Z fd'd(Zed)d* Zd+d, Zd-d. Z  ZS )0	TargetIosiosc                    s8   t  | tjpd}|dg| _dg| _d | _d | _d S )Npythonztoolchain.py
xcodebuild)super__init__sys
executable_toolchain_cmd_xcodebuild_cmdios_dirios_deploy_dir)self	buildozerr   	__class__ 9/tmp/pip-unpacked-wheel-ohd2lbr1/buildozer/targets/ios.pyr   ?   s    

zTargetIos.__init__c                 C   s   t jdkrtd| jj}| jj}|dd |dd |dd |d	d
 |dd |dd |dd |dd | jd |ddddd }|stdn| jd|  | jd |ddgddd }|std| jd| d S )Ndarwinz&Only macOS is supported for iOS targetzXcode xcodebuildr   zXcode xcode-selectzxcode-selectzGit gitgitzCython cythonZcythonz
pkg-configZautoconfZautomakeZlibtoolz"Check availability of a iPhone SDKzExcodebuild -showsdks | fgrep "iphoneos" |tail -n 1 | awk '{print $2}'T)
get_stdoutshellr   z9No iPhone SDK found. Please install at least one iOS SDK.z -> found %rzCheck Xcode pathz-print-pathr   zUnable to get xcode pathz -> found {0})	r   platformNotImplementedErrorr   checkbincmddebug	Exceptionformat)r   r#   r$   ZsdkZxcoder   r   r   check_requirementsH   s:    








 zTargetIos.check_requirementsc                 C   s(   | j ddd| _| j ddddd| _dS )	z
        Clones `kivy/kivy-ios` and `phonegap/ios-deploy` then sets `ios_dir`
        and `ios_deploy_dir` accordingly.
        kivy-iosr   )r!   
ios-deployz1.7.0Zphonegap)r!   branchownerN)Zinstall_or_update_repor   r   )r   r   r   r   install_platformg   s    zTargetIos.install_platformc                 K   s$   | d| j | jj| j|f|S )Ncwd)
setdefaultr   r   r$   r   )r   r$   kwargsr   r   r   	toolchainr   s    zTargetIos.toolchainc                 O   s$   dd |D }| j j| j|f|S )Nc                 S   s   g | ]}|d k	r|qS Nr   ).0argr   r   r   
<listcomp>w   s      z(TargetIos.xcodebuild.<locals>.<listcomp>)r   r$   r   )r   argsr0   Zfiltered_argsr   r   r   r   v   s    zTargetIos.xcodebuildc                 C   s&   | j jdd}|rdnd}d| S )Nappios.codesign.allowedZYESNOzCODE_SIGNING_ALLOWED=)r   config
getboolean)r   allowedr   r   r   code_signing_allowedz   s    zTargetIos.code_signing_allowedc                 C   s,   | j jdd| j d }|r(d| S d S )Nr7   zios.codesign.development_team.zDEVELOPMENT_TEAM=)r   r:   
getdefault
build_mode)r   Zteamr   r   r   code_signing_development_team   s    z'TargetIos.code_signing_development_teamc                 C   s&   | j ddgddd }| d  S )NZrecipesz	--compactTr    r   )r1   
splitlinessplit)r   available_modulesr   r   r   get_available_packages   s    z TargetIos.get_available_packagesc              
   C   s,   t |d}t|W  5 Q R  S Q R X d S )Nrb)openplistlibload)r   	plist_rfnfr   r   r   load_plist_from_file   s    zTargetIos.load_plist_from_filec              	   C   s&   t |d}t|| W 5 Q R X d S )Nwb)rF   rG   dump)r   plistrI   rJ   r   r   r   dump_plist_to_file   s    zTargetIos.dump_plist_to_filec              	      s  | j jdd}| j jddd}|   dd  fdd|D }d	}||krVd
}dd | j jdD }|rd
}| j j| | j 	d
ddd | D  |s| j 	d d S | d| | j | jds| j| jd || j jd< | j j  d S )Nzios.requirements r7   requirementsc                 S   s   |  dd S )Nz==r   )rB   )xr   r   r   <lambda>       z,TargetIos.compile_platform.<locals>.<lambda>c                    s   g | ]}| kr|qS r   r   r3   rR   rC   Zonlynamer   r   r5      s
      z.TargetIos.compile_platform.<locals>.<listcomp>r      c                 S   s:   i | ]2\}}| d rd|dd  tt|qS )zrequirements.source.z{}_DIR   N)
startswithr'   upperr   r   )r3   namevaluer   r   r   
<dictcomp>   s   
z.TargetIos.compile_platform.<locals>.<dictcomp>z Using custom source dirs:
    {}z
    c                 S   s   g | ]\}}d  ||qS )z{} = {})r'   )r3   kvr   r   r   r5      s   z$Distribution already compiled, pass.buildr*   r.   )r`   )r   stategetr:   getlistrD   itemsenvironupdateinfor'   r   r1   file_existsr   r   sync)r   Zlast_requirementsZapp_requirementsZios_requirementsZneed_compileZsource_dirsr   rV   r   compile_platform   s:    

zTargetIos.compile_platformc                 C   s:   | j j}|ddd}|dd}|r2|d | }| S )Nr7   zpackage.domainrP   package.name.)r   r:   r>   rc   lower)r   r:   Zpackage_domainpackager   r   r   _get_package   s    zTargetIos._get_packagec                 C   sv  |    | j| jjdd}| jjddd}g }|D ]}|d|  q:t| jd	|
 | _| j| jsd;||| jjf}nd<|| d	f}| | d
	|
 }t| j|}| j }| jd	| | |}	|  |	d< ||	d< d	|| jj|	d< |   | jjddd }
| jjddd }| jjddd }t|
||frt|
||fs|| jd d S |
||d|	d< | |	| | j }| jd|dd| j| jdd| jd	 dj	|
 |d}|| jjd< t| jd	||}t|d 	||}d!	||}t||}t| jj |}t| jd"	|
 }| j!| | jd# | jd$d|d%|
 d&|d'd(d)d| j| j|d d*	| j}| jjd|d}|s| jd+	| d S |d, d-krd.	|}| jd/ | jd0d1| d2d3| d2d4| d2d5| d|d | jd6 | j"|| | jd7 | jd8	t#| || jjd9< | j| jjd:< d S )=Nr7   rl   zios.frameworksrP   z--add-framework=z{0}-ioscreaterg   -iosz{}-Info.plistzUpdate Plist {}ZCFBundleIdentifierZCFBundleShortVersionStringz{}.{}ZCFBundleVersionzios.manifest.app_urlzios.manifest.display_image_urlz ios.manifest.full_size_image_urlzOptions ios.manifest.app_url, ios.manifest.display_image_url and ios.manifest.full_size_image_url should be defined all together)ZappURLZdisplayImageURLZfullSizeImageURLmanifestz-configurationz-allowProvisioningUpdateszENABLE_BITCODE=NOcleanr`   ra   z5{app_lower}-ios/build/{mode}-iphoneos/{app_lower}.app)Z	app_lowermodeios:latestappdirz{}-{}.intermediatesz{}-{}.xcarchivez	{}-{}.ipaz{}-ioszCreating archive...z-alltargetsz-schemez-archivePathz-destinationzgeneric/platform=iOSarchivezios.codesign.{}zNCannot create the IPA package without signature. You must fill the "{}" token.r   )"'"{}"zCreating IPA...z-exportArchivez-archivePath "rx   z-exportOptionsPlist "z-exportPath "zCODE_SIGN_IDENTITY=zMoving IPA to bin...ziOS packaging done!z&IPA {0} available in the bin directoryzios:latestipazios:latestmode)rq   )rg   )$_unlock_keychainr   namifyr:   rc   rd   appendr   r   r'   rn   app_project_dirri   app_dirr1   get_versionrh   rK   rp   Zbuild_id_create_iconsr>   anyallerrorrO   r?   
capitalizer   r=   r@   rb   bin_dirrmdirZfile_renamer   )r   app_nameZios_frameworksZframeworks_cmdZ	frameworkr$   Zplist_fnrI   versionrN   Zapp_urlZdisplay_image_urlZfull_size_image_urlru   ios_app_dirZintermediate_dirZ	xcarchiveZipa_nameZipa_tmpZipa	build_dirkeyZioscodesignr   r   r   build_package   s    





  





	
zTargetIos.build_packagec                    s   t  j|  | jdd d S )NFlldb)r   
cmd_deploy_run_ios_deployr   r6   r   r   r   r   >  s    zTargetIos.cmd_deployc                    s   t  j|  | jdd d S )NTr   )r   cmd_runr   r   r   r   r   r   B  s    zTargetIos.cmd_runc                 G   s\   | j | j jdd}| }t| j jd }}| j jd| dgt|| dd dS )	z Open the xcode project.
        r7   rl   r)   rF   z
.xcodeprojrr   ra   N)r   r|   r:   rc   rn   r   Zplatform_dirr$   )r   r6   r   r   r   r   r   	cmd_xcodeF  s     zTargetIos.cmd_xcodeFc                 C   sx   | j j}d|kr | j d d S |d}|r@d}| j d nd}| j d | j jt| jd|d|g| jd	d
 d S )Nrv   z2App not built yet. Run "debug" or "release" first.z-dz Deploy and start the applicationrP   zDeploy the applicationr*   z-bT)r.   Zshow_output)	r   rb   r   rc   rh   r$   r   r   r   )r   r   rb   r   Z
debug_moder   r   r   r   R  s"    
zTargetIos._run_ios_deployc                 C   s`   | j jddd}|sd S t| j j|}| j |sJ| j d| d S | d| j	|g d S )Nr7   zicon.filenamerP   zIcon {} does not existsicon)
r   r:   r>   r   r   ri   r   r'   r1   r~   )r   r   Zicon_fnr   r   r   r   g  s    zTargetIos._create_iconsc                    s   g }| j j}|ddsd S |ddd}|dd|}|  }|sP|d n||krh|d| |sx|d n||kr|d	| t | d S )
Nr7   r8   zios.codesign.debugrP   zios.codesign.releasezP[app] "ios.codesign.debug" key missing, you must give a certificate name to use.z7[app] identity {} not found. Check with list_identitieszR[app] "ios.codesign.release" key missing, you must give a certificate name to use.z9[app] identity "{}" not found. Check with list_identities)	r   r:   r;   r>   _get_available_identitiesr}   r'   r   check_configuration_tokens)r   errorsr:   Zidentity_debugZidentity_releaseZavailable_identitiesr   r   r   r   r  s,    z$TargetIos.check_configuration_tokensc                 G   s,   |   }td |D ]}td| qdS )z:List the available identities to use for signing.
        zAvailable identities:z  - {}N)r   printr'   )r   r6   Z
identitiesrR   r   r   r   cmd_list_identities  s    zTargetIos.cmd_list_identitiesc                 C   s@   | j jdddddgddd }| d d	 }d
d |D }|S )Nsecurityzfind-identityz-v-pZcodesigningTr    r   c                 S   s    g | ]}d  |dd qS )rz   rx   rW   )r'   rB   rU   r   r   r   r5     s     z7TargetIos._get_available_identities.<locals>.<listcomp>)r   r$   rA   )r   outputlinesr   r   r   r     s     z#TargetIos._get_available_identitiesc           	   	   C   sf  t | jjd}d }| j|r:t|}| }W 5 Q R X |sb| jjdddgddd }|sd S n&| jjddd|gdd	d
d }|sd S d}d}|r|d8 }td}| jjddd|gdd	d
d }|sd	}q| jd q|s| jd t	 t
d d }|d kr8td}|dkrd	}q|dkr.d}qt
d q|rbt|d}||  W 5 Q R X d S )Nz.ioscodesignr   zunlock-keychainz-uF)break_on_error   r   T)r   Zsensible   rW   z(Password to unlock the default keychain:zInvalid keychain passwordz'Unable to unlock the keychain, exiting.zThe keychain password can be saved in the build directory
As soon as the build directory will be cleaned, the password will be erased.z(Do you want to save the password (Y/n): )rP   YnzInvalid answer!rL   )r   r   Zbuildozer_dirri   rF   readr$   r	   r   r   r   inputwriteencode)	r   Zpassword_filepasswordfdr   correctattemptsaveqr   r   r   r{     sn    






zTargetIos._unlock_keychain)F)__name__
__module____qualname__Z
targetnamer   r(   r-   r1   r   propertyr=   r@   rD   rK   rO   rk   rp   r   r   r   r   r   r   r   r   r   r   r{   __classcell__r   r   r   r   r
   <   s4   	

)|

	r
   c                 C   s   t | S r2   )r
   )r   r   r   r   
get_target  s    r   )__doc__r   rG   r   r   Zbuildozer.targetr   r   os.pathr   r   r   r   r	   ZPHP_TEMPLATEr
   r   r   r   r   r   <module>   s   /   $