Skip to content

Commit

Permalink
WIP references
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed Jan 3, 2025
1 parent 10fd6a3 commit 67561a8
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 16 deletions.
2 changes: 2 additions & 0 deletions app/lib/file-context/file-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {

const Indexer = require('./indexer.js');
const Processor = require('./processor.js');
const References = require('./references.js');
const Watcher = require('./watcher.js');
const Workqueue = require('./workqueue.js');

Expand Down Expand Up @@ -53,6 +54,7 @@ module.exports = class FileContext extends EventEmitter {
this._workqueue = new Workqueue(this);
this._processor = new Processor(logger, processors);
this._indexer = new Indexer(logger, this, this._processor, this._workqueue);
this._references = new References(logger, this);

this._init(options);
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/file-context/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = class Processor {
* @returns {Promise<IndexItem>}
*/
process(item) {
this._logger.info('processor:process', item);
this._logger.info('processor:process', item.uri);

const processor = this._processors.find(processor => processor.extensions.includes(getFileExtension(item.file.path)));

Expand Down
27 changes: 21 additions & 6 deletions app/lib/file-context/processors/bpmnProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,31 @@ module.exports = {
process: async (item) => {
const processApplicationFilePath = findFileInParentDirectories(item.file.path, '.process-application');

const { rootElement } = await moddle.fromXML(item.file.contents);
let rootElement, ids, linkedIds;

return {
type: 'bpmn',
ids: findProcessIds(rootElement),
linkedIds: [
try {
({ rootElement } = await moddle.fromXML(item.file.contents));

ids = findProcessIds(rootElement);
linkedIds = [
...findLinkedProcessIds(rootElement),
...findLinkedDecisionIds(rootElement),
...findLinkedFormIds(rootElement)
],
];
} catch (error) {
return {
type: 'bpmn',
error: error.message,
ids: [],
linkedIds: [],
processApplication: processApplicationFilePath
};
};

return {
type: 'bpmn',
ids,
linkedIds,
processApplication: processApplicationFilePath
};
}
Expand Down
17 changes: 15 additions & 2 deletions app/lib/file-context/processors/dmnProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ module.exports = {
process: async (item) => {
const processApplicationFilePath = findFileInParentDirectories(item.file.path, '.process-application');

const { rootElement } = await moddle.fromXML(item.file.contents);
let rootElement, ids;

try {
({ rootElement } = await moddle.fromXML(item.file.contents));

ids = findDecisionIds(rootElement);
} catch (error) {
return {
type: 'dmn',
error: error.message,
ids: [],
processApplication: processApplicationFilePath
};
};

return {
type: 'dmn',
ids: findDecisionIds(rootElement),
ids,
processApplication: processApplicationFilePath
};
}
Expand Down
13 changes: 12 additions & 1 deletion app/lib/file-context/processors/formProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ module.exports = {
process: async (item) => {
const processApplicationFilePath = findFileInParentDirectories(item.file.path, '.process-application');

const form = JSON.parse(item.file.contents);
let form;

try {
form = JSON.parse(item.file.contents);
} catch (error) {
return {
type: 'form',
error: error.message,
ids: [],
processApplication: processApplicationFilePath
};
}

return {
type: 'form',
Expand Down
198 changes: 198 additions & 0 deletions app/lib/file-context/references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/**
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership.
*
* Camunda licenses this file to you under the MIT; you may not use this file
* except in compliance with the MIT License.
*/

/**
* @typedef { {
* uri: string,
* ids: string[],
* linkedIds: string[],
* type: string
* } } File
*
* @typedef { {
* uri: string,
* id: string,
* type: string;
* } } Link
*/

const path = require('path');

const { toFilePath } = require('./util');

/**
* Keeps track of references between files.
*/
module.exports = class References {

/**
* @type { Map<string, File> }
*/
filesByUri = new Map();

/**
* @param { import('./logger').default } logger
* @param { import('node:events').EventEmitter } eventBus
*/
constructor(logger, eventBus) {
this._logger = logger;
this._eventBus = eventBus;

eventBus.on('indexer:updated', (item) => {
try {
const {
uri,
metadata: {
ids = [],
linkedIds = [],
type
}
} = item;

this.addFile({
uri,
ids,
linkedIds,
type
});
} catch (err) {
this._logger.error('references:failed to process ' + item.uri, err);
}
});

eventBus.on('indexer:removed', (item) => {
this.removeFile(item.uri);
});

eventBus.on('references:changed', () => {
this._logger.info('references:changed');
});
}

/**
* @internal
*/
_changed() {
clearTimeout(this._changedTimer);

this._changedTimer = setTimeout(() => {
this._eventBus.emit('references:changed', Array.from(this.filesByUri.values()).reduce((references, file) => {
return {
...references,
[ file.uri ]: {
linkedFiles: this.findLinkedFiles(file),
linkingFiles: this.findLinkingFiles(file)
}
};
}, {}));
}, 300);
}

addFile(file) {
this.filesByUri.set(file.uri, file);

this._changed();
}

removeFile(uri) {
this.filesByUri.delete(uri);

this._changed();
}

/**
* Find linked files.
*/
findLinkedFiles({ linkedIds, type, uri }) {
if (type === 'processApplication') {
return Array.from(this.filesByUri.values()).filter(file => {
return hasProcessApplication(uri, file.uri);
});
}

return linkedIds
.reduce((linkedFiles, { linkedId, type }) => {
const linkedFile = this.findLinkedFile(linkedId, type);

if (linkedFile) {
return [
...linkedFiles,
linkedFile
];
}

return linkedFiles;
}, []);
}

/**
* Find linked file.
*/
findLinkedFile(id, type) {
const linkedFile = Array.from(this.filesByUri.values())
.filter(file => file.type === type)
.find(file => file.ids.includes(id));

if (linkedFile) {
return {
uri: linkedFile.uri,
id,
type
};
}

return null;
}

/**
* Find files linking to file.
*/
findLinkingFiles({ ids, type, uri }) {
const files = Array.from(this.filesByUri.values());

return files
.reduce((acc, file) => {
const linkedId = file.linkedIds.find(linkedId => linkedId.type === type && ids.includes(linkedId.linkedId));

if (linkedId) {
return [
...acc,
{
elementId: linkedId.elementId,
linkedId: linkedId.linkedId,
type: file.type,
uri: file.uri
}
];
}

if (file.type === 'processApplication' && hasProcessApplication(file.uri, uri)) {
return [
...acc,
{
type: file.type,
uri: file.uri
}
];
}

return acc;
}, []);
}
};

function hasProcessApplication(processApplicationUri, fileUri) {
const processApplicationPath = toFilePath(processApplicationUri),
filePath = toFilePath(fileUri);

const processApplicationDirectory = path.dirname(processApplicationPath);

return filePath.startsWith(processApplicationDirectory);
}
22 changes: 16 additions & 6 deletions app/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,12 @@ function bootstrap() {
fileContextLog.info(`//////////////////
///// FILE CONTEXT INDEXER UPDATED /////
////////////////////////////////////////`);
fileContextLog.info('indexer updated; index items', JSON.stringify(fileContext._indexer.getItems().map(({ file, metadata }) => {
return {
file: { ...file, contents: file.contents.length > 10 ? `${file.contents.substring(0, 10)}...` : file.contents },
metadata
};
}), null, 2));
// fileContextLog.info('indexer updated; index items', JSON.stringify(fileContext._indexer.getItems().map(({ file, metadata }) => {

Check failure on line 746 in app/lib/index.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Expected line before comment

Check failure on line 746 in app/lib/index.js

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Expected line before comment

Check failure on line 746 in app/lib/index.js

View workflow job for this annotation

GitHub Actions / build (windows-2022)

Expected line before comment
// return {
// file: { ...file, contents: file.contents.length > 10 ? `${file.contents.substring(0, 10)}...` : file.contents },
// metadata
// };
// }), null, 2));

const items = fileContext._indexer.getItems();

Expand All @@ -758,6 +758,16 @@ function bootstrap() {
fileContext.on('indexer:updated', onIndexerUpdated);
fileContext.on('indexer:removed', onIndexerUpdated);

function onReferencesChanged(references) {
fileContextLog.info('references changed');

renderer.send('file-context:references-changed', references);
}

fileContext.on('references:changed', (references) => {
onReferencesChanged(references);
});

app.on('quit', () => fileContext.close());

return {
Expand Down
1 change: 1 addition & 0 deletions app/lib/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const allowedEvents = [
'file-context:add-root',
'file-context:remove-root',
'file-context:changed',
'file-context:references-changed',
'file:opened',
'file:closed',
'file:content-changed'
Expand Down
1 change: 1 addition & 0 deletions client/src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,7 @@ export class App extends PureComponent {
<StatusBar />

<ProcessApplicationsStatusBar
activeTab={ activeTab }
processApplication={ this.state.processApplication }
onOpen={ async (path) => {
const files = await this.readFileList([ path ]);
Expand Down
8 changes: 8 additions & 0 deletions client/src/app/process-applications/ProcessApplications.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class ProcessApplications {
this._app.getGlobal('backend').on('file-context:changed', (_, items) => {
this._items = items;

const noMetadata = this._items.filter(item => !item.metadata);

console.log('no metadata', noMetadata);

const activeTab = this._app.state.activeTab;

if (this.hasOpen()) {
Expand All @@ -36,6 +40,10 @@ export default class ProcessApplications {
}
});

this._app.getGlobal('backend').on('file-context:references-changed', (_, references) => {
console.log('references changed', references);
});

this._app.on('app.activeTabChanged', ({ activeTab }) => {
const { file } = activeTab;

Expand Down
Loading

0 comments on commit 67561a8

Please sign in to comment.