reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit aeb112c07e76d70acdb72a1908527a47a85bebc5
parent 29147b57f0798e97c2d4ff0bc930ecd0b8de017b
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Sat,  5 Mar 2016 02:14:40 -0500

refactor the transaction's list of changes

Diffstat:
Msrc/reapack.cpp | 8+++++---
Asrc/receipt.cpp | 38++++++++++++++++++++++++++++++++++++++
Asrc/receipt.hpp | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/report.cpp | 34++++++++++++++++------------------
Msrc/report.hpp | 6+++---
Msrc/transaction.cpp | 38++++++++++++++------------------------
Msrc/transaction.hpp | 56++++++++++++++++++++------------------------------------
7 files changed, 160 insertions(+), 84 deletions(-)

diff --git a/src/reapack.cpp b/src/reapack.cpp @@ -403,15 +403,17 @@ Transaction *ReaPack::createTransaction() Dialog::Destroy(m_progress); m_progress = nullptr; - if(!m_transaction->isReportEnabled()) + const Receipt *receipt = m_transaction->receipt(); + + if(m_transaction->isCancelled() || !receipt->isEnabled()) return; LockDialog lock(m_manager); - if(m_transaction->taskCount() == 0 && m_transaction->errors().empty()) + if(m_transaction->taskCount() == 0 && !receipt->hasErrors()) ShowMessageBox("Nothing to do!", "ReaPack", 0); else - Dialog::Show<Report>(m_instance, m_mainWindow, m_transaction); + Dialog::Show<Report>(m_instance, m_mainWindow, receipt); }); m_transaction->onDestroy([=] { diff --git a/src/receipt.cpp b/src/receipt.cpp @@ -0,0 +1,38 @@ +/* ReaPack: Package manager for REAPER + * Copyright (C) 2015-2016 Christian Fillion + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "receipt.hpp" + +#include "transaction.hpp" + +Receipt::Receipt() + : m_enabled(false), m_needRestart(false) +{ +} + +void Receipt::addTicket(const InstallTicket &ticket) +{ + if(ticket.regQuery.status == Registry::UpdateAvailable) + m_updates.push_back(ticket); + else + m_installs.push_back(ticket); +} + +void Receipt::addRemovals(const std::set<Path> &pathList) +{ + m_removals.insert(pathList.begin(), pathList.end()); +} diff --git a/src/receipt.hpp b/src/receipt.hpp @@ -0,0 +1,64 @@ +/* ReaPack: Package manager for REAPER + * Copyright (C) 2015-2016 Christian Fillion + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef REAPACK_RECEIPT_HPP +#define REAPACK_RECEIPT_HPP + +#include <set> +#include <string> +#include <vector> + +class Path; +struct InstallTicket; + +class Receipt { +public: + struct Error { + std::string message; + std::string title; + }; + + Receipt(); + + bool isEnabled() const { return m_enabled; } + void setEnabled(bool newVal) { m_enabled = newVal; } + + bool isRestartNeeded() const { return m_needRestart; } + void setRestartNeeded(bool newVal) { m_needRestart = newVal; } + + void addTicket(const InstallTicket &ticket); + const std::vector<InstallTicket> &installs() const { return m_installs; } + const std::vector<InstallTicket> &updates() const { return m_updates; } + + void addRemovals(const std::set<Path> &); + const std::set<Path> &removals() const { return m_removals; } + + void addError(const Error &err) { m_errors.push_back(err); } + const std::vector<Error> &errors() const { return m_errors; } + bool hasErrors() const { return !m_errors.empty(); } + +private: + bool m_enabled; + bool m_needRestart; + + std::vector<InstallTicket> m_installs; + std::vector<InstallTicket> m_updates; + std::set<Path> m_removals; + std::vector<Error> m_errors; +}; + +#endif diff --git a/src/report.cpp b/src/report.cpp @@ -79,27 +79,27 @@ void ReportDialog::printIndented(const string &text) m_stream << "\x20\x20" << line.substr(line.find_first_not_of('\x20')) << NL; } -Report::Report(Transaction *transaction) - : ReportDialog(), m_transaction(transaction) +Report::Report(const Receipt *receipt) + : ReportDialog(), m_receipt(receipt) { } void Report::fillReport() { - const size_t newPackages = m_transaction->newPackages().size(); - const size_t updates = m_transaction->updates().size(); - const size_t removals = m_transaction->removals().size(); - const size_t errors = m_transaction->errors().size(); + const size_t installs = m_receipt->installs().size(); + const size_t updates = m_receipt->updates().size(); + const size_t removals = m_receipt->removals().size(); + const size_t errors = m_receipt->errors().size(); stream() - << newPackages << " new packages, " + << installs << " new packages, " << updates << " updates, " << removals << " removed files and " << errors << " errors" << NL ; - if(m_transaction->isRestartNeeded()) { + if(m_receipt->isRestartNeeded()) { stream() << NL << "Notice: One or more native REAPER extensions were installed." << NL @@ -110,7 +110,7 @@ void Report::fillReport() if(errors) printErrors(); - if(newPackages) + if(installs) printNewPackages(); if(updates) @@ -124,19 +124,17 @@ void Report::printNewPackages() { printHeader("New packages"); - for(const Transaction::InstallTicket &entry : m_transaction->newPackages()) { - const Version *ver = entry.first; - stream() << ver->fullName() << NL; - } + for(const InstallTicket &ticket : m_receipt->installs()) + stream() << ticket.version->fullName() << NL; } void Report::printUpdates() { printHeader("Updates"); - for(const Transaction::InstallTicket &entry : m_transaction->updates()) { - const Package *pkg = entry.first->package(); - const auto &queryRes = entry.second; + for(const InstallTicket &ticket : m_receipt->updates()) { + const Package *pkg = ticket.version->package(); + const auto &queryRes = ticket.regQuery; const VersionSet &versions = pkg->versions(); stream() << pkg->fullName() << ':' << NL; @@ -156,7 +154,7 @@ void Report::printErrors() { printHeader("Errors"); - for(const Transaction::Error &err : m_transaction->errors()) { + for(const Receipt::Error &err : m_receipt->errors()) { stream() << err.title << ':' << NL; printIndented(err.message); stream() << "\n"; @@ -167,6 +165,6 @@ void Report::printRemovals() { printHeader("Removed files"); - for(const Path &path : m_transaction->removals()) + for(const Path &path : m_receipt->removals()) stream() << path.join() << NL; } diff --git a/src/report.hpp b/src/report.hpp @@ -22,7 +22,7 @@ #include <sstream> -class Transaction; +class Receipt; class Version; class ReportDialog : public Dialog { @@ -47,7 +47,7 @@ private: class Report : public ReportDialog { public: - Report(Transaction *); + Report(const Receipt *); protected: void fillReport() override; @@ -58,7 +58,7 @@ private: void printErrors(); void printRemovals(); - Transaction *m_transaction; + const Receipt *m_receipt; }; #endif diff --git a/src/transaction.cpp b/src/transaction.cpp @@ -31,7 +31,7 @@ using namespace std; Transaction::Transaction() - : m_isCancelled(false), m_enableReport(false), m_needRestart(false) + : m_isCancelled(false) { m_registry = new Registry(Path::prefixCache(Path::REGISTRY_FILE)); @@ -68,7 +68,7 @@ void Transaction::synchronize(const Remote &remote, const bool isUserAction) { // show the report dialog even if no task are ran if(isUserAction) - m_enableReport = true; + m_receipt.setEnabled(true); fetchIndex(remote, [=] { const RemoteIndex *ri; @@ -179,25 +179,19 @@ void Transaction::installQueued() void Transaction::installTicket(const InstallTicket &ticket) { - const Version *ver = ticket.first; - const Registry::QueryResult &queryRes = ticket.second; - const set<Path> &currentFiles = m_registry->getFiles(queryRes.entry); + const Version *ver = ticket.version; + const set<Path> &currentFiles = m_registry->getFiles(ticket.regQuery.entry); InstallTask *task = new InstallTask(ver, currentFiles, this); task->onCommit([=] { - if(queryRes.status == Registry::UpdateAvailable) - m_updates.push_back(ticket); - else - m_new.push_back(ticket); - - const set<Path> &removedFiles = task->removedFiles(); - m_removals.insert(removedFiles.begin(), removedFiles.end()); + m_receipt.addTicket(ticket); + m_receipt.addRemovals(task->removedFiles()); const Registry::Entry newEntry = m_registry->push(ver); if(newEntry.type == Package::ExtensionType) - m_needRestart = true; + m_receipt.setRestartNeeded(true); registerInHost(true, newEntry); }); @@ -226,7 +220,7 @@ void Transaction::unregisterAll(const Remote &remote) void Transaction::uninstall(const Remote &remote) { inhibit(remote); - remove(RemoteIndex::pathFor(remote.name()).join().c_str()); + FS::remove(RemoteIndex::pathFor(remote.name())); const vector<Registry::Entry> &entries = m_registry->getEntries(remote.name()); @@ -249,11 +243,7 @@ void Transaction::uninstall(const Remote &remote) } RemoveTask *task = new RemoveTask(allFiles, this); - - task->onCommit([=] { - const set<Path> &removedFiles = task->removedFiles(); - m_removals.insert(removedFiles.begin(), removedFiles.end()); - }); + task->onCommit([=] { m_receipt.addRemovals(task->removedFiles()); }); addTask(task); } @@ -294,8 +284,8 @@ void Transaction::finish() void Transaction::addError(const string &message, const string &title) { - m_errors.push_back({message, title}); - m_enableReport = true; + m_receipt.addError({message, title}); + m_receipt.setEnabled(true); } bool Transaction::allFilesExists(const set<Path> &list) const @@ -313,7 +303,7 @@ void Transaction::addTask(Task *task) m_tasks.push_back(task); m_taskQueue.push(task); - m_enableReport = true; + m_receipt.setEnabled(true); } void Transaction::runTasks() @@ -336,7 +326,7 @@ void Transaction::registerInHost(const bool add, const Registry::Entry &entry) void Transaction::registerQueued() { while(!m_regQueue.empty()) { - const HostRegistration &reg = m_regQueue.front(); + const HostTicket &reg = m_regQueue.front(); // don't register in host if the remote got disabled meanwhile if(reg.add && m_remotes.count(reg.entry.remote) == 0) { @@ -356,7 +346,7 @@ void Transaction::registerQueued() } } -void Transaction::registerScript(const HostRegistration &reg) +void Transaction::registerScript(const HostTicket &reg) { enum Section { MainSection = 0, MidiEditorSection = 32060 }; diff --git a/src/transaction.hpp b/src/transaction.hpp @@ -20,34 +20,29 @@ #include "download.hpp" #include "path.hpp" +#include "receipt.hpp" #include "registry.hpp" #include <boost/signals2.hpp> #include <functional> #include <set> -class InstallTask; class Remote; class RemoteIndex; -class RemoteList; -class RemoveTask; class Task; +struct InstallTicket { + const Version *version; + const Registry::QueryResult regQuery; +}; + +struct HostTicket { bool add; Registry::Entry entry; std::string file; }; + class Transaction { public: typedef boost::signals2::signal<void ()> Signal; typedef Signal::slot_type Callback; - typedef std::pair<const Version *, const Registry::QueryResult> InstallTicket; - typedef std::vector<InstallTicket> InstallTicketList; - - struct Error { - std::string message; - std::string title; - }; - - typedef std::vector<Error> ErrorList; - Transaction(); ~Transaction(); @@ -61,57 +56,46 @@ public: void runTasks(); bool isCancelled() const { return m_isCancelled; } - bool isReportEnabled() const { return m_enableReport && !m_isCancelled; } - bool isRestartNeeded() const { return m_needRestart; } - DownloadQueue *downloadQueue() { return &m_downloadQueue; } + const Receipt *receipt() const { return &m_receipt; } size_t taskCount() const { return m_tasks.size(); } - const InstallTicketList &newPackages() const { return m_new; } - const InstallTicketList &updates() const { return m_updates; } - const std::set<Path> &removals() const { return m_removals; } - const ErrorList &errors() const { return m_errors; } + DownloadQueue *downloadQueue() { return &m_downloadQueue; } bool saveFile(Download *, const Path &); void addError(const std::string &msg, const std::string &title); private: - struct HostRegistration { bool add; Registry::Entry entry; std::string file; }; - typedef std::function<void ()> IndexCallback; void fetchIndex(const Remote &, const IndexCallback &cb); + void saveIndex(Download *, const std::string &remoteName); + + void upgrade(const Package *pkg); void installQueued(); void installTicket(const InstallTicket &); - void finish(); + void addTask(Task *); - void saveIndex(Download *, const std::string &remoteName); - void upgrade(const Package *pkg); bool allFilesExists(const std::set<Path> &) const; - void addTask(Task *); void registerInHost(bool add, const Registry::Entry &); void registerQueued(); - void registerScript(const HostRegistration &); + void registerScript(const HostTicket &); + + void finish(); void inhibit(const Remote &); bool m_isCancelled; - bool m_enableReport; - bool m_needRestart; Registry *m_registry; + Receipt m_receipt; std::multimap<std::string, IndexCallback> m_remotes; std::vector<const RemoteIndex *> m_remoteIndexes; std::vector<Task *> m_tasks; DownloadQueue m_downloadQueue; - std::queue<InstallTicket> m_installQueue; std::queue<Task *> m_taskQueue; - std::queue<HostRegistration> m_regQueue; - - InstallTicketList m_new; - InstallTicketList m_updates; - std::set<Path> m_removals; - ErrorList m_errors; + std::queue<InstallTicket> m_installQueue; + std::queue<HostTicket> m_regQueue; Signal m_onFinish; Signal m_onDestroy;