-
Notifications
You must be signed in to change notification settings - Fork 977
/
Copy pathsdk-generation.ts
241 lines (222 loc) · 7.26 KB
/
sdk-generation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import * as vscode from "vscode";
import { Uri } from "vscode";
import { firstWhere, firstWhereDefined } from "../utils/signal";
import { currentOptions } from "../options";
import { dataConnectConfigs, ResolvedConnectorYaml } from "./config";
import { runCommand, setTerminalEnvVars } from "./terminal";
import { ExtensionBrokerImpl } from "../extension-broker";
import { DATA_CONNECT_EVENT_NAME } from "../analytics";
import { getPlatformFromFolder } from "../../../src/dataconnect/fileUtils";
import { ConnectorYaml, Platform } from "../../../src/dataconnect/types";
import * as yaml from "yaml";
import * as fs from "fs-extra";
import { getSettings } from "../utils/settings";
import {
FDC_APP_FOLDER,
generateSdkYaml,
} from "../../../src/init/features/dataconnect/sdk";
import { createE2eMockable } from "../utils/test_hooks";
import { AnalyticsLogger} from "../analytics";
export function registerFdcSdkGeneration(
broker: ExtensionBrokerImpl,
analyticsLogger: AnalyticsLogger,
): vscode.Disposable {
const settings = getSettings();
// For testing purposes.
const selectFolderSpy = createE2eMockable(
async () => {
return selectAppFolder();
},
"select-folder",
async () => {
return Promise.resolve("src/test/test_projects/fishfood/test-node-app");
},
);
const initSdkCmd = vscode.commands.registerCommand(
"fdc.init-sdk",
(args: { appFolder: string }) => {
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.INIT_SDK_CLI);
// Lets do it from the right directory
const e: Record<string, string> = {}
e[FDC_APP_FOLDER] = args.appFolder;
setTerminalEnvVars(e);
runCommand(`${settings.firebasePath} init dataconnect:sdk`);
},
);
// codelense from inside connector.yaml file
const configureSDKCodelense = vscode.commands.registerCommand(
"fdc.connector.configure-sdk",
async (connectorConfig) => {
analyticsLogger.logger.logUsage(
DATA_CONNECT_EVENT_NAME.INIT_SDK_CODELENSE,
);
const configs = await firstWhereDefined(dataConnectConfigs).then(
(c) => c.requireValue,
);
await openAndWriteYaml(connectorConfig);
},
);
// Sidebar "configure generated sdk" button
const configureSDK = vscode.commands.registerCommand(
"fdc.configure-sdk",
async () => {
analyticsLogger.logger.logUsage(DATA_CONNECT_EVENT_NAME.INIT_SDK);
const configs = await firstWhereDefined(dataConnectConfigs).then(
(c) => c.requireValue,
);
// pick service, auto pick if only one
const pickedService =
configs?.serviceIds.length === 1
? configs?.serviceIds[0]
: await pickService(configs?.serviceIds ?? []);
if (!pickedService) {
return;
}
const serviceConfig = configs?.findById(pickedService);
const connectorIds = serviceConfig?.connectorIds;
// pick connector for service, auto pick if only one
const pickedConnectorId =
connectorIds?.length === 1
? connectorIds[0]
: await pickConnector(connectorIds);
if (pickedConnectorId) {
const connectorConfig =
serviceConfig?.findConnectorById(pickedConnectorId);
if (connectorConfig) {
await openAndWriteYaml(connectorConfig);
}
}
},
);
async function openAndWriteYaml(connectorConfig: ResolvedConnectorYaml) {
const connectorYamlPath = Uri.joinPath(
Uri.file(connectorConfig.path),
"connector.yaml",
);
const connectorYaml = connectorConfig.value;
// open connector.yaml file
await vscode.window.showTextDocument(connectorYamlPath);
const appFolder = await selectFolderSpy.call();
if (appFolder) {
await writeYaml(
appFolder,
connectorYamlPath,
connectorConfig.path, // needed for relative path comparison
connectorYaml,
);
}
}
async function selectAppFolder() {
// confirmation prompt for selecting app folder; skip if configured to skip
const configs = vscode.workspace.getConfiguration("firebase.dataConnect");
const skipToAppFolderSelect = "skipToAppFolderSelect";
if (!configs.get(skipToAppFolderSelect)) {
const result = await vscode.window.showInformationMessage(
"Please select your app folder to generate an SDK for.",
{ modal: true },
"Yes",
"Don't show again",
);
if (result !== "Yes" && result !== "Don't show again") {
return;
}
if (result === "Don't show again") {
configs.update(
skipToAppFolderSelect,
true,
vscode.ConfigurationTarget.Global,
);
}
}
// open app folder selector
const folderUris: Uri[] | undefined = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
title: "Select your app folder to link Data Connect to:",
openLabel: "Select app folder",
});
if (!folderUris?.length) {
return;
}
return folderUris[0].fsPath; // can only pick one folder, but return type is an array
}
async function writeYaml(
appFolder: string,
connectorYamlPath: Uri,
connectorYamlFolderPath: string,
connectorYaml: ConnectorYaml,
) {
const platform = await getPlatformFromFolder(appFolder);
// if app platform undetermined, run init command
if (platform === Platform.NONE || platform === Platform.MULTIPLE) {
vscode.window.showErrorMessage(
"Could not determine platform for specified app folder. Configuring from command line.",
);
vscode.commands.executeCommand("fdc.init-sdk", { appFolder });
} else {
// generate yaml
const newConnectorYaml = await generateSdkYaml(
platform,
connectorYaml,
connectorYamlFolderPath,
appFolder,
);
const connectorYamlContents = yaml.stringify(newConnectorYaml);
fs.writeFileSync(connectorYamlPath.fsPath, connectorYamlContents, "utf8");
}
}
const configureSDKSub = broker.on("fdc.configure-sdk", async () =>
vscode.commands.executeCommand("fdc.configure-sdk"),
);
return vscode.Disposable.from(
initSdkCmd,
configureSDK,
configureSDKCodelense,
{
dispose: configureSDKSub,
},
);
}
async function pickService(serviceIds: string[]): Promise<string | undefined> {
const options = firstWhere(
currentOptions,
(options) => options.project?.length !== 0,
).then((options) => {
return serviceIds.map((serviceId) => {
return {
label: serviceId,
options,
picked: true,
};
});
});
const picked = await vscode.window.showQuickPick<{ label: string }>(options, {
title: "Select service",
canPickMany: false,
});
return picked?.label;
}
async function pickConnector(
connectorIds: string[] | undefined,
): Promise<string | undefined> {
const options = firstWhere(
currentOptions,
(options) => options.project?.length !== 0,
).then((options) => {
return connectorIds?.map((connectorId) => {
return {
label: connectorId,
options,
picked: true,
};
});
});
const picked = await vscode.window.showQuickPick<{ label: string }>(
options as any,
{
title: `Select connector to generate SDK for.`,
canPickMany: false,
},
);
return picked?.label;
}