Skip to content

Commit

Permalink
Supply FS getPath to CommandLine class
Browse files Browse the repository at this point in the history
And create install tree on buildir, now we don't need git metadata for loading resources
  • Loading branch information
Royna2544 committed Nov 19, 2024
1 parent dc92d17 commit 0698dd4
Show file tree
Hide file tree
Showing 39 changed files with 332 additions and 288 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,10 @@ add_my_executable(
)
endif()
#####################################################################

install(DIRECTORY resources/ DESTINATION resources FILES_MATCHING PATTERN "*"
# Resource dirs
install(DIRECTORY resources/ DESTINATION share/${PROJECT_NAME} FILES_MATCHING PATTERN "*"
PATTERN "*.in" EXCLUDE)
file(COPY resources/ DESTINATION ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME})

# Set CPack configurations
set(CPACK_PACKAGE_NAME "TgBot++")
Expand Down
1 change: 0 additions & 1 deletion resources/test.txt

This file was deleted.

4 changes: 3 additions & 1 deletion src/api/TgBotApiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <string_view>
#include <utility>

#include "CommandLine.hpp"

bool TgBotApiImpl::validateValidArgs(const DynModule* module,
MessageExt::Ptr& message) {
if (!module->valid_args.enabled) {
Expand Down Expand Up @@ -525,7 +527,7 @@ TgBotApiImpl::TgBotApiImpl(const std::string_view token, AuthContext* auth,
std::make_unique<TgBotApiImpl::OnMyChatMemberImpl>(this);
// Load modules (../lib/modules)
kModuleLoader = std::make_unique<ModulesManagement>(
this, providers->cmdline->exe().parent_path().parent_path() / "lib" / "modules");
this, providers->cmdline->getPath(FS::PathType::CMD_MODULES));
// Restart command
restartCommand = std::make_unique<RestartCommand>(this);

Expand Down
3 changes: 3 additions & 0 deletions src/command_modules/android_builder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ add_my_executable(
OPTIONAL
)

install(DIRECTORY configs/ DESTINATION share/${PROJECT_NAME}/android_builder)
file(COPY configs/ DESTINATION ${CMAKE_BINARY_DIR}/share/${PROJECT_NAME}/android_builder)

endif()
2 changes: 2 additions & 0 deletions src/command_modules/android_builder/ConfigParsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ ConfigParser::Parser::Parser(const std::filesystem::path& jsonFileDir) {
ConfigParser::ConfigParser(const std::filesystem::path& jsonFileDir) {
// Load files
LOG(INFO) << "Loading JSON files from directory: " << jsonFileDir;
// Directory iterator exception handling is intentionally not implemented
// As invalid config directory should abort creating the ROMBUILD handler
for (const auto& entry : std::filesystem::directory_iterator(jsonFileDir)) {
if (entry.is_regular_file() && entry.path().extension() == ".json") {
LOG(INFO) << "Parsing JSON file: " << entry.path().filename();
Expand Down
20 changes: 12 additions & 8 deletions src/command_modules/android_builder/tasks/RepoSyncTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <trivial_helpers/raii.hpp>
#include <utility>

#include "CommandLine.hpp"
#include "ForkAndRun.hpp"
#include "RepoUtils.hpp"

Expand Down Expand Up @@ -116,16 +117,15 @@ DeferredExit RepoSyncTask::runFunction() {
<< repoDirExists;
}

auto gitAskPass =
FS::getPath(FS::PathType::RESOURCES_SCRIPTS) / kGitAskPassFile;
const auto& rom = getValue(data.localManifest->rom);
GitBranchSwitcher switcher{.gitDirectory = kLocalManifestGitPath,
.desiredBranch = rom->branch,
.desiredUrl = rom->romInfo->url,
.checkout = false};
if (!repoDirExists || !switcher()) {
ForkAndRunSimple shell(fmt::format(initCommand, rom->romInfo->url, rom->branch, 1));
shell.env[kGitAskPassEnv] = gitAskPass.string();
ForkAndRunSimple shell(
fmt::format(initCommand, rom->romInfo->url, rom->branch, 1));
shell.env[kGitAskPassEnv] = _gitAskPassFile.string();
auto ret = shell.execute();
LOG(INFO) << "Repo init result: " << ret;
if (!ret) {
Expand All @@ -138,9 +138,9 @@ DeferredExit RepoSyncTask::runFunction() {
return DeferredExit::generic_fail;
}
unsigned int job_count = std::thread::hardware_concurrency() / 2;
auto sync = [&gitAskPass](unsigned int jobCount) {
auto sync = [this](unsigned int jobCount) {
ForkAndRunSimple shell(fmt::format(syncCommand, jobCount));
shell.env[kGitAskPassEnv] = gitAskPass.string();
shell.env[kGitAskPassEnv] = _gitAskPassFile.string();
auto ret = shell.execute();
LOG(INFO) << "Repo sync result: " << ret;
return ret;
Expand Down Expand Up @@ -208,5 +208,9 @@ void RepoSyncTask::onSignal(int signalCode) {
}

RepoSyncTask::RepoSyncTask(TgBotApi::CPtr api, Message::Ptr message,
PerBuildData data)
: data(std::move(data)), api(api), message(std::move(message)) {}
PerBuildData data,
std::filesystem::path gitAskPassFile)
: data(std::move(data)),
api(api),
message(std::move(message)),
_gitAskPassFile(std::move(gitAskPassFile)) {}
4 changes: 3 additions & 1 deletion src/command_modules/android_builder/tasks/RepoSyncTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ struct RepoSyncTask : ForkAndRun {
* @param data Per-build configuration and path data.
*/
explicit RepoSyncTask(TgBotApi::CPtr api, Message::Ptr message,
PerBuildData data);
PerBuildData data,
std::filesystem::path _gitAskPassFile);

private:
PerBuildData data;
Expand All @@ -129,5 +130,6 @@ struct RepoSyncTask : ForkAndRun {
Message::Ptr message;
std::chrono::system_clock::time_point clock =
std::chrono::system_clock::now();
std::filesystem::path _gitAskPassFile;
bool runWithReducedJobs = false;
};
21 changes: 11 additions & 10 deletions src/command_modules/android_builder/tasks/UploadFileTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <mutex>
#include <regex>
#include <system_error>
#include <utility>
#include <vector>

#include "ConfigParsers.hpp"
Expand All @@ -33,9 +34,9 @@ DeferredExit UploadFileTask::runFunction() {
LOG(ERROR) << "No artifact matcher found";
return DeferredExit::generic_fail;
}
const auto dir =
std::filesystem::path() / "out" / "target" / "product" / data.device->codename;
for (const auto &it : std::filesystem::directory_iterator(dir)) {
const auto dir = std::filesystem::path() / "out" / "target" / "product" /
data.device->codename;
for (const auto& it : std::filesystem::directory_iterator(dir)) {
if (it.is_regular_file()) {
auto file = it.path();
if ((*matcher)(file.filename().string())) {
Expand All @@ -49,7 +50,7 @@ DeferredExit UploadFileTask::runFunction() {
LOG(ERROR) << "Artifact file not found";

// Iterate over and print debug info.
for (const auto &it : std::filesystem::directory_iterator(dir)) {
for (const auto& it : std::filesystem::directory_iterator(dir)) {
if (it.is_regular_file()) {
(*matcher)(it.path().filename().string(), true);
} else {
Expand All @@ -59,16 +60,14 @@ DeferredExit UploadFileTask::runFunction() {
return DeferredExit::generic_fail;
}
std::error_code ec;
const auto scripts =
FS::getPath(FS::PathType::RESOURCES) / "scripts";
std::filesystem::path scriptFile;
if (std::filesystem::exists(scripts / "upload.bash", ec)) {
if (std::filesystem::exists(_scriptDirectory / "upload.bash", ec)) {
LOG(INFO) << "Using upload.bash file";
scriptFile = scripts / "upload.bash";
scriptFile = _scriptDirectory / "upload.bash";
} else {
// Else, use default upload script.
LOG(INFO) << "Using default upload script";
scriptFile = scripts / "upload.default.bash";
scriptFile = _scriptDirectory / "upload.default.bash";
}

// Run the upload script.
Expand Down Expand Up @@ -137,7 +136,9 @@ void UploadFileTask::onExit(int exitCode) {
}
}

UploadFileTask::UploadFileTask(PerBuildData data) : data(std::move(data)) {
UploadFileTask::UploadFileTask(PerBuildData data,
std::filesystem::path scriptDirectory)
: data(std::move(data)), _scriptDirectory(std::move(scriptDirectory)) {
smem = std::make_unique<AllocatedShmem>(kShmemUpload,
sizeof(PerBuildData::ResultData));
}
Expand Down
10 changes: 7 additions & 3 deletions src/command_modules/android_builder/tasks/UploadFileTask.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <absl/log/log.h>

#include <cstdlib>
#include <ConfigParsers.hpp>
#include <cstdlib>

#include "Shmem.hpp"
#include "ForkAndRun.hpp"
#include "Shmem.hpp"

struct UploadFileTask : ForkAndRun {
static constexpr std::string_view kShmemUpload = "shmem_upload";
Expand Down Expand Up @@ -40,13 +40,17 @@ struct UploadFileTask : ForkAndRun {
*
* @param data The data object containing the necessary configuration and
* paths.
* @param scriptsDirectory The directory containing the script files.
*/
explicit UploadFileTask(PerBuildData data);
explicit UploadFileTask(PerBuildData data,
std::filesystem::path scriptsDirectory);

~UploadFileTask() override;

private:
PerBuildData data;
std::unique_ptr<AllocatedShmem> smem;
std::string outputString;
std::mutex stdout_mutex;
std::filesystem::path _scriptDirectory;
};
67 changes: 39 additions & 28 deletions src/command_modules/romBuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <tasks/UploadFileTask.hpp>
#include <utility>

#include "CommandLine.hpp"
#include "ConfigManager.hpp"
#include "ForkAndRun.hpp"
#include "Shmem.hpp"
Expand Down Expand Up @@ -112,6 +113,7 @@ class ROMBuildQueryHandler {
Message::Ptr sentMessage;
Message::Ptr _userMessage;
TgBotApi::Ptr _api;
CommandLine* _commandLine;
using KeyboardType = TgBot::InlineKeyboardMarkup::Ptr;

struct {
Expand Down Expand Up @@ -142,7 +144,8 @@ class ROMBuildQueryHandler {

public:
bool pinned() const { return didpin; }
ROMBuildQueryHandler(TgBotApi::Ptr api, Message::Ptr userMessage);
ROMBuildQueryHandler(TgBotApi::Ptr api, Message::Ptr userMessage,
CommandLine* line);

void updateSentMessage(Message::Ptr message);
void start(Message::Ptr userMessage);
Expand Down Expand Up @@ -336,24 +339,34 @@ class TaskWrapperBase {
*/
virtual void onExecuteFinished(PerBuildData::ResultData result) {}

bool execute()
requires canCreateWithApi<Impl>
{
void preexecute() {
if (sentMessage && queryHandler->pinned()) {
api->unpinMessage(sentMessage);
}
sentMessage = onPreExecute();
if (queryHandler->pinned()) {
api->pinMessage(sentMessage);
}
Impl impl(api, sentMessage, data);
}
bool execute(std::filesystem::path gitAskPassFile)
requires std::is_same_v<Impl, RepoSyncTask>
{
preexecute();
RepoSyncTask impl(api, sentMessage, data, std::move(gitAskPassFile));
return executeCommon(std::move(impl));
}
bool execute()
requires canCreateWithData<Impl>
requires std::is_same_v<Impl, ROMBuildTask>
{
sentMessage = onPreExecute();
if (queryHandler->pinned()) {
api->pinMessage(sentMessage);
}
Impl impl(data);
preexecute();
ROMBuildTask impl(api, sentMessage, data);
return executeCommon(std::move(impl));
}
bool execute(std::filesystem::path scriptDirectory)
requires std::is_same_v<Impl, UploadFileTask>
{
preexecute();
UploadFileTask impl(data, std::move(scriptDirectory));
return executeCommon(std::move(impl));
}
};
Expand Down Expand Up @@ -500,10 +513,10 @@ class CwdRestorer {
};

ROMBuildQueryHandler::ROMBuildQueryHandler(TgBotApi::Ptr api,
Message::Ptr userMessage)
Message::Ptr userMessage,
CommandLine* line)
: _api(api),
parser(FS::getPath(FS::PathType::GIT_ROOT) / "src" / "command_modules" /
"android_builder" / "configs") {
parser(line->getPath(FS::PathType::RESOURCES) / "android_builder") {
settingsKeyboard =
createKeyboardWith<Buttons::repo_sync, Buttons::upload,
Buttons::pin_message, Buttons::back>();
Expand Down Expand Up @@ -606,21 +619,18 @@ void ROMBuildQueryHandler::handle_confirm(const Query& query) {
if (didpin) {
_api->unpinMessage(sentMessage);
}

std::error_code ec;
CwdRestorer cwd(std::filesystem::current_path(ec) / kBuildDirectory);

if (ec) {
_api->editMessage(sentMessage, "Failed to determine cwd directory");
return;
}
auto scriptDirectory =
_commandLine->getPath(FS::PathType::RESOURCES_SCRIPTS);
auto gitAskFile = scriptDirectory / RepoSyncTask::kGitAskPassFile;
CwdRestorer cwd(_commandLine->getPath(FS::PathType::INSTALL_ROOT) /
kBuildDirectory);
if (!cwd) {
_api->editMessage(sentMessage, "Failed to push cwd");
return;
}
if (do_repo_sync) {
RepoSync repoSync(this, per_build, _api, _userMessage);
if (!repoSync.execute()) {
if (!repoSync.execute(std::move(gitAskFile))) {
LOG(INFO) << "RepoSync::execute fails...";
return;
}
Expand All @@ -632,7 +642,7 @@ void ROMBuildQueryHandler::handle_confirm(const Query& query) {
}
if (do_upload) {
Upload upload(this, per_build, _api, _userMessage);
if (!upload.execute()) {
if (!upload.execute(std::move(scriptDirectory))) {
LOG(INFO) << "Upload::execute fails...";
return;
}
Expand Down Expand Up @@ -745,17 +755,18 @@ DECLARE_COMMAND_HANDLER(rombuild) {
}

try {
handler =
std::make_shared<ROMBuildQueryHandler>(api, message->message());
handler = std::make_shared<ROMBuildQueryHandler>(
api, message->message(), provider->cmdline.get());
} catch (const std::exception& e) {
LOG(ERROR) << "Failed to create ROMBuildQueryHandler: " << e.what();
api->sendMessage(message->get<MessageAttrs::Chat>(),
"Failed to initialize ROM build: "s + e.what());
return;
}

auto gitAskPass = FS::getPath(FS::PathType::RESOURCES_SCRIPTS) /
RepoSyncTask::kGitAskPassFile;
auto gitAskPass =
provider->cmdline->getPath(FS::PathType::RESOURCES_SCRIPTS) /
RepoSyncTask::kGitAskPassFile;
if (auto token =
provider->config->get(ConfigManager::Configs::GITHUB_TOKEN)) {
LOG(INFO) << "Create and write git-askpass file";
Expand Down
Loading

0 comments on commit 0698dd4

Please sign in to comment.