in powershell/plugins/sdk-modifiers.ts [75:142]
async function tweakModel(state: State): Promise<SdkModel> {
// only look at directives without the `transform` node.
for (const directive of directives.filter(each => !each.transform)) {
const getPatternToMatch = (selector: string | undefined): RegExp | undefined => {
return selector ? !hasSpecialChars(selector) ? new RegExp(`^${selector}$`, 'gi') : new RegExp(selector, 'gi') : undefined;
};
if (isWhereModelDirective(directive)) {
const selectType = directive.select;
const modelNameRegex = getPatternToMatch(directive.where['model-name']);
const propertyNameRegex = getPatternToMatch(directive.where['property-name']);
const modelNameReplacer = directive.set['model-name'];
const propertyNameReplacer = directive.set['property-name'];
// select all models
let models = [...state.model.schemas.objects ?? []];
// let models = values(state.model.schemas).toArray();
if (modelNameRegex) {
models = values(models)
.where(model =>
!!`${model.language.csharp?.name}`.match(modelNameRegex))
.toArray();
}
if (propertyNameRegex && selectType === 'model') {
models = values(models)
.where(model => values(allVirtualProperties(model.language.csharp?.virtualProperties))
.any(property => !!`${property.name}`.match(propertyNameRegex)))
.toArray();
}
if (propertyNameRegex && (selectType === undefined || selectType === 'property')) {
const properties = values(models)
.selectMany(model => allVirtualProperties(model.language.csharp?.virtualProperties))
.where(property => !!`${property.name}`.match(propertyNameRegex))
.toArray();
for (const property of values(properties)) {
const prevName = property.name;
property.name = propertyNameReplacer ? propertyNameRegex ? property.name.replace(propertyNameRegex, propertyNameReplacer) : propertyNameReplacer : property.name;
if (!property.name) {
state.message({ Channel: Channel.Error, Text: `Directive '${directive.where['model-name']}/${directive.where['property-name']}' attempted to change '${prevName}' to '' ` });
}
if (propertyNameRegex) {
state.message({
Channel: Channel.Debug, Text: `[DIRECTIVE] Changed property-name from ${prevName} to ${property.name}.`
});
}
}
} else if (models) {
// comment out below to disable model name change, which will be added in the tweakModelName before the plugin csnamerSdk
// for (const model of values(models)) {
// const prevName = model.language.csharp?.name;
// if (model.language.csharp) {
// model.language.default.fullname = model.language.csharp.fullname = model.language.default.name = model.language.csharp.name = modelNameReplacer ? modelNameRegex ? model.language.csharp.name.replace(modelNameRegex, modelNameReplacer) : modelNameReplacer : model.language.csharp.name;
// }
// state.message({
// Channel: Channel.Debug, Text: `[DIRECTIVE] Changed model-name from ${prevName} to ${model.language.csharp?.name}.`
// });
// }
}
}
}
return state.model;
}