Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:
jobs:
test:
runs-on: ubuntu-24.04
timeout-minutes: 60
timeout-minutes: 30
steps:
- uses: actions/checkout@v6
- uses: docker/setup-buildx-action@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:
jobs:
lint:
runs-on: ubuntu-24.04
timeout-minutes: 60
timeout-minutes: 30
steps:
- uses: actions/checkout@v6
- id: python
Expand Down
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ find_package(CURL 7.58.0 REQUIRED)
find_package(nlohmann_json 3.7.3 REQUIRED)
find_package(Threads REQUIRED)

file(GLOB TYPE_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/types/*.cpp")

add_library(TgBot
src/Api.cpp
src/ApiCodec.cpp
Expand All @@ -26,7 +24,8 @@ add_library(TgBot
src/TgLongPoll.cpp
src/TgWebhookLocalServer.cpp
src/TgWebhookTcpServer.cpp
${TYPE_SOURCES}
src/InputFile.cpp
src/Types.cpp
)
add_library(TgBot::TgBot ALIAS TgBot)

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_test
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ RUN make test && \
RUN make example EXAMPLE=echobot
RUN make example EXAMPLE=echobot-proxy
RUN make example EXAMPLE=echobot-setmycommands
RUN make example EXAMPLE=echobot-submodule
RUN make example EXAMPLE=echobot-webhook-server
RUN make example EXAMPLE=inline-keyboard
RUN make example EXAMPLE=photo
RUN make example EXAMPLE=receive-file
RUN make example EXAMPLE=received-text-processing
RUN make example EXAMPLE=reply-keyboard
RUN make example EXAMPLE=echobot-submodule

CMD ["make", "test-only"]
76 changes: 3 additions & 73 deletions api_codegen/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
trim_blocks=True,
lstrip_blocks=True,
)
GENERATED_MARKER = "// Generated by `make api-generate`. Do not edit."
CONFIG = yaml.safe_load(DEFAULT_CONFIG.read_text(encoding="utf-8"))
TYPE_CONFIG = CONFIG.get("types", {})
API_CONFIG = CONFIG.get("api", {})
Expand Down Expand Up @@ -111,41 +110,18 @@ def generate_openapi(schema_path: Path, root: Path) -> GeneratedCount:
}
objects = _build_objects(components)
methods = _build_methods(document["paths"])
api_objects = _api_objects(methods, objects)
outputs = {
root / "include" / "tgbot" / "types" / "types_fwd.h": _render_template(
"types_fwd.h.j2", objects=objects
),
root / "include" / "tgbot" / "types" / "types.h": _render_template(
root / "include" / "tgbot" / "Types.h": _render_template(
"types.h.j2", objects=objects
),
root / "src" / "Types.cpp": _render_template("types.cpp.j2", objects=objects),
root / "include" / "tgbot" / "ApiMethods.inc.h": _render_template(
"api_methods.inc.h.j2", methods=methods
),
root / "src" / "ApiMethods.cpp": _render_template(
"api_methods.cpp.j2", methods=methods, objects=api_objects
"api_methods.cpp.j2", methods=methods
),
}
type_headers = {
root / "include" / "tgbot" / "types" / f"{object.name}.h": _render_template(
"type.h.j2",
object=object,
)
for object in objects
}
outputs.update(type_headers)
type_sources = {
root / "src" / "types" / f"{object.name}.cpp": _render_template(
"type.cpp.j2",
object=object,
)
for object in objects
}
outputs.update(type_sources)
expected_type_headers = set(type_headers)
expected_type_headers.add(root / "include" / "tgbot" / "types" / "types_fwd.h")
_sync_generated_type_headers(root, expected_type_headers)
_sync_generated_type_sources(root, set(type_sources))
for path, content in outputs.items():
_write(path, content)

Expand Down Expand Up @@ -220,24 +196,6 @@ def _object_constants(fields: tuple[FieldModel, ...]) -> tuple[ConstantModel, ..
return tuple(field.constant for field in fields if field.constant)


def _api_objects(
methods: tuple[MethodModel, ...], objects: tuple[ObjectModel, ...]
) -> tuple[ObjectModel, ...]:
object_names = {object.name for object in objects}
cpp_types = [method.return_type for method in methods]
cpp_types.extend(
parameter.cpp_type for method in methods for parameter in method.parameters
)
used_names = {
token
for cpp_type in cpp_types
for token in re.findall(r"\b[A-Z][A-Za-z0-9]*\b", cpp_type)
if token in object_names
}

return tuple(object for object in objects if object.name in used_names)


def _object_dependencies(
name: str,
fields: tuple[FieldModel, ...],
Expand Down Expand Up @@ -426,34 +384,6 @@ def _write(path: Path, content: str) -> None:
path.write_text(content, encoding="utf-8")


def _sync_generated_type_headers(root: Path, expected_paths: set[Path]) -> None:
types_dir = root / "include" / "tgbot" / "types"
if not types_dir.exists():
return
stale_paths = [
path
for path in types_dir.glob("*.h")
if path not in expected_paths
and path.read_text(encoding="utf-8").startswith(GENERATED_MARKER)
]
for path in stale_paths:
path.unlink()


def _sync_generated_type_sources(root: Path, expected_paths: set[Path]) -> None:
types_dir = root / "src" / "types"
if not types_dir.exists():
return
stale_paths = [
path
for path in types_dir.glob("*.cpp")
if path not in expected_paths
and path.read_text(encoding="utf-8").startswith(GENERATED_MARKER)
]
for path in stale_paths:
path.unlink()


def _format_cpp(path: Path, content: str) -> str:
if path.name.endswith(".inc.h"):
return _format_cpp_class_fragment(path, content)
Expand Down
4 changes: 1 addition & 3 deletions api_codegen/templates/api_methods.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

#include "tgbot/Api.h"
#include "tgbot/ApiCodec.h"
{% for object in objects %}
#include "tgbot/types/{{ object.name }}.h"
{% endfor %}
#include "tgbot/Types.h"

namespace TgBot {

Expand Down
75 changes: 0 additions & 75 deletions api_codegen/templates/type.h.j2

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// Generated by `make api-generate`. Do not edit.

#include "tgbot/types/{{ object.name }}.h"
#include "tgbot/Json.h"
{% for dependency in object.dependencies %}
#include "tgbot/types/{{ dependency }}.h"
{% endfor %}
{% if object.enums %}
#include "tgbot/Types.h"

#include <stdexcept>
{% endif %}

namespace TgBot {

{% for object in objects %}
{% for constant in object.constants %}
const std::string {{ object.name }}::{{ constant.name }} = "{{ constant.value }}";
{% endfor %}
Expand Down Expand Up @@ -63,4 +59,5 @@ void to_json(nlohmann::json& json, const {{ object.name }}& value) {
{% endif %}
}

{% endfor %}
} // namespace TgBot
74 changes: 72 additions & 2 deletions api_codegen/templates/types.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,77 @@

#pragma once

#include "tgbot/types/InputFile.h"
#include "tgbot/export.h"

#include <nlohmann/json_fwd.hpp>

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>

namespace TgBot {

{% for object in objects %}
#include "tgbot/types/{{ object.name }}.h"
struct {{ object.name }};
{% endfor %}

{% for object in objects %}
/**
* @brief{{ " " + object.description[0] if object.description else "" }}
{% for line in object.description[1:] %}
* {{ line }}
{% endfor %}
*/
struct {{ object.name }} {
using Ptr = std::shared_ptr<{{ object.name }}>;

{% for constant in object.constants %}
static TGBOT_API const std::string {{ constant.name }};
{% endfor %}
{% if object.constants %}

{% endif %}
{% for enum in object.enums %}
enum class {{ enum.name }} {
{% for value in enum.values %}
{{ value.name }}{{ "," if not loop.last else "" }}
{% endfor %}
};

{% endfor %}
{% if object.union_members %}
std::variant<
{% for member in object.union_members %}
std::shared_ptr<{{ member }}>{{ "," if not loop.last else "" }}
{% endfor %}
>
value;
{% else %}
{% for field in object.fields %}
/**
* @brief{{ " " + field.description[0] if field.description else "" }}
{% for line in field.description[1:] %}
* {{ line }}
{% endfor %}
*/
{{ field.cpp_type }} {{ field.cpp_name }} { {{ field.constant.name if field.constant else "" }} };

{% endfor %}
{% endif %}
};

{% for enum in object.enums %}
TGBOT_API void from_json(const nlohmann::json& json, {{ object.name }}::{{ enum.name }}& value);
TGBOT_API void to_json(nlohmann::json& json, const {{ object.name }}::{{ enum.name }}& value);
{% endfor %}
{% if object.enums %}

{% endif %}
TGBOT_API void from_json(const nlohmann::json& json, {{ object.name }}& value);
TGBOT_API void to_json(nlohmann::json& json, const {{ object.name }}& value);

{% endfor %}
} // namespace TgBot
11 changes: 0 additions & 11 deletions api_codegen/templates/types_fwd.h.j2

This file was deleted.

Loading