Skip to content

Commit

Permalink
Remove GitData, make CMake provide it
Browse files Browse the repository at this point in the history
  • Loading branch information
Royna2544 committed Dec 9, 2024
1 parent 689d16d commit b6af5b1
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ libTgBot*.so
# Generated
resources/scripts/git-askpass.sh
resources/about.html
src/include/GitBuildInfo.hpp
__pycache__

# Visual studio code
Expand Down
53 changes: 52 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,59 @@ if (NOT MSVC)
endif()

##################### Fill in readme with cmake #####################
string(TIMESTAMP TODAY "%Y-%m-%d")
string(TIMESTAMP TODAY "%Y-%m-%d %H:%M:%S UTC" UTC)

find_package(Git REQUIRED) # It will obviously have
function(git_execute_proc)
cmake_parse_arguments(GIT_PROC
"" # Option
"NAME;VAR" # Single
"COMMAND" # Multiple
${ARGN}
)
if (NOT (GIT_PROC_NAME AND GIT_PROC_VAR AND GIT_PROC_COMMAND))
message(SEND_ERROR "Missing arguments")
endif()

# Run the git command to get the commit ID or other info
execute_process(
COMMAND ${GIT_EXECUTABLE} ${GIT_PROC_COMMAND}
OUTPUT_VARIABLE GIT_PROC_OUT
ERROR_VARIABLE GIT_PROC_ERR
RESULT_VARIABLE GIT_PROC_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Check if the git command was successful
if (GIT_PROC_RESULT EQUAL 0)
set(${GIT_PROC_VAR} ${GIT_PROC_OUT} PARENT_SCOPE)
message(STATUS "Git ${GIT_PROC_NAME}: ${GIT_PROC_OUT}")
else()
set(${GIT_PROC_VAR} "-" PARENT_SCOPE)
message(WARNING "Error retrieving Git ${GIT_PROC_NAME}: ${GIT_PROC_ERR}")
endif()
endfunction()

git_execute_proc(
COMMAND rev-parse HEAD
NAME "commit-id"
VAR GIT_COMMIT_ID
)

git_execute_proc(
COMMAND log -1 --pretty=%B
NAME "commit-message"
VAR GIT_COMMIT_MESSAGE
)

git_execute_proc(
COMMAND remote get-url origin
NAME "origin-url"
VAR GIT_ORIGIN_URL
)

configure_file(resources/about.html.in ${CMAKE_SOURCE_DIR}/resources/about.html)
configure_file(src/include/GitBuildInfo.hpp.inc ${CMAKE_SOURCE_DIR}/src/include/GitBuildInfo.hpp)
#####################################################################

################## Declare common macros ####################
Expand Down
6 changes: 3 additions & 3 deletions resources/about.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Hi! I'm <a href="https://t.me/_botusername_">_botname_</a>
- Host OS: @CMAKE_SYSTEM_NAME@
- C Compiler: @CMAKE_C_COMPILER@ v@CMAKE_C_COMPILER_VERSION@
- CXX Compiler: @CMAKE_CXX_COMPILER@ v@CMAKE_CXX_COMPILER_VERSION@
- Date: @TODAY@</blockquote>
- Build Date: @TODAY@</blockquote>
<blockquote><b>GIT INFO</b>:
- Commit-Id: _commitid_
- Commit-Msg: _commitmsg_</blockquote>
- Commit-Id: @GIT_COMMIT_ID@
- Commit-Msg: @GIT_COMMIT_MESSAGE@</blockquote>
18 changes: 7 additions & 11 deletions src/api/TgBotApiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Authorization.hpp>
#include <CommandLine.hpp>
#include <ConfigManager.hpp>
#include <GitData.hpp>
#include <GitBuildInfo.hpp>
#include <StringResLoader.hpp>
#include <api/CommandModule.hpp>
#include <api/MessageExt.hpp>
Expand Down Expand Up @@ -164,7 +164,8 @@ void TgBotApiImpl::commandHandler(const std::string& command,
}

if (!_rateLimiter.check()) {
LOG(INFO) << fmt::format("Ratelimiting user {}", ext->get<MessageAttrs::User>());
LOG(INFO) << fmt::format("Ratelimiting user {}",
ext->get<MessageAttrs::User>());
return;
}

Expand Down Expand Up @@ -232,14 +233,8 @@ void TgBotApiImpl::startPoll() {
// Deleting webhook
getApi().deleteWebhook();

std::string sourceString;
GitData data;
if (GitData::Fill(&data)) {
sourceString = fmt::format(", sources: {}", data.originurl);
}

getApi().setMyDescription(
fmt::format("A C++ written Telegram bot{}", sourceString));
getApi().setMyDescription(fmt::format(
"A C++ written Telegram bot, sources: {}", git::buildinfo::ORIGIN_URL));

std::string ownerString;
if (auto owner = _provider->database->getOwnerUserId(); owner) {
Expand Down Expand Up @@ -560,7 +555,8 @@ bool TgBotApiImpl::answerCallbackQuery_impl(

TgBotApiImpl::TgBotApiImpl(const std::string_view token, AuthContext* auth,
StringResLoaderBase* loader, Providers* providers)
: _bot(std::string(token), std::make_unique<TgBot::CurlHttpClient>(std::chrono::seconds(30))),
: _bot(std::string(token),
std::make_unique<TgBot::CurlHttpClient>(std::chrono::seconds(30))),
_auth(auth),
_loader(loader),
_provider(providers),
Expand Down
12 changes: 2 additions & 10 deletions src/command_modules/alive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <tgbot/TgException.h>
#include <trivial_helpers/_tgbot.h>

#include <GitData.hpp>
#include <GitBuildInfo.hpp>
#include <api/CommandModule.hpp>
#include <api/Providers.hpp>
#include <api/TgBotApi.hpp>
Expand All @@ -20,19 +20,11 @@ DECLARE_COMMAND_HANDLER(alive) {

std::call_once(once, [provider, api] {
std::string _version;
GitData data;

GitData::Fill(&data);
_version = provider->resource->get("about.html");

std::vector<std::string> splitMsg =
absl::StrSplit(data.commitmsg, '\n');

// Replace placeholders in the version string with actual values.
version = absl::StrReplaceAll(
_version, {{"_commitid_", data.commitid},
{"_commitmsg_", splitMsg.front()},
{"_botname_", api->getBotUser()->firstName},
_version, {{"_botname_", api->getBotUser()->firstName},
{"_botusername_",
api->getBotUser()->username.value_or("unknown")}});
});
Expand Down
9 changes: 9 additions & 0 deletions src/include/GitBuildInfo.hpp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string_view>

namespace git::buildinfo {
constexpr static std::string_view COMMIT_ID = "@GIT_COMMIT_ID@";
constexpr static std::string_view COMMIT_MESSAGE = "@GIT_COMMIT_MESSAGE@";
constexpr static std::string_view ORIGIN_URL = "@GIT_ORIGIN_URL@";
} // namespace git::buildinfo
4 changes: 1 addition & 3 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
find_package(Boost COMPONENTS program_options REQUIRED)
find_package(libgit2 REQUIRED)

################# TgBot Utilities (generic) Library #################
add_my_library(
NAME Utils
SRCS
ConfigManager.cpp
Env_${TARGET_VARIANT}.cpp
GitData.cpp
CommandLine.cpp
ResourceManager.cpp
libfs_${TARGET_VARIANT}.cpp
LIBS Boost::program_options ${LIBGIT2_LIBRARIES}
LIBS Boost::program_options
LIBS_WIN32 shlwapi
)
88 changes: 0 additions & 88 deletions src/utils/GitData.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions src/utils/GitData.hpp

This file was deleted.

0 comments on commit b6af5b1

Please sign in to comment.