Add Job System (#656)
* Add Job System * Remove testing stuff * changelog * plugin work * Convert a couple more jobs * simplify callback * assert * Logging changes and rendezvous perf * changelog * sb segment viewed job * clear cache, and few changes * Lint fix * a couple more jobs * reduce rendezvous in web server * refactor * Lint fix * no message * test shutdown * comment * free fields --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
45983a67af
commit
d70a07049a
74 files changed
+1090
-298
No files matched your search
@@ -0,0 +1,118 @@
|
||||
// This plugin generates a job component for each function annotated with @job
|
||||
|
||||
import {
|
||||
CompilerPlugin,
|
||||
BscFile,
|
||||
isBrsFile,
|
||||
WalkMode,
|
||||
createVisitor,
|
||||
Program,
|
||||
FunctionStatement,
|
||||
DiagnosticSeverity,
|
||||
} from 'brighterscript';
|
||||
|
||||
export class JobSystemPlugin implements CompilerPlugin {
|
||||
public name = 'JobSystemPlugin';
|
||||
|
||||
afterFileParse(file: BscFile) {
|
||||
if (!isBrsFile(file)) {
|
||||
return
|
||||
}
|
||||
|
||||
const program = file.program
|
||||
|
||||
file.ast.walk(createVisitor({
|
||||
FunctionExpression: (func) => {
|
||||
const jobName = this.getJobName(func.functionStatement);
|
||||
if (!jobName) {
|
||||
return
|
||||
}
|
||||
|
||||
const xml = this.generateJobXml(jobName, file.pkgPath)
|
||||
const xmlFile = `components/JobSystem/generated/${jobName}.xml`
|
||||
|
||||
if (program.hasFile(xmlFile)) {
|
||||
const currentContent = program.getFile(xmlFile).fileContents
|
||||
if (currentContent !== xml) {
|
||||
file.addDiagnostics([{
|
||||
file: file,
|
||||
range: func.range,
|
||||
message: `JobSystemPlugin: file ${xmlFile} already exists`,
|
||||
severity: DiagnosticSeverity.Error,
|
||||
code: 'JOB_SYSTEM_FILE_EXISTS',
|
||||
}]);
|
||||
}
|
||||
}
|
||||
file.program.setFile(xmlFile, xml)
|
||||
},
|
||||
}), {
|
||||
walkMode: WalkMode.visitExpressionsRecursive
|
||||
});
|
||||
}
|
||||
|
||||
beforeProgramValidate(program: Program) {
|
||||
this.generateJobListEnum(program);
|
||||
}
|
||||
|
||||
getJobName(functionStatement: FunctionStatement | undefined): string | null {
|
||||
const annotations = functionStatement?.annotations
|
||||
if (!annotations || annotations.length === 0) {
|
||||
return null;
|
||||
}
|
||||
for (let index = 0; index < annotations.length; index++) {
|
||||
const annotation = annotations[index];
|
||||
if (annotation.name === "job") {
|
||||
const annotationArgs = annotation.getArguments();
|
||||
if (annotationArgs && annotationArgs.length > 0) {
|
||||
return annotationArgs[0] as string;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
generateJobXml(jobName: string, bsFile: string): string {
|
||||
return `<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<component name="${jobName}" extends="BaseJob">
|
||||
<script type="text/brightscript" uri="pkg:/${bsFile}" />
|
||||
</component>`
|
||||
}
|
||||
|
||||
generateJobListEnum(program: Program) {
|
||||
const jobNames = Object.values(program.files).reduce((acc, file) => {
|
||||
if (!isBrsFile(file)) {
|
||||
return acc
|
||||
}
|
||||
|
||||
file.ast.walk(createVisitor({
|
||||
FunctionExpression: (func) => {
|
||||
const jobName = this.getJobName(func.functionStatement);
|
||||
if (!jobName) {
|
||||
return
|
||||
}
|
||||
|
||||
acc.add(jobName)
|
||||
},
|
||||
}), {
|
||||
walkMode: WalkMode.visitExpressionsRecursive
|
||||
});
|
||||
|
||||
return acc
|
||||
}, new Set<string>());
|
||||
if (jobNames.size === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const enumItems = Array.from(jobNames).map((jobName) => {
|
||||
return `${jobName} = "${jobName}"`
|
||||
})
|
||||
|
||||
const content = 'enum Jobs\n ' + enumItems.join('\n ') + '\nend enum\n';
|
||||
|
||||
program.setFile('components/JobSystem/Jobs.bs', content);
|
||||
}
|
||||
}
|
||||
|
||||
export default () => {
|
||||
return new JobSystemPlugin();
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
// This plugin generates logging functions based on the usage of the LogError, LogWarn, LogInfo, and LogDebug functions.
|
||||
// Also strips out print statements and comments when not in debug mode.
|
||||
|
||||
import {
|
||||
AstEditor,
|
||||
@@ -102,10 +103,25 @@ export class LoggerPlugin implements CompilerPlugin {
|
||||
const isDebug = !!event.program.options.debug;
|
||||
|
||||
const visitor = createVisitor({
|
||||
PrintStatement: (statement) => {
|
||||
if (!isDebug) {
|
||||
event.editor.overrideTranspileResult(statement, '');
|
||||
}
|
||||
},
|
||||
CommentStatement: (statement) => {
|
||||
if (!isDebug) {
|
||||
event.editor.overrideTranspileResult(statement, '');
|
||||
}
|
||||
},
|
||||
ExpressionStatement: (statement) => {
|
||||
// @ts-ignore
|
||||
const funcName = statement.expression.callee?.name?.text;
|
||||
if (funcName && logFunctionKeys.includes(funcName)) {
|
||||
if (!isDebug && funcName === 'LogDebug') {
|
||||
event.editor.overrideTranspileResult(statement, '');
|
||||
return;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const args = statement.expression.args;
|
||||
if (isDebug) {
|
||||
|
||||
Reference in new issue
Block a user