This commit is contained in:
parent
1aea5cd924
commit
c8e6f51e1b
1 changed files with 168 additions and 166 deletions
|
@ -1,8 +1,9 @@
|
||||||
#include "UserIntent.hpp"
|
#include "UserIntent.hpp"
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <spdlog/spdlog.h>
|
|
||||||
|
|
||||||
#include "Archiver.hpp"
|
#include "Archiver.hpp"
|
||||||
|
|
||||||
|
@ -10,10 +11,7 @@ namespace xwim {
|
||||||
unique_ptr<UserIntent> make_compress_intent(const UserOpt &userOpt) {
|
unique_ptr<UserIntent> make_compress_intent(const UserOpt &userOpt) {
|
||||||
if (userOpt.paths.size() == 1) {
|
if (userOpt.paths.size() == 1) {
|
||||||
return make_unique<CompressSingleIntent>(
|
return make_unique<CompressSingleIntent>(
|
||||||
CompressSingleIntent{
|
CompressSingleIntent{*userOpt.paths.begin(), userOpt.out});
|
||||||
*userOpt.paths.begin(),
|
|
||||||
userOpt.out
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userOpt.out.has_value()) {
|
if (!userOpt.out.has_value()) {
|
||||||
|
@ -21,10 +19,7 @@ namespace xwim {
|
||||||
}
|
}
|
||||||
|
|
||||||
return make_unique<CompressManyIntent>(
|
return make_unique<CompressManyIntent>(
|
||||||
CompressManyIntent{
|
CompressManyIntent{userOpt.paths, userOpt.out.value()});
|
||||||
userOpt.paths,
|
|
||||||
userOpt.out.value()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unique_ptr<UserIntent> make_extract_intent(const UserOpt &userOpt) {
|
unique_ptr<UserIntent> make_extract_intent(const UserOpt &userOpt) {
|
||||||
|
@ -34,42 +29,45 @@ namespace xwim {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return make_unique<ExtractIntent>(
|
return make_unique<ExtractIntent>(ExtractIntent{userOpt.paths, userOpt.out});
|
||||||
ExtractIntent{
|
|
||||||
userOpt.paths,
|
|
||||||
userOpt.out
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unique_ptr<UserIntent> try_infer_compress_intent(const UserOpt &userOpt) {
|
unique_ptr<UserIntent> try_infer_compress_intent(const UserOpt &userOpt) {
|
||||||
if (!userOpt.out.has_value()) {
|
if (!userOpt.out.has_value()) {
|
||||||
spdlog::debug("No <out> provided");
|
spdlog::debug("No <out> provided");
|
||||||
if (userOpt.paths.size() != 1) {
|
if (userOpt.paths.size() != 1) {
|
||||||
spdlog::debug("Not a single-path compression. Cannot guess <out> for many-path compression");
|
spdlog::debug(
|
||||||
|
"Not a single-path compression. Cannot guess <out> for many-path "
|
||||||
|
"compression");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::debug("Only one <path> provided. Assume single-path compression.");
|
spdlog::debug("Only one <path> provided. Assume single-path compression.");
|
||||||
return make_unique<CompressSingleIntent>(CompressSingleIntent{*userOpt.paths.begin(), userOpt.out});
|
return make_unique<CompressSingleIntent>(
|
||||||
|
CompressSingleIntent{*userOpt.paths.begin(), userOpt.out});
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::debug("<out> provided: {}", userOpt.out.value());
|
spdlog::debug("<out> provided: {}", userOpt.out.value());
|
||||||
if (can_handle_archive(userOpt.out.value())) {
|
if (can_handle_archive(userOpt.out.value())) {
|
||||||
spdlog::debug("{} given and a known archive format, assume compression", userOpt.out.value());
|
spdlog::debug("{} given and a known archive format, assume compression",
|
||||||
|
userOpt.out.value());
|
||||||
return make_compress_intent(userOpt);
|
return make_compress_intent(userOpt);
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::debug("Cannot compress multiple paths without a user-provided output archive");
|
spdlog::debug(
|
||||||
|
"Cannot compress multiple paths without a user-provided output archive");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
unique_ptr<UserIntent> try_infer_extract_intent(const UserOpt &userOpt) {
|
unique_ptr<UserIntent> try_infer_extract_intent(const UserOpt &userOpt) {
|
||||||
bool can_extract_all = std::all_of(
|
bool can_extract_all =
|
||||||
userOpt.paths.begin(), userOpt.paths.end(),
|
std::all_of(userOpt.paths.begin(), userOpt.paths.end(),
|
||||||
[](const path &path) { return can_handle_archive(path); });
|
[](const path &path) { return can_handle_archive(path); });
|
||||||
|
|
||||||
if (!can_extract_all) {
|
if (!can_extract_all) {
|
||||||
spdlog::debug("Cannot extract all provided <paths>. Assume this is not an extraction.");
|
spdlog::debug(
|
||||||
|
"Cannot extract all provided <paths>. Assume this is not an "
|
||||||
|
"extraction.");
|
||||||
for (const path &p : userOpt.paths) {
|
for (const path &p : userOpt.paths) {
|
||||||
if (!can_handle_archive(p)) {
|
if (!can_handle_archive(p)) {
|
||||||
spdlog::debug("Cannot handle {}", p);
|
spdlog::debug("Cannot handle {}", p);
|
||||||
|
@ -80,11 +78,16 @@ namespace xwim {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userOpt.out.has_value() && can_handle_archive(userOpt.out.value())) {
|
if (userOpt.out.has_value() && can_handle_archive(userOpt.out.value())) {
|
||||||
spdlog::debug("Could extract all provided <paths>. But also {} looks like an archive. Ambiguous intent. Assume this is not an extraction.", userOpt.out.value());
|
spdlog::debug(
|
||||||
|
"Could extract all provided <paths>. But also {} looks like an "
|
||||||
|
"archive. Ambiguous intent. Assume this is not an extraction.",
|
||||||
|
userOpt.out.value());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
spdlog::debug("Could extract all provided <paths>. But also <out> looks like an archive. Ambiguous intent. Assume this is not an extraction.");
|
spdlog::debug(
|
||||||
|
"Could extract all provided <paths>. But also <out> looks like an "
|
||||||
|
"archive. Ambiguous intent. Assume this is not an extraction.");
|
||||||
return make_extract_intent(userOpt);
|
return make_extract_intent(userOpt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,16 +117,14 @@ namespace xwim {
|
||||||
}
|
}
|
||||||
spdlog::info("Cannot infer compression intent");
|
spdlog::info("Cannot infer compression intent");
|
||||||
|
|
||||||
|
|
||||||
throw XwimError("Cannot guess intent");
|
throw XwimError("Cannot guess intent");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ExtractIntent::execute() {
|
void ExtractIntent::execute() {
|
||||||
bool has_out = this->out.has_value();
|
bool has_out = this->out.has_value();
|
||||||
bool is_single = this->archives.size() == 1;
|
bool is_single = this->archives.size() == 1;
|
||||||
|
|
||||||
for (path p: this->archives) {
|
for (const path &p : this->archives) {
|
||||||
unique_ptr<Archiver> archiver = make_archiver(p);
|
unique_ptr<Archiver> archiver = make_archiver(p);
|
||||||
path out;
|
path out;
|
||||||
|
|
||||||
|
@ -154,7 +155,8 @@ namespace xwim {
|
||||||
|
|
||||||
if (next_entry == std::filesystem::directory_iterator()) {
|
if (next_entry == std::filesystem::directory_iterator()) {
|
||||||
spdlog::debug("Archive has single entry which is a directory");
|
spdlog::debug("Archive has single entry which is a directory");
|
||||||
if(std::filesystem::equivalent(first_path.filename(), out.filename())) {
|
if (std::filesystem::equivalent(first_path.filename(),
|
||||||
|
out.filename())) {
|
||||||
spdlog::debug("Archive entry named like archive");
|
spdlog::debug("Archive entry named like archive");
|
||||||
int i = rand_int(0, 100000);
|
int i = rand_int(0, 100000);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue