Skip to content

Commit

Permalink
fixed build command by injecting in project environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Feb 11, 2024
1 parent da3db6e commit f68c438
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 513 deletions.
32 changes: 0 additions & 32 deletions include/Frate/Lua/LuaAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,9 @@ namespace Frate::Lua {

static std::string format(const std::string &str, sol::table in_table,
sol::this_state s);

static void print_table(sol::table in_table);
static std::string fetch_text(const std::string &url);
static sol::table fetch_json(const std::string &url, sol::this_state lua);
};

/*
* Registers the project scripts with the project that is specifed
*/
[[deprecated("Use TemplateEnvironment")]]
bool registerProjectScripts(inja::Environment &env, sol::state &lua,
path script_path,
std::shared_ptr<Project::Config> project);

/*
* Registers the project with the user types for the project struct
*/
[[deprecated("Use TemplateEnvironment")]]
bool registerProject(sol::state &lua, std::shared_ptr<Project::Config> pro);
/*
* Registers api functions for the lua state
* and initializes the lua state
*/
[[deprecated("Use TemplateEnvironment")]]
void registerAPI(sol::state &lua);

/*
* Runs __init__ scripts
*/
[[deprecated("Use TemplateEnvironment")]]
bool initScripts(sol::state &lua, std::shared_ptr<Project::Config> project);

/*
* Runs __post__ scripts
*/
[[deprecated("Use TemplateEnvironment")]]
bool postScripts(sol::state &lua, std::shared_ptr<Project::Config> project);
} // namespace Frate::Lua
57 changes: 35 additions & 22 deletions include/Frate/Project/Local.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once
#include <string>
#include <nlohmann/json.hpp>
#include <Frate/FrateException.hpp>
#include <nlohmann/json.hpp>
#include <string>

namespace Frate::Project {

const std::string LOCAL_FILE_NAME = ".frate-local.json";

class OverrideHashException : public FrateException {
public:
OverrideHashException(const std::string &message) : FrateException(message) {}
OverrideHashException(const std::string &message)
: FrateException(message) {}
};

class LocalIOException : public FrateException {
Expand All @@ -18,61 +19,73 @@ namespace Frate::Project {
};

class Config;

class Local {
private:
std::string build_command{"cmake -build ."};
std::string test_command{"ctest"};
std::string run_command{"./bin/"};
std::string build_command;
std::string test_command;
std::string run_command;
std::string build_mode;
int requested_jobs{1};
int max_jobs{1};
std::string override_change_hash{};
std::shared_ptr<Config> config;
void create_local_file();
std::shared_ptr<Config> project;
bool local_file_loaded = false;
void create_local_file();
void gen_default_build_command();
void gen_default_test_command();
void gen_default_run_command();

public:
Local() = default;

Local(std::shared_ptr<Config> config) : config(config) {}
// Getters
std::string &getBuildCommand() { return build_command; }
Local(std::shared_ptr<Config> config) : project(config) {}

std::string &getTestCommand() { return test_command; }

std::string &getRunCommand() { return run_command; }
// Getters
std::string &getBuildCommand();
std::string &getTestCommand();
std::string &getRunCommand();

std::string &getOverrideChangeHash() { return override_change_hash; }

int &getRequestedJobs() { return requested_jobs; }

int &getMaxJobs() { return max_jobs; }

std::string &getBuildMode() { return build_mode; }

std::string getProjectPath();

// Setters
void setBuildCommand(const std::string &build_command) {
void setBuildCommand(std::string build_command) {
this->build_command = build_command;
}

void setTestCommand(const std::string &test_command) {
void setTestCommand(std::string test_command) {
this->test_command = test_command;
}

void setRunCommand(const std::string &run_command) {
void setRunCommand(std::string run_command) {
this->run_command = run_command;
}

void setRequestedJobs(int requested_jobs) {
this->requested_jobs = requested_jobs;
}

void setBuildMode(std::string build_mode) { this->build_mode = build_mode; }

void generateOverrideChangeHash();

//JSON
// JSON
friend void from_json(const nlohmann::json &json_obj, Local &cache_obj);

friend void to_json(nlohmann::json &json_obj, const Local &cache_obj);

//IO
// IO

void load();

void save();


};
} // namespace Frate::Project
26 changes: 6 additions & 20 deletions src/Command/Actions/Build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace Frate::Command::Build {
}

inter->pro->load();
inter->pro->current_template.refresh(inter->pro, inter->local);


Utils::info << "Building project with: " << std::endl;
Expand All @@ -42,27 +41,14 @@ namespace Frate::Command::Build {
Utils::info << "Jobs: " << jobs << std::endl;
// TODO: Handle different targets

for (Project::Mode &curr_mode : inter->pro->modes) {
if (curr_mode.name == mode) {
std::string workdir_cmd = "cd " + inter->pro->path.string();
Utils::replaceKey(inter->pro->build_command, "\n", ";");
std::string full_build_cmd = inter->pro->build_command;
if (Utils::hSystem(workdir_cmd + ";" + full_build_cmd) != 0) {
Utils::error << "Build failed" << std::endl;
return false;
}
else {
Utils::info << "Build success" << std::endl;
return true;
}
}
}
inter->local->setBuildMode(mode == "" ? inter->pro->default_mode : mode);
inter->local->setRequestedJobs(jobs);

inter->pro->current_template.refresh(inter->pro, inter->local);

std::string workdir_cmd = "cd " + inter->pro->path.string();
Utils::replaceKey(inter->pro->build_command, "\n", ";");
std::string full_build_cmd = workdir_cmd + ";" + inter->pro->build_command;
Utils::verbose << "Build command" << inter->local->getBuildCommand() << std::endl;

if (Utils::hSystem(full_build_cmd) != 0) {
if (Utils::hSystem(inter->local->getBuildCommand()) != 0) {
Utils::error << "Build failed" << std::endl;
return false;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Command/Helpers/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ namespace Frate::Command {
this->argc = argc;
this->argv = argv;
this->pro = std::make_shared<Project::Config>();
this->local = std::make_shared<Project::Local>(this->pro);
#ifdef DEBUG
//Setting the path to ./build so that we can test the build process
std::cout << "DEBUG MODE ENABLED\n";
Expand All @@ -90,6 +89,8 @@ namespace Frate::Command {
config = std::make_shared<Config::ConfigManager>();

config->load();

this->local = std::make_shared<Project::Local>(this->pro);
local->load();


Expand All @@ -104,6 +105,10 @@ namespace Frate::Command {
Utils::error << "Error loading installed templates: " << e.what()
<< std::endl;
}

Utils::verbose << local->getBuildCommand() << std::endl;
Utils::verbose << local->getTestCommand() << std::endl;
Utils::verbose << local->getRunCommand() << std::endl;
}

bool execute(std::shared_ptr<Interface> inter) {
Expand Down
43 changes: 22 additions & 21 deletions src/Generators/DockerTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@

namespace Frate::Generators::DockerTemplate {
bool create(std::shared_ptr<Command::Interface> inter) {
inja::Environment env;
sol::state lua;
Lua::registerAPI(lua);

if (!Lua::registerProject(lua, inter->pro)) {
Utils::debug("Error while registering project");
return false;
}

if (!Lua::registerProjectScripts(
env, lua, inter->pro->path / "templates/scripts", inter->pro)) {
Utils::debug("Error while registering project scripts");
return false;
}
return true;
}

bool remove(std::shared_ptr<Command::Interface> inter) {
(void)inter;
return true;
}
// inja::Environment env;
// sol::state lua;
// Lua::registerAPI(lua);
//
// if (!Lua::registerProject(lua, inter->pro)) {
// Utils::debug("Error while registering project");
// return false;
// }
//
// if (!Lua::registerProjectScripts(
// env, lua, inter->pro->path / "templates/scripts", inter->pro)) {
// Utils::debug("Error while registering project scripts");
// return false;
// }
// return true;
// }
//
// bool remove(std::shared_ptr<Command::Interface> inter) {
// (void)inter;
// return true;
// }
}
} // namespace Frate::Generators::DockerTemplate
Loading

0 comments on commit f68c438

Please sign in to comment.