Skip to content

Commit

Permalink
tom added force option to make commands
Browse files Browse the repository at this point in the history
Allow to overwrite already existing migration, model, seeder.
  • Loading branch information
silverqx committed Jul 10, 2022
1 parent a0b743f commit 1f84337
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 225 deletions.
2 changes: 2 additions & 0 deletions cmake/Modules/TinySources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ function(tinytom_sources out_headers out_sources)
commands/integratecommand.hpp
commands/listcommand.hpp
commands/make/concerns/prepareoptionvalues.hpp
commands/make/makecommand.hpp
commands/make/migrationcommand.hpp
commands/make/modelcommand.hpp
commands/make/modelcommandconcepts.hpp
Expand Down Expand Up @@ -334,6 +335,7 @@ function(tinytom_sources out_headers out_sources)
commands/integratecommand.cpp
commands/listcommand.cpp
commands/make/concerns/prepareoptionvalues.cpp
commands/make/makecommand.cpp
commands/make/migrationcommand.cpp
commands/make/modelcommand.cpp
# commands/make/projectcommand.cpp
Expand Down
1 change: 1 addition & 0 deletions tom/include/include.pri
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ headersList += \
$$PWD/tom/commands/integratecommand.hpp \
$$PWD/tom/commands/listcommand.hpp \
$$PWD/tom/commands/make/concerns/prepareoptionvalues.hpp \
$$PWD/tom/commands/make/makecommand.hpp \
$$PWD/tom/commands/make/migrationcommand.hpp \
$$PWD/tom/commands/make/modelcommand.hpp \
$$PWD/tom/commands/make/modelcommandconcepts.hpp \
Expand Down
56 changes: 56 additions & 0 deletions tom/include/tom/commands/make/makecommand.hpp
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
4 changes: 2 additions & 2 deletions tom/include/tom/commands/make/migrationcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <orm/macros/systemheader.hpp>
TINY_SYSTEM_HEADER

#include "tom/commands/command.hpp"
#include "tom/commands/make/makecommand.hpp"
#include "tom/commands/make/support/migrationcreator.hpp"
#include "tom/tomconstants.hpp"

Expand All @@ -15,7 +15,7 @@ namespace Tom::Commands::Make
{

/*! Create a new migration file. */
class MigrationCommand : public Command
class MigrationCommand : public MakeCommand
{
Q_DISABLE_COPY(MigrationCommand)

Expand Down
15 changes: 7 additions & 8 deletions tom/include/tom/commands/make/modelcommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ TINY_SYSTEM_HEADER

#include <unordered_set>

#include "tom/commands/command.hpp"
#include "tom/commands/make/concerns/prepareoptionvalues.hpp"
#include "tom/commands/make/makecommand.hpp"
#include "tom/commands/make/modelcommandconcepts.hpp"
#include "tom/commands/make/support/modelcreator.hpp"
#include "tom/tomconstants.hpp"
Expand All @@ -28,7 +28,7 @@ namespace Support
} // namespace Support

/*! Create a new model class. */
class ModelCommand : public Command,
class ModelCommand : public MakeCommand,
protected Concerns::PrepareOptionValues
{
Q_DISABLE_COPY(ModelCommand)
Expand Down Expand Up @@ -121,13 +121,12 @@ namespace Support
private:
/*! Throw if the model name constains a namespace or path. */
static void throwIfContainsNamespaceOrPath(
const std::vector<QStringList> &classNames, const QString &source);
const std::vector<QStringList> &classNames, const QString &source,
const QString &commandType = QStringLiteral("model"));
/*! Throw if the model name constains a namespace or path. */
static void throwIfContainsNamespaceOrPath(const QStringList &classNames,
const QString &source);
/*! Throw if the model name constains a namespace or path. */
static void throwIfContainsNamespaceOrPath(const QString &className,
const QString &source);
static void throwIfContainsNamespaceOrPath(
const QStringList &classNames, const QString &source,
const QString &commandType = QStringLiteral("model"));
};

/* public */
Expand Down
8 changes: 2 additions & 6 deletions tom/include/tom/commands/make/seedercommand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <orm/macros/systemheader.hpp>
TINY_SYSTEM_HEADER

#include "tom/commands/command.hpp"
#include "tom/commands/make/makecommand.hpp"
#include "tom/commands/make/support/seedercreator.hpp"
#include "tom/tomconstants.hpp"

Expand All @@ -15,7 +15,7 @@ namespace Tom::Commands::Make
{

/*! Create a new seeder class. */
class SeederCommand : public Command
class SeederCommand : public MakeCommand
{
Q_DISABLE_COPY(SeederCommand)

Expand Down Expand Up @@ -54,10 +54,6 @@ namespace Tom::Commands::Make

/*! The seeder creator instance. */
Support::SeederCreator m_creator {};

private:
/*! Throw if the seeder name constains a namespace or path. */
static void throwIfContainsNamespaceOrPath(const QString &className);
};

/* public */
Expand Down
7 changes: 0 additions & 7 deletions tom/include/tom/commands/make/support/migrationcreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ namespace Tom::Commands::Make::Support
populateStub(const QString &name, QString &&stub, const QString &table);
/*! Get the class name of a migration name. */
static QString getClassName(const QString &name);
/*! Ensure a directory exists. */
static void ensureDirectoryExists(const fspath &path);

private:
/*! Ensure that a migration with the given name doesn't already exist. */
static void throwIfMigrationAlreadyExists(const QString &name,
const fspath &migrationsPath);
};

} // namespace Tom::Commands::Make::Support
Expand Down
9 changes: 0 additions & 9 deletions tom/include/tom/commands/make/support/modelcreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ namespace Tom::Commands::Make::Support
/*! Get the full path to the model. */
static fspath getPath(const QString &basename, const fspath &path);

/*! Ensure a directory exists. */
static void ensureDirectoryExists(const fspath &path);

/*! Populate the place-holders in the model stub. */
std::string populateStub(const QString &className, const CmdOptions &cmdOptions,
bool isSetPreserveOrder);
Expand Down Expand Up @@ -192,12 +189,6 @@ namespace Tom::Commands::Make::Support

/*! Cached relations list size to avoid recomputation. */
std::size_t m_relationsListSize = 0;

private:
/*! Ensure that a model with the given name doesn't already exist. */
static void throwIfModelAlreadyExists(
const QString &className, const QString &basename,
const fspath &modelsPath);
};

} // namespace Tom::Commands::Make::Support
Expand Down
9 changes: 0 additions & 9 deletions tom/include/tom/commands/make/support/seedercreator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,12 @@ namespace Tom::Commands::Make::Support
/*! Get the full path to the seeder. */
static fspath getPath(const QString &basename, const fspath &path);

/*! Ensure a directory exists. */
static void ensureDirectoryExists(const fspath &path);

/*! Populate the place-holders in the seeder stub. */
static std::string
populateStub(const QString &className, QString &&table);

/*! Get the table name from the seeder class name. */
static QString getTableName(QString className);

private:
/*! Ensure that a seeder with the given name doesn't already exist. */
void throwIfSeederAlreadyExists(
const QString &className, const QString &basename,
const fspath &seedersPath) const;
};

} // namespace Tom::Commands::Make::Support
Expand Down
1 change: 1 addition & 0 deletions tom/src/src.pri
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sourcesList += \
$$PWD/tom/commands/integratecommand.cpp \
$$PWD/tom/commands/listcommand.cpp \
$$PWD/tom/commands/make/concerns/prepareoptionvalues.cpp \
$$PWD/tom/commands/make/makecommand.cpp \
$$PWD/tom/commands/make/migrationcommand.cpp \
$$PWD/tom/commands/make/modelcommand.cpp \
# $$PWD/tom/commands/make/projectcommand.cpp \
Expand Down
100 changes: 100 additions & 0 deletions tom/src/tom/commands/make/makecommand.cpp
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
Loading

0 comments on commit 1f84337

Please sign in to comment.