Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension API #104

Merged
merged 5 commits into from Feb 26, 2019

Conversation

Projects
None yet
2 participants
@anzwdev
Copy link
Contributor

anzwdev commented Feb 24, 2019

This pull request allows other vs code extensions to access object running and object files renaming functionality by exporting it as na api. It is done by returning new ObjectNamesApi object from extension activation function. This object has 2 public members: RunObjectApi and ObjectNamesApi.

Exported API

RunObjectApi is just a proxy that calls methods from existing DynamicsNAV class and exposes these 4 functions:

  • RunObjectInWebClient(objecttype: any, objectid: any) : void;
  • RunObjectInTabletClient(objecttype: any, objectid: any) : void;
  • RunObjectInPhoneClient(objecttype: any, objectid: any) : void;
  • RunObjectInWindowsClient(objecttype: any, objectid: any) : void;

ObjectNamesApi is also a proxy that uses existing NAVObject class and allows other vs code extensions to use crs-al-language-extension patterns to generate object names and object file names. These 4 functions are exposed:

  • GetObjectFileName(objectType: string, objectId : string, objectName: string) : string;
  • GetObjectExtensionFileName(objectType: string, objectId : string, objectName: string, extendedObjectId : string, extendedObjectName : string) : string;
  • GetObjectExtensionName(objectType: string, objectId : string, objectName: string, extendedObjectId : string, extendedObjectName : string) : string;
  • GetBestPracticeAbbreviatedObjectType(ObjectType: String): string;

How to use it

To use these new APIs in dependent extensions, you have to call vscode.extensions.getExtension function to get the extension object. The next step is to check if extension is active. If it is active, then public api can be found in 'exports' member of the extension object. If it is not active then call to async activate() method will activate it and return public api object. This example gets crs-al-language-extension api and calls GetObjectFileName function.

import * as vscode from 'vscode';  

async function textCRSExtensionApi() {  
    //get crs-al-language-extension public api
    let crsExtension = vscode.extensions.getExtension('waldo.crs-al-language-extension');
    let crsALLangExtApi : any;

    if (crsExtension) {
        if (crsExtension.isActive)
            crsALLangExtApi = crsExtension.exports;
        else
            crsALLangExtApi = await crsExtension.activate();
        vscode.window.showInformationMessage('CRS-AL-LANGUAGE-EXTENSION GetObjectFileName("table", "50000", "My Table") api call result: ' +
            crsALLangExtApi.ObjectNamesApi.GetObjectFileName('table', '50000', 'My Table'));
    } else
        vscode.window.showErrorMessage('CRS-AL-LANGUAGE-EXTENSION not found');
}

Example above is not the best one as it uses 'any' type so it is not helping developer to choose the right functions using intellisense. To solve this problem, ObjectNamesApi, RunObjectApi and ObjectNamesApi classes implement interfaces defined in 'src/api/CRSExtensionPublicApiInterfaces.ts' file. You can copy this file to your extension and then import it to be able to see all available members and functions. Modified sample code can look like this one:

import * as vscode from 'vscode';
import * as crsALExt from 'crs-al-language-extension-api';

async function textCRSExtensionApi() {
    //get crs-al-language-extension public api
    let crsExtension = vscode.extensions.getExtension('waldo.crs-al-language-extension');            
    let crsALLangExtApi : crsALExt.ICRSExtensionPublicApi;
    
    if (crsExtension) {
        if (crsExtension.isActive)
            crsALLangExtApi = crsExtension.exports;
        else
            crsALLangExtApi = await crsExtension.activate();
        vscode.window.showInformationMessage('CRS-AL-LANGUAGE-EXTENSION GetObjectFileName("table", "50000", "My Table") api call result: ' +
            crsALLangExtApi.ObjectNamesApi.GetObjectFileName('table', '50000', 'My Table'));
    } else
        vscode.window.showErrorMessage('CRS-AL-LANGUAGE-EXTENSION not found');
}

anzwdev added some commits Feb 5, 2019

Merge pull request #1 from CloudReadySoftware/master
Merge updates from the original repository
@waldo1001

This comment has been minimized.

Copy link
Contributor

waldo1001 commented Feb 26, 2019

Wow, awesome! Thanks so much!

@waldo1001 waldo1001 merged commit 312cc76 into CloudReadySoftware:master Feb 26, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.