Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ccrma/chump # Please enter …
Browse files Browse the repository at this point in the history
…a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
  • Loading branch information
nshaheed committed Apr 16, 2024
2 parents bb2822f + c807785 commit e0b7bb8
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 126 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ set(SOURCES
src/exec.h
src/fetch.h
src/fetch.cpp
src/uninstaller.h
src/uninstaller.cpp
src/util.h
src/util.cpp
)

find_package(Curses REQUIRED)
Expand Down
8 changes: 5 additions & 3 deletions data/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"api_version": "10.1",
"homepage": "https://github.com/nshaheed/hydra-chugin",
"repository": "https://github.com/nshaheed/hydra-chugin",
"files": [
"https://ccrma.stanford.edu/~nshaheed/chugins/Hydra/linux/Hydra.chug"
],
"files": {
"linux": [
"https://ccrma.stanford.edu/~nshaheed/chugins/Hydra/linux/Hydra.chug"
]
},
"spec_file": "specA",
"authors": ["Nick Shaheed"],
"license": "tbd",
Expand Down
115 changes: 41 additions & 74 deletions src/exec.h

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <curses.h>

#include "fetch.h"
#include "util.h"

// Callback function to update progress
int progressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) {
Expand Down Expand Up @@ -66,12 +67,14 @@ int progressCallback(void *clientp, double dltotal, double dlnow, double ultotal
- chump info www.package.com/ABSaturator.zip # the path is a url to be downloaded
- chump info pkg-name # search pre-defined directories for the pkg name (or online pkg list)
*/
optional<Package> Fetch::fetch(std::string url, std::string package_name) {
optional<Package> Fetch::fetch(std::string url, Package package) {
if (!isURL(url)) {
std::cerr << "Not a URL!" << std::endl;
return {};
}

std::string package_name = package.name;

// struct progress data;
CURL *curl;
FILE *fp;
Expand Down Expand Up @@ -129,7 +132,7 @@ optional<Package> Fetch::fetch(std::string url, std::string package_name) {
// get home environment variable
const char* env_p = std::getenv("HOME");
fs::path home = fs::path(env_p);
fs::path install_dir = home / ".chuck/lib" / package_name;
fs::path install_dir = packagePath(package);
fs::path install_path = install_dir / filename;

// create dir if needed
Expand Down
4 changes: 2 additions & 2 deletions src/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ namespace fs = std::filesystem;
class FetchBase {
public:
virtual ~FetchBase() = default;
virtual optional<Package> fetch(std::string data, std::string package_name) = 0;
virtual optional<Package> fetch(std::string data, Package package) = 0;
};


class Fetch : public FetchBase {
public:
optional<Package> fetch(std::string data, std::string package_name);
optional<Package> fetch(std::string data, Package package);

public:
bool isJSONFile(std::string path);
Expand Down
17 changes: 15 additions & 2 deletions src/manager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "manager.h"
#include "util.h"

Manager::Manager() {
fetch = new Fetch();
Expand All @@ -9,6 +10,7 @@ Manager::Manager() {
Manager::Manager(std::string package_list_path) {
fetch = new Fetch();
package_list = new PackageList(package_list_path);
uninstaller = new Uninstaller(package_list);
}

optional<Package> Manager::getPackage(string name) {
Expand All @@ -27,9 +29,11 @@ bool Manager::install(std::string packageName) {

auto package = pkg.value();

// TODO actually fetch operating system
std::string os = whichOS();
// fetch
for (auto file: package.files) {
fetch->fetch(file, package.name);
for (auto file: package.files[os]) {
fetch->fetch(file, package);
}

// validate
Expand All @@ -39,4 +43,13 @@ bool Manager::install(std::string packageName) {
return true;
}

bool Manager::uninstall(std::string packageName) {
if(!uninstaller->uninstall(packageName)) {
std::cerr << "Failed to uninstall " << packageName << std::endl;
return false;
} else {
std::cout << "Successfully uninstalled " << packageName << std::endl;
}

return true;
}
3 changes: 3 additions & 0 deletions src/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "package.h"
#include "package_list.h"
#include "fetch.h"
#include "uninstaller.h"

using std::optional;
using std::string;
Expand All @@ -28,6 +29,7 @@ class Manager {
optional<Package> getPackage(string name);

bool install(string packageName);
bool uninstall(string packageName);


public:
Expand All @@ -36,6 +38,7 @@ class Manager {

private:
PackageList* package_list;
UninstallerBase* uninstaller;
};


Expand Down
63 changes: 34 additions & 29 deletions src/package.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,50 @@ bool Package::operator==(const Package& other) const {

// Output stream operator overload
std::ostream& operator<<(std::ostream& os, const Package& pkg) {
os << "Owner: " << pkg.owner << "\n"
<< "Name: " << pkg.name << "\n"
<< "Version: " << pkg.version << "\n"
<< "API Version: " << pkg.api_version << "\n"
<< "Homepage: " << pkg.homepage << "\n"
<< "Repository: " << pkg.repository << "\n"
<< "Spec File: " << pkg.specFile << "\n"
<< "Authors: [";
os << "Owner: " << pkg.owner << "\n"
<< "Name: " << pkg.name << "\n"
<< "Version: " << pkg.version << "\n"
<< "API Version: " << pkg.api_version << "\n"
<< "Homepage: " << pkg.homepage << "\n"
<< "Repository: " << pkg.repository << "\n"
<< "Spec File: " << pkg.specFile << "\n"
<< "Authors: [";

if (!pkg.authors.empty()) {
os << pkg.authors[0];
if (!pkg.authors.empty()) {
os << pkg.authors[0];

for (size_t i = 1; i < pkg.authors.size(); ++i) {
os << ", " << pkg.authors[i];
}
for (size_t i = 1; i < pkg.authors.size(); ++i) {
os << ", " << pkg.authors[i];
}
}

os << "]\nLicense: " << pkg.license << "\n"
<< "Description: " << pkg.description << "\n"
<< "Keywords: [";
os << "]\nLicense: " << pkg.license << "\n"
<< "Description: " << pkg.description << "\n"
<< "Keywords: [";

if (!pkg.keywords.empty()) {
os << pkg.keywords[0];
if (!pkg.keywords.empty()) {
os << pkg.keywords[0];

for (size_t i = 1; i < pkg.keywords.size(); ++i) {
os << ", " << pkg.keywords[i];
}
for (size_t i = 1; i < pkg.keywords.size(); ++i) {
os << ", " << pkg.keywords[i];
}
}

os << "]\nFiles: [";
if (!pkg.files.empty()) {
os << pkg.files[0];
os << "]\nFiles: [";
if (!pkg.files.empty()) {

for (size_t i = 1; i < pkg.files.size(); ++i) {
os << ", " << pkg.files[i];
}
for (const auto& pair : pkg.files) {
os << pair.first << ":" << std::endl;

// os << pkg.files[0];

for (size_t i = 1; i < pair.second.size(); ++i) {
os << ", " << pair.second[i];
}
}
os << "]\n";
return os;
}
os << "]\n";
return os;
}

void to_json(json& j, const Package& p) {
Expand Down
27 changes: 15 additions & 12 deletions src/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
#include <nlohmann/json.hpp>

using json = nlohmann::json;
using std::string;
using std::map;
using std::vector;

// A Package describes the package spec.
struct Package {
std::string owner;
std::string name;
std::string version;
std::string api_version;
std::vector<std::string> files; // download urls
std::string homepage;
std::string repository;
std::string specFile;
std::vector<std::string> authors;
std::string license;
std::string description;
std::vector<std::string> keywords;
string owner;
string name;
string version;
string api_version;
map<string, vector<string>> files; // map of operating system to list of files to download
string homepage;
string repository;
string specFile;
vector<string> authors;
string license;
string description;
vector<string> keywords;

// Equality operator overload
bool operator==(const Package& other) const;
Expand Down
4 changes: 3 additions & 1 deletion src/test/data/test-package-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"api_version": "9.0",
"homepage": "http://example.com",
"repository": "http://repo.com",
"files": ["https://ccrma.stanford.edu/~nshaheed/chugins/Hydra/linux/butt.chug"],
"files": {
"linux": ["https://ccrma.stanford.edu/~nshaheed/chugins/Hydra/linux/butt.chug"]
},
"spec_file": "specA",
"authors": ["AuthorA", "AuthorB"],
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/test/package_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_CASE("Load db file", "[PackageList]") {
std::string path = "./data/test-package-list.json";
PackageList pkglist = PackageList(path);

Package want = {"John", "Butt", "1.0", "9.0", {"https://ccrma.stanford.edu/~nshaheed/chugins/Hydra/linux/butt.chug"}, "http://example.com", "http://repo.com", "specA", {"AuthorA", "AuthorB"}, "MIT", "DescriptionA", {"KeywordA", "KeywordB"}};
Package want = {"John", "Butt", "1.0", "9.0", {{"linux", {"https://ccrma.stanford.edu/~nshaheed/chugins/Hydra/linux/butt.chug"}}}, "http://example.com", "http://repo.com", "specA", {"AuthorA", "AuthorB"}, "MIT", "DescriptionA", {"KeywordA", "KeywordB"}};

optional<Package> got = pkglist.lookup("Butt");

Expand Down
46 changes: 46 additions & 0 deletions src/uninstaller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "uninstaller.h"
#include "util.h"

Uninstaller::Uninstaller(PackageList* pkg_list) {
package_list = pkg_list;
}

bool Uninstaller::uninstall(std::string package_name) {
// lookup package name (default to latest version)
auto pkg = package_list->lookup(package_name);

if (!pkg) {
std::cerr << "BADBADBAD" << std::endl;
return false;
}

auto package = pkg.value();

fs::path package_dir = packagePath(package);

// validate that it's actually a directory
if (!fs::exists(package_dir) || !fs::is_directory(package_dir)) {
std::cerr << "Package " << package.name << " was not found." << std::endl;
return false;
}

std::string os = whichOS();

// only delete files that chump knows about
for (auto file: package.files[os]) {
fs::path filename = fs::path(file).filename();

fs::path filepath = package_dir / filename;

if (!fs::remove(filepath)) {
std::cerr << "File " << filepath << " not found." << std::endl;
}
}

// If the package directory still has files, don't remove it.
if (!fs::remove(package_dir)) {
std::cerr << "Directory " << package_dir << " not removed." << std::endl;
}

return true;
}
30 changes: 30 additions & 0 deletions src/uninstaller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef __UNINSTALLER_H__
#define __UNINSTALLER_H__

#include "package.h"
#include "package_list.h"

using std::optional;

namespace fs = std::filesystem;

// Abstract base class
class UninstallerBase {
public:
virtual ~UninstallerBase() = default;
virtual bool uninstall(std::string package_name) = 0;
};


class Uninstaller : public UninstallerBase {
public:
Uninstaller(PackageList* package_list);

public:
bool uninstall(std::string package_name);

private:
PackageList* package_list;
};

#endif
23 changes: 23 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "util.h"

// Returns the path to the directory where a package will be installed
fs::path packagePath(Package p) {
const char* env_p = std::getenv("HOME");
fs::path home = fs::path(env_p);
fs::path package_dir = home / ".chuck/lib" / p.name;

return package_dir;
}

std::string whichOS() {
#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
return "windows";
#elif defined(__APPLE__)
return "mac";
#elif defined(__linux__)
return "linux";
#else
std::cerr << "Unknown operating system" << std::endl;
return "";
#endif
}
12 changes: 12 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __UTIL_H__
#define __UTIL_H__

#include "package.h"

namespace fs = std::filesystem;

fs::path packagePath(Package p);

std::string whichOS();

#endif

0 comments on commit e0b7bb8

Please sign in to comment.