@@ -133,11 +133,11 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
133133 Services . logger ( ) . log ( `${ args . request } (${ JSON . stringify ( args ) } )` ) ;
134134 }
135135
136- private processRequest ( args : DebugProtocol . IRequestArgs ) : Promise < void > {
136+ private async processRequest ( args : DebugProtocol . IRequestArgs ) {
137137 // Initialize the request
138138 this . configureLoggingForRequest ( args ) ;
139139 Services . appRoot = args . appRoot ;
140- return Services . extensionClient ( ) . getInitSettings ( ) . then ( settings => {
140+ return Services . extensionClient ( ) . getInitSettings ( ) . then ( async settings => {
141141 Services . cliPath = settings . tnsPath || Services . cliPath ;
142142 this . _request = new DebugRequest ( args , Services . cli ( ) ) ;
143143 Services . extensionClient ( ) . analyticsLaunchDebugger ( { request : args . request , platform : args . platform } ) ;
@@ -147,7 +147,22 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
147147 Services . logger ( ) . log ( '[NSDebugAdapter] Running tns command...' , Tags . FrontendMessage ) ;
148148 let cliCommand : DebugResult ;
149149 if ( this . _request . isLaunch ) {
150- cliCommand = this . _request . project . debug ( { stopOnEntry : this . _request . launchArgs . stopOnEntry , watch : this . _request . launchArgs . watch } , this . _request . args . tnsArgs ) ;
150+ let tnsArgs = this . _request . args . tnsArgs ;
151+
152+ // For iOS the TeamID is required if there's more than one.
153+ // Therefore if not set, show selection to the user.
154+ if ( args . platform && args . platform . toLowerCase ( ) === 'ios' ) {
155+ let teamId = this . getTeamId ( path . join ( Services . appRoot , 'app' ) , this . _request . args . tnsArgs ) ;
156+ if ( ! teamId ) {
157+ let selectedTeam = ( await Services . extensionClient ( ) . selectTeam ( ) ) ;
158+ if ( selectedTeam ) {
159+ // add the selected by the user Team Id
160+ tnsArgs = ( tnsArgs || [ ] ) . concat ( [ '--teamId' , selectedTeam . id ] ) ;
161+ }
162+ }
163+ }
164+
165+ cliCommand = this . _request . project . debug ( { stopOnEntry : this . _request . launchArgs . stopOnEntry , watch : this . _request . launchArgs . watch } , tnsArgs ) ;
151166 }
152167 else if ( this . _request . isAttach ) {
153168 cliCommand = this . _request . project . attach ( this . _request . args . tnsArgs ) ;
@@ -687,6 +702,56 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
687702 private scriptIsNotUnknown ( scriptId : WebKitProtocol . Debugger . ScriptId ) : boolean {
688703 return ! ! this . _scriptsById . get ( scriptId ) ;
689704 }
705+
706+ private getTeamId ( appRoot : string , tnsArgs ?: string [ ] ) : string {
707+ // try to get the TeamId from the TnsArgs
708+ if ( tnsArgs ) {
709+ const teamIdArgIndex = tnsArgs . indexOf ( '--teamId' ) ;
710+ if ( teamIdArgIndex > 0 && teamIdArgIndex + 1 < tnsArgs . length ) {
711+ return tnsArgs [ teamIdArgIndex + 1 ] ;
712+ }
713+ }
714+
715+ // try to get the TeamId from the buildxcconfig or teamid file
716+ const teamIdFromConfig = this . readTeamId ( appRoot ) ;
717+ if ( teamIdFromConfig ) {
718+ return teamIdFromConfig ;
719+ }
720+
721+ // we should get the Teams from the machine and ask the user if they are more than 1
722+ return null ;
723+ }
724+
725+ private readXCConfig ( appRoot : string , flag : string ) : string {
726+ let xcconfigFile = path . join ( appRoot , "App_Resources/iOS/build.xcconfig" ) ;
727+ if ( fs . existsSync ( xcconfigFile ) ) {
728+ let text = fs . readFileSync ( xcconfigFile , { encoding : 'utf8' } ) ;
729+ let teamId : string ;
730+ text . split ( / \r ? \n / ) . forEach ( ( line ) => {
731+ line = line . replace ( / \/ ( \/ ) [ ^ \n ] * $ / , "" ) ;
732+ if ( line . indexOf ( flag ) >= 0 ) {
733+ teamId = line . split ( "=" ) [ 1 ] . trim ( ) ;
734+ if ( teamId [ teamId . length - 1 ] === ';' ) {
735+ teamId = teamId . slice ( 0 , - 1 ) ;
736+ }
737+ }
738+ } ) ;
739+ if ( teamId ) {
740+ return teamId ;
741+ }
742+ }
743+
744+ let fileName = path . join ( appRoot , "teamid" ) ;
745+ if ( fs . existsSync ( fileName ) ) {
746+ return fs . readFileSync ( fileName , { encoding : 'utf8' } ) ;
747+ }
748+
749+ return null ;
750+ }
751+
752+ private readTeamId ( appRoot ) : string {
753+ return this . readXCConfig ( appRoot , "DEVELOPMENT_TEAM" ) ;
754+ }
690755}
691756
692757function scriptIdToSourceReference ( scriptId : WebKitProtocol . Debugger . ScriptId ) : number {
0 commit comments