@manuth/generator-wsc-package
Version:
A Generator for WoltLab Suite Core Packages.
325 lines • 13.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { EOL } from "node:os";
import { GeneratorSettingKey } from "@manuth/extended-yo-generator";
import { TSProjectSettingKey } from "@manuth/generator-ts-project";
import { printNode, SyntaxKind, ts, VariableDeclarationKind } from "ts-morph";
import { InstructionComponent } from "../../../Components/InstructionComponent.js";
import { WoltLabTypeScriptFileMapping } from "../../../FileMappings/WoltLabTypeScriptFileMapping.js";
import { WoltLabComponentSettingKey } from "../../../Settings/WoltLabComponentSettingKey.js";
import { WoltLabSettingKey } from "../../../Settings/WoltLabSettingKey.js";
/**
* Provides the functionality to generate a package-file.
*
* @template TSettings
* The type of the generator-settings.
*
* @template TOptions
* The type of the generator-options.
*/
export class WoltLabPackageFileMapping extends WoltLabTypeScriptFileMapping {
/**
* Initializes a new instance of the {@link WoltLabPackageFileMapping `WoltLabPackageFileMapping<TSettings, TOptions>`} class.
*
* @param generator
* The component to create an instruction-file for.
*/
constructor(generator) {
super(generator);
this.packageGenerator = generator;
}
/**
* @inheritdoc
*/
get Generator() {
return this.packageGenerator;
}
/**
* @inheritdoc
*/
get Destination() {
return this.Generator.sourcePath(`${this.Generator.PackageVariableName}.ts`);
}
/**
* Gets the creation-date for the package-metadata.
*/
get CreationDate() {
let date = new Date();
return `${date.getFullYear()}-${`${date.getMonth() + 1}`.padStart(2, "0")}-${`${date.getDate()}`.padStart(2, "0")}`;
}
/**
* Gets options to pass to the constructor of the {@link RequiredPackageDescriptor `RequiredPackageDescriptor`} class.
*/
get RequiredPackageOptions() {
let options = this.GetObjectLiteral();
options.addPropertyAssignments([
{
name: "Identifier",
initializer: printNode(ts.factory.createStringLiteral("com.woltlab.wcf"))
},
{
name: "MinVersion",
initializer: printNode(ts.factory.createStringLiteral("3.0.0"))
}
]);
return options;
}
/**
* Gets a constructor-call of the {@link RequiredPackageDescriptor `RequiredPackageDescriptor`} class.
*/
get RequiredPackageConstructor() {
let constructor = this.GetConstructorCall("RequiredPackageDescriptor");
constructor.addArgument(`${EOL}${this.RequiredPackageOptions.getFullText()}`);
return constructor;
}
/**
* Gets options to pass to the constructor of the {@link ConflictingPackageDescriptor `ConflictingPackageDescriptor`} class.
*/
get ConflictingPackageOptions() {
let options = this.GetObjectLiteral();
options.addPropertyAssignments([
{
name: "Identifier",
initializer: printNode(ts.factory.createStringLiteral("com.woltlab.wcf"))
},
{
name: "Version",
initializer: printNode(ts.factory.createStringLiteral("6.0.0 Alpha 1"))
}
]);
return options;
}
/**
* Gets a constructor-call of the {@link ConflictingPackageDescriptor `ConflictingPackageDescriptor`} class.
*/
get ConflictingPackageConstructor() {
let constructor = this.GetConstructorCall("ConflictingPackageDescriptor");
constructor.addArgument(`${EOL}${this.ConflictingPackageOptions.getFullText()}`);
return constructor;
}
/**
* Gets the options to pass to the package-constructor.
*/
get PackageOptions() {
var _a, _b, _c, _d, _e;
let options = this.GetObjectLiteral();
let displayName = this.GetObjectLiteral();
let author = this.GetObjectLiteral();
let description = this.GetObjectLiteral();
let installSet = this.GetObjectLiteral();
let requiredPackages = this.GetArrayLiteral();
let conflictingPackages = this.GetArrayLiteral();
displayName.addPropertyAssignment({
name: printNode(ts.factory.createComputedPropertyName(ts.factory.createIdentifier("InvariantCultureName"))),
initializer: printNode(ts.factory.createStringLiteral((_a = this.Generator.Settings[TSProjectSettingKey.DisplayName]) !== null && _a !== void 0 ? _a : ""))
});
author.addPropertyAssignments([
{
name: "Name",
initializer: printNode(ts.factory.createStringLiteral((_b = this.Generator.Settings[WoltLabSettingKey.Author]) !== null && _b !== void 0 ? _b : ""))
},
{
name: "URL",
initializer: printNode(ts.factory.createStringLiteral((_c = this.Generator.Settings[WoltLabSettingKey.HomePage]) !== null && _c !== void 0 ? _c : ""))
}
]);
description.addPropertyAssignment({
name: printNode(ts.factory.createComputedPropertyName(ts.factory.createIdentifier("InvariantCultureName"))),
initializer: printNode(ts.factory.createStringLiteral((_d = this.Generator.Settings[TSProjectSettingKey.Description]) !== null && _d !== void 0 ? _d : ""))
});
installSet.addPropertyAssignment({
name: "Instructions",
initializer: printNode(ts.factory.createArrayLiteralExpression())
});
requiredPackages.addElement(`${EOL}${this.RequiredPackageConstructor.getFullText()}${EOL}`);
conflictingPackages.addElement(`${EOL}${this.ConflictingPackageConstructor.getFullText()}${EOL}`);
options.addPropertyAssignments([
{
name: "Identifier",
initializer: printNode(ts.factory.createStringLiteral((_e = this.Generator.Settings[WoltLabSettingKey.Identifier]) !== null && _e !== void 0 ? _e : ""))
},
{
name: "DisplayName",
initializer: displayName.getFullText()
},
{
name: "Version",
initializer: printNode(ts.factory.createStringLiteral("0.0.0"))
},
{
name: "Author",
initializer: author.getFullText()
},
{
name: "License",
initializer: printNode(ts.factory.createNull())
},
{
name: "CreationDate",
initializer: printNode(ts.factory.createNewExpression(ts.factory.createIdentifier("Date"), [], [
ts.factory.createStringLiteral(this.CreationDate)
]))
},
{
name: "Description",
initializer: description.getFullText()
},
{
name: "InstallSet",
initializer: installSet.getFullText()
},
{
name: "RequiredPackages",
initializer: requiredPackages.getFullText()
},
{
name: "ConflictingPackages",
initializer: conflictingPackages.getFullText()
}
]);
return options;
}
/**
* Gets an {@link ObjectLiteralExpression `ObjectLiteralExpression`}-node.
*
* @returns
* The newly created {@link ObjectLiteralExpression `ObjectLiteralExpression`}.
*/
GetObjectLiteral() {
return this.WrapNode(ts.factory.createParenthesizedExpression(ts.factory.createObjectLiteralExpression())).getExpressionIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
}
/**
* Gets a constructor-call.
*
* @param className
* The name of the class to create a constructor for.
*
* @returns
* The newly created constructor-call.
*/
GetConstructorCall(className) {
return this.WrapNode(ts.factory.createNewExpression(ts.factory.createIdentifier(className), [], []));
}
/**
* Gets an {@link ArrayLiteralExpression `ArrayLiteralExpression`}-node.
*
* @returns
* The newly created {@link ArrayLiteralExpression `ArrayLiteralExpression`}.
*/
GetArrayLiteral() {
return this.WrapNode(ts.factory.createArrayLiteralExpression());
}
/**
* Registers the specified {@link component `component`}.
*
* @param file
* The file to register the specified {@link component `component`} to.
*
* @param component
* The component to register.
*/
AddComponent(file, component) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
let packageOptions;
let installSet;
let installInstructions;
let installSetKey = "InstallSet";
let installInstructionKey = "Instructions";
file.addImportDeclaration(Object.assign(Object.assign({}, yield this.GetImportDeclaration(component.ComponentOptions[WoltLabComponentSettingKey.Path])), { namedImports: [
component.VariableName
] }));
packageOptions = file.getVariableDeclaration(this.Generator.PackageVariableName).getInitializerIfKindOrThrow(SyntaxKind.NewExpression).getArguments()[0].asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
try {
installSet = packageOptions.getProperty(installSetKey).asKindOrThrow(SyntaxKind.PropertyAssignment).getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
}
catch (_c) {
(_a = packageOptions.getProperty(installSetKey)) === null || _a === void 0 ? void 0 : _a.remove();
installSet = packageOptions.addPropertyAssignment({
name: installSetKey,
initializer: printNode(ts.factory.createObjectLiteralExpression())
}).getInitializerIfKindOrThrow(SyntaxKind.ObjectLiteralExpression);
}
try {
installInstructions = installSet.getProperty(installInstructionKey).asKindOrThrow(SyntaxKind.PropertyAssignment).getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
}
catch (_d) {
(_b = installSet.getProperty(installInstructionKey)) === null || _b === void 0 ? void 0 : _b.remove();
installInstructions = installSet.addPropertyAssignment({
name: installInstructionKey,
initializer: printNode(ts.factory.createArrayLiteralExpression())
}).getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
}
let prefix = installInstructions.getElements().length === 0 ? EOL : "";
installInstructions.addElement(prefix + printNode(ts.factory.createIdentifier(component.VariableName)) + EOL);
});
}
/**
* @inheritdoc
*
* @param file
* The {@link SourceFile `SourceFile`} to transform.
*
* @returns
* The transformed file.
*/
Transform(file) {
const _super = Object.create(null, {
Transform: { get: () => super.Transform }
});
var _a;
return __awaiter(this, void 0, void 0, function* () {
file = yield _super.Transform.call(this, file);
file.addImportDeclaration({
moduleSpecifier: "@manuth/woltlab-compiler",
namedImports: [
{
name: "ConflictingPackageDescriptor"
},
{
name: "InvariantCultureName"
},
{
name: "Package"
},
{
name: "RequiredPackageDescriptor"
}
]
});
let constructor = this.WrapNode(ts.factory.createNewExpression(ts.factory.createIdentifier("Package"), [], []));
constructor.addArgument(`${EOL}${this.PackageOptions.getFullText()}`);
file.addVariableStatement({
isExported: true,
declarationKind: VariableDeclarationKind.Let,
docs: [
{
description: `${EOL}The package.`
}
],
declarations: [
{
name: this.Generator.PackageVariableName,
initializer: constructor.getFullText()
}
]
});
for (let component of this.Generator.InstructionComponents) {
if (component instanceof InstructionComponent &&
((_a = this.Generator.Settings[GeneratorSettingKey.Components]) !== null && _a !== void 0 ? _a : []).includes(component.ID)) {
yield this.AddComponent(file, component);
}
}
file.organizeImports();
return file;
});
}
}
//# sourceMappingURL=WoltLabPackageFileMapping.js.map