iOS Automated compilation

Source: Internet
Author: User
Tags call shell

recently studied the automated compilation of iOS, the purpose is to simplify the test and development of the number of peer communication and coordination, the implementation of test students can be at any time from the web operation to compile SVN latest source and packaging IPA for testing.

The specific idea is to read from the configuration file needs to compile a list of project configuration displayed on the Web page, test students choose to compile the project, determined to select the relevant parameters of the project into the shell script run, the compilation will automatically jump to the download page.
The main processes include:

    1. The writing of the shell script. Automated compilation and packaging via Xcodebuild and Xcrun.
    2. PHP call script. Open OS X's Apache server and write PHP to invoke the shell.
Writing shell scripts

Automatic compilation is actually the use xcodebuild of the relevant commands to implement the compilation function, and then use xcrun to package the app into IPA.
xcodebuildSee here for official documentation.

Key commands to use
    • Compiling workspace

      xcodebuild -workspace workspacename -scheme schemename -configuration [-configuration configurationname] clean build SYMROOT=(SYMROOT)
    • Compiling project

      xcodebuild -target targetname -configuration [-configuration configurationname] clean build SYMROOT=(SYMROOT)
    • View configuration information

      xcodebuild -list
    • Xcrun Packaging IPA

      xcrun -sdk iphoneos PackageApplication -v projectName.app -o ipaName.ipa

The full shell script is placed slightly longer at the end of the article, and the script is changed from Bashshell.
It is important to note that paths in scripts are best used for absolute paths.

Configure Apache and PHP to start Apache
启动:sudo apachectl start停止:sudo apachectl stop重启:sudo apachectl restart
root directory of the file root system level

http://localhosts/

The corresponding file directories are:
/Library/WebServer/Documents/

The system-level root directory does not open the directory list by default, and the method:
Edit /etc/apache2/httpd.conf File
Search to find<Directory "/Library/WebServer/Documents">
Options MultiviewsChange the revision toOptions Indexes Multiviews

User-level root directory

Another Web root is, by default ~/Sites , 10.9 in which you need to manually create this sites directory.

Check if there are any files in this directory username.conf
/etc/apache2/users/
If not, then you need to create a new one, username need to be your account name, it is recommended to use the terminal created this file:

cd /etc/apache2/userssudo vi username.conf

Paste in the following, notice to change the username name of your account

<Directory "/Users/username/Sites/">Options Indexes MultiViews FollowSymLinksAllowOverride AllOrder allow,denyAllow from allRequire all granted</Directory>

The permissions for this file should be:
-rw-r--r-- 1 root wheel 298 Jun 28 16:47 username.conf
If not, please modify
sudo chmod 644 username.conf
Edit the /etc/apache2/httpd.conf file and delete the following code before the comment symbol:#

Include /private/etc/apache2/extra/httpd-userdir.confLoadModule authz_core_module libexec/apache2/mod_authz_core.soLoadModule authz_host_module libexec/apache2/mod_authz_host.soLoadModule userdir_module libexec/apache2/mod_userdir.so

Edit the /etc/apache2/extra/httpd-userdir.conf file and delete the following code before the comment symbol:#

Include /private/etc/apache2/users/*.conf
RestartApache
sudo apachectl restart
At this point, the URL should already be available:
http://localhost/~username/

PHP Invoke Shell Script

Here the main use of the PHP system command:system($cmd)
PHP Call Shell permissions are relatively low, our shell will need to create file and folder permissions, the solution is to use the command line PHP files in the directory and all the files in the directory to elevate permissions, or the script will report permissions error. The steps are as follows:

    1. Open Directory/private/etc/apache2
    2. Open Filehttpd.conf
      Found it

      User _www Group _www

      Modify _WWW for your login user name

      User <登录用户名>
    3. Restart Apache from the command line

      sudo apachectl restart
    4. Promote Site Directory permissions. Because my site root is the sites file for the user mentioned above, execute the following command

      sudo chmod 775 ~/Sitessudo chmod 775 ~/Sites/*

OK, the permissions problem is solved. When confident to see the script output from the Web page, the result is not able to find the certificate error, OMG, but the call from the terminal script can be successful, initially thought that the permissions are not enough to invoke the certificate, around a large circle after the discovery of this problem only because 钥匙串 the certificate is generally installed 登录 under the Just move on to the 系统 next line.

The part about getting the source code from the SVN repository is not written, since the script can be called, this part is also very simple. This process is still a bit of a toss, I hope this article can save your time:)

Finally, the source of the relevant documents.

Shell script file (buildtool.sh)
#!/bin/shexport LC_ALL=ZH_CN. Gb2312;export LANG=ZH_CN. Gb2312username= User name ############## #配置项目名称和路径等相关参数projectName =$1 #项目所在目录的名称isWorkSpace =$2 # Determine if the workspace or direct project,workspace is set to true, otherwise set to falseprojectdir=/users/${username}/workspace/projects/$3/# The absolute path to the directory where the project resides is Buildconfig=$4 #编译的方式, the default is release, and debug ############## #配置下载的文件名称和路径等相关参数wwwIPADir =/users/${username }/sites/$projectName #html, ipa,icon,plist final directory absolute path url= "Http://localhost/${projectname}" #下载路径 ################# ###################################################################################################### #以下部分为自动生产部分 , you do not need to manually modify the ####################################################################################################### ###################################### fuction START ###################### #replaceString () {Local inputstring=$    1result=${inputstring//(/}result=${result//)/}echo $result}date_y_m_d_w_t () {weekdays= (Sunday Mon Tuesday Wed Thursday Fri Saturday) weekday=$ (date +%w) dt= "$ (date +%y year%m month%d day) ${weekdays[$WEEKDAY]}$ (date "+%h:%m:%s") "echo" $DT "}####################### fuction END ######################### #Log的路径, If found in the log and garbled in the terminal execution: Export LC_ALL=ZH_CN. Gb2312;export LANG=ZH_CN. GB2312LOGDIR=/USERS/${USERNAME}/WORKSPACE/XCODEBUILDMKDIR-PV $logDirlogPath = $logDir/$projectName-$ Buildconfig.logecho "~~~~~~~~~~~~~~~~~~~ start compiling ~~~~~~~~~~~~~~~~~~~" >> $logPathloginInfo = ' Who am I ' loginuser= ' echo $loginInfo |awk ' {print $} ' echo ' Login User: $loginUser >> $logPathloginDate = ' echo $loginInfo |awk ' {print $3,$4,$ 5} ' echo ' Login time: $loginDate >> $logPathloginServer = ' echo $loginInfo |awk ' {print $6} ' if [-N ' $loginServer ']; Thenecho "Login user ip:$ (replacestring $loginServer)" >> $logPathelse echo "Login user Ip:localhost (127.0.0.1)" >>$ Logpathfiif [-D "$logDir"]; Thenecho "${logdir} file directory exists" else echo "${logdir} file directory does not exist, create ${logdir} directory succeeded" echo "create ${logdir} directory successful" >> $logPathfiecho "<br/>" ############## #检查html等文件放置目录是否存在, does not exist create echo "Start time: $ (date_y_m_d_w_t)" >> $logPathecho "project name: $ ProjectName " >> $logPathecho "compile mode: $buildConfig" >> $logPathecho "Start directory check ..." >> $logPathif [-D "$wwwIPADir"]; Thenecho "file directory exists" >> $logPathelse echo "file directory does not exist" >> $logPath MKDIR-PV $wwwIPADirecho "Create ${wwwipadir} directory succeeded" &G t;> $logPathfi ############## #进入项目目录rm-rf./buildbuildapptodir=/users/${username}/workspace/build/$projectName #编译打包完成后. Archive. IPA file directory ############## #获取版本号, bundleidinfoplist= "${projectdir}${projectname}/$ Projectname-info.plist "Bundledisplayname= '/usr/libexec/plistbuddy-c" Print cfbundledisplayname "$infoPlist ' Bundleversion= '/usr/libexec/plistbuddy-c "Print cfbundleshortversionstring" $infoPlist ' bundleidentifier= '/usr/ Libexec/plistbuddy-c "Print Cfbundleidentifier" $infoPlist ' bundlebuildversion= '/usr/libexec/plistbuddy-c "Print Cfbundleversion "$infoPlist ' ############## #在网页上显示的名字和bundleDisplayName一致appName = $bundleDisplayName echo" $ Bundledisplayname "############## #开始编译appif $isWorkSpace; Then #判断编译方式 echo "Start compiling workspace ...." >&gt, $logPath echo "$projectDir $projectname.xcworkspace" Xcodebuild-workspace ${projectdir} $projectName. xcworkspace-sc heme $projectName-configuration $buildConfig Clean build symroot= $buildAppToDirelse echo "Start compiling target ..." >> $lo Gpath CD ${projectdir} xcodebuild-target $projectName-configuration $buildConfig Clean build symroot= $buildAppToDi rfi# to judge the results of the compilation if test $? -eq 0thenecho "~~~~~~~~~~~~~~~~~~~ compilation succeeded ~~~~~~~~~~~~~~~~~~~" Elseecho "~~~~~~~~~~~~~~~~~~~ compilation failed ~~~~~~~~~~~~~~~~~~~" >> $logPathecho "\ n" >> $logPathexit 1fi############## #开始打包成. Ipaipaname= ' echo $projectName | TR "[: Upper:]" "[: Lower:]" ' #将项目名转小写appDir = $buildAppToDir/$buildConfig-iphoneos #app所在路径echo "Start packing $ Projectname.xcarchive into $projectname.ipa ... ">> $logPathxcrun-sdk iphoneos packageapplication-v $appDir/$ Projectname.app-o $appDir/$ipaName. IPA #将app打包成ipa ############## #开始拷贝到目标下载目录iconName = "Icon.png" #icon名称iconSize = #icon大小 #unzipappdir= $appDir/$projectName. appunzipappdir= $projectDiricOnimages= ($ (Find $unzipAppDir-path "$buildAppToDir"-prune-o-type f-size +1k-name "*[ii]con*.png" |xargs ls-lsar| gr EP ^-)) #查找带Icon或icon的图标, take the largest picture, ignore the build directory, sort the output #iconimages= by size ($ (Find $unzipAppDir-size +1k-name "*[ii]con*.png")) # Look for icons with icon, take maximum picture iconimageslength=${#iconImages [@]} #获取数组的countcp-F-P ${iconimages[iconimageslength-1]} $ wwwipadir/$iconName #拷贝icon. png file # Check if the file exists if [-f ' $appDir/$ipaName. IPA "]thenecho" packaging $ipaname.ipa successful. ">>$ Logpathelseecho "Package $ipaname.ipa failed." >> $logPathexit 1ficp-f-P $appDir/$ipaName. IPA $wwwIPADir/$ipaName. IPA # Copy IPA file echo "copy $ipaname.ipa to ${wwwipadir} success" >> $logPath ############## #计算文件大小和最后更新时间fileSize = ' stat $appDir/$ Ipaname.ipa |awk ' {if ($8!=4096) {size=size+$8;}} End{print "File Size:", size/1024/1024, "M"} "lastupdatedate=" stat $appDir/$ipaName. IPA | awk ' {print ' Last update time: ', $13,$14,$15,$16} ' echo ' $fileSize >> $logPathecho $lastUpdateDate >> $logPath plistdir=${wwwipadir}/$ipaName. plist #plist文件的路径htmlDir =${wwwipadir}/index.html #html文件的路径 ############## #生成PLIST文件cat << EOF > $plistDir <?xml version= "1.0" encoding= " UTF-8 "? ><! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" ><plist Version= "1.0" ><dict><key>items</key><array><dict><key>assets</key ><array><dict><key>kind</key><string>software-package</string><key >url</key> <string> $url/$ipaName .ipa</string></dict></array><key> metadata</key><dict><key>bundle-identifier</key> <string> $bundleIdentifier </ String><key>bundle-version</key><string> $bundleVersion </string><key>kind</ Key><string>software</string><key>title</key><string> $appName </string> </dict></dict></array></dict></plist>eofecho "Generating plist files to $plisTdir Success ">> $logPath ############## #生成html下载页面cat << EOF > $htmlDir <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
Configuration file Format (Data.json)
[{"name":"项目展示的名称1","projectname":"项目名称1","isworkspace":"是否是workspace","foldername":"项目文件夹名1","buildconfig":"Release/Debug/其他自定义编译名称"},{"name":"项目展示的名称2","projectname":"项目名称2","isworkspace":"是否是workspace","foldername":"项目文件夹名2","buildconfig":"Release/Debug/其他自定义编译名称"}]
PHP file (index.php)

iOS Automated compilation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.