Skip to content

Commit a13193e

Browse files
authored
Merge pull request #130 from NativeScript/yosifov/add-teams-selector
Yosifov/add teams selector
2 parents 39375f6 + f6ffb8a commit a13193e

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

src/debug-adapter/webKitDebugAdapter.ts

+68-3
Original file line numberDiff line numberDiff line change
@@ -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

692757
function scriptIdToSourceReference(scriptId: WebKitProtocol.Debugger.ScriptId): number {

src/ipc/extensionClient.ts

+4
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@ export class ExtensionClient {
5959
public runRunCommand(args: extProtocol.AnalyticsRunRunCommandArgs): Promise<any> {
6060
return this.callRemoteMethod('runRunCommand', args);
6161
}
62+
63+
public selectTeam(): Promise<{ id: string, name: string }> {
64+
return this.callRemoteMethod('selectTeam');
65+
}
6266
}

src/ipc/extensionServer.ts

+60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'path';
22
import * as os from 'os';
3+
import * as fs from 'fs';
34
import * as crypto from 'crypto';
45
import * as vscode from 'vscode';
56
import * as extProtocol from './extensionProtocol';
@@ -68,4 +69,63 @@ export class ExtensionServer {
6869
public runRunCommand(args: extProtocol.AnalyticsRunRunCommandArgs): Promise<any> {
6970
return Services.analyticsService().runRunCommand(args.platform);
7071
}
72+
73+
public selectTeam() : Promise<{ id: string, name: string }> {
74+
return new Promise((resolve, reject) => {
75+
let developmentTeams = this.getDevelopmentTeams();
76+
if(developmentTeams.length > 1) {
77+
let quickPickItems = developmentTeams.map((team) => {
78+
return {
79+
label: team.name,
80+
description: team.id
81+
};
82+
});
83+
vscode.window.showQuickPick(quickPickItems)
84+
.then(val => resolve({
85+
id: val.description,
86+
label: val.label
87+
}));
88+
} else {
89+
resolve();
90+
}
91+
});
92+
}
93+
94+
private getDevelopmentTeams(): Array<{ id: string, name: string }> {
95+
let dir = path.join(process.env.HOME, "Library/MobileDevice/Provisioning Profiles/");
96+
let files = fs.readdirSync(dir);
97+
let teamIds: any = {};
98+
for (let file of files) {
99+
let filePath = path.join(dir, file);
100+
let data = fs.readFileSync(filePath, { encoding: "utf8" });
101+
let teamId = this.getProvisioningProfileValue("TeamIdentifier", data);
102+
let teamName = this.getProvisioningProfileValue("TeamName", data);
103+
if (teamId) {
104+
teamIds[teamId] = teamName;
105+
}
106+
}
107+
108+
let teamIdsArray = new Array<{ id: string, name: string }>();
109+
for (let teamId in teamIds) {
110+
teamIdsArray.push({ id: teamId, name: teamIds[teamId] });
111+
}
112+
113+
return teamIdsArray;
114+
}
115+
116+
private getProvisioningProfileValue(name: string, text: string): string {
117+
let findStr = "<key>" + name + "</key>";
118+
let index = text.indexOf(findStr);
119+
if (index > 0) {
120+
index = text.indexOf("<string>", index + findStr.length);
121+
if (index > 0) {
122+
index += "<string>".length;
123+
let endIndex = text.indexOf("</string>", index);
124+
let result = text.substring(index, endIndex);
125+
return result;
126+
}
127+
}
128+
129+
return null;
130+
}
71131
}

0 commit comments

Comments
 (0)