made bakeJS a static function

This commit is contained in:
utkarshgautamnyu 2017-09-26 19:35:54 -07:00 committed by GitHub
parent 4fd0452694
commit 167dda9f80

View file

@ -26,30 +26,53 @@ JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) :
void JSBaker::bake() { void JSBaker::bake() {
qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting"; qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting";
// Import the script to start baking // Import file to start baking
importJS();
}
void JSBaker::importJS() {
// Import the file to be processed from _jsURL
QFile jsFile(_jsURL.toLocalFile()); QFile jsFile(_jsURL.toLocalFile());
if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
handleError("Error opening " + _jsURL.fileName() + " for reading"); handleError("Error opening " + _jsURL.fileName() + " for reading");
return; return;
} }
// Import successful, Call the baking function with the imported file // Read file into an array
bakeJS(&jsFile); QByteArray inputJS = jsFile.readAll();
QByteArray outputJS;
// Call baking on inputJS and store result in outputJS
bool success = bakeJS(&inputJS, &outputJS);
if (!success) {
qCDebug(js_baking) << "Bake Failed";
handleError("Eror unterminated multi line comment");
return;
}
// Bake Successful. Export the file
auto fileName = _jsURL.fileName();
auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + BAKED_JS_EXTENSION;
_bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename;
QFile bakedFile;
bakedFile.setFileName(_bakedJSFilePath);
if (!bakedFile.open(QIODevice::WriteOnly)) {
handleError("Error opening " + _bakedJSFilePath + " for writing");
return;
}
bakedFile.write(outputJS);
// Export successful
_outputFiles.push_back(_bakedJSFilePath);
qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath;
// emit signal to indicate the JS baking is finished
emit finished();
} }
void JSBaker::bakeJS(QFile* inputFile) { bool JSBaker::bakeJS(QByteArray* inputFile, QByteArray* outputFile) {
// Create an output file which will be exported as the bakedFile
QFile outputFile;
outputFile.open(QIODevice::WriteOnly | QIODevice::Text);
// Read from inputFile and write to outputFile per character // Read from inputFile and write to outputFile per character
QTextStream in(inputFile); QTextStream in(inputFile,QIODevice::ReadOnly);
QTextStream out(&outputFile); QTextStream out(outputFile, QIODevice::WriteOnly);
// Algorithm requires the knowledge of previous and next character for each character read // Algorithm requires the knowledge of previous and next character for each character read
QChar currentCharacter; QChar currentCharacter;
@ -75,9 +98,10 @@ void JSBaker::bakeJS(QFile* inputFile) {
continue; continue;
} else if (nextCharacter == '*') { } else if (nextCharacter == '*') {
// Check if multi line comment i.e. /* // Check if multi line comment i.e. /*
handleMultiLineComments(&in); bool success = handleMultiLineComments(&in);
if (hasErrors()) { if (!success) {
return; // Errors present return false
return false;
} }
//Start fresh after handling comments //Start fresh after handling comments
previousCharacter = '\n'; previousCharacter = '\n';
@ -119,7 +143,7 @@ void JSBaker::bakeJS(QFile* inputFile) {
// Print the current quote and nextCharacter as is // Print the current quote and nextCharacter as is
out << currentCharacter; out << currentCharacter;
out << nextCharacter; out << nextCharacter;
// Store the type of quote we are processing // Store the type of quote we are processing
QChar quote = currentCharacter; QChar quote = currentCharacter;
@ -135,7 +159,7 @@ void JSBaker::bakeJS(QFile* inputFile) {
continue; continue;
} else { } else {
// In all other cases write the currentCharacter to outputFile // In all other cases write the currentCharacter to outputFile
out << currentCharacter; out << currentCharacter;
} }
previousCharacter = currentCharacter; previousCharacter = currentCharacter;
@ -147,37 +171,8 @@ void JSBaker::bakeJS(QFile* inputFile) {
out << currentCharacter; out << currentCharacter;
} }
// Reading done. Closing the inputFile // Successful bake. Return true
inputFile->close(); return true;
// Bake successful, Export the compressed outputFile
exportJS(&outputFile);
}
void JSBaker::exportJS(QFile* bakedFile) {
// save the relative path to this JS inside the output folder
auto fileName = _jsURL.fileName();
auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + BAKED_JS_EXTENSION;
_bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename;
bakedFile->setFileName(_bakedJSFilePath);
if (!bakedFile->open(QIODevice::WriteOnly)) {
handleError("Error opening " + _bakedJSFilePath + " for writing");
return;
}
// Export successful
_outputFiles.push_back(_bakedJSFilePath);
qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath;
// Exporting done. Closing the outputFile
bakedFile->close();
// emit signal to indicate the JS baking is finished
emit finished();
} }
void JSBaker::handleSingleLineComments(QTextStream * in) { void JSBaker::handleSingleLineComments(QTextStream * in) {
@ -190,17 +185,17 @@ void JSBaker::handleSingleLineComments(QTextStream * in) {
} }
} }
void JSBaker::handleMultiLineComments(QTextStream * in) { bool JSBaker::handleMultiLineComments(QTextStream * in) {
QChar character; QChar character;
while (!in->atEnd()) { while (!in->atEnd()) {
*in >> character; *in >> character;
if (character == '*') { if (character == '*') {
if (in->read(1) == '/') { if (in->read(1) == '/') {
return; return true;
} }
} }
} }
handleError("Eror unterminated multi line comment"); return false;
} }
bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) {