Skip to content

Commit

Permalink
Fixed resolving default variables
Browse files Browse the repository at this point in the history
Implemented defined() function
Automatic class invalidation upon fields change
  • Loading branch information
desertkun committed Jan 8, 2019
1 parent 6c85302 commit 028edc6
Show file tree
Hide file tree
Showing 7 changed files with 451 additions and 71 deletions.
3 changes: 3 additions & 0 deletions src/ipc/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export interface IpcAPI
getEnvironmentTree(name: string): Promise<any>;
findNode(localPath: string): Promise<any>;
acquireNodeClass(nodePath: string, className: string): Promise<any>;
hasNodeClassProperty(nodePath: string, className: string, propertyName: string): Promise<boolean>;
setNodeClassProperty(nodePath: string, className: string, propertyName: string, value: any): Promise<any>;
removeNodeClassProperty(nodePath: string, className: string, propertyName: string): Promise<any>;
removeNodeClassProperties(nodePath: string, className: string): Promise<any>;
invalidateNodeClass(nodePath: string, className: string): Promise<void>;
getClassInfo(env: string): Promise<any>;
refreshWorkspace(): Promise<any>;
showOpenDirectoryDialog(defaultPath?: string): Promise<string>;
Expand All @@ -26,6 +28,7 @@ export interface IpcAPI
removeResourceFromNode(nodePath: string, definedTypeName: string, title: string): Promise<void>;
renameNodeResource(nodePath: string, definedTypeName: string, title: string, newTitle: string): Promise<boolean>;
removeResourcesFromNode(nodePath: string, definedTypeName: string): Promise<string[]>;
invalidateNodeResource(nodePath: string, definedTypeName: string, title: string): Promise<void>;
chooseDefinedType(nodePath: string): Promise<string>;
createNewResourceToNode(nodePath: string, definedTypeName: string, title: string): Promise<any>;
removeAllResourcesFromNode(nodePath: string): Promise<any[]>;
Expand Down
49 changes: 49 additions & 0 deletions src/ipc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ export class IpcServer implements IpcAPI

return await node.setClassProperty(className, propertyName, value);
}

public async hasNodeClassProperty(nodePath: string, className: string, propertyName: string): Promise<boolean>
{
const workspace: puppet.Workspace = getCurrentWorkspace();

if (workspace == null)
{
return false;
}

const node = await workspace.findNode(nodePath);

if (node == null)
{
return false;
}

return await node.hasClassProperty(className, propertyName);
}

public async removeNodeClassProperty(
nodePath: string, className: string, propertyName: string
Expand Down Expand Up @@ -545,6 +564,36 @@ export class IpcServer implements IpcAPI

await node.invalidate();
}

public async invalidateNodeClass(nodePath: string, className: string): Promise<void>
{
const workspace: puppet.Workspace = getCurrentWorkspace();

if (workspace == null)
return;

const node = await workspace.findNode(nodePath);

if (node == null)
return;

await node.invalidateClass(className);
}

public async invalidateNodeResource(nodePath: string, definedTypeName: string, title: string): Promise<void>
{
const workspace: puppet.Workspace = getCurrentWorkspace();

if (workspace == null)
return;

const node = await workspace.findNode(nodePath);

if (node == null)
return;

await node.invalidateDefinedType(definedTypeName, title);
}

public async isNodeClassValid(nodePath: string, className: string): Promise<boolean>
{
Expand Down
150 changes: 129 additions & 21 deletions src/puppet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export module puppet
return this.info["file"];
}

public get fields(): Array<string>
public get defaults(): Array<string>
{
return Object.keys(this.info["defaults"] || {});
}
Expand Down Expand Up @@ -170,7 +170,7 @@ export module puppet
return {
"name": this.name,
"file": this.info["file"],
"fields": this.fields,
"defaults": this.defaults,
"inherits": this.info["inherits"],
"description": this.description,
"options": this.options,
Expand Down Expand Up @@ -241,7 +241,7 @@ export module puppet
return this.info["source"];
}

public get fields(): Array<string>
public get defaults(): Array<string>
{
return Object.keys(this.info["defaults"] || {});
}
Expand Down Expand Up @@ -271,7 +271,7 @@ export module puppet
return {
"name": this.name,
"file": this.info["file"],
"fields": this.fields,
"defaults": this.defaults,
"inherits": this.info["inherits"],
"description": this.description,
"options": this.options,
Expand Down Expand Up @@ -1098,7 +1098,11 @@ export module puppet
}
}

export type GlobalVariableResolver = (key: string) => string;
export interface GlobalVariableResolver
{
get (key: string): string;
has (key: string): boolean;
}

export class ResolvedResource
{
Expand Down Expand Up @@ -1727,6 +1731,31 @@ export module puppet
this._compiledResources.clear();
}

public async invalidateClass(className: string): Promise<void>
{
if (this._compiledClasses.has(className))
{
const compiled = this._compiledClasses.get(className);
this._compiledClasses.remove(className);

// invalidate also a direct parent, if any
if (compiled.parentName != null)
{
await this.invalidateClass(compiled.parentName);
}
}
}

public async invalidateDefinedType(definedTypeName: string, title: string): Promise<void>
{
if (this._compiledResources.has(definedTypeName))
{
const titles = this._compiledResources.get(definedTypeName);

titles.remove(title);
}
}

public async remove(): Promise<boolean>
{
if (this._parent == null)
Expand Down Expand Up @@ -1800,9 +1829,14 @@ export module puppet
return zis.resolveFunction(name, global);
}

public async resolveGlobalVariable(name: string): Promise<string>
public getGlobalVariable(name: string): string
{
return global.get(name);
}

public hasGlobalVariable(name: string): boolean
{
return global(name);
return global.has(name);
}
});
}
Expand Down Expand Up @@ -1911,9 +1945,14 @@ export module puppet
return zis.resolveFunction(name, global);
}

public async resolveGlobalVariable(name: string): Promise<string>
public getGlobalVariable(name: string): string
{
return global(name);
return global.get(name);
}

public hasGlobalVariable(name: string): boolean
{
return global.has(name);
}
});
}
Expand Down Expand Up @@ -2118,14 +2157,45 @@ export module puppet
return this.configClasses.indexOf(className) >= 0
}

public hasGlobal(key: string): boolean
{
if (key == "facts")
{
return this.configFacts != null;
}

if (this.configFacts != null && this.configFacts.hasOwnProperty(key))
return true;

if (this._env.global.has(key) || this._env.workspace.global.has(key))
return true;

if (this._config != null && this._config.hasOwnProperty(key))
return true;

return false;
}

public getGlobal(key: string): string
{
if (key == "facts")
{
return this.configFacts;
}

return this.configFacts[key] || this._env.global.get(key) || this._env.workspace.global.get(key);
if (this.configFacts != null && this.configFacts.hasOwnProperty(key))
return this.configFacts[key];

if (this._env.global.has(key))
return this._env.global.get(key);

if (this._env.workspace.global.has(key))
return this._env.workspace.global.get(key);

if (this._config != null)
return this._config[key];

return null;
}

public async removeClass(className: string): Promise<void>
Expand Down Expand Up @@ -2274,9 +2344,9 @@ export module puppet
if (!this.hasClass(className))
throw Error("No such class: " + className);

return await this.resolveClass(className, (key: string) =>
{
return zis.getGlobal(key);
return await this.resolveClass(className, {
get: (key: string) => zis.getGlobal(key),
has: (key: string) => zis.hasGlobal(key)
});
}

Expand All @@ -2303,9 +2373,9 @@ export module puppet

values["title"] = title;

return await this.resolveResource(definedTypeName, title, values, (key: string) =>
{
return zis.getGlobal(key);
return await this.resolveResource(definedTypeName, title, values, {
get: (key: string) => zis.getGlobal(key),
has: (key: string) => zis.hasGlobal(key)
});
}

Expand Down Expand Up @@ -2362,6 +2432,23 @@ export module puppet
await this.save();
}

public async hasClassProperty(className: string, propertyName: string): Promise<boolean>
{
const classInfo = this._env.findClassInfo(className);

if (classInfo == null)
return false;

const compiled = await this.acquireClass(className);

if (!compiled)
return false;

const propertyPath = this.compilePropertyPath(className, propertyName);

return this.config != null && this.config.hasOwnProperty(propertyPath);
}

public async removeClassProperty(className: string, propertyName: string): Promise<any>
{
const classInfo = this._env.findClassInfo(className);
Expand Down Expand Up @@ -2417,7 +2504,7 @@ export module puppet
if (!compiled)
return;

for (const propertyName of classInfo.fields)
for (const propertyName of compiled.resolvedFields.getKeys())
{
const propertyPath = this.compilePropertyPath(className, propertyName);
delete this.config[propertyPath];
Expand All @@ -2440,6 +2527,8 @@ export module puppet
const errors: any = {};
const hints: any = {};
const fields: string[] = [];
const definedFields: string[] = [];
const requiredFields: string[] = [];
const values: any = {};
const classHints: any = compiled.hints;

Expand All @@ -2448,6 +2537,11 @@ export module puppet
const property = compiled.getResolvedProperty(name);
fields.push(name);

if (classInfo.defaults.indexOf(name) < 0)
{
requiredFields.push(name);
}

if (property.hasType)
{
types[name] = {
Expand Down Expand Up @@ -2475,10 +2569,12 @@ export module puppet
}

const propertyPath = this.compilePropertyPath(className, name);
const configValue = this.config[propertyPath];
if (configValue != null)

if (this.config.hasOwnProperty(propertyPath))
{
const configValue = this.config[propertyPath];
values[name] = configValue;
definedFields.push(name);
}
}

Expand All @@ -2491,7 +2587,9 @@ export module puppet
"errors": errors,
"propertyHints": hints,
"hints": classHints,
"fields": fields
"definedFields": definedFields,
"fields": fields,
"requiredFields": requiredFields
}
}

Expand All @@ -2507,6 +2605,8 @@ export module puppet
const defaultValues: any = {};
const types: any = {};
const fields: string[] = [];
const definedFields: string[] = [];
const requiredFields: string[] = [];
const errors: any = {};
const hints: any = {};
const values: any = {};
Expand All @@ -2520,6 +2620,7 @@ export module puppet
for (const k in t)
{
values[k] = t[k];
definedFields.push(k);
}
}
}
Expand All @@ -2528,6 +2629,11 @@ export module puppet
{
const property = compiled.resource.resolvedFields.get(name);

if (classInfo.defaults.indexOf(name) < 0)
{
requiredFields.push(name);
}

if (property.hasType)
{
types[name] = {
Expand Down Expand Up @@ -2565,7 +2671,9 @@ export module puppet
"types": types,
"errors": errors,
"propertyHints": hints,
"fields": fields
"definedFields": definedFields,
"fields": fields,
"requiredFields": requiredFields
}
}
}
Expand Down
Loading

0 comments on commit 028edc6

Please sign in to comment.