Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
myrrc committed Jul 8, 2023
1 parent b7f37e3 commit 4a5cbc4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/archive_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@ void ExportTask::rollback()

void ExportIndexTask::commit()
{
const std::string index_abs_path = Path::INDEX.prependRoot().join();
std::ofstream index{index_abs_path, std::ios::out | std::ios::trunc};
std::ofstream index{m_path, std::ios::out | std::ios::trunc};
if (!index.good())
return tx()->receipt()->addError({"Error opening index file", index_abs_path});
return tx()->receipt()->addError({"Error opening index file", m_path});

for(const Remote &remote : g_reapack->config()->remotes) {
index << "REPO " << remote.toString() << '\n';
Expand Down
8 changes: 5 additions & 3 deletions src/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "index.hpp"
#include "reapack.hpp"
#include "transaction.hpp"
#include <iostream>

InstallTask::InstallTask(const Version *ver, const int flags,
const Registry::Entry &re, const ArchiveReaderPtr &reader, Transaction *tx)
Expand Down Expand Up @@ -146,13 +147,14 @@ bool InstallFromIndexTask::start()

const ErrorInfo err = {
String::format("%s/%s/%s v%s",
pkg.remote.name().c_str(), pkg.category.c_str(), pkg.package.c_str(), pkg.version.c_str()),
pkg.remote.name().c_str(), pkg.category.c_str(), pkg.name.c_str(), pkg.version.c_str()),
"Package cannot be found or is incompatible with your operating system"};

const Package *const pkg_handle = [&]() -> const Package * {
for(const Package *other : index->packages())
if(other->name() == pkg.package && other->category()->name() == pkg.category)
for(const Package *other : index->packages()) {
if(other->name() == pkg.name && other->category()->name() == pkg.category)
return other;
}
return nullptr;
}();

Expand Down
31 changes: 19 additions & 12 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
static const Win32::char_type *ARCHIVE_FILTER =
L("ReaPack Offline Archive (*.ReaPackArchive)\0*.ReaPackArchive\0");
static const Win32::char_type *ARCHIVE_EXT = L("ReaPackArchive");
static const Win32::char_type *INDEX_FILTER =
L("ReaPack Index (*.txt)\0*.txt\0");
static const Win32::char_type *INDEX_EXT = L("txt");

enum {
ACTION_UNINSTALL = 80, ACTION_ABOUT, ACTION_REFRESH, ACTION_COPYURL,
Expand Down Expand Up @@ -506,7 +509,7 @@ void Manager::importArchive()
{
const char *title = "Import offline archive";

const std::string &path = FileDialog::getOpenFileName(handle(), instance(),
const std::string path = FileDialog::getOpenFileName(handle(), instance(),
title, Path::DATA.prependRoot(), ARCHIVE_FILTER, ARCHIVE_EXT);

if(path.empty())
Expand All @@ -524,7 +527,7 @@ void Manager::importArchive()

void Manager::exportArchive()
{
const std::string &path = FileDialog::getSaveFileName(handle(), instance(),
const std::string path = FileDialog::getSaveFileName(handle(), instance(),
"Export offline archive", Path::DATA.prependRoot(), ARCHIVE_FILTER, ARCHIVE_EXT);

if(!path.empty()) {
Expand All @@ -541,11 +544,12 @@ void Manager::importIndex()
if(!tx) return;
Receipt * const receipt = tx->receipt();

// TODO saveFileName
const std::string indexAbsPath = Path::INDEX.prependRoot().join();
std::ifstream index{indexAbsPath};
const std::string path = FileDialog::getOpenFileName(handle(), instance(),
"Import index", Path::root(), INDEX_FILTER, INDEX_EXT);

std::ifstream index{path};
if(!index.good()) {
receipt->addError({"Error opening index file", indexAbsPath});
receipt->addError({"Error opening index file", path});
tx->runTasks();
return;
}
Expand All @@ -561,13 +565,14 @@ void Manager::importIndex()
receipt->addError({"Error adding remote", remote.toString()});
else {
g_reapack->addSetRemote(remote);
// strange but above function doesn't fetch index files despite issuing same SynchronizeTask
tx->fetchIndexes({remote}, true);
}
break;
case 'P': {
auto& ref = packages.emplace_back(PackageFromIndex{remote});
std::istringstream{line}
>> quoted(ref.category) >> quoted(ref.package) >> quoted(ref.version)
std::istringstream{line.substr(5)}
>> quoted(ref.category) >> quoted(ref.name) >> quoted(ref.version)
>> ref.flags;
break;
}
Expand All @@ -576,18 +581,20 @@ void Manager::importIndex()
}
}

g_reapack->commitConfig(true); // runs tx

for(PackageFromIndex& pkg : packages)
tx->installFromIndex(std::move(pkg));

tx->runTasks();
reset();
g_reapack->commitConfig();
}

void Manager::exportIndex()
{
const std::string path = FileDialog::getSaveFileName(handle(), instance(),
"Export index", Path::root(), INDEX_FILTER, INDEX_EXT);

if(Transaction *tx = g_reapack->setupTransaction()) {
tx->exportIndex();
tx->exportIndex(path);
tx->runTasks();
}
}
Expand Down
1 change: 0 additions & 1 deletion src/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const Path Path::DATA("ReaPack");
const Path Path::CACHE = Path::DATA + "cache";
const Path Path::CONFIG("reapack.ini");
const Path Path::REGISTRY = Path::DATA + "registry.db";
const Path Path::INDEX("reapack-index.txt");

Path Path::s_root;

Expand Down
1 change: 0 additions & 1 deletion src/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Path {
static const Path CACHE;
static const Path CONFIG;
static const Path REGISTRY;
static const Path INDEX;

static const Path &root() { return s_root; }

Expand Down
5 changes: 3 additions & 2 deletions src/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class InstallTask : public Task {

struct PackageFromIndex {
Remote remote;
std::string package, category, version;
std::string name, category, version;
int flags;
};

Expand Down Expand Up @@ -162,10 +162,11 @@ class ExportTask : public Task {

class ExportIndexTask : public Task {
public:
inline ExportIndexTask(Transaction *tx): Task(tx) {}
inline ExportIndexTask(std::string_view path, Transaction *tx): Task(tx), m_path(path) {}

protected:
void commit() final;
std::string m_path;
};

#endif
4 changes: 2 additions & 2 deletions src/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ void Transaction::exportArchive(const std::string &path)
m_nextQueue.push(std::make_shared<ExportTask>(path, this));
}

void Transaction::exportIndex()
void Transaction::exportIndex(std::string_view path)
{
m_nextQueue.push(std::make_shared<ExportIndexTask>(this));
m_nextQueue.push(std::make_shared<ExportIndexTask>(path, this));
}

bool Transaction::runTasks()
Expand Down
2 changes: 1 addition & 1 deletion src/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Transaction {
void uninstall(const Remote &);
void uninstall(const Registry::Entry &);
void exportArchive(const std::string &path);
void exportIndex();
void exportIndex(std::string_view path);
bool runTasks();

bool isCancelled() const { return m_isCancelled; }
Expand Down

0 comments on commit 4a5cbc4

Please sign in to comment.