-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tom added force option to make commands
Allow to overwrite already existing migration, model, seeder.
- Loading branch information
Showing
18 changed files
with
251 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
#ifndef TOM_COMMANDS_MAKE_MAKECOMMAND_HPP | ||
#define TOM_COMMANDS_MAKE_MAKECOMMAND_HPP | ||
|
||
#include <orm/macros/systemheader.hpp> | ||
TINY_SYSTEM_HEADER | ||
|
||
#include <filesystem> | ||
|
||
#include "tom/commands/command.hpp" | ||
|
||
TINYORM_BEGIN_COMMON_NAMESPACE | ||
|
||
namespace Tom::Commands::Make | ||
{ | ||
|
||
/*! Base class for the make-related commands. */ | ||
class MakeCommand : public Command | ||
{ | ||
Q_DISABLE_COPY(MakeCommand) | ||
|
||
/*! Alias for the filesystem path. */ | ||
using fspath = std::filesystem::path; | ||
|
||
public: | ||
/*! Constructor. */ | ||
MakeCommand(Application &application, QCommandLineParser &parser); | ||
/*! Virtual destructor. */ | ||
inline ~MakeCommand() override = default; | ||
|
||
protected: | ||
/*! Check whether the created file already exists and create folder if needed. */ | ||
void prepareFileSystem( | ||
const QString &type, const fspath &folder, const QString &basename, | ||
const QString &className = {}) const; | ||
|
||
/*! Throw if the given classname constains a namespace or path. */ | ||
static void throwIfContainsNamespaceOrPath( | ||
const QString &type, const QString &className, | ||
const QString &source); | ||
|
||
private: | ||
/*! Ensure that a file in the given folder doesn't already exist. */ | ||
void throwIfModelAlreadyExists( | ||
const QString &type, const fspath &folder, const QString &basename, | ||
const QString &className) const; | ||
|
||
/*! Ensure a directory exists. */ | ||
static void ensureDirectoryExists(const fspath &path); | ||
}; | ||
|
||
} // namespace Tom::Commands::Make | ||
|
||
TINYORM_END_COMMON_NAMESPACE | ||
|
||
#endif // TOM_COMMANDS_MAKE_MAKECOMMAND_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "tom/commands/make/makecommand.hpp" | ||
|
||
#include "tom/exceptions/invalidargumenterror.hpp" | ||
#include "tom/tomconstants.hpp" | ||
|
||
namespace fs = std::filesystem; | ||
|
||
using fspath = std::filesystem::path; | ||
|
||
using Tom::Constants::DateTimePrefix; | ||
using Tom::Constants::force; | ||
|
||
TINYORM_BEGIN_COMMON_NAMESPACE | ||
|
||
namespace Tom::Commands::Make | ||
{ | ||
|
||
/* public */ | ||
|
||
MakeCommand::MakeCommand(Application &application, QCommandLineParser &parser) | ||
: Command(application, parser) | ||
{} | ||
|
||
/* protected */ | ||
|
||
void MakeCommand::prepareFileSystem( | ||
const QString &type, const fspath &folder, const QString &basename, | ||
const QString &className) const | ||
{ | ||
throwIfModelAlreadyExists(type, folder, basename, className); | ||
|
||
ensureDirectoryExists(folder); | ||
} | ||
|
||
void MakeCommand::throwIfContainsNamespaceOrPath( | ||
const QString &type, const QString &className, const QString &source) | ||
{ | ||
if (!className.contains(QStringLiteral("::")) && !className.contains(QChar('/')) && | ||
!className.contains(QChar('\\')) | ||
) | ||
return; | ||
|
||
throw Exceptions::InvalidArgumentError( | ||
QStringLiteral("Namespace or path is not allowed in the %1 name (%2).") | ||
.arg(type, source)); | ||
} | ||
|
||
/* private */ | ||
|
||
void MakeCommand::throwIfModelAlreadyExists( | ||
const QString &type, const fspath &folder, const QString &basename, | ||
const QString &className) const | ||
{ | ||
// Nothing to check | ||
if (!fs::exists(folder)) | ||
return; | ||
|
||
auto itemName = className.isEmpty() ? basename : className; | ||
|
||
using options = fs::directory_options; | ||
|
||
for (const auto &entry : | ||
fs::directory_iterator(folder, options::skip_permission_denied) | ||
) { | ||
// Check only files | ||
if (!entry.is_regular_file()) | ||
continue; | ||
|
||
// Extract base filename without the extension | ||
auto entryName = QString::fromStdString(entry.path().stem().string()); | ||
|
||
// If checking a migration then also remove the datetime prefix | ||
if (type == QStringLiteral("migration")) | ||
entryName = entryName.mid(DateTimePrefix.size() + 1); | ||
|
||
if (entryName == basename) { | ||
// Allow overwriting a file using the --force option | ||
if (!isSet(force)) | ||
throw Exceptions::InvalidArgumentError( | ||
QStringLiteral("A '%1' %2 already exists.") | ||
.arg(std::move(itemName), type)); | ||
|
||
comment(QStringLiteral("Overwriting '%1' already existing %2.") | ||
.arg(std::move(itemName), type)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void MakeCommand::ensureDirectoryExists(const fspath &path) | ||
{ | ||
if (fs::exists(path) && fs::is_directory(path)) | ||
return; | ||
|
||
fs::create_directories(path); | ||
} | ||
|
||
} // namespace Tom::Commands::Make | ||
|
||
TINYORM_END_COMMON_NAMESPACE |
Oops, something went wrong.