* Fresh start * Create an xml include plugin (#111) * Restructure and add testing (#114) * rooibos setup * Restructure and add testing * docs * Restore manifestpipi * Description in tools * A bit of docs * Auto bind feature (#115) * Auto bind feature * Fix children merge * bs plugin fix * Feature/app controller (#120) * Initial layout * Restructuring and plugin work * A couple of tweaks in binding stuff * tweaks * basic stack * Add basic app controller * Support json5/yaml in playlet config files (#123) * Support json5 in playlet config files * Add yaml to the mix * Delete old lib project * plugin fix * plugin tweak * verbose logging * Revert "verbose logging" This reverts commit 25867f4226703d83102519a0d48d1cc46a9876de. * One idea to fix the include plugin * Fix instability in plugins * Feature/navbar (#126) * Navbar * typo * comment * Single content node, and tests * Remove unused field * tweaks * update dependencies command * tweak formatting * Add web app screen * Set id in created screens * Search screen * Tests * Show playlists and channels in search results * Move screen stuff to app controller * Home screen (wip) * Add preferences node * README.md copyedits (#130) * README.md: Fix typos * README.md: Standardize capitalization and punctuation * Update docs/README.md --------- Co-authored-by: Brahim Hadriche <brahim.hadriche@gmail.com> * Web server: round 1 (#131) * web server object * Logging * Web server round 1 * Lint fix * Small tweaks --------- Co-authored-by: github-action linter <githubaction@githubaction.com> * Logging to file * Leftover * Add logs file to web server * Fix for timeout * Disable keepalive connection * add openapi spec file * Lint fix * add prefs version in api * Add ecp api definition * Lint fix * Settings rework (#135) * Settings rework * Standard OnFocusChange event * Video player stuff (#136) * Video player stuff * Restructure things so that focus is better handled * Lint fix --------- Co-authored-by: github-action linter <githubaction@githubaction.com> * Exit dialog * A bunch of changes * New http client * Set TODO priorities * picture in picture * mark video watched * Video player stuff mostly there * update spec file * Sponsorblock * FuncName * SponsorBlock notifications * Lint fix * misc * remove basic auth feature flag (until we start using it) * Play queue part 1 * type fix * handle unicode in request body * SponsorBlock chapter label * modal base * remove a hack * Invidious settings * web server reduce timeout * Test for captions * Lint fix * remove rpc stuff * small change * Solve focus management * a couple of fixes * Remove a bunch of todos * feed error handling wip * Lint fix * navigation in instance settings * home screen pagination support * Refactor feed to support pagination on search page * cell loading animation * Tweak caching * delete test screen * playlist support part 1 * Basic playlist view * Restore playlist/channel handler * Refactor some content node stuff, and add an auth popup dialog * A bad implementation of a playqueue * Lint fix * playlist cast fix * Feature/web app catch up (#139) * playlist cast fix * Move things around * support playlists in web app * update api * Bump deps * Downgrade todo --------- Co-authored-by: Bryce Harrington <bryce@bryceharrington.org> Co-authored-by: github-action linter <githubaction@githubaction.com>
217 lines
7.5 KiB
TypeScript
217 lines
7.5 KiB
TypeScript
// This plugin takes functions annotated with @oninit and adds them to the component's Init function
|
|
|
|
import {
|
|
AnnotationExpression,
|
|
BeforeFileTranspileEvent,
|
|
BrsFile,
|
|
BscFile,
|
|
Callable,
|
|
CallableContainerMap,
|
|
CompilerPlugin,
|
|
DiagnosticSeverity,
|
|
FunctionStatement,
|
|
Scope,
|
|
XmlScope,
|
|
isBrsFile,
|
|
isXmlScope,
|
|
} from 'brighterscript';
|
|
|
|
import { RawCodeStatement } from './Classes/RawCodeStatement';
|
|
|
|
// The plugin processes the file that includes the Init function, not the file that includes the @oninit functions
|
|
// This is because the modifications will be made to the Init function.
|
|
// The rules are:
|
|
// 1. If the scope inclues @oninit functions, the scope must include an Init function
|
|
// 2. An Init function that will be calling @oninit functions must be included in only one scope
|
|
// 3. @oninit functions must have a name
|
|
|
|
export class OnInitPlugin implements CompilerPlugin {
|
|
public name = 'OnInitPlugin';
|
|
|
|
afterScopeValidate(scope: Scope, files: BscFile[], callables: CallableContainerMap) {
|
|
if (!isXmlScope(scope)) {
|
|
return;
|
|
}
|
|
|
|
const onInitCallables = this.getOnInitCallables(scope);
|
|
if (onInitCallables.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const initFunction = this.getInitCallableInScope(scope);
|
|
if (!initFunction) {
|
|
onInitCallables.forEach((onInitCallable) => {
|
|
// TODO:P2 we currently add diagnostics to the xml file, but we should add them to the brs file
|
|
// The reasons is, when we add to a generated file (like Component_bindings.brs), the errors
|
|
// Are stuck, and won't be fixed until the file is changed, which we can't do since it's not a real file.
|
|
scope.xmlFile.addDiagnostics([{
|
|
file: scope.xmlFile,
|
|
range: onInitCallable.annotation!.range,
|
|
message: `function ${onInitCallable.callable.functionStatement.name.text} with @oninit annotation is included in ${scope.name}, but no Init function was found in the component.`,
|
|
severity: DiagnosticSeverity.Error,
|
|
code: 1818
|
|
}]);
|
|
});
|
|
}
|
|
}
|
|
|
|
afterFileValidate(file: BscFile) {
|
|
if (!isBrsFile(file)) {
|
|
return
|
|
}
|
|
|
|
const program = file.program;
|
|
|
|
const scopes = this.getScopes(file);
|
|
if (scopes.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const scope = scopes[0];
|
|
const onInitCallables = this.getOnInitCallables(scope);
|
|
if (onInitCallables.length === 0) {
|
|
program.logger.info(this.name, `No Init processing: No @oninit functions found in scope ${scope.name}`)
|
|
return;
|
|
}
|
|
|
|
const initFunction = this.getInitCallableInFile(file);
|
|
if (initFunction && scopes.length > 1 && onInitCallables.length > 0) {
|
|
file.addDiagnostics([{
|
|
file,
|
|
range: initFunction.functionStatement!.func.range,
|
|
message: `Init function will call @oninit functions, but is included in multiple scopes.`,
|
|
severity: DiagnosticSeverity.Error,
|
|
code: 1819
|
|
}]);
|
|
}
|
|
|
|
for (let i = 0; i < onInitCallables.length; i++) {
|
|
const onInitCallable = onInitCallables[i];
|
|
|
|
const functionName = onInitCallable.callable.functionStatement?.name.text;
|
|
if (!functionName) {
|
|
file.addDiagnostics([{
|
|
file: onInitCallable.callable.file,
|
|
range: onInitCallable.annotation!.range,
|
|
message: `function with @oninit annotation must have a name`,
|
|
severity: DiagnosticSeverity.Error,
|
|
code: 1820
|
|
}]);
|
|
}
|
|
}
|
|
}
|
|
|
|
beforeFileTranspile(event: BeforeFileTranspileEvent) {
|
|
if (!isBrsFile(event.file)) {
|
|
return
|
|
}
|
|
|
|
const program = event.program;
|
|
const file = event.file;
|
|
|
|
const scopes = this.getScopes(file);
|
|
if (scopes.length === 0) {
|
|
program.logger.info(this.name, `No Init processing: No scopes found in ${file.pkgPath}`)
|
|
return;
|
|
}
|
|
|
|
const initFunction = this.getInitCallableInFile(file);
|
|
if (!initFunction) {
|
|
program.logger.info(this.name, `No Init processing: No Init function found in ${file.pkgPath}`)
|
|
return;
|
|
}
|
|
|
|
if (scopes.length > 1) {
|
|
program.logger.info(this.name, `No Init processing: Init function included in more than one scope`)
|
|
return;
|
|
}
|
|
|
|
const scope = scopes[0];
|
|
|
|
const onInitCallables = this.getOnInitCallables(scope);
|
|
if (onInitCallables.length === 0) {
|
|
program.logger.info(this.name, `No Init processing: No @oninit functions found in scope ${scope.name}`)
|
|
return;
|
|
}
|
|
|
|
const orderedStatements = initFunction.functionStatement.func.body.statements.map((statement) => {
|
|
return { order: 0, statement: statement }
|
|
});
|
|
|
|
for (let i = 0; i < onInitCallables.length; i++) {
|
|
const onInitCallable = onInitCallables[i];
|
|
|
|
const functionName = onInitCallable.callable.functionStatement?.name.text;
|
|
if (!functionName) {
|
|
continue;
|
|
}
|
|
|
|
const callSource = `${functionName}() ' auto-generated!`
|
|
|
|
const callStatement = new RawCodeStatement(callSource, file);
|
|
|
|
const order = this.getOnInitOrder(onInitCallable.annotation!);
|
|
|
|
orderedStatements.push({ order, statement: callStatement });
|
|
|
|
program.logger.info(this.name, `Added call to ${functionName} in Init function of ${scope.name} (${file.pkgPath})`)
|
|
}
|
|
|
|
const statements = orderedStatements.sort((a, b) => {
|
|
return a.order - b.order;
|
|
}).map((orderedStatement) => {
|
|
return orderedStatement.statement;
|
|
});
|
|
|
|
event.editor.setProperty(initFunction.functionStatement.func.body, 'statements', statements);
|
|
}
|
|
|
|
getScopes(file: BrsFile): XmlScope[] {
|
|
return file.program.getScopesForFile(file).filter((scope) => {
|
|
return isXmlScope(scope);
|
|
}) as XmlScope[];
|
|
}
|
|
|
|
getInitCallableInFile(file: BrsFile): Callable | undefined {
|
|
return file.callables.find((callable) => {
|
|
return callable.functionStatement?.name.text === "Init";
|
|
});
|
|
}
|
|
|
|
getInitCallableInScope(scope: XmlScope): Callable | undefined {
|
|
return scope.getOwnCallables().find((callable) => {
|
|
return callable.callable.functionStatement?.name.text === "Init";
|
|
})?.callable;
|
|
}
|
|
|
|
getOnInitCallables(scope: XmlScope) {
|
|
return scope.getOwnCallables().map((callable) => {
|
|
return {
|
|
callable: callable.callable,
|
|
annotation: this.getOnInitAnnotation(callable.callable.functionStatement)
|
|
}
|
|
}).filter((callable) => {
|
|
return callable.annotation;
|
|
});
|
|
}
|
|
|
|
getOnInitAnnotation(functionStatement: FunctionStatement | undefined) {
|
|
return functionStatement?.annotations?.find((annotation) => {
|
|
return annotation.name === "oninit";
|
|
});
|
|
}
|
|
|
|
getOnInitOrder(annotation: AnnotationExpression): number {
|
|
const args = annotation.getArguments();
|
|
if (!args || args.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return args[0].valueOf() as number;
|
|
}
|
|
}
|
|
|
|
export default () => {
|
|
return new OnInitPlugin();
|
|
};
|