-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBaseTemplateManager.ts
More file actions
159 lines (140 loc) · 4.86 KB
/
BaseTemplateManager.ts
File metadata and controls
159 lines (140 loc) · 4.86 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
import * as path from "path";
import { Framework, ProjectLibrary, Template } from "../types";
import { ProjectConfig, Util } from "../util";
export abstract class BaseTemplateManager {
protected frameworks: Framework[] = [];
constructor(private templatesAbsPath: string) {
// read dirs and push dir names into frameworks
const frameworks = Util.getDirectoryNames(this.templatesAbsPath);
// load and initialize templates
for (const framework of frameworks) {
this.frameworks.push(require(path.join(this.templatesAbsPath, framework)) as Framework);
}
// load external templates
this.loadExternalTemplates();
}
public getFrameworkIds(): string[] {
return this.frameworks.map(f => f.id);
}
public getFrameworkNames(): string[] {
// exclude WebComponents from the Step-By-Step wizard
return this.frameworks.map(f => f.name);
}
/** Returns framework found by its name or undefined. */
public getFrameworkByName(name: string): Framework {
return this.frameworks.find(s => s.name === name);
}
/** Returns framework found by its ID or undefined. */
public getFrameworkById(id: string): Framework {
return this.frameworks.find(f => f.id === id);
}
/**
* Get ProjectLibrary Names list
* @param frameworkId
* @returns Returns projectLibrary names array
*/
public getProjectLibraryNames(frameworkId: string): string[] {
let projects = [];
const framework = this.frameworks.find(f => f.id === frameworkId);
if (framework) {
projects = framework.projectLibraries.map(x => x.name);
}
return projects;
}
/**
* Get ProjectLibrary by name
* @param framework
* @param name
* @returns Returns matching projectLibrary or undefined
*/
public getProjectLibraryByName(framework: Framework, name: string): ProjectLibrary {
let projectLib: ProjectLibrary;
if (name) {
projectLib = framework.projectLibraries.find(x => x.name === name);
}
return projectLib;
}
/**
* Get a specific project library
* @param frameworkId
* @param projectType
* @returns Returns projectLibrary, or null.
*/
public getProjectLibrary(frameworkId: string, projectType?: string): ProjectLibrary {
const framework = this.frameworks.find(f => f.id === frameworkId);
if (framework) {
if (projectType) {
return framework.projectLibraries.find(x => x.projectType === projectType);
} else {
return framework.projectLibraries.length > 0 ? framework.projectLibraries[0] : null;
}
}
return null;
}
public updateProjectConfiguration(template: Template) {
const config = ProjectConfig.getConfig();
// add each separately to avoid duplicates:
for (const element of template.dependencies) {
if (typeof element === "string" && config.project.components.indexOf(element) === -1) {
config.project.components.push(element);
}
}
ProjectConfig.setConfig(config);
}
//#region plugin templates
/**
* Loads properties from a JSON file and initializes a base Template implementation
* @param filePath Path to a json config file representing a template
* @returns null if no proper file is found
*/
protected abstract loadFromConfig(filePath: string): Template;
/** Read config and load custom templates based on type */
private loadExternalTemplates() {
const config = ProjectConfig.getConfig();
const customTemplates: Template[] = [];
for (const entry of config.customTemplates) {
const [protocol, value] = entry.split(/(^[^:]+):/).filter(x => x);
let templateDir = protocol === "file" || protocol === "path"
? value
: entry;
templateDir = templateDir.replace(/template\.json$/, "");
if (!Util.directoryExists(templateDir)) {
// TODO: Util.log(`Ignored: Incorrect custom template path for "${entry}".`);
continue;
}
// try single template
let template = this.loadFromConfig(path.join(templateDir, "template.json"));
if (template !== null) {
customTemplates.push(template);
continue;
}
// try folder of templates:
for (const folder of Util.getDirectoryNames(templateDir)) {
template = this.loadFromConfig(path.join(templateDir, folder, "template.json"));
if (template !== null) {
customTemplates.push(template);
}
}
}
this.addTemplates(customTemplates);
}
private addTemplates(templates: Template[]) {
for (const template of templates) {
const projectLib = this.getProjectLibrary(template.framework, template.projectType);
if (!projectLib) {
Util.error(`The framework/project type for template with id "${template.id}" is not supported.`);
continue;
}
if (projectLib.hasTemplate(template.id)) {
Util.error(`Template with id "${template.id}" already exists.`);
continue;
}
if (projectLib.getComponentGroupNames().indexOf(template.controlGroup) === -1 && !template.listInCustomTemplates) {
Util.error(`No supported group for template with id "${template.id}".`);
continue;
}
projectLib.registerTemplate(template);
}
}
//#endregion
}