Skip to content

Commit

Permalink
downloading script
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Jan 17, 2024
1 parent c021a0d commit 84c45c0
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 3 deletions.
9 changes: 9 additions & 0 deletions include/Frate/Command/Actions/Template.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <Frate/Interface.hpp>

namespace Frate::Command::Templates{
bool options(std::shared_ptr<Interface> inter);
bool run(std::shared_ptr<Interface> inter);
std::vector<Handler> handlers(std::shared_ptr<Interface> inter);
}


28 changes: 28 additions & 0 deletions include/Frate/Template/InstalledTemplate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Frate/System/GitCommit.hpp"
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

namespace Frate::Project {
class InstalledTemplate {
private:
// This will be the latest commit hash, if getLatestCommit() is called more
// than once, it will return the cached value
System::GitCommit latest;

public:
InstalledTemplate() = default;
std::string name;
// Git url to template
std::string git;
// The latest commit hash
std::vector<System::GitCommit> commits;

// Returns the latest commit hash
System::GitCommit &getLatestCommit();
friend void to_json(nlohmann::json &j, const InstalledTemplate &t);
friend void from_json(const nlohmann::json &j, InstalledTemplate &t);
friend std::ostream &operator<<(std::ostream &os_stream,
const InstalledTemplate &temp);
};
} // namespace Frate::Project
6 changes: 6 additions & 0 deletions include/Frate/Template/New.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include <Frate/Interface.hpp>

namespace Frate::Template{
bool newTemplate(std::shared_ptr<Command::Interface> inter);
}
44 changes: 44 additions & 0 deletions include/Frate/Template/Template.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include "Frate/Dependency.hpp"
#include <Frate/Mode.hpp>
#include <Frate/ProjectPrompt.hpp>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_map>
#include <vector>

namespace Frate::Command {
// class Mode;
// class Package;
// class ProjectPrompt;
class Project;
using nlohmann::json;

class Template {
public:
Template();
std::string name;
std::string description;
std::string version;
std::string src_dir{"src"};
std::string include_dir{"include"};
std::string build_dir{"build"};
std::vector<Dependency> dependencies{};
std::string default_mode{"Release"};
std::vector<Mode> modes{};
std::vector<std::string> flags{};
std::vector<std::string> system_libs{};
std::vector<std::string> supported_toolchains{};
std::vector<std::string> supported_languages{};
std::vector<std::string> supported_language_versions{};
std::string min_cmake_version{"3.10"};
std::vector<std::string> supported_compilers{};
std::vector<std::string> keywords{};
std::unordered_map<std::string, ProjectPrompt> prompts{};
std::unordered_map<std::string, json> global{};
friend void from_json(const json &json_obj, Template &temp);
friend void to_json(json &json_obj, const Template &temp);
friend void to_project(Project &pro, const Template &temp);
};

} // namespace Frate::Command
75 changes: 75 additions & 0 deletions include/Frate/Template/TemplateManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "Frate/Template/TemplateMeta.hpp"
#include <Frate/Template/InstalledTemplate.hpp>
#include <variant>
#include <vector>

namespace Frate {
class TemplateManager {
private:
std::vector<Command::TemplateMeta> index;
std::vector<Project::InstalledTemplate> installed;
bool index_loaded = false;
/*
* Loads the index from the github repo
*/
bool load_index();
/*
* Checks if a template is installed
* @param name the name of the template to check
* @param hash the hash of the template if it is installed
* @return true if the template is installed
*/
Project::InstalledTemplate &find_template(const std::string &name);
bool is_installed(const std::string &name, std::string &hash);

bool template_to_installed_path(std::filesystem::path &tmp_path,
std::filesystem::path &template_path,
std::string &hash);

public:
TemplateManager() = default;
~TemplateManager() = default;
/*
* Installs a template based on the index
*/
Command::TemplateMeta install(const std::string &name,
std::string hash = "");
/*
* Uninstalls a template
* @param name the name of the template to uninstall
*/
bool uninstall(const std::string &name);
/*
* Updates to the latest version of the template
* @param name the name of the template to update
*/
bool update(const std::string &name);
/*
* Creates a tempalte in a tmp path and then returns the path that rendering
* is supposed to happen in
* @param override_path the path to override the template path with
* @param name the name of the template to create
* @param hash the hash of the template to create
*/
std::filesystem::path
makeTemplate(const std::filesystem::path &override_path,
const std::string &name, std::string &hash);
/*
* Grabs the current installed templates from the config file
* @return a vector of TemplateMeta objects
*/
std::vector<Project::InstalledTemplate> &getInstalled();
/*
* Grabs the current index from the the github repo, if called more than
* once then it will pull the index from memory
* @return a vector of TemplateMeta objects
*/
std::vector<Command::TemplateMeta> &getIndex();

friend void from_json(const nlohmann::json &json_obj,
TemplateManager &config);

friend void to_json(nlohmann::json &json_obj,
const TemplateManager &config);
};
} // namespace Frate
19 changes: 19 additions & 0 deletions include/Frate/Template/TemplateMeta.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <nlohmann/json.hpp>
#include <string>

namespace Frate::Command {
class TemplateMeta {
public:
TemplateMeta() = default;
TemplateMeta(const nlohmann::json &json_obj);
std::string name;
std::string description;
std::string hash;
std::string git;
friend void from_json(const nlohmann::json &json_obj, TemplateMeta &temp);
friend void to_json(nlohmann::json &json_obj, const TemplateMeta &temp);
friend std::ostream &operator<<(std::ostream &os_stream,
const TemplateMeta &temp);
};
} // namespace Frate::Command
64 changes: 64 additions & 0 deletions src/Command/Actions/Template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <Frate/Command/Actions/Template.hpp>
#include <Frate/Project/Config.hpp>

namespace Frate::Command::Templates {
bool options(std::shared_ptr<Interface> inter) {
inter->InitHeader();
inter->options->parse_positional({"command", "subcommand"});
inter->options->allow_unrecognised_options().add_options()(
"command", "Command to run",
cxxopts::value<std::string>()->default_value("help"))(
"subcommand", "Subcommand to run",
cxxopts::value<std::string>())("h,help", "Print usage");
inter->options->help();
return inter->parse();
}

std::vector<Handler> handlers(std::shared_ptr<Interface> inter) {
(void)inter;
return {
Handler{
.aliases = {"new", "n"},
.flags = {},
.callback =
[](std::shared_ptr<Interface> inter) {
(void)inter;
return true;
}
},
Handler{
.aliases = {"generate", "g"},
.flags = {},
.callback =
[](std::shared_ptr<Interface> inter) {
(void)inter;
return true;
}
},

};
}

bool run(std::shared_ptr<Interface> inter) {
options(inter);
std::vector<Handler> template_handlers = handlers(inter);
std::string subcommand;
if (inter->args->count("subcommand") > 0) {

subcommand = inter->args->operator[]("subcommand").as<std::string>();
}
else {
Utils::error << "No subcommand given" << std::endl;

inter->getHelpString("template", template_handlers);

return false;
}

inter->pro->load();

return runCommand(inter, subcommand, template_handlers);

return true;
}
} // namespace Frate::Command::Templates
8 changes: 8 additions & 0 deletions src/Command/Template/New.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <Frate/Template/New.hpp>

namespace Frate::Template{
bool newTemplate(std::shared_ptr<Command::Interface> inter) {
(void)inter;
return true;
}
}
8 changes: 5 additions & 3 deletions src/System/Git/Watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace Frate::System {


void UVDirectoryWatcher::add_watcher(const std::string &path) {
Watcher watcher;
watcher.handle = std::make_unique<uv_fs_event_t>();
watcher.path = path;
watcher.handle->data = this;
uv_fs_event_init(loop.get(), watcher.handle.get());
uv_fs_event_start(watcher.handle.get(), fs_event_callback,
watcher.path.c_str(), 0);
watcher.path.c_str(), UV_FS_EVENT_RECURSIVE);
watchers.push_back(std::move(watcher));
};

Expand All @@ -22,7 +23,7 @@ namespace Frate::System {
auto *watcher = static_cast<UVDirectoryWatcher *>(handle->data);
// combining the path and the filename
std::string changed_path =
(std::string)handle->path + "/" + (std::string)filename;
static_cast<std::string>(handle->path) + "/" + static_cast<std::string>(filename);
// For some reason the filename returns a number, so we need to check if the
// path exists
bool path_exists = std::filesystem::exists(changed_path);
Expand Down Expand Up @@ -55,13 +56,14 @@ namespace Frate::System {
std::function<void(std::string)> callback) {
this->callback = callback;
add_watcher(path);

#ifdef linux
for (const auto &entry :
std::filesystem::recursive_directory_iterator(path)) {
if (entry.is_directory()) {
add_watcher(entry.path().string());
}
}
#endif
}

void UVDirectoryWatcher::run() { uv_run(loop.get(), UV_RUN_DEFAULT); };
Expand Down

0 comments on commit 84c45c0

Please sign in to comment.