Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #45 from AntelopeIO/jobs-number-fix
Browse files Browse the repository at this point in the history
default --jobs parameter to zero
  • Loading branch information
dimas1185 authored Apr 28, 2023
2 parents 38f2fff + abf390d commit f9cf2b0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 31 deletions.
32 changes: 30 additions & 2 deletions include/antler/project/location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,55 @@ namespace antler::project::location {
/// @param l Location to evaluate.
/// @return true if l looks like a github repository.
[[nodiscard]] bool is_github_repo(std::string_view l);

/// @param l Location to evaluate.
/// @return true if l looks like an archive from github.
[[nodiscard]] bool is_github_archive(std::string_view l);

/// @param l Location to evaluate.
/// @return true if l looks like a local file.
[[nodiscard]] bool is_local(std::string_view l);

/// @note requires `gh` is installed and user is authenticated or the repo is public.
/// @param l Location to evaluate.
/// @return true if l is a repo, calls `gh` to test.
[[nodiscard]] bool is_github_shorthand(std::string_view l);

/// @param l Location to evaluate.
/// @return true if l is reachable.
[[nodiscard]] bool is_reachable(std::string_view l);

/// @param l Location to evaluate.
/// @return true if l looks like a url.
[[nodiscard]] bool is_url(std::string_view l);

[[nodiscard]] bool clone_github_repo(const std::string& org, const std::string& repo, const std::string& branch, uint32_t jobs, system::fs::path dest = ".");
[[nodiscard]] bool clone_git_repo(const std::string& url, const std::string& branch, uint32_t jobs, system::fs::path dest = ".");
/// @param org github organization name
/// @param repo git repository name
/// @param branch git branch name
/// @param dest directory to clone to
/// @return true if successful
[[nodiscard]] bool clone_github_repo(const std::string& org, const std::string& repo, const std::string& branch, system::fs::path dest = ".");

/// @param url git repository url
/// @param branch git branch name
/// @param dest directory to clone to
/// @return true if successful
[[nodiscard]] bool clone_git_repo(const std::string& url, const std::string& branch, system::fs::path dest = ".");

/// @note perform git pull for specific directory
/// @param src working directory
/// @return true if successful
[[nodiscard]] bool pull_git_repo(system::fs::path src);

/// @note fetches repos json using github API
/// @param org github organization
/// @param repo git repository name
/// @return json string
[[nodiscard]] std::string github_request(const std::string& org, const std::string& repo);

/// @param org github organization name
/// @param repo github repository name
/// @return branch name
[[nodiscard]] std::string get_github_default_branch(const std::string& org, const std::string& repo);

} // namespace antler::project::location
8 changes: 4 additions & 4 deletions include/antler/project/net_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ namespace antler::project {
/// @param org
/// @param repo
/// @param branch
static bool clone(const std::string& org, const std::string& repo, const std::string& branch, uint32_t jobs, const system::fs::path& dest) {
int32_t ret = system::execute(std::string(executable), { "clone", "-j", std::to_string(jobs), "https://github.com/"+org+"/"+repo, "--depth", "1", "--branch", branch, dest.string() });
static bool clone(const std::string& org, const std::string& repo, const std::string& branch, const system::fs::path& dest) {
int32_t ret = system::execute(std::string(executable), { "clone", "https://github.com/"+org+"/"+repo, "--depth", "1", "--branch", branch, dest.string() });
system::debug_log("clone for {0}/{1} returned {2}\n", org, repo, ret);
if (ret != 0)
return false;
Expand All @@ -187,8 +187,8 @@ namespace antler::project {
/// @brief clone a repo from git
/// @param url
/// @param branch
static bool clone(const std::string& url, const std::string& branch, uint32_t jobs, const system::fs::path& dest) {
int32_t ret = system::execute(std::string(executable), { "clone", "-j", std::to_string(jobs), url, "--depth", "1", "--branch", branch, dest.string() });
static bool clone(const std::string& url, const std::string& branch, const system::fs::path& dest) {
int32_t ret = system::execute(std::string(executable), { "clone", url, "--depth", "1", "--branch", branch, dest.string() });
system::debug_log("clone for {0} returned {1}\n", url,ret);
if (ret != 0)
return false;
Expand Down
9 changes: 3 additions & 6 deletions include/antler/project/populator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ namespace antler::project {
populator(populator&&) = default;

/// Populate the directory by generating files for build and gathering the dependencies.
/// @param jobs The number of jobs used to clone or pull dependencies.
/// @return true for success; false for failure.
[[nodiscard]] bool populate(uint32_t jobs=1);
[[nodiscard]] bool populate();

template <typename P>
void emit_cmake(P& pops) { emitter.emit(pops); }
Expand All @@ -32,15 +31,13 @@ namespace antler::project {

/// Populate a given project.
/// @param proj The project to populate from.
/// @param jobs The number of jobs used to clone or pull dependencies.
/// @return true for success; false for failure.
[[nodiscard]] bool populate_project(project& proj, uint32_t jobs=1);
[[nodiscard]] bool populate_project(project& proj);

/// Populate a particular dependency.
/// @param dep The dependency to populate.
/// @param jobs The number of jobs used to clone or pull the dependency.
/// @return true for success; false for failure.
[[nodiscard]] bool populate_dependency(const dependency& dep, const project& proj, uint32_t jobs=1);
[[nodiscard]] bool populate_dependency(const dependency& dep, const project& proj);


cmake emitter;
Expand Down
8 changes: 4 additions & 4 deletions src/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ bool is_reachable(std::string_view l) {
}
}

bool clone_github_repo(const std::string& org, const std::string& repo, const std::string& branch, uint32_t jobs, system::fs::path dest) {
return git::clone(org, repo, branch, jobs, dest);
bool clone_github_repo(const std::string& org, const std::string& repo, const std::string& branch, system::fs::path dest) {
return git::clone(org, repo, branch, dest);
}

bool clone_git_repo(const std::string& url, const std::string& branch, uint32_t jobs, system::fs::path dest) {
return git::clone(url, branch, jobs, dest);
bool clone_git_repo(const std::string& url, const std::string& branch, system::fs::path dest) {
return git::clone(url, branch, dest);
}

bool pull_git_repo(system::fs::path src) { return git::pull(src); }
Expand Down
18 changes: 9 additions & 9 deletions src/populator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace antler::project {

bool populator::populate_dependency(const dependency& d, const project& p, uint32_t jobs) {
bool populator::populate_dependency(const dependency& d, const project& p) {
using namespace antler::project::location;
system::fs::path depends_dir = p.path() / p.dependencies_dir;
system::debug_log("populating dependency {0} from {1}", d.name(), d.location());
Expand All @@ -29,7 +29,7 @@ bool populator::populate_dependency(const dependency& d, const project& p, uint3
if (system::fs::exists(depends_dir / repo))
return pull_git_repo(depends_dir / repo);
else
return clone_github_repo(org, repo, tag, jobs, depends_dir / repo);
return clone_github_repo(org, repo, tag, depends_dir / repo);
} else {
system::error_log("Dependency {0} is not a github shorthand.", d.name());
return false;
Expand All @@ -41,7 +41,7 @@ bool populator::populate_dependency(const dependency& d, const project& p, uint3
// if (d.tag().empty()) {
// tag = "main";
// }
// return clone_git_repo(d.location(), tag, jobs, depends_dir / d.name());
// return clone_git_repo(d.location(), tag, depends_dir / d.name());
// } else if (is_github_shorthand(d.location())) {
// std::string org = std::string{github::get_org(d.location())};
// std::string repo = std::string{github::get_repo(d.location())};
Expand All @@ -50,13 +50,13 @@ bool populator::populate_dependency(const dependency& d, const project& p, uint3
// tag = get_github_default_branch(org, repo);
// }
// system::debug_log("Cloning {0} with branch {1}", d.location(), tag);
// return clone_github_repo(org, repo, tag, jobs, depends_dir / d.name());
// return clone_github_repo(org, repo, tag, depends_dir / d.name());
// }
//}
//return false;
}

bool populator::populate_project(project& p, uint32_t jobs) {
bool populator::populate_project(project& p) {
system::fs::path depends_dir = p.path() / p.dependencies_dir;

system::fs::create_directories(depends_dir);
Expand All @@ -69,7 +69,7 @@ bool populator::populate_project(project& p, uint32_t jobs) {
const auto& pop_deps = [&](auto& objs) {
for (const auto& [_, o] : objs) {
for (const auto& [_, d] : o.dependencies()) {
if (!populate_dependency(d, p, jobs))
if (!populate_dependency(d, p))
return false;
try {
if (d.location().empty())
Expand All @@ -86,7 +86,7 @@ bool populator::populate_project(project& p, uint32_t jobs) {
}

populators::add_mapping(d, std::string(next_proj.name()));
if (!populate_project(next_proj, jobs))
if (!populate_project(next_proj))
return false;
}
} catch(...) {
Expand All @@ -112,9 +112,9 @@ bool populator::populate_project(project& p, uint32_t jobs) {
}


bool populator::populate(uint32_t jobs) {
bool populator::populate() {
populators::add_mapping("", std::string(proj->name())); // add the default empty location to this project mapping
return populate_project(*proj, jobs);
return populate_project(*proj);
}

} // namespace antler::project
4 changes: 2 additions & 2 deletions tests/location_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ using namespace antler::project;
TEST_CASE("Testing location clone") {
antler::system::fs::remove_all("./clone_test");

CHECK(location::clone_github_repo("antelopeio", "antler-proj", "main", 10, "./clone_test/foo2"));
CHECK(location::clone_github_repo("antelopeio", "antler-proj", "main", "./clone_test/foo2"));
CHECK(antler::system::fs::exists("./clone_test/foo2/.git"));

CHECK(location::clone_git_repo("https://github.com/larryk85/cturtle", "main", 10, "./clone_test/foo3"));
CHECK(location::clone_git_repo("https://github.com/larryk85/cturtle", "main", "./clone_test/foo3"));
CHECK(antler::system::fs::exists("./clone_test/foo3/.git"));
}

Expand Down
13 changes: 9 additions & 4 deletions tools/build.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace antler {
subcommand->footer(std::string(R"(Examples:)")
+ "\n\t" + app.get_name() +R"( build -j3)");
subcommand->add_option("-p, path", path, "This is the path to the root of the project.");
subcommand->add_option("-j, --jobs", jobs, "The number of submodules fetched at the same time.")->default_val(1);
subcommand->add_option("-j, --jobs", jobs, "The number of jobs to use with cmake build tool. Default is number of CPUs.");
subcommand->add_flag("-c, --clean", clean, "This will force a clean build.")->default_val(false);
}

Expand All @@ -39,7 +39,11 @@ namespace antler {
system::fs::create_directory(bin_dir);
system::info_log("Building project...");

return system::execute("cmake", {"--build", bin_dir.string(), "-j", std::to_string(jobs)});
CLI::results_t args = {"--build", bin_dir.string(), "-j"};
if (jobs)
args.push_back(std::to_string(jobs));

return system::execute("cmake", std::move(args));
}

void move_artifacts(const project::project& proj) noexcept {
Expand All @@ -66,12 +70,13 @@ namespace antler {
}

int32_t exec() {

auto proj = load_project(path);

bool repopulated = false;
if (should_repopulate(proj)) {
repopulated = true;
ANTLER_CHECK(project::populators::get(proj).populate(jobs), "failed to populate dependencies");
ANTLER_CHECK(project::populators::get(proj).populate(), "failed to populate dependencies");

}

Expand All @@ -97,7 +102,7 @@ namespace antler {

CLI::App* subcommand = nullptr;
std::string path;
uint32_t jobs = 1;
uint32_t jobs = 0;
bool clean = false;
};
} // namespace antler

0 comments on commit f9cf2b0

Please sign in to comment.