Skip to content

Commit

Permalink
feat: improve automatic versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Dec 20, 2023
1 parent fd0be5a commit 6898470
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
32 changes: 28 additions & 4 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from conan import ConanFile
from conan.tools.build import check_min_cppstd
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.scm import Git
from conans.errors import ConanInvalidConfiguration
from conans.model.version import Version

from setuptools_scm import Configuration, _get_version


class EndstoneRecipe(ConanFile):
name = "endstone"
version = _get_version(Configuration.from_file("pyproject.toml"), force_write_version_files=False)
package_type = "library"

# Optional metadata
Expand All @@ -25,9 +23,31 @@ class EndstoneRecipe(ConanFile):
default_options = {"shared": False, "fPIC": True, "fmt/*:header_only": True}

# Sources are located in the same place as this recipe, copy them to the recipe
exports = "pyproject.toml"
exports_sources = "CMakeLists.txt", "endstone_api/*", "endstone_core/*", "endstone_python/*"

def set_version(self):
if self.version:
return

git = Git(self)
tag = git.run("describe --tags --long")

import re

tag, num_commits, commit_hash = re.match(r"^(\S+)-(\d+)-g([a-f0-9]+)$", tag).groups()
version = Version(tag)
value = ".".join(str(i) for i in version.main)
if version.pre:
value += f"-{version.pre}"

if (num_commits := int(num_commits)) > 0:
value += f".dev{num_commits}"

if version.build:
value += f"+{version.build}"

self.version = value

@property
def _min_cppstd(self):
return 17
Expand Down Expand Up @@ -84,6 +104,10 @@ def generate(self):
deps = CMakeDeps(self)
deps.generate()
tc = CMakeToolchain(self)
tc.variables["ENDSTONE_VERSION"] = self.version
tc.variables["ENDSTONE_MINECRAFT_VERSION"] = (lambda v: f"{v[0]}.{v[1]}.{v[2]}.{v[3]:02}")(
[int(a.value) for a in Version(self.version).main]
)
tc.generate()

def build(self):
Expand Down
10 changes: 10 additions & 0 deletions endstone_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ target_link_libraries(endstone_core PUBLIC endstone::api PRIVATE spdlog::spdlog)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
target_link_libraries(endstone_core PRIVATE ${CMAKE_DL_LIBS} "stdc++fs")
endif ()
if (NOT DEFINED ENDSTONE_VERSION)
message(FATAL_ERROR "ENDSTONE_VERSION is not defined")
else ()
add_definitions(-DENDSTONE_VERSION="${ENDSTONE_VERSION}")
endif ()
if (NOT DEFINED ENDSTONE_MINECRAFT_VERSION)
message(FATAL_ERROR "ENDSTONE_MINECRAFT_VERSION is not defined")
else ()
add_definitions(-DENDSTONE_MINECRAFT_VERSION="${ENDSTONE_MINECRAFT_VERSION}")
endif ()

include(GNUInstallDirs)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Expand Down
8 changes: 5 additions & 3 deletions endstone_core/include/endstone_core/endstone_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
#define ENDSTONE_VERSION "unknown"
#endif

#ifndef MINECRAFT_VERSION
#define MINECRAFT_VERSION "unknown"
#ifndef ENDSTONE_MINECRAFT_VERSION
#define ENDSTONE_MINECRAFT_VERSION "unknown"
#endif

#define TO_STRING_VERSION(x) #x

class EndstoneServer : public Server {
public:
static EndstoneServer &getInstance()
Expand Down Expand Up @@ -58,7 +60,7 @@ class EndstoneServer : public Server {

[[nodiscard]] std::string_view getMinecraftVersion() const override
{
return MINECRAFT_VERSION;
return ENDSTONE_MINECRAFT_VERSION;
}

private:
Expand Down
4 changes: 4 additions & 0 deletions endstone_python/python/endstone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from importlib import import_module

from packaging.version import Version

from _version import __version__, __version_tuple__

__minecraft__version__ = (lambda v: f"{v[0]}.{v[1]}.{v[2]}.{v[3]:02}")([int(a) for a in Version(__version__).release])

module = import_module(f"{__name__}._api")
globals().update(module.__dict__)
del module
2 changes: 2 additions & 0 deletions test_package/src/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ int main()
logger.error("Hello World!");
logger.critical("Hello World!");

logger.info("Endstone version: v{} (Minecraft: v{})", server.getVersion(), server.getMinecraftVersion());

auto constexpr PluginName = "TestPlugin";

auto &plugin_manager = server.getPluginManager();
Expand Down

0 comments on commit 6898470

Please sign in to comment.