Skip to content

Commit

Permalink
Remove LibraryCode nodes on library removal. Fixes #1085 (#1094)
Browse files Browse the repository at this point in the history
* WIP #1085 use the core to remove libraries and dependent LibraryCode

* WIP #1085 fixed issues with removing library code node(s)
  • Loading branch information
brollb committed Apr 2, 2018
1 parent 4e6f069 commit 228778c
Showing 1 changed file with 66 additions and 8 deletions.
74 changes: 66 additions & 8 deletions src/visualizers/panels/ForgeActionButton/LibraryDialog.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* globals define, $, WebGMEGlobal */
define([
'common/core/coreQ',
'common/storage/constants',
'q',
'underscore',
'text!./Libraries.json',
'text!./LibraryDialogModal.html',
'css!./LibraryDialog.css'
], function(
Core,
STORAGE_CONSTANTS,
Q,
_,
LibrariesText,
LibraryHtml
) {
Expand All @@ -23,7 +29,6 @@ define([
this.$tableContent = this.$table.find('tbody');

Libraries.forEach(library => this.addLibraryToTable(library));
// TODO: clicking on them should import the library
};

LibraryDialog.prototype.addLibraryToTable = function(libraryInfo) {
Expand Down Expand Up @@ -80,8 +85,6 @@ define([
libraryInfo: libraryInfo
};

// Pass in the library info
// TODO: show loading circles?
return Q.ninvoke(this.client, 'runServerPlugin', pluginId, context)
.then(() => {
this.logger.info('imported library: ', libraryInfo.name);
Expand All @@ -92,11 +95,66 @@ define([
};

LibraryDialog.prototype.uninstall = function(libraryInfo) {
this.client.startTransaction(`Removed "${libraryInfo.name}" library`);
this.client.removeLibrary(libraryInfo.name);
this.client.completeTransaction();
this.onChange();
this.hide();
const commitMsg = `Removed "${libraryInfo.name}" library`;
const rootGuid = this.client.getActiveRootHash();
const branchName = this.client.getActiveBranchName();
const project = this.client.getProjectObject();
const startCommit = this.client.getActiveCommitHash();
const libName = libraryInfo.name;
const core = new Core(project, {
globConf: WebGMEGlobal.gmeConfig,
logger: this.logger.fork('core')
});

// Load the first node/commit...
let root;
return core.loadRoot(rootGuid)
.then(node => {
root = node;
return core.loadChildren(root);
})
.then(nodes => {
const metanodes = _.values(core.getAllMetaNodes(root));
const libraryCode = metanodes
.find(node => core.getAttribute(node, 'name') === 'LibraryCode');
const libraryNode = nodes.find(node => {
return core.isLibraryRoot(node) &&
core.getAttribute(node, 'name') === libName;
});
const libPath = core.getPath(libraryNode);

// Remove any LibraryCode nodes with a ptr to the given library
const libraryCodeNodes = nodes
.filter(node => core.isTypeOf(node, libraryCode))
.filter(node => core.getPointerPath(node, 'library') === libPath);

libraryCodeNodes.forEach(node => core.deleteNode(node));
this.logger.info(`Removed ${libraryCodeNodes.length} library code nodes`);

core.removeLibrary(root, libName);
this.logger.info(`Removed ${libName} library`);
// Make the given commit
const persisted = core.persist(root);
return project.makeCommit(
branchName,
[ startCommit ],
persisted.rootHash,
persisted.objects,
commitMsg
);
})
.then(result => {
if (result.status === STORAGE_CONSTANTS.SYNCED) {
// Throw out the changes... warn the user?
this.logger.info('SYNCED!');
} else {
// Throw out the changes... warn the user?
this.logger.warn(`Could not remove library ${libName}`);
}
this.onChange();
this.hide();
})
.catch(err => this.logger.error(`Could not remove lib ${libName}: ${err}`));
};

LibraryDialog.prototype.onChange = function() {
Expand Down

0 comments on commit 228778c

Please sign in to comment.