Skip to content

Commit

Permalink
Implement fs.readFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
iBelieve committed Mar 26, 2016
1 parent 24c6fb0 commit 82d050e
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 15 deletions.
3 changes: 1 addition & 2 deletions quickly/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"license": "MPL-2.0",
"scripts": {
"build": "qmlify --no-polyfills -d build src",
"test": "qmlify -d test/build test && qmltestrunner -input test/build",
"install-qml": "mkdir -p `qmake -query QT_INSTALL_QML`/Quickly && cp -r build/* src/qmldir `qmake -query QT_INSTALL_QML`/Quickly"
"test": "qmlify -d test/build test && cp -r test/assets test/build/ && qmltestrunner -input test/build"
},
"dependencies": {
"aurelia-polyfills": "^1.0.0-beta.1.0.6",
Expand Down
8 changes: 6 additions & 2 deletions quickly/quickly.pri
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
QT += qml quick quick_private

HEADERS += $$PWD/src/plugin.h \
$$PWD/src/nodejs/filesystem.h \
$$PWD/src/nodejs/path.h \
$$PWD/src/nodejs/process.h
$$PWD/src/nodejs/process.h \
$$PWD/src/nodejs/basemodule.h

SOURCES += $$PWD/src/plugin.cpp \
$$PWD/src/nodejs/filesystem.cpp \
$$PWD/src/nodejs/path.cpp \
$$PWD/src/nodejs/process.cpp
$$PWD/src/nodejs/process.cpp \
$$PWD/src/nodejs/basemodule.cpp

RESOURCES += $$PWD/resources.qrc
8 changes: 5 additions & 3 deletions quickly/quickly.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ VERSION = 0.1

CONFIG += c++11

QT += qml quick
QT += qml quick quick_private

HEADERS += $$PWD/src/plugin.h \
$$PWD/src/nodejs/filesystem.h \
$$PWD/src/nodejs/path.h \
$$PWD/src/nodejs/process.h
$$PWD/src/nodejs/process.h \
$$PWD/src/nodejs/basemodule.h

SOURCES += $$PWD/src/plugin.cpp \
$$PWD/src/nodejs/filesystem.cpp \
$$PWD/src/nodejs/path.cpp \
$$PWD/src/nodejs/process.cpp
$$PWD/src/nodejs/process.cpp \
$$PWD/src/nodejs/basemodule.cpp

QMLIFY += src

Expand Down
20 changes: 20 additions & 0 deletions quickly/src/nodejs/basemodule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Quickly - ES6 and Node.js-like environment for QML
*
* Copyright (C) 2016 Michael Spencer <sonrisesoftware@gmail.com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include "basemodule.h"

#include <QFile>
#include <QTextStream>
#include <private/qv8engine_p.h>

void BaseModule::throwException(const QString &message) const
{
QV8Engine::getV4(m_engine)->throwError(message);
}
33 changes: 33 additions & 0 deletions quickly/src/nodejs/basemodule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Quickly - ES6 and Node.js-like environment for QML
*
* Copyright (C) 2016 Michael Spencer <sonrisesoftware@gmail.com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef BASE_MODULE_H
#define BASE_MODULE_H

#include <QObject>

#include <QString>
#include <QQmlEngine>

class BaseModule : public QObject
{
Q_OBJECT

public:
BaseModule(QQmlEngine *engine = nullptr) : QObject(engine), m_engine(engine) {}

protected:
void throwException(const QString &message) const;

private:
QQmlEngine *m_engine;
};

#endif // BASE_MODULE_H
29 changes: 28 additions & 1 deletion quickly/src/nodejs/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,36 @@

#include "filesystem.h"

#include <QFile>
#include <QTextStream>
#include <private/qv8engine_p.h>

QString FileSystem::readFile(const QString &path) const
{
QString resolvedPath = resolve(path);

QFile file(resolvedPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throwException(QStringLiteral("File does not exist or is not readable: %1").arg(resolvedPath));
return "";
}

QTextStream in(&file);

return in.readAll();
}

QString FileSystem::resolve(const QString &pathOrUrl) const
{
if (pathOrUrl.startsWith("file://")) {
return pathOrUrl.right(pathOrUrl.length() - 7);
} else {
return pathOrUrl;
}
}

QObject *FileSystem::qmlSingleton(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)

return new FileSystem(engine);
Expand Down
11 changes: 7 additions & 4 deletions quickly/src/nodejs/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@
#ifndef FILE_SYSTEM_H
#define FILE_SYSTEM_H

#include <QObject>
#include "basemodule.h"

#include <QString>
#include <QQmlEngine>

class FileSystem : public QObject
class FileSystem : public BaseModule
{
Q_OBJECT

public:
FileSystem(QObject *parent = nullptr) : QObject(parent) {}
FileSystem(QQmlEngine *engine) : BaseModule(engine) {}

// Q_INVOKABLE QString readFile(const QString &path) const;
Q_INVOKABLE QString readFile(const QString &path) const;
// Q_INVOKABLE void writeFile(const QString &path, const QString &data) const;
// Q_INVOKABLE bool exists(const QString &path) const;
// Q_INVOKABLE void mkdir(const QString &path) const;
// Q_INVOKABLE void rmdir(const QString &path) const;

static QObject *qmlSingleton(QQmlEngine *engine, QJSEngine *scriptEngine);

private:
QString resolve(const QString &pathOrUrl) const;
};

#endif // FILE_SYSTEM_H
6 changes: 3 additions & 3 deletions quickly/src/nodejs/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
#ifndef PATH_H
#define PATH_H

#include <QObject>
#include "basemodule.h"

#include <QString>
#include <QVariantMap>
#include <QQmlEngine>

class Path : public QObject
class Path : public BaseModule
{
Q_OBJECT

Q_PROPERTY(QString delimiter READ delimiter CONSTANT)

public:
Path(QObject *parent = nullptr) : QObject(parent) {}
Path(QQmlEngine *engine) : BaseModule(engine) {}

QString delimiter() const;

Expand Down
1 change: 1 addition & 0 deletions quickly/test/assets/sample_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sample File
13 changes: 13 additions & 0 deletions quickly/test/nodejs/tst_fs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@ import Quickly 0.1

TestCase {
name: "FileSystemTests"

function test_readFile_valid_file_works() {
compare(FileSystem.readFile(Qt.resolvedUrl("../assets/sample_file.txt")), "Sample File\n")
}

function test_readFile_no_file_throws_exception() {
try {
FileSystem.readFile("missing_file.txt")
fail("An error should have been thrown!")
} catch (error) {
compare(error.message, "File does not exist or is not readable: missing_file.txt")
}
}
}

0 comments on commit 82d050e

Please sign in to comment.