Skip to content

Commit

Permalink
cargo-generate project template POC
Browse files Browse the repository at this point in the history
This is a proof of concept that turns exemplar into a template that
generates skeleton Beman projects using the [cargo-generate tool](https://github.com/cargo-generate/cargo-generate).

See https://github.com/camio/foobar.git for an example skeleton called
"foobar" generated from this template.

Use of this template requires a cargo-generate installation:

1. [Install Rust](https://www.rust-lang.org/tools/install) (a cargo-generate prerequisite)
2. `cargo install cargo-generate` (detailed instructions [here](https://cargo-generate.github.io/cargo-generate/installation.html))

Once `cargo-generate` is installed, run

```
cargo generate gh:camio/example --branch cargo-generate
```

and follow the prompts to generate a template.
  • Loading branch information
camio committed Jan 3, 2025
1 parent 641d099 commit 91c4062
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 62 deletions.
1 change: 1 addition & 0 deletions .genignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.github/workflows/pre-commit.yml
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
cmake_minimum_required(VERSION 3.25)

project(
beman.exemplar # CMake Project Name, which is also the name of the top-level
beman.{{project-name}} # CMake Project Name, which is also the name of the top-level
# targets (e.g., library, executable, etc.).
DESCRIPTION "A Beman library exemplar"
LANGUAGES CXX
)

# [CMAKE.SKIP_TESTS]
option(
BEMAN_EXEMPLAR_BUILD_TESTS
BEMAN_{{project-name | shouty_snake_case}}_BUILD_TESTS
"Enable building tests and test infrastructure. Default: ON. Values: { ON, OFF }."
${PROJECT_IS_TOP_LEVEL}
)

# [CMAKE.SKIP_EXAMPLES]
option(
BEMAN_EXEMPLAR_BUILD_EXAMPLES
BEMAN_{{project-name | shouty_snake_case}}_BUILD_EXAMPLES
"Enable building examples. Default: ON. Values: { ON, OFF }."
${PROJECT_IS_TOP_LEVEL}
)

include(FetchContent)
include(GNUInstallDirs)

if(BEMAN_EXEMPLAR_BUILD_TESTS)
if(BEMAN_{{project-name | shouty_snake_case}}_BUILD_TESTS)
enable_testing()

# Fetch GoogleTest
Expand All @@ -43,12 +43,12 @@ if(BEMAN_EXEMPLAR_BUILD_TESTS)
endblock()
endif()

add_subdirectory(src/beman/exemplar)
add_subdirectory(src/beman/{{project-name}})

if(BEMAN_EXEMPLAR_BUILD_TESTS)
add_subdirectory(tests/beman/exemplar)
if(BEMAN_{{project-name | shouty_snake_case}}_BUILD_TESTS)
add_subdirectory(tests/beman/{{project-name}})
endif()

if(BEMAN_EXEMPLAR_BUILD_EXAMPLES)
if(BEMAN_{{project-name | shouty_snake_case}}_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->

# beman.exemplar: A Beman Library Exemplar
# beman.{{project-name}}: A Beman Library

![Continuous Integration Tests](https://github.com/bemanproject/exemplar/actions/workflows/ci_tests.yml/badge.svg)

Expand Down
6 changes: 3 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()
message("Examples to be built: ${ALL_EXAMPLES}")

foreach(example ${ALL_EXAMPLES})
add_executable(beman.exemplar.examples.${example})
target_sources(beman.exemplar.examples.${example} PRIVATE ${example}.cpp)
target_link_libraries(beman.exemplar.examples.${example} beman::exemplar)
add_executable(beman.{{project-name}}.examples.${example})
target_sources(beman.{{project-name}}.examples.${example} PRIVATE ${example}.cpp)
target_link_libraries(beman.{{project-name}}.examples.${example} beman::{{project-name}})
endforeach()
6 changes: 3 additions & 3 deletions examples/identity_as_default_projection.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

// This example demonstrates the usage of beman::exemplar::identity as a default projection in a range-printer.
// This example demonstrates the usage of beman::{{project-name}}::identity as a default projection in a range-printer.
// Requires: range support (C++20) and std::identity support (C++20).
// TODO Darius: Do we need to selectively compile this example?
// Or should we assume that this project is compiled with C++20 support only?

#include <beman/exemplar/identity.hpp>
#include <beman/{{project-name}}/identity.hpp>

#include <algorithm>
#include <functional> // std::identity
#include <iostream>
#include <ranges>
#include <string>

namespace exe = beman::exemplar;
namespace exe = beman::{{project-name}};

// Class with a pair of values.
struct Pair {
Expand Down
4 changes: 2 additions & 2 deletions examples/identity_direct_usage.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/exemplar/identity.hpp>
#include <beman/{{project-name}}/identity.hpp>

#include <iostream>

namespace exe = beman::exemplar;
namespace exe = beman::{{project-name}};

int main() {
std::cout << exe::identity()(2024) << '\n';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef BEMAN_EXEMPLAR_IDENTITY_HPP
#define BEMAN_EXEMPLAR_IDENTITY_HPP
#ifndef BEMAN_{{project-name | shouty_snake_case}}_IDENTITY_HPP
#define BEMAN_{{project-name | shouty_snake_case}}_IDENTITY_HPP

// C++ Standard Library: std::identity equivalent.
// See https://eel.is/c++draft/func.identity:
Expand All @@ -22,7 +22,7 @@

#include <utility> // std::forward

namespace beman::exemplar {
namespace beman::{{project-name}} {

struct __is_transparent; // not defined

Expand All @@ -37,6 +37,6 @@ struct identity {
using is_transparent = __is_transparent;
};

} // namespace beman::exemplar
} // namespace beman::{{project-name}}

#endif // BEMAN_EXEMPLAR_IDENTITY_HPP
#endif // BEMAN_{{project-name | shouty_snake_case}}_IDENTITY_HPP
25 changes: 0 additions & 25 deletions src/beman/exemplar/CMakeLists.txt

This file was deleted.

25 changes: 25 additions & 0 deletions src/beman/{{project-name}}/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_library(beman.{{project-name}} STATIC)
add_library(beman::{{project-name}} ALIAS beman.{{project-name}})

target_sources(beman.{{project-name}} PRIVATE identity.cpp)

target_sources(
beman.{{project-name}}
PUBLIC
FILE_SET HEADERS
BASE_DIRS ${PROJECT_SOURCE_DIR}/include
FILES ${PROJECT_SOURCE_DIR}/include/beman/{{project-name}}/identity.hpp
)

set_target_properties(beman.{{project-name}} PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON)

install(
TARGETS beman.{{project-name}}
EXPORT beman.{{project-name}}
DESTINATION
$<$<CONFIG:Debug>:debug/>${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION $<$<CONFIG:Debug>:debug/>${CMAKE_INSTALL_BINDIR}
FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/exemplar/identity.hpp>
#include <beman/{{project-name}}/identity.hpp>
12 changes: 0 additions & 12 deletions tests/beman/exemplar/CMakeLists.txt

This file was deleted.

12 changes: 12 additions & 0 deletions tests/beman/{{project-name}}/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

include(GoogleTest)

add_executable(beman.{{project-name}}.tests.identity)
target_sources(beman.{{project-name}}.tests.identity PRIVATE identity.test.cpp)
target_link_libraries(
beman.{{project-name}}.tests.identity
PRIVATE beman::{{project-name}} GTest::gtest GTest::gtest_main
)

gtest_add_tests(beman.{{project-name}}.tests.identity "" AUTO)
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/exemplar/identity.hpp>
#include <beman/{{project-name}}/identity.hpp>

#include <gtest/gtest.h>

#include <algorithm>
#include <functional>

namespace exe = beman::exemplar;
namespace exe = beman::{{project-name}};

TEST(IdentityTest, call_identity_with_int) {
for (int i = -100; i < 100; ++i) {
Expand Down

0 comments on commit 91c4062

Please sign in to comment.