@@ -94,19 +94,7 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
9494 this . $errors . fail ( "Xcode is not installed. Make sure you have Xcode installed and added to your PATH" ) ;
9595 }
9696
97- let xcodeBuildVersion = "" ;
98-
99- try {
100- xcodeBuildVersion = this . $childProcess . exec ( "xcodebuild -version | head -n 1 | sed -e 's/Xcode //'" ) . wait ( ) ;
101- } catch ( error ) {
102- this . $errors . fail ( "xcodebuild execution failed. Make sure that you have latest Xcode and tools installed." ) ;
103- }
104-
105- let splitedXcodeBuildVersion = xcodeBuildVersion . split ( "." ) ;
106- if ( splitedXcodeBuildVersion . length === 3 ) {
107- xcodeBuildVersion = `${ splitedXcodeBuildVersion [ 0 ] } .${ splitedXcodeBuildVersion [ 1 ] } ` ;
108- }
109-
97+ let xcodeBuildVersion = this . getXcodeVersion ( ) ;
11098 if ( helpers . versionCompare ( xcodeBuildVersion , IOSProjectService . XCODEBUILD_MIN_VERSION ) < 0 ) {
11199 this . $errors . fail ( "NativeScript can only run in Xcode version %s or greater" , IOSProjectService . XCODEBUILD_MIN_VERSION ) ;
112100 }
@@ -290,6 +278,14 @@ export class IOSProjectService extends projectServiceBaseLib.PlatformProjectServ
290278 ] ) ;
291279
292280 args = args . concat ( ( buildConfig && buildConfig . architectures ) || defaultArchitectures ) ;
281+
282+ let xcodeBuildVersion = this . getXcodeVersion ( ) ;
283+ if ( helpers . versionCompare ( xcodeBuildVersion , "8.0" ) >= 0 ) {
284+ let teamId = this . getDevelopmentTeam ( ) ;
285+ if ( teamId ) {
286+ args = args . concat ( "DEVELOPMENT_TEAM=" + teamId ) ;
287+ }
288+ }
293289 } else {
294290 args = basicArgs . concat ( [
295291 "-sdk" , "iphonesimulator" ,
@@ -1073,6 +1069,124 @@ We will now place an empty obsolete compatability white screen LauncScreen.xib f
10731069 }
10741070 } ) . future < void > ( ) ( ) ;
10751071 }
1072+
1073+ private getXcodeVersion ( ) : string {
1074+ let xcodeBuildVersion = "" ;
1075+
1076+ try {
1077+ xcodeBuildVersion = this . $childProcess . exec ( "xcodebuild -version | head -n 1 | sed -e 's/Xcode //'" ) . wait ( ) ;
1078+ } catch ( error ) {
1079+ this . $errors . fail ( "xcodebuild execution failed. Make sure that you have latest Xcode and tools installed." ) ;
1080+ }
1081+
1082+ let splitedXcodeBuildVersion = xcodeBuildVersion . split ( "." ) ;
1083+ if ( splitedXcodeBuildVersion . length === 3 ) {
1084+ xcodeBuildVersion = `${ splitedXcodeBuildVersion [ 0 ] } .${ splitedXcodeBuildVersion [ 1 ] } ` ;
1085+ }
1086+
1087+ return xcodeBuildVersion ;
1088+ }
1089+
1090+ private getDevelopmentTeams ( ) : Array < { id :string , name : string } > {
1091+ let dir = path . join ( process . env . HOME , "Library/MobileDevice/Provisioning Profiles/" ) ;
1092+ let files = this . $fs . readDirectory ( dir ) . wait ( ) ;
1093+ let teamIds : any = { } ;
1094+ for ( let file of files ) {
1095+ let filePath = path . join ( dir , file ) ;
1096+ let data = this . $fs . readText ( filePath , "utf8" ) . wait ( ) ;
1097+ let teamId = this . getProvisioningProfileValue ( "TeamIdentifier" , data ) ;
1098+ let teamName = this . getProvisioningProfileValue ( "TeamName" , data ) ;
1099+ if ( teamId ) {
1100+ teamIds [ teamId ] = teamName ;
1101+ }
1102+ }
1103+ let teamIdsArray = new Array < { id :string , name : string } > ( ) ;
1104+ for ( let teamId in teamIds ) {
1105+ teamIdsArray . push ( { id :teamId , name :teamIds [ teamId ] } ) ;
1106+ }
1107+ return teamIdsArray ;
1108+ }
1109+
1110+ private getProvisioningProfileValue ( name : string , text : string ) : string {
1111+ let findStr = "<key>" + name + "</key>" ;
1112+ let index = text . indexOf ( findStr ) ;
1113+ if ( index > 0 ) {
1114+ index = text . indexOf ( "<string>" , index + findStr . length ) ;
1115+ if ( index > 0 ) {
1116+ index += "<string>" . length ;
1117+ let endIndex = text . indexOf ( "</string>" , index ) ;
1118+ let result = text . substring ( index , endIndex ) ;
1119+ return result ;
1120+ }
1121+ }
1122+ return null ;
1123+ }
1124+
1125+ private readTeamId ( ) : string {
1126+ let xcconfigFile = path . join ( this . $projectData . appResourcesDirectoryPath , this . platformData . normalizedPlatformName , "build.xcconfig" ) ;
1127+ if ( this . $fs . exists ( xcconfigFile ) . wait ( ) ) {
1128+ let text = this . $fs . readText ( xcconfigFile ) . wait ( ) ;
1129+ let teamId : string ;
1130+ text . split ( / \r ? \n / ) . forEach ( ( line ) => {
1131+ line = line . replace ( / \/ ( \/ ) [ ^ \n ] * $ / , "" ) ;
1132+ if ( line . indexOf ( "DEVELOPMENT_TEAM" ) >= 0 ) {
1133+ teamId = line . split ( "=" ) [ 1 ] . trim ( ) ;
1134+ }
1135+ } ) ;
1136+ if ( teamId ) {
1137+ return teamId ;
1138+ }
1139+ }
1140+ let fileName = path . join ( this . platformData . projectRoot , "teamid" ) ;
1141+ if ( this . $fs . exists ( fileName ) . wait ( ) ) {
1142+ return this . $fs . readText ( fileName ) . wait ( ) ;
1143+ }
1144+ return null ;
1145+ }
1146+
1147+ private getDevelopmentTeam ( ) : string {
1148+ let teamId : string ;
1149+ if ( this . $options . teamId ) {
1150+ teamId = this . $options . teamId ;
1151+ } else {
1152+ teamId = this . readTeamId ( ) ;
1153+ }
1154+ if ( ! teamId ) {
1155+ let teams = this . getDevelopmentTeams ( ) ;
1156+ this . $logger . warn ( "Xcode 8 requires a team id to be specified when building for device." ) ;
1157+ this . $logger . warn ( "You can specify the team id by setting the DEVELOPMENT_TEAM setting in build.xcconfig file located in App_Resources folder of your app, or by using the --teamId option when calling run, debug or livesync commnads." ) ;
1158+ if ( teams . length === 1 ) {
1159+ teamId = teams [ 0 ] . id ;
1160+ this . $logger . warn ( "Found and using the following development team installed on your system: " + teams [ 0 ] . name + " (" + teams [ 0 ] . id + ")" ) ;
1161+ } else if ( teams . length > 0 ) {
1162+ let choices : string [ ] = [ ] ;
1163+ for ( let team of teams ) {
1164+ choices . push ( team . name + " (" + team . id + ")" ) ;
1165+ }
1166+ let choice = this . $prompter . promptForChoice ( 'Found multiple development teams, select one:' , choices ) . wait ( ) ;
1167+ teamId = teams [ choices . indexOf ( choice ) ] . id ;
1168+
1169+ let choicesPersist = [
1170+ "Yes, set the DEVELOPMENT_TEAM setting in build.xcconfig file." ,
1171+ "Yes, persist the team id in platforms folder." ,
1172+ "No, don't persist this setting."
1173+ ] ;
1174+ let choicePersist = this . $prompter . promptForChoice ( "Do you want to make teamId: " + teamId + " a persistent choice for your app?" , choicesPersist ) . wait ( ) ;
1175+ switch ( choicesPersist . indexOf ( choicePersist ) ) {
1176+ case 0 :
1177+ let xcconfigFile = path . join ( this . $projectData . appResourcesDirectoryPath , this . platformData . normalizedPlatformName , "build.xcconfig" ) ;
1178+ this . $fs . appendFile ( xcconfigFile , "\nDEVELOPMENT_TEAM = " + teamId + "\n" ) . wait ( ) ;
1179+ break ;
1180+ case 1 :
1181+ this . $fs . writeFile ( path . join ( this . platformData . projectRoot , "teamid" ) , teamId ) ;
1182+ break ;
1183+ default :
1184+ break ;
1185+ }
1186+ }
1187+ }
1188+ return teamId ;
1189+ }
10761190}
10771191
10781192$injector . register ( "iOSProjectService" , IOSProjectService ) ;
0 commit comments