-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathRemoteDocument.ts
More file actions
160 lines (133 loc) · 5.71 KB
/
RemoteDocument.ts
File metadata and controls
160 lines (133 loc) · 5.71 KB
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
/*!
* Copyright 2018-2021 VMware, Inc.
* SPDX-License-Identifier: MIT
*/
import * as path from "path"
import { Logger, VroRestClient } from "@vmware/vrdt-common"
import * as AdmZip from "adm-zip"
import { XMLParser } from "fast-xml-parser"
import * as fs from "fs-extra"
import * as vscode from "vscode"
import { ContentLocation } from "./ContentLocation"
export class RemoteDocument {
private readonly logger = Logger.get("RemoteDocument")
private source: string
constructor(public uri: vscode.Uri, private restClient: VroRestClient, private storagePath: string) {}
get value(): string | Thenable<string> {
if (!this.source) {
return this.fetch()
}
return this.source
}
private fetch(): Thenable<string> {
return vscode.window.withProgress(
{
location: vscode.ProgressLocation.Window
},
progress => {
return new Promise(async (resolve, reject) => {
this.logger.info(`Fetching resource: ${this.uri.toString()}`)
const location = ContentLocation.from(this.uri)
progress.report({ message: `Fetching ${location.name}...` })
try {
const filePath = path.join(this.storagePath, location.id)
switch (location.type) {
case "action": {
await this.restClient.fetchAction(location.id, filePath)
const fileBuffer = new AdmZip(filePath).readFile("action-content")
if (!fileBuffer) {
throw new Error(`Could not extract action content from $filePath`)
}
this.source = fileBuffer // content is in UTF-16BE
.swap16() // convert to UTF-16LE
.toString("utf16le")
this.source = this.toJavaScript(this.source)
fs.removeSync(filePath)
break
}
case "workflow": {
await this.restClient.fetchWorkflow(location.id, filePath)
const fileBuffer = new AdmZip(filePath).readFile("workflow-content")
if (!fileBuffer) {
throw new Error(`Could not extract action content from $filePath`)
}
this.source = fileBuffer // content is in UTF-16BE
.swap16() // convert to UTF-16LE
.toString("utf16le")
fs.removeSync(filePath)
break
}
case "config": {
this.source = await this.restClient.getConfigElementXml(location.id)
break
}
case "resource": {
await this.restClient.fetchResource(location.id, filePath)
this.source = fs.readFileSync(filePath).toString()
fs.removeSync(filePath)
break
}
default:
throw new Error(`Unknow resource type: ${location.type}`)
}
this.logger.info(`Successfully fetched resource: ${location}`)
resolve(this.source)
} catch (err) {
this.logger.error(`Failed fetching resource '${location}'. Error: `, err)
reject(err)
vscode.window.showErrorMessage(
`Failed fetching resource (${err.code}). See logs for more information.`
)
}
})
}
)
}
private toJavaScript(source: string): string {
interface ParamInfo {
"@n": string
"@t": string
"#text": string
}
let xml = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "@" }).parse(source)
xml = xml["dunes-script-module"]
let js = "/**\n"
if (xml.description) {
const description: string = xml.description
js += description
.trim()
.split("\n")
.map(line => ` * ${line}`)
.join("\n")
js += "\n"
}
js += " * \n"
const params: ParamInfo[] = Array.isArray(xml.param) || !xml.param ? xml.param : [xml.param]
if (params && params.length > 0) {
js += params
.map(p => {
const desc = p["#text"] ? ` - ${p["#text"]}` : ""
return ` * @param {${p["@t"]}} ${p["@n"]}${desc}`
})
.join("\n")
js += "\n"
}
js += ` * @return {${xml["@result-type"] || "Any"}}\n`
js += ` */\n`
js += `(function (`
if (params && params.length > 0) {
js += params.map(p => p["@n"]).join(",")
}
js += ") {\n"
if (xml.script) {
const script: string = xml.script["#text"]
js += script
.trim()
.split("\n")
.map(line => `\t${line}`)
.join("\n")
}
js += "\n});\n"
return js
}
}