-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcreateTag.ts
198 lines (184 loc) · 7.14 KB
/
createTag.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
import { ProgressLocation, QuickPickItem, Uri, window, workspace } from "vscode";
import { functionsClient } from "../../client";
import { getTarReadStream } from "../../utils/tar";
import { ext } from "../../extensionVariables";
import * as fs from "fs";
import { TagsTreeItem } from "../../tree/functions/tags/TagsTreeItem";
import { selectWorkspaceFolder } from "../../utils/workspace";
import { ProgressMessage } from "../../utils/types";
import { Tag } from "../../appwrite";
import { activateTag } from "./activateTag";
import { execSync } from "child_process";
export async function createTag(item?: TagsTreeItem | Uri): Promise<void> {
if (item instanceof Uri) {
const functions = await functionsClient?.list();
if (functions === undefined) {
return;
}
const pick = await window.showQuickPick(
functions.functions.map<QuickPickItem>(
(func): QuickPickItem => ({ label: func.name, description: func.env, detail: func.$id })
),
{ placeHolder: "Select a function to create tag" }
);
if (pick === undefined || pick.detail === undefined) {
return;
}
const tags = await functionsClient?.listTags(pick.detail);
let value;
if (tags && tags.tags.length > 0) {
value = tags.tags[tags.tags.length - 1].command;
}
const command = await window.showInputBox({ value, prompt: "Command to run your code" });
if (command === undefined) {
return;
}
const tag = await window.withProgress(
{ location: ProgressLocation.Notification, title: "Creating tag..." },
async (progress, _token) => {
if (pick.detail === undefined) {
return;
}
return await createTagFromUri(pick.detail, command, item.path, progress, "", "");
}
);
if (tag) {
await tagNotification(tag);
}
return;
}
if (item instanceof TagsTreeItem) {
const func = item.parent.func;
const folder = await selectWorkspaceFolder("Select folder of your function code.");
if (folder === undefined || folder === "") {
return;
}
const tags = await functionsClient?.listTags(func.$id);
let value;
if (tags && tags.tags.length > 0) {
value = tags.tags[tags.tags.length - 1].command;
}
const command = await window.showInputBox({ value, prompt: "Command to run your code" });
if (command === undefined) {
return;
}
// there is a special way to publishing a tag for .net5.0
const envPick = await window.showQuickPick(
[".net5.0", "other"],
{ placeHolder: "Select runtime" }
);
let customTarCreationCommand = "";
let customTarPath = "";
try {
if (envPick === ".net5.0") {
const comm = "dotnet publish --runtime linux-x64 --framework net5.0 --no-self-contained " + folder;
execSync(comm);
if (fs.statSync(folder + "/bin/Debug/net5.0/linux-x64/")) {
customTarPath = folder + "/code.tar.gz";
customTarCreationCommand = "tar -C " + folder + "/bin/Debug/net5.0/linux-x64 -zcvf " + customTarPath + " publish";
}
}
} catch (error) {
window.showErrorMessage("Error publishing .net archive.\n" + error);
}
const tag = await window.withProgress(
{ location: ProgressLocation.Notification, title: "Creating tag..." },
async (progress, _token) => {
return await createTagFromUri(func.$id, command, folder, progress, customTarCreationCommand, customTarPath);
}
);
if (tag) {
await tagNotification(tag);
return;
}
}
if (item === undefined) {
const functions = await functionsClient?.list();
if (functions === undefined) {
return;
}
const pick = await window.showQuickPick(
functions.functions.map<QuickPickItem>(
(func): QuickPickItem => ({ label: func.name, description: func.env, detail: func.$id })
),
{ placeHolder: "Select a function to create tag" }
);
if (pick === undefined || pick.detail === undefined) {
return;
}
const funcId = pick.detail;
const folder = await selectWorkspaceFolder("Select folder of your function code.");
const tags = await functionsClient?.listTags(funcId);
let value;
if (tags && tags.tags.length > 0) {
value = tags.tags[tags.tags.length - 1].command;
}
const command = await window.showInputBox({ value, prompt: "Command to run your code" });
if (command === undefined) {
return;
}
const tag = await window.withProgress(
{ location: ProgressLocation.Notification, title: "Creating tag..." },
async (progress, _token) => {
return await createTagFromUri(funcId, command, folder, progress, "", "");
}
);
if (tag) {
await tagNotification(tag);
return;
}
}
}
async function createTagFromUri(functionId: string, command: string, folder: string, progress: ProgressMessage, customTarCreationCommand: string, customTarPath: string): Promise<Tag | undefined> {
progress.report({ message: "Creating tarball", increment: 10 });
if (functionsClient === undefined) {
return;
}
let tarFilePath;
try {
if (customTarCreationCommand !== "") {
execSync(customTarCreationCommand);
if (fs.statSync(customTarPath)) {
tarFilePath = customTarPath;
} else {
window.showErrorMessage("Error creating tar file.\n");
}
}
else {
tarFilePath = await getTarReadStream(Uri.parse(folder));
}
} catch (e) {
window.showErrorMessage("Error creating tar file.\n" + e);
return;
}
if (tarFilePath === undefined) {
window.showErrorMessage("Failed to create tar file.");
ext.outputChannel.appendLog("Failed to create tar file.");
return;
}
// somehow makes the upload work
await workspace.fs.readFile(Uri.file(tarFilePath));
progress.report({ message: "Uploading tag", increment: 60 });
try {
return await functionsClient.createTag(functionId, command, fs.createReadStream(tarFilePath));
} catch (e) {
ext.outputChannel.appendLog("Creating tag error: " + e);
}
}
async function tagNotification(tag: Tag): Promise<void> {
ext.tree?.functions?.refresh();
if (tag) {
const action = await window.showInformationMessage(
`Successfully created tag with size ${tag.size}B.`,
"Activate tag",
"View in console"
);
if (action === "Activate tag") {
await activateTag(tag);
}
if (action === "View in console") {
//
}
return;
}
}