Skip to content

Commit bdc447f

Browse files
committed
Fix local storage & add compiler name to unfurling
1 parent 08cd1ed commit bdc447f

File tree

7 files changed

+458
-20
lines changed

7 files changed

+458
-20
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ etc/config/*.local.properties
1616
*.map
1717
static/dist
1818
static/vs
19+
lib/storage/data

Diff for: app.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,28 @@ aws.initConfig(awsProps)
342342
ogTitle: result.specialMetadata ? result.specialMetadata.title.S : "Compiler Explorer"
343343
};
344344
if (!metadata.ogDescription) {
345-
const sources = utils.glGetEditorSources(config.content);
346-
if (sources.length === 1) {
347-
const lang = languages[sources[0].language];
348-
metadata.ogDescription = sources[0].source;
345+
const sources = utils.glGetMainContents(config.content);
346+
if (sources.editors.length === 1) {
347+
const editor = sources.editors[0];
348+
const lang = languages[editor.language];
349349
if (lang) {
350+
let code = editor.source;
351+
if (lang.previewFilter) {
352+
code = _.filter(code.split('\n'), line => {
353+
return !line.match(lang.previewFilter);
354+
}).join('\n');
355+
}
356+
metadata.ogDescription = code;
350357
metadata.ogTitle += ` - ${lang.name}`;
358+
if (sources.compilers.length === 1) {
359+
const compilerId = sources.compilers[0].compiler;
360+
const compiler = apiHandler.compilers.find(c => c.id === compilerId);
361+
if (compiler) {
362+
metadata.ogTitle += ` (${compiler.name})`;
363+
}
364+
}
365+
} else {
366+
metadata.ogDescription = editor.source;
351367
}
352368
}
353369
} else if (metadata.ogAuthor && metadata.ogAuthor !== '.') {
@@ -361,7 +377,8 @@ aws.initConfig(awsProps)
361377
metadata: metadata
362378
}));
363379
})
364-
.catch(() => {
380+
.catch(err => {
381+
logger.warn(`Exception thrown when expanding ${id}: `, err);
365382
next({
366383
statusCode: 404,
367384
message: `ID "${id}" could not be found`

Diff for: lib/languages.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const languages = {
4848
name: 'C++',
4949
monaco: 'cppp',
5050
extensions: ['.cpp', '.cxx', '.h', '.hpp', '.hxx', '.c'],
51-
alias: ['gcc', 'cpp']
51+
alias: ['gcc', 'cpp'],
52+
previewFilter: /^\s*#include/
5253
},
5354
llvm: {
5455
name: 'LLVM IR',
@@ -60,13 +61,15 @@ const languages = {
6061
name: 'Cppx',
6162
monaco: 'cppp',
6263
extensions: ['.cpp', '.cxx', '.h', '.hpp', '.hxx', '.c'],
63-
alias: []
64+
alias: [],
65+
previewFilter: /^\s*#include/
6466
},
6567
c: {
6668
name: 'C',
6769
monaco: 'c',
6870
extensions: ['.c', '.h'],
69-
alias: []
71+
alias: [],
72+
previewFilter: /^\s*#include/
7073
},
7174
rust: {
7275
name: 'Rust',

Diff for: lib/storage/storage-local.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class StorageLocal extends StorageBase {
4242

4343
storeItem(item) {
4444
const filePath = path.join(this.storageFolder, item.uniqueSubHash);
45-
return fs.writeJson(filePath, item, {encoding: 'utf8'});
45+
return fs.writeJson(filePath, item, {encoding: 'utf8'})
46+
.then(() => item)
47+
.catch(logger.error);
4648
}
4749

4850
findUniqueSubhash(hash) {
@@ -95,7 +97,16 @@ class StorageLocal extends StorageBase {
9597
const expectedPath = path.join(this.storageFolder, id);
9698
logger.info(`Expanding local id ${id} to ${expectedPath}`);
9799
return fs.readJson(expectedPath)
98-
.then(item => item.config);
100+
.then(item => {
101+
return {
102+
config: item.config,
103+
specialMetadata: null
104+
};
105+
})
106+
.catch(err => {
107+
logger.error(err);
108+
throw err;
109+
});
99110
}
100111
}
101112

Diff for: lib/utils.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,26 @@ exports.getHash = function getHash(object, HashVersion = DefaultHash) {
144144
};
145145

146146
/***
147-
* Gets every pair of source, lang available
148-
* @param content {Array}
149-
* @returns {Array}
147+
* Gets every (source, lang) & (compilerId) available
148+
* @param content {Array} GoldenLayout config topmost content field
149+
* @returns {Object} contents - Available Editors & Compilers
150+
* @returns {Array} contents.editors - Available editors
151+
* @returns {Array} contents.compilers - Available compilers
150152
*/
151-
exports.glGetEditorSources = function glGetEditorSources(content) {
152-
let sources = [];
153+
exports.glGetMainContents = function glGetMainContents(content) {
154+
let contents = {editors: [], compilers: []};
153155
_.each(content, element => {
154156
if (element.type === 'component') {
155157
if (element.componentName === 'codeEditor') {
156-
sources.push({source: element.componentState.source, language: element.componentState.lang});
158+
contents.editors.push({source: element.componentState.source, language: element.componentState.lang});
159+
} else if (element.componentName === 'compiler') {
160+
contents.compilers.push({compiler: element.componentState.compiler});
157161
}
158162
} else {
159-
sources = glGetEditorSources(element.content).concat(sources);
163+
const subComponents = glGetMainContents(element.content);
164+
contents.editors = contents.editors.concat(subComponents.editors);
165+
contents.compilers = contents.compilers.concat(subComponents.compilers);
160166
}
161167
});
162-
return sources;
163-
168+
return contents;
164169
};

0 commit comments

Comments
 (0)