Skip to content

Commit

Permalink
extension auto loads models
Browse files Browse the repository at this point in the history
  • Loading branch information
muremwa committed Sep 22, 2021
1 parent 819eac6 commit bb75ee2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
6 changes: 4 additions & 2 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const provider = require('./treeProvider');
const externalUrls = require('./extraUrls').loadUrls;
const clipboard = require('clipboardy');
const extensionSetting = require('./extensionSettings/settings');
const modelDetection = require('./modelDetection');

/**
* @param {string} stringToAdd
Expand Down Expand Up @@ -79,11 +80,12 @@ function readAndDisplayUrls (projects) {
});

// load pre defined url configurations
const extraUrlPatterns = externalUrls(projectPath, (error, file) => {
const detectedModels = settings.autoLoadModels? modelDetection.detect(projectPath, settings.registeredAppsOnly): new Map();
const extraUrlPatterns = externalUrls(projectPath, settings, detectedModels, (error, file) => {
vscode.window.showErrorMessage(`The configurations in ${file} are incorrect`);
}, () => {
vscode.window.showErrorMessage(`Wrong format on ${_project[0]}/.vscode/urlConfigs/models.json`)
}, settings);
});

// merge both patterns
const mergedPatterns = [...extraUrlPatterns.keys()].length && realProject? new Map([...extraUrlPatterns, ...urlPatterns]): urlPatterns;
Expand Down
3 changes: 2 additions & 1 deletion extensionSettings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function cleanUpSettings (tempSettings) {
adminUrls: boolean,
autoLoadModels: boolean,
builtInAuth: boolean,
expandApps: string
expandApps: string,
registeredAppsOnly: false
}} extensionSettings
*/
function loadSettings (rootPath, errorWithSettings) {
Expand Down
7 changes: 4 additions & 3 deletions extraUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ function _loadExtraUrls (files, errorCallBack) {

/**
* @param {string} home
* @param {{}} settings
* @param {Map<string, string[]>} detectedModels
* @param {() => void} handleExternalReadError
* @param {() => void} wrongFormatModels
* @param {{}} settings
* @returns {Map<string, []>}
*/
function loadUrls (home, handleExternalReadError, wrongFormatModels, settings) {
function loadUrls (home, settings, detectedModels, handleExternalReadError, wrongFormatModels) {
// Read json files in extraUrls
let userConfigs;

Expand All @@ -85,7 +86,7 @@ function loadUrls (home, handleExternalReadError, wrongFormatModels, settings) {
const urls = _loadExtraUrls(jFiles, handleExternalReadError);

if (urls.has('admin')) {
const modelUrls = modelAdmin(home, wrongFormatModels, () => {});
const modelUrls = modelAdmin(home, detectedModels, wrongFormatModels, () => {});
urls.set('admin', [...urls.get('admin'), ...modelUrls]);
};

Expand Down
9 changes: 7 additions & 2 deletions modelAdminLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function createSingleModelUrls (model, appName, appLabel, extraLabel, objectId)

/**
*@param {string} home
*@param {Map<string, string[]>} detectedModels
*@param {() => void} wrongFomartCallback
*@param {() => void} noModelsCallback
*@returns {{
Expand All @@ -76,9 +77,12 @@ function createSingleModelUrls (model, appName, appLabel, extraLabel, objectId)
hasArgs: boolean
}[]}
**/
function loadModelAdminConfigs (home, wrongFomartCallback, noModelsCallback) {
function loadModelAdminConfigs (home, detectedModels, wrongFomartCallback, noModelsCallback) {
let patterns = [];
const models = _getModels(home, wrongFomartCallback, noModelsCallback);
const extraModels = _getModels(home, wrongFomartCallback, noModelsCallback);
const models = detectedModels;
[...extraModels.keys()].forEach((key) => !models.has(key)? models.set(key, extraModels.get(key)): void 0);

const modelAdminUrls = [
['changelist', false],
['add', false],
Expand All @@ -92,6 +96,7 @@ function loadModelAdminConfigs (home, wrongFomartCallback, noModelsCallback) {

if (Array.isArray(appModels)) {
patterns = [...patterns, ...appModels.map((appModel) => {
appModel = appModel.toLowerCase();
return modelAdminUrls.map((url) => createSingleModelUrls(appModel, 'admin', model, url[0], url[1]))
}).flat()];
};
Expand Down

0 comments on commit bb75ee2

Please sign in to comment.