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

Add option to export library as webgmex #333

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/client/gmeServerRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,14 @@ define(['common/Constants', 'common/regexp'], function (CONSTANTS, REGEXP) {
}

//package save
function exportProjectToFile(projectId, branchName, commitHash, withAssets, callback) {
function _exportProjectOrLibToFile(projectId, branchName, commitHash, withAssets, libraryName, callback) {
var parameters = {
command: CONSTANTS.SERVER_WORKER_REQUESTS.EXPORT_PROJECT_TO_FILE,
projectId: projectId,
branchName: branchName,
commitHash: commitHash,
withAssets: withAssets
projectId,
branchName,
commitHash,
withAssets,
libraryName,
};

logger.debug('exportProjectToFile, parameters', parameters);
Expand All @@ -347,6 +348,10 @@ define(['common/Constants', 'common/regexp'], function (CONSTANTS, REGEXP) {
}
}

function exportProjectToFile(projectId, branchName, commitHash, withAssets, callback) {
_exportProjectOrLibToFile(projectId, branchName, commitHash, withAssets, undefined, callback);
}

function exportSelectionToFile(projectId, commitHash, selectedIds, withAssets, callback) {
var parameters = {
command: CONSTANTS.SERVER_WORKER_REQUESTS.EXPORT_SELECTION_TO_FILE,
Expand Down Expand Up @@ -407,6 +412,7 @@ define(['common/Constants', 'common/regexp'], function (CONSTANTS, REGEXP) {
autoMerge: autoMerge,
resolve: resolve,
exportProjectToFile: exportProjectToFile,
exportLibraryToFile: _exportProjectOrLibToFile,
exportSelectionToFile: exportSelectionToFile,
importSelectionFromFile: importSelectionFromFile
},
Expand Down
70 changes: 51 additions & 19 deletions src/common/util/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ define([
* @param {boolean} [parameters.withAssets=false] - Bundle the encountered assets linked from attributes.
* @param {string} [parameters.kind] - If not given will use the one defined in project (if any).
* @param {string} [parameters.outName] - Name of the output blob (.webgmex will be appended).
* @param {string} [parameters.libraryName] - If provided - will export library as webgmex file.
* @param {function} [callback]
* @param {Error|null} callback.err - If there was an error
* @param {object} callback.result - Data about the exported project
Expand All @@ -45,16 +46,47 @@ define([
* @returns {Promise}
*/
exports.exportProjectToFile = function exportProjectToFile(project, blobClient, parameters, callback) {
var fileName;

return storageUtils.getProjectJson(project, {
branchName: parameters.branchName,
commitHash: parameters.commitHash,
rootHash: parameters.rootHash,
tagName: parameters.tagName,
kind: parameters.kind
})
let fileName;
let libInfo;

return storageUtils.getRootHash(project, parameters)
.then(function (rootHash) {
if (!parameters.libraryName) {
return {
branchName: parameters.branchName,
commitHash: parameters.commitHash,
rootHash: parameters.rootHash,
tagName: parameters.tagName,
kind: parameters.kind
};
}

const core = new Core(project, { globConf: project.gmeConfig, logger: project.logger });

return core.loadRoot(rootHash)
.then(function (rootNode) {
const libNode = core.getLibraryRoot(rootNode, parameters.libraryName);
libInfo = core.getLibraryInfo(rootNode, parameters.libraryName);
return { rootHash: core.getHash(libNode) };
});
})
.then(function (exportContext) {
return storageUtils.getProjectJson(project, {
branchName: exportContext.branchName,
commitHash: exportContext.commitHash,
rootHash: exportContext.rootHash,
tagName: exportContext.tagName,
kind: exportContext.kind
});
})
.then(function (rawJson) {
if (parameters.libraryName) {
// We need to adjust this when the exportee was a library.
delete rawJson.kind; // This is lost at this point.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe i know where this come from, but is this really the good behavior?
shouldn't we enhance the library info with the kind to begin with?

rawJson.projectId = libInfo.projectId;
rawJson.commitHash = libInfo.commitHash;
}

fileName = typeof parameters.outName === 'string' ?
parameters.outName :
rawJson.projectId + '_' + (rawJson.commitHash || '').substr(1, 6);
Expand Down Expand Up @@ -132,19 +164,19 @@ define([
closureInfo = core.getClosureInformation(nodes);

return Q.all(nodes.map(function (node) {
return storageUtils.getProjectJson(project, {rootHash: core.getHash(node)});
return storageUtils.getProjectJson(project, { rootHash: core.getHash(node) });
}));
})
.then(function (rawJsons) {
var output = {
projectId: project.projectId,
commitHash: parameters.commitHash,
selectionInfo: closureInfo,
kind: rawJsons[0].kind,
objects: [],
hashes: {objects: [], assets: []}
},
i;
const output = {
projectId: project.projectId,
commitHash: parameters.commitHash,
selectionInfo: closureInfo,
kind: rawJsons[0].kind,
objects: [],
hashes: { objects: [], assets: [] }
};
let i;

fileName = typeof parameters.outName === 'string' ?
parameters.outName :
Expand Down
1 change: 1 addition & 0 deletions src/server/worker/workerrequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,7 @@ function WorkerRequests(mainLogger, gmeConfig, webgmeUrl) {
* @param {string} [parameters.tagName] - The tree at the given tag.
* @param {boolean} [parameters.withAssets=false] - Bundle the encountered assets linked from attributes.
* @param {string} [parameters.kind] - If not given will use the one defined in project (if any).
* @param {string} [parameters.libraryName] - If provided - will export library as webgmex file.
* @param {function} callback
*/
function exportProjectToFile(webgmeToken, parameters, callback) {
Expand Down
67 changes: 67 additions & 0 deletions test/server/worker/simpleworker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Simple worker', function () {
constraintProjectImportResult,
constraintProjectImportResult2,
renameProjectImportResult,
libProjectImportResult,

oldSend = process.send,
oldOn = process.on,
Expand Down Expand Up @@ -207,12 +208,20 @@ describe('Simple worker', function () {
branchName: 'master',
logger: logger,
gmeConfig: gmeConfig
}),
testFixture.importProject(storage, {
projectSeed: './test/plugin/PluginManagerBase/Lib.A.B.webgmex',
projectName: 'LibraryProject',
branchName: 'master',
logger: logger,
gmeConfig: gmeConfig
})
]);
})
.then(function (result) {
constraintProjectImportResult = result[0];
constraintProjectImportResult2 = result[1];
libProjectImportResult = result[2];

return testFixture.importProject(storage, {
projectSeed: './test/common/core/users/rename/propagate.webgmex',
Expand Down Expand Up @@ -1742,6 +1751,64 @@ describe('Simple worker', function () {
.nodeify(done);
});

it('exportProjectToFile should export library when name provided', function (done) {
var worker = getSimpleWorker();

worker.send({command: CONSTANTS.workerCommands.initialize, gmeConfig: gmeConfig})
.then(function (msg) {
expect(msg.pid).equal(process.pid);
expect(msg.type).equal(CONSTANTS.msgTypes.initialized);

return worker.send({
command: CONSTANTS.workerCommands.exportProjectToFile,
projectId: libProjectImportResult.project.projectId,
commitHash: libProjectImportResult.commitHash,
libraryName: 'A'
});
})
.then(function (msg) {
expect(msg.pid).equal(process.pid);
expect(msg.type).equal(CONSTANTS.msgTypes.result);
expect(msg.error).equal(null);

expect(msg.result).not.equal(null);
expect(typeof msg.result).to.equal('object');
expect(msg.result.downloadUrl).to.include('/rest/blob/download/');
expect(msg.result.fileName).to.include('guest+A_');
})
.finally(restoreProcessFunctions)
.nodeify(done);
});

it('exportProjectToFile should export nested library when name provided', function (done) {
var worker = getSimpleWorker();

worker.send({command: CONSTANTS.workerCommands.initialize, gmeConfig: gmeConfig})
.then(function (msg) {
expect(msg.pid).equal(process.pid);
expect(msg.type).equal(CONSTANTS.msgTypes.initialized);

return worker.send({
command: CONSTANTS.workerCommands.exportProjectToFile,
projectId: libProjectImportResult.project.projectId,
commitHash: libProjectImportResult.commitHash,
libraryName: 'A.B'
});
})
.then(function (msg) {
expect(msg.pid).equal(process.pid);
expect(msg.type).equal(CONSTANTS.msgTypes.result);
expect(msg.error).equal(null);

expect(msg.result).not.equal(null);
expect(typeof msg.result).to.equal('object');
expect(msg.result.downloadUrl).to.include('/rest/blob/download/');
expect(msg.result.fileName).to.include('guest+B_');
})
.finally(restoreProcessFunctions)
.nodeify(done);
});

it('should fail to exportProjectToFile when branchName does not exist.', function (done) {
var worker = getSimpleWorker();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance to add a test that would use the exported libary and run some traversal to see if everything goes well during the read of the nodes?

Expand Down
Loading