-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dependency: extract dependency download logic from Manifest
- Loading branch information
1 parent
9fc0647
commit 20fd646
Showing
5 changed files
with
166 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "Dependency.hpp" | ||
|
||
#include "Algos.hpp" | ||
#include "Command.hpp" | ||
#include "Git2.hpp" | ||
#include "Logger.hpp" | ||
|
||
#include <cstdlib> | ||
#include <filesystem> | ||
#include <string> | ||
|
||
namespace cabin { | ||
|
||
namespace fs = std::filesystem; | ||
|
||
static fs::path | ||
getXdgCacheHome() noexcept { | ||
if (const char* envP = std::getenv("XDG_CACHE_HOME")) { | ||
return envP; | ||
} | ||
const fs::path userDir = std::getenv("HOME"); | ||
return userDir / ".cache"; | ||
} | ||
|
||
static const fs::path CACHE_DIR(getXdgCacheHome() / "cabin"); | ||
static const fs::path GIT_DIR(CACHE_DIR / "git"); | ||
static const fs::path GIT_SRC_DIR(GIT_DIR / "src"); | ||
|
||
Result<DepMetadata> | ||
GitDependency::install() const { | ||
fs::path installDir = GIT_SRC_DIR / name; | ||
if (target.has_value()) { | ||
installDir += '-' + target.value(); | ||
} | ||
|
||
if (fs::exists(installDir) && !fs::is_empty(installDir)) { | ||
logger::debug("{} is already installed", name); | ||
} else { | ||
git2::Repository repo; | ||
repo.clone(url, installDir.string()); | ||
|
||
if (target.has_value()) { | ||
// Checkout to target. | ||
const std::string target = this->target.value(); | ||
const git2::Object obj = repo.revparseSingle(target); | ||
repo.setHeadDetached(obj.id()); | ||
repo.checkoutHead(true); | ||
} | ||
|
||
logger::info( | ||
"Downloaded", "{} {}", name, target.has_value() ? target.value() : url | ||
); | ||
} | ||
|
||
const fs::path includeDir = installDir / "include"; | ||
std::string includes = "-isystem"; | ||
|
||
if (fs::exists(includeDir) && fs::is_directory(includeDir) | ||
&& !fs::is_empty(includeDir)) { | ||
includes += includeDir.string(); | ||
} else { | ||
includes += installDir.string(); | ||
} | ||
|
||
// Currently, no libs are supported. | ||
return Ok(DepMetadata{ .includes = includes, .libs = "" }); | ||
} | ||
|
||
Result<DepMetadata> | ||
PathDependency::install() const { | ||
const fs::path installDir = fs::weakly_canonical(path); | ||
if (fs::exists(installDir) && !fs::is_empty(installDir)) { | ||
logger::debug("{} is already installed", name); | ||
} else { | ||
Bail("{} can't be accessible as directory", installDir.string()); | ||
} | ||
|
||
const fs::path includeDir = installDir / "include"; | ||
std::string includes = "-isystem"; | ||
|
||
if (fs::exists(includeDir) && fs::is_directory(includeDir) | ||
&& !fs::is_empty(includeDir)) { | ||
includes += includeDir.string(); | ||
} else { | ||
includes += installDir.string(); | ||
} | ||
|
||
// Currently, no libs are supported. | ||
return Ok(DepMetadata{ .includes = includes, .libs = "" }); | ||
} | ||
|
||
Result<DepMetadata> | ||
SystemDependency::install() const { | ||
const std::string pkgConfigVer = versionReq.toPkgConfigString(name); | ||
const Command cflagsCmd = | ||
Command("pkg-config").addArg("--cflags").addArg(pkgConfigVer); | ||
const Command libsCmd = | ||
Command("pkg-config").addArg("--libs").addArg(pkgConfigVer); | ||
|
||
std::string cflags = Try(getCmdOutput(cflagsCmd)); | ||
cflags.pop_back(); // remove '\n' | ||
std::string libs = Try(getCmdOutput(libsCmd)); | ||
libs.pop_back(); // remove '\n' | ||
|
||
return Ok(DepMetadata{ .includes = cflags, .libs = libs }); | ||
} | ||
|
||
} // namespace cabin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#pragma once | ||
|
||
#include "Rustify/Result.hpp" | ||
#include "VersionReq.hpp" | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <utility> | ||
#include <variant> | ||
|
||
namespace cabin { | ||
|
||
struct DepMetadata { | ||
const std::string includes; // -Isomething | ||
const std::string libs; // -Lsomething -lsomething | ||
}; | ||
|
||
struct GitDependency { | ||
const std::string name; | ||
const std::string url; | ||
const std::optional<std::string> target; | ||
|
||
Result<DepMetadata> install() const; | ||
|
||
GitDependency( | ||
std::string name, std::string url, std::optional<std::string> target | ||
) | ||
: name(std::move(name)), url(std::move(url)), target(std::move(target)) {} | ||
}; | ||
|
||
struct PathDependency { | ||
const std::string name; | ||
const std::string path; | ||
|
||
Result<DepMetadata> install() const; | ||
|
||
PathDependency(std::string name, std::string path) | ||
: name(std::move(name)), path(std::move(path)) {} | ||
}; | ||
|
||
struct SystemDependency { | ||
const std::string name; | ||
const VersionReq versionReq; | ||
|
||
Result<DepMetadata> install() const; | ||
|
||
SystemDependency(std::string name, VersionReq versionReq) | ||
: name(std::move(name)), versionReq(std::move(versionReq)) {}; | ||
}; | ||
|
||
using Dependency = | ||
std::variant<GitDependency, PathDependency, SystemDependency>; | ||
|
||
} // namespace cabin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters