Skip to content

Commit

Permalink
Add check running service and make extension activation order consist…
Browse files Browse the repository at this point in the history
…ent (#981)
  • Loading branch information
lyonsil authored Jul 12, 2024
1 parent d584028 commit bc0754a
Show file tree
Hide file tree
Showing 24 changed files with 1,349 additions and 188 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"MAIN_ARGS": "--inspect=5858 --remote-debugging-port=9223",
"IN_VSCODE": "true",
"DEV_NOISY": "false"
}
},
"autoAttachChildProcesses": true
},
{
"name": "Attach to Renderer",
Expand Down
15 changes: 9 additions & 6 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
"autodocs",
"biblionexus",
"camelcase",
"Consts",
"consts",
"deuterocanon",
"dockbox",
"electronmon",
"endregion",
"finalizer",
"Fragmenter",
"fragmenter",
"guids",
"hopkinson",
"iframes",
"IIFE",
"iife",
"iusfm",
"iusj",
"iusx",
"localizable",
"localstorage",
"maximizable",
Expand All @@ -41,8 +44,8 @@
"papis",
"paranext",
"paratext",
"PDPE",
"Pdpef",
"pdpe",
"pdpef",
"pdpf",
"pdps",
"plusplus",
Expand All @@ -56,7 +59,7 @@
"stringifiable",
"stringz",
"stylelint",
"Stylesheet",
"stylesheet",
"tailwindcss",
"testid",
"typedefs",
Expand Down
29 changes: 16 additions & 13 deletions extensions/src/hello-someone/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,24 @@ export async function activate(context: ExecutionActivationContext): Promise<voi
existingPeopleWebViewId = undefined;
}

// Get the existing web view if one exists or create a new one
const peopleWebViewId = await papi.webViews.getWebView(
peopleWebViewType,
{ type: 'panel', direction: 'top' },
{ existingId: existingPeopleWebViewId },
);
// Don't block on the web view to keep going
setTimeout(async () => {
// Get the existing web view if one exists or create a new one
const peopleWebViewId = await papi.webViews.getWebView(
peopleWebViewType,
{ type: 'panel', direction: 'top' },
{ existingId: existingPeopleWebViewId },
);

// Save newly acquired webview id
await papi.storage.writeUserData(
context.executionToken,
peopleWebViewIdKey,
peopleWebViewId || '',
);
// Save newly acquired webview id
await papi.storage.writeUserData(
context.executionToken,
peopleWebViewIdKey,
peopleWebViewId || '',
);
}, 1000);

await papi.webViews.getWebView(emotionTestWebViewType, undefined, { existingId: '?' });
papi.webViews.getWebView(emotionTestWebViewType, undefined, { existingId: '?' });

// Await the registration promises at the end so we don't hold everything else up
context.registrations.add(
Expand Down
100 changes: 100 additions & 0 deletions extensions/src/hello-world/src/checks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { logger, projectDataProviders } from '@papi/backend';
import { VerseRef } from '@sillsdev/scripture';
import {
Check,
CheckCreatorFunction,
CheckDetails,
CheckRunResult,
IUSFMBookProjectDataProvider,
ScriptureRange,
} from 'platform-scripture';

export const checkDetails: CheckDetails = Object.freeze({
checkName: 'helloWordSimpleCheck',
checkDescription: 'find the word "sheep"',
});

class HelloCheck implements Check {
targetProjectId: string | undefined;
pdp: IUSFMBookProjectDataProvider | undefined;

async initialize(projectId: string): Promise<string[]> {
try {
this.targetProjectId = projectId;
this.pdp = await projectDataProviders.get('platformScripture.USFM_Book', projectId);
this.pdp.onDidDispose(this.dispose);
} catch (error) {
return [JSON.stringify(error)];
}
return [];
}

async dispose(): Promise<boolean> {
this.targetProjectId = undefined;
this.pdp = undefined;
return true;
}

// eslint-disable-next-line class-methods-use-this
getCheckDetails(): CheckDetails {
return checkDetails;
}

async getCheckResults(range: ScriptureRange): Promise<CheckRunResult[]> {
if (range.end && range.end.bookNum !== range.start.bookNum)
throw new Error('This only supports checks within a single book right now');

const usfm = await this.pdp?.getBookUSFM(range.start);
if (!usfm)
throw new Error(`Could not load ${range.start.toString()} from ${this.targetProjectId}`);

logger.debug(
`Getting test check results for ${JSON.stringify(range)} on ${this.targetProjectId}`,
);

let results: CheckRunResult[] = [];
let startingPoint = 0;
startingPoint = usfm.indexOf(`\\c ${range.start.chapterNum.toString()}`, startingPoint);
startingPoint = usfm.indexOf(`\\v ${range.start.verseNum.toString()}`, startingPoint);
let stoppingPoint = 0;
if (!range.end) stoppingPoint = usfm.length - 1;
else {
stoppingPoint = usfm.indexOf(`\\c ${range.end.chapterNum.toString()}`, startingPoint);
stoppingPoint = usfm.indexOf(`\\v ${range.end.verseNum.toString()}`, stoppingPoint);
let nextChapter = usfm.indexOf('\\c ', stoppingPoint);
nextChapter = nextChapter > 0 ? nextChapter : 0;
let nextVerse = usfm.indexOf('\\v ', stoppingPoint);
nextVerse = nextVerse > 0 ? nextVerse : 0;
stoppingPoint = Math.max(stoppingPoint, nextChapter, nextVerse);
}
let cursor = startingPoint;
while (cursor <= stoppingPoint) {
cursor = usfm.indexOf('sheep', cursor);
if (cursor < 0 || cursor > stoppingPoint) break;
const chapterIndex = usfm.lastIndexOf('\\c ', cursor) + 3;
const inChapter = parseInt(usfm.slice(chapterIndex, chapterIndex + 4), 10);
const verseIndex = usfm.lastIndexOf('\\v ', cursor) + 3;
const inVerse = parseInt(usfm.slice(verseIndex, verseIndex + 4), 10);
const offset = cursor - verseIndex - inVerse.toString().length - 1;
const newResult: CheckRunResult = {
projectId: this.targetProjectId ?? '',
messageFormatString: 'Found the word "sheep"',
start: {
verseRef: new VerseRef(range.start.book, inChapter.toString(), inVerse.toString()),
offset,
},
end: {
verseRef: new VerseRef(range.start.book, inChapter.toString(), inVerse.toString()),
offset: offset + 5,
},
};
results = results.concat([newResult]);
cursor += 1;
}
return results;
}
}

export const createHelloCheck: CheckCreatorFunction = async () => {
return new HelloCheck();
};
27 changes: 17 additions & 10 deletions extensions/src/hello-world/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import helloWorldProjectWebViewStyles from './web-views/hello-world-project/hell
import helloWorldProjectViewerWebView from './web-views/hello-world-project/hello-world-project-viewer.web-view?inline';
import { HTML_COLOR_NAMES } from './util';
import { HELLO_WORLD_PROJECT_INTERFACES } from './models/hello-world-project-data-provider-engine.model';
import { checkDetails, createHelloCheck } from './checks';

/** User data storage key for all hello world project data */
const allProjectDataStorageKey = 'allHelloWorldProjectData';
Expand Down Expand Up @@ -369,17 +370,7 @@ export async function activate(context: ExecutionActivationContext): Promise<voi
.fetch('https://www.example.com')
.catch((e) => logger.error(`Could not get data from example.com! Reason: ${e}`));

// Create webviews or get an existing webview if one already exists for this type
// Note: here, we are using `existingId: '?'` to indicate we do not want to create a new webview
// if one already exists. The webview that already exists could have been created by anyone
// anywhere; it just has to match `webViewType`. See `hello-someone.ts` for an example of keeping
// an existing webview that was specifically created by `hello-someone`.
papi.webViews.getWebView(htmlWebViewProvider.webViewType, undefined, { existingId: '?' });
papi.webViews.getWebView(reactWebViewProvider.webViewType, undefined, { existingId: '?' });
papi.webViews.getWebView(reactWebView2Provider.webViewType, undefined, { existingId: '?' });

const peopleDataProvider = await papi.dataProviders.get('helloSomeone.people');

if (peopleDataProvider) {
// Test subscribing to a data provider
const unsubGreetings = await peopleDataProvider.subscribeGreeting(
Expand All @@ -390,6 +381,12 @@ export async function activate(context: ExecutionActivationContext): Promise<voi
context.registrations.add(unsubGreetings);
}

const checkPromise = papi.commands.sendCommand(
'platformScripture.registerCheck',
checkDetails,
createHelloCheck,
);

// Await the registration promises at the end so we don't hold everything else up
context.registrations.add(
await helloWorldPdpefPromise,
Expand All @@ -409,7 +406,17 @@ export async function activate(context: ExecutionActivationContext): Promise<voi
onHelloWorldEmitter,
await helloWorldPromise,
await helloExceptionPromise,
await checkPromise,
);

// Create webviews or get an existing webview if one already exists for this type
// Note: here, we are using `existingId: '?'` to indicate we do not want to create a new webview
// if one already exists. The webview that already exists could have been created by anyone
// anywhere; it just has to match `webViewType`. See `hello-someone.ts` for an example of keeping
// an existing webview that was specifically created by `hello-someone`.
papi.webViews.getWebView(htmlWebViewProvider.webViewType, undefined, { existingId: '?' });
papi.webViews.getWebView(reactWebViewProvider.webViewType, undefined, { existingId: '?' });
papi.webViews.getWebView(reactWebView2Provider.webViewType, undefined, { existingId: '?' });

logger.info('Hello World is finished activating!');
}
19 changes: 16 additions & 3 deletions extensions/src/platform-scripture/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,57 @@
"dictionaryDefinitions": [],
"dictionaries": [],
"words": [
"altnumber",
"appdata",
"asyncs",
"autodocs",
"biblionexus",
"camelcase",
"consts",
"deuterocanon",
"dockbox",
"electronmon",
"endregion",
"finalizer",
"Fragmenter",
"fragmenter",
"guids",
"hopkinson",
"iframes",
"iife",
"iusfm",
"iusj",
"iusx",
"localizable",
"localstorage",
"maximizable",
"networkable",
"Newtonsoft",
"newtonsoft",
"nodebuffer",
"nums",
"papi",
"papis",
"paranext",
"paratext",
"pdpe",
"pdpef",
"pdpf",
"pdps",
"plusplus",
"proxied",
"pubnumber",
"reinitializing",
"reserialized",
"shadcn",
"sillsdev",
"steenwyk",
"stringifiable",
"Stylesheet",
"stringz",
"stylelint",
"stylesheet",
"tailwindcss",
"testid",
"typedefs",
"unlocalized",
"unregistering",
"unregisters",
"unsub",
Expand Down
Loading

0 comments on commit bc0754a

Please sign in to comment.