-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPluginProject.ts
More file actions
176 lines (148 loc) · 4.63 KB
/
PluginProject.ts
File metadata and controls
176 lines (148 loc) · 4.63 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { ProjectConfig, Project } from ".";
import {
loadPluginManifest,
PluginManifestLanguage,
pluginManifestLanguages,
isPluginManifestLanguage,
pluginManifestLanguageToBindLanguage,
pluginManifestOverrideCodegenDir,
} from "./manifests";
import { resetDir } from "../system";
import { PluginManifest } from "@polywrap/polywrap-manifest-types-js";
import {
bindSchema,
BindOutput,
BindOptions,
bindLanguageToWrapInfoType,
} from "@polywrap/schema-bind";
import { WrapAbi } from "@polywrap/schema-parse";
import path from "path";
import { latestWrapManifestVersion } from "@polywrap/wrap-manifest-types-js";
export interface PluginProjectConfig extends ProjectConfig {
pluginManifestPath: string;
}
export class PluginProject extends Project<PluginManifest> {
private _pluginManifest: PluginManifest | undefined;
public static cacheLayout = {
root: "plugin",
};
constructor(protected _config: PluginProjectConfig) {
super(_config, {
rootDir: _config.rootDir,
subDir: PluginProject.cacheLayout.root,
});
}
/// Project Base Methods
public reset(): void {
this._pluginManifest = undefined;
this._cache.resetCache();
}
public async validate(): Promise<void> {
const manifest = await this.getManifest();
// Validate language
Project.validateManifestLanguage(
manifest.project.type,
pluginManifestLanguages,
isPluginManifestLanguage
);
}
/// Manifest (polywrap.plugin.yaml)
public async getName(): Promise<string> {
return (await this.getManifest()).project.name;
}
public async getManifest(): Promise<PluginManifest> {
if (!this._pluginManifest) {
this._pluginManifest = await loadPluginManifest(
this.getManifestPath(),
this.logger
);
}
return Promise.resolve(this._pluginManifest);
}
public getManifestDir(): string {
return path.dirname(this._config.pluginManifestPath);
}
public getManifestPath(): string {
return this._config.pluginManifestPath;
}
public async getManifestLanguage(): Promise<PluginManifestLanguage> {
const language = (await this.getManifest()).project.type;
Project.validateManifestLanguage(
language,
pluginManifestLanguages,
isPluginManifestLanguage
);
return language as PluginManifestLanguage;
}
/// Schema
public async getSchemaNamedPath(): Promise<string> {
const manifest = await this.getManifest();
const dir = this.getManifestDir();
if (!manifest.source?.schema) {
throw new Error(
`No schema path specified in project manifest with name "${manifest.project.name}". This should never happen.`
);
}
return path.isAbsolute(manifest.source.schema)
? manifest.source.schema
: path.join(dir, manifest.source.schema);
}
public async getImportAbis(): Promise<PluginManifest["import_abis"]> {
const manifest = await this.getManifest();
return manifest.import_abis || [];
}
public async getGenerationDirectory(
generationSubPath?: string
): Promise<string> {
const manifest = await this.getManifest();
return this._getGenerationDirectory(generationSubPath, manifest);
}
public async generateSchemaBindings(
abi: WrapAbi,
generationSubPath?: string,
bindgenUri?: string
): Promise<BindOutput> {
const moduleDirectory = await this.getGenerationDirectory(
generationSubPath
);
// Clean the code generation
resetDir(moduleDirectory);
const bindLanguage = pluginManifestLanguageToBindLanguage(
await this.getManifestLanguage()
);
const options: BindOptions = {
bindLanguage,
wrapInfo: {
version: latestWrapManifestVersion,
name: await this.getName(),
type: bindLanguageToWrapInfoType(bindLanguage),
abi,
},
outputDirAbs: moduleDirectory,
};
return bindSchema(options, bindgenUri);
}
private _getGenerationDirectory(
useDefinedPath: string | undefined,
manifest: PluginManifest,
defaultDir = "./src/wrap"
): string {
const genPath =
// 1. Use what the user has specified
useDefinedPath ||
// 2. Check to see if there exists an override for this language type
pluginManifestOverrideCodegenDir(
manifest.project.type as PluginManifestLanguage
) ||
// 3. If a module path exists, generate within a "wrap" dir next to it
(manifest.source?.module &&
path.join(path.dirname(manifest.source.module), "wrap")) ||
// 4. Use the default
defaultDir;
if (path.isAbsolute(genPath)) {
return genPath;
} else {
return path.join(this.getManifestDir(), genPath);
}
}
}