Skip to content
Permalink
1e63f07ee9
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 73 lines (67 sloc) 2.6 KB
#!/usr/bin/env node
/*! @license ©2014 Ruben Verborgh - Multimedia Lab / iMinds / Ghent University */
/* Merges the queries from the 'queries' folder into 'settings.json'. */
var fs = require('fs'),
path = require('path');
var queryDir = path.join(__dirname, 'queries'),
settingsFile = path.join(__dirname, 'settings.json'),
outputFile = path.join(__dirname, 'queries.json');
// Write the settings with queries to the output file
var settings = JSON.parse(fs.readFileSync(settingsFile, { encoding: 'utf8' }));
settings.queries = readQueryDirectory(queryDir)
// Sort by ascending priority and name
.sort(function (a, b) {
return (a.priority - b.priority) ||
(a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1);
})
// Remove priority numbers in output
.map(function (d) { return delete d.priority, d; });
fs.writeFileSync(outputFile, JSON.stringify(settings));
// Recursively reads queries in the given directory
function readQueryDirectory(directory) {
return [].concat.apply([], fs.readdirSync(directory).map(function (filename) {
filename = path.join(directory, filename);
if (fs.statSync(filename).isDirectory())
return readQueryDirectory(filename);
else if (/\.sparql$/.test(filename))
return readQuery(filename);
}));
}
// Creates a query from the file
function readQuery(queryFile) {
// Construct the initial query
var query = { sparql: fs.readFileSync(queryFile, { encoding: 'utf8' }),
name: queryFile.replace(/^.+\/|\.\w+$/g, ''),
priority: 1000, datasources: [] };
// Parse SPARQL comments for metadata
var inputLines = query.sparql.split('\n'), outputLines = [];
inputLines.forEach(function (line, index) {
// The line might be a key/value pair
var keyValue = line.match(/^#\s*(\w+)\s*:\s*(.*)\s*/);
if (keyValue) {
var key = keyValue[1].toLowerCase(), value = keyValue[2];
switch (key) {
case 'datasource':
case 'datasources':
value.split(/\s+/)
.forEach(function (d) { if (d.length) query.datasources.push(d); });
return;
}
}
// The first line might contain a name
if (index === 0 && line[0] === '#') {
query.name = line.replace(/#\s*/, '');
// A number preceding the name indicates the priority
if (/^\d/.test(query.name)) {
query.priority = parseInt(query.name, 10);
query.name = query.name.replace(/^\d+\W\s*/, '');
}
}
// Any other line is part of the query
else {
outputLines.push(line);
}
});
query.sparql = outputLines.join('\n').trim();
return query;
}