-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathhotreload.cpp
More file actions
115 lines (101 loc) · 4.09 KB
/
hotreload.cpp
File metadata and controls
115 lines (101 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "hotreload.h"
#include <QFileSystemWatcher>
#include <QDir>
#include <QGuiApplication>
#include <QProcess>
#include <QTimer>
QString HotReload::syncScript() const
{
return QString( "#!/bin/sh \n"
"echo 'Syncing modified files...' \n"
"rsync -rau \"%1/qml/\" HotReload/qml/ \n"
"rsync -rau \"%1/../app/qml/\" HotReload/app/qml/ \n" )
.arg( GALLERY_SOURCE_DIR );
}
HotReload::HotReload( QQmlApplicationEngine &engine, QObject *parent ):
_engine( engine )
{
// create dirs for sync (near the app)
if ( !QDir( "HotReload/qml/" ).exists() )
QDir().mkpath( QGuiApplication::applicationDirPath() + "/HotReload/qml/" );
if ( !QDir( "HotReload/app/qml/" ).exists() )
QDir().mkpath( QGuiApplication::applicationDirPath() + "/HotReload/app/qml/" );
// create runnable sync script (near the app)
QString scriptFilename = QGuiApplication::applicationDirPath() + "/syncGallery.sh";
qInfo() << "Sync script location: " << scriptFilename;
{
QFile file( QFileInfo( scriptFilename ).absoluteFilePath() );
const QString script = syncScript();
if ( !file.open( QIODevice::WriteOnly ) )
{
qInfo() << "Cannot create script file";
exit( 1 );
}
QTextStream out( &file );
out << script;
file.close();
QProcess::execute( "chmod", QStringList() << "+x" << file.fileName() );
}
// run sync script
QProcess::startDetached( "./syncGallery.sh" );
// start watching the changes in synced dirs
QTimer::singleShot( 2000, this, &HotReload::startHotReload );
}
void HotReload::clearCache()
{
_engine.clearComponentCache();
}
void HotReload::startHotReload()
{
_debounceTimer = new QTimer( this );
_debounceTimer->setSingleShot( true );
_debounceTimer->setInterval( 300 );
// when the timer starts, run the sync script ONCE, then reload
connect( _debounceTimer, &QTimer::timeout, this, [this]()
{
// run the sync synchronously so it finishes before reloading
QProcess::execute( "./syncGallery.sh" );
emit watchedSourceChanged();
} );
_watcher = new QFileSystemWatcher( this );
// Set up base paths for your source code
QString gallerySrc = QString( GALLERY_SOURCE_DIR ) + "/qml";
QString appSrc = QString( GALLERY_SOURCE_DIR ) + "/../app/qml";
// Watch the SOURCE directories instead of the destination
_watcher->addPath( gallerySrc );
_watcher->addPath( gallerySrc + "/Pages" );
_watcher->addPath( gallerySrc + "/pages" );
_watcher->addPath( gallerySrc + "/components" );
_watcher->addPath( appSrc + "/account" );
_watcher->addPath( appSrc + "/account/components" );
_watcher->addPath( appSrc + "/components" );
_watcher->addPath( appSrc + "/dialogs" );
_watcher->addPath( appSrc + "/form" );
_watcher->addPath( appSrc + "/form/components" );
_watcher->addPath( appSrc + "/form/editors" );
_watcher->addPath( appSrc + "/gps" );
_watcher->addPath( appSrc + "/inputs" );
_watcher->addPath( appSrc + "/layers" );
_watcher->addPath( appSrc + "/map" );
_watcher->addPath( appSrc + "/project" );
_watcher->addPath( appSrc + "/project/components" );
_watcher->addPath( appSrc + "/settings" );
_watcher->addPath( appSrc + "/settings/components" );
// when you save the file, start the debounce timer
connect( _watcher, &QFileSystemWatcher::directoryChanged, this, [this]( const QString & path )
{
_debounceTimer->start();
} );
connect( _watcher, &QFileSystemWatcher::fileChanged, this, [this]( const QString & path )
{
_debounceTimer->start();
} );
}