mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 07:15:35 +08:00
Ref T131, utility functions
* recursive file copy function * build directories * find newest files
This commit is contained in:
@@ -45,6 +45,25 @@ namespace BlackMisc
|
||||
return pDir;
|
||||
}
|
||||
|
||||
const QString &CDirectoryUtils::getXSwiftBusBuildDirectory()
|
||||
{
|
||||
if (!CBuildConfig::isLocalDeveloperDebugBuild())
|
||||
{
|
||||
static const QString e;
|
||||
return e;
|
||||
}
|
||||
|
||||
// the xswiftbus directory in out, not in dist
|
||||
static const QString bd = []
|
||||
{
|
||||
QDir dir(binDirectory());
|
||||
if (!dir.cdUp()) { return QString(); }
|
||||
if (!dir.cd("xswiftbus")) { return QString(); }
|
||||
return dir.absolutePath();
|
||||
}();
|
||||
return bd;
|
||||
}
|
||||
|
||||
QString CDirectoryUtils::executableFilePath(const QString &executable)
|
||||
{
|
||||
Q_ASSERT_X(!executable.isEmpty(), Q_FUNC_INFO, "Missing executable file path");
|
||||
@@ -401,6 +420,14 @@ namespace BlackMisc
|
||||
return !dir.isEmpty();
|
||||
}
|
||||
|
||||
bool CDirectoryUtils::mkPathIfNotExisting(const QString &dir)
|
||||
{
|
||||
const QDir d(dir);
|
||||
if (d.exists()) { return true; }
|
||||
QDir mkDir;
|
||||
return mkDir.mkpath(dir);
|
||||
}
|
||||
|
||||
QStringList CDirectoryUtils::getExistingUnemptyDirectories(const QStringList &directories)
|
||||
{
|
||||
QStringList dirs;
|
||||
@@ -447,6 +474,40 @@ namespace BlackMisc
|
||||
return found;
|
||||
}
|
||||
|
||||
int CDirectoryUtils::copyDirectoryRecursively(const QString &fromDir, const QString &toDir, bool replaceOnConflict)
|
||||
{
|
||||
QDir dir(fromDir);
|
||||
const QStringList fromFiles = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
|
||||
if (!mkPathIfNotExisting(toDir)) { return -1; }
|
||||
|
||||
int count = 0;
|
||||
for (const QString ©File : fromFiles)
|
||||
{
|
||||
const QString from = CFileUtils::appendFilePaths(fromDir, copyFile);
|
||||
const QString to = CFileUtils::appendFilePaths(toDir, copyFile);
|
||||
if (QFile::exists(to))
|
||||
{
|
||||
if (!replaceOnConflict) { continue; }
|
||||
if (!QFile::remove(to)) { return -1; }
|
||||
}
|
||||
if (!QFile::copy(from, to)) { return -1; }
|
||||
count++;
|
||||
}
|
||||
|
||||
const QStringList subDirs = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||
for (const QString ©Dir : subDirs)
|
||||
{
|
||||
const QString fromSubDir = CFileUtils::appendFilePaths(fromDir, copyDir);
|
||||
const QString toSubDir = CFileUtils::appendFilePaths(toDir, copyDir);
|
||||
if (!mkPathIfNotExisting(toDir)) { return -1; }
|
||||
|
||||
const int c = copyDirectoryRecursively(fromSubDir, toSubDir, replaceOnConflict);
|
||||
if (c < 0) { return -1; }
|
||||
count += c;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
CDirectoryUtils::DirComparison CDirectoryUtils::compareTwoDirectories(const QString &dirSource, const QString &dirTarget, bool nestedDirs)
|
||||
{
|
||||
DirComparison comp;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <QMap>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
#include <QFileInfoList>
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -36,6 +37,10 @@ namespace BlackMisc
|
||||
//! Plugins directory
|
||||
static const QString &pluginsDirectory();
|
||||
|
||||
//! The build directory
|
||||
//! \remark if is a local build
|
||||
static const QString &getXSwiftBusBuildDirectory();
|
||||
|
||||
//! The executable file path
|
||||
static QString executableFilePath(const QString &executable);
|
||||
|
||||
@@ -129,6 +134,13 @@ namespace BlackMisc
|
||||
//! Exists directory and does it contains files
|
||||
static bool existsUnemptyDirectory(const QString &testDir);
|
||||
|
||||
//! Make directory if not already existing
|
||||
//! \remark returns true if path exists or was created
|
||||
static bool mkPathIfNotExisting(const QString &dir);
|
||||
|
||||
//! Copy directory recursively
|
||||
static int copyDirectoryRecursively(const QString &fromDir, const QString &toDir, bool replaceOnConflict);
|
||||
|
||||
//! Get the existing directories
|
||||
static QStringList getExistingUnemptyDirectories(const QStringList &directories);
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "blackmisc/worker.h"
|
||||
#include "blackmisc/fileutils.h"
|
||||
#include "blackmisc/math/mathutils.h"
|
||||
|
||||
#include "blackconfig/buildconfig.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
@@ -116,7 +115,8 @@ namespace BlackMisc
|
||||
|
||||
QString CFileUtils::appendFilePathsAndFixUnc(const QString &path1, const QString &path2)
|
||||
{
|
||||
return CFileUtils::fixWindowsUncPath(appendFilePaths(path1, path2));
|
||||
static const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
|
||||
return win ? CFileUtils::fixWindowsUncPath(appendFilePaths(path1, path2)) : appendFilePaths(path1, path2);
|
||||
}
|
||||
|
||||
QString CFileUtils::stripFileFromPath(const QString &path)
|
||||
@@ -133,38 +133,10 @@ namespace BlackMisc
|
||||
|
||||
QString CFileUtils::appendFilePathsAndFixUnc(const QString &path1, const QString &path2, const QString &path3)
|
||||
{
|
||||
return CFileUtils::fixWindowsUncPath(CFileUtils::appendFilePaths(CFileUtils::appendFilePaths(path1, path2), path3));
|
||||
}
|
||||
|
||||
bool CFileUtils::copyRecursively(const QString &sourceDir, const QString &destinationDir)
|
||||
{
|
||||
QFileInfo sourceFileInfo(sourceDir);
|
||||
if (sourceFileInfo.isDir())
|
||||
{
|
||||
QDir targetDir(destinationDir);
|
||||
if (!targetDir.mkpath("."))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const QDir originDir(sourceFileInfo.absoluteFilePath());
|
||||
const auto fileNames = originDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
|
||||
for (const QString &fileName : fileNames)
|
||||
{
|
||||
if (!copyRecursively(originDir.absoluteFilePath(fileName), targetDir.absoluteFilePath(fileName)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!QFile::copy(sourceDir, destinationDir))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
static const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
|
||||
return win ?
|
||||
CFileUtils::fixWindowsUncPath(CFileUtils::appendFilePaths(CFileUtils::appendFilePaths(path1, path2), path3)) :
|
||||
CFileUtils::appendFilePaths(CFileUtils::appendFilePaths(path1, path2), path3);
|
||||
}
|
||||
|
||||
QString CFileUtils::normalizeFilePathToQtStandard(const QString &filePath)
|
||||
@@ -325,7 +297,7 @@ namespace BlackMisc
|
||||
return result;
|
||||
}
|
||||
|
||||
QString CFileUtils::findNewestFile(const QDir &dir, bool recursive, const QStringList &nameFilters, const QStringList &excludeDirectories)
|
||||
QFileInfo CFileUtils::findNewestFile(const QDir &dir, bool recursive, const QStringList &nameFilters, const QStringList &excludeDirectories)
|
||||
{
|
||||
if (isExcludedDirectory(dir, excludeDirectories)) { return QString(); }
|
||||
const QFileInfoList files = enumerateFiles(dir, recursive, nameFilters, excludeDirectories);
|
||||
@@ -335,7 +307,7 @@ namespace BlackMisc
|
||||
{
|
||||
return a.lastModified() < b.lastModified();
|
||||
});
|
||||
return it->filePath();
|
||||
return *it;
|
||||
}
|
||||
|
||||
const QStringList &CFileUtils::getSwiftExecutables()
|
||||
@@ -390,6 +362,8 @@ namespace BlackMisc
|
||||
|
||||
QString CFileUtils::fixWindowsUncPath(const QString &filePath)
|
||||
{
|
||||
static const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
|
||||
if (!win) { return filePath; }
|
||||
if (!filePath.startsWith('/')) { return filePath; }
|
||||
if (filePath.startsWith("//")) { return filePath; }
|
||||
static const QString f("/%1");
|
||||
@@ -398,6 +372,9 @@ namespace BlackMisc
|
||||
|
||||
QStringList CFileUtils::fixWindowsUncPaths(const QStringList &filePaths)
|
||||
{
|
||||
static const bool win = CBuildConfig::isRunningOnWindowsNtPlatform();
|
||||
if (!win) { return filePaths; }
|
||||
|
||||
QStringList fixedPaths;
|
||||
for (const QString &path : filePaths)
|
||||
{
|
||||
|
||||
@@ -82,10 +82,6 @@ namespace BlackMisc
|
||||
//! Strip file from path a/b/c.json a/b
|
||||
static QString stripFileFromPath(const QString &path);
|
||||
|
||||
//! If `sourceDir` is a directory, copies it recursively, so that `sourceDir` becomes `destinationDir`.
|
||||
//! If it is a file, just copies the file.
|
||||
static bool copyRecursively(const QString &sourceDir, const QString &destinationDir);
|
||||
|
||||
//! Normalize file path to Qt standard, e.g by turning \ to /
|
||||
static QString normalizeFilePathToQtStandard(const QString &filePath);
|
||||
|
||||
@@ -130,7 +126,7 @@ namespace BlackMisc
|
||||
static QFileInfoList enumerateFiles(const QDir &dir, bool recursive, const QStringList &nameFilters = {}, const QStringList &excludeDirectories = {}, std::function<bool(const QFileInfo &)> predicate = {});
|
||||
|
||||
//! Returns path to the newest file in dir, optionally matching a wildcard, or empty string.
|
||||
static QString findNewestFile(const QDir &dir, bool recursive, const QStringList &nameFilters = {}, const QStringList &excludeDirectories = {});
|
||||
static QFileInfo findNewestFile(const QDir &dir, bool recursive, const QStringList &nameFilters = {}, const QStringList &excludeDirectories = {});
|
||||
|
||||
//! Get all swift executables
|
||||
static const QStringList &getSwiftExecutables();
|
||||
|
||||
Reference in New Issue
Block a user