Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create library script #103

Merged
merged 7 commits into from Jun 2, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions scripts/create_library/create.py
@@ -0,0 +1,67 @@
import sys
import glob
import argparse
import os
import shutil
import datetime

def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--name', required=True, type=str, help='Name of new project')
parser.add_argument('--template', type=str, help='Source template directory')
parser.add_argument('--dest', type=str, help='Destination directory')
args = parser.parse_args(argv[1:])

name = args.name

year = datetime.datetime.now().year

if args.template is not None:
source_dir = args.template
else:
source_dir = os.path.join(os.path.dirname(argv[0]), 'files')

if args.dest is not None:
dest_dir = args.dest
else:
dest_dir = os.path.relpath(os.path.join(source_dir, '..', '..', '..', 'source', 'library'))
target_dir = os.path.realpath(os.path.join(dest_dir, name))

print('Creating `{1}` from `{2}`'.format(name, target_dir, source_dir))

for source_file in glob.iglob(pathname=os.path.join(source_dir, '**', '*'), recursive=True):
if os.path.isdir(source_file):
continue

relative_path=os.path.relpath(source_file, source_dir)
dir_name=os.path.dirname(relative_path)

target_dir_name=os.path.join(target_dir, dir_name).replace('__name__', name)

root_file, ext = os.path.splitext(relative_path)

if not os.path.isdir(target_dir_name):
os.makedirs(target_dir_name)

if ext == '.in':
target_file=os.path.join(target_dir, root_file).replace('__name__', name)
with open(source_file, 'rt') as template_file:
template = template_file.read()

processed = template.replace('@NAME@', name).replace('@NAME_UPPER@', name.upper()).replace('@YEAR@', str(year))

print('write template', target_file)
with open(target_file, 'wt') as output_file:
output_file.write(processed)
else:
target_file=os.path.join(target_dir, relative_path).replace('__name__', name)
print('copy to', target_file)
shutil.copyfile(src=source_file, dst=target_file)

cmakelist_path = os.path.join(dest_dir, 'CMakeLists.txt')
if os.path.isfile(cmakelist_path):
with open(cmakelist_path, 'at') as cmake_file:
cmake_file.write('add_subdirectory({0})\n'.format(name))

if __name__ == '__main__':
main(sys.argv)
18 changes: 18 additions & 0 deletions scripts/create_library/files/CMakeLists.txt.in
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.11)
project(@NAME@ VERSION 0.1 LANGUAGES CXX)

add_library(@NAME@)
add_library(potato::@NAME@ ALIAS @NAME@)

include(up_set_common_properties)
up_set_common_properties(@NAME@)

target_link_libraries(@NAME@
PUBLIC
potato::runtime
potato::foundation
)

add_subdirectory(public/potato/@NAME@)
add_subdirectory(private)
add_subdirectory(tests)
3 changes: 3 additions & 0 deletions scripts/create_library/files/private/CMakeLists.txt.in
@@ -0,0 +1,3 @@
target_sources(@NAME@ PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/@NAME@.cpp
)
7 changes: 7 additions & 0 deletions scripts/create_library/files/private/__name__.cpp.in
@@ -0,0 +1,7 @@
// Copyright (C) @YEAR@ Potato Engine authors and contributors, all rights reserverd.

#include "potato/@NAME@/@NAME@.h"

auto up::example(int x) noexcept -> int {
return x;
}
@@ -0,0 +1,3 @@
target_sources(@NAME@ PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/_export.h
)
@@ -0,0 +1,9 @@
// Copyright (C) @YEAR@ Potato Engine authors and contributors, all rights reserverd.

#include "_export.h"

namespace up {

UP_@NAME_UPPER@_API auto example(int x) noexcept -> int;

} // namespace up
11 changes: 11 additions & 0 deletions scripts/create_library/files/public/potato/__name__/_export.h.in
@@ -0,0 +1,11 @@
#pragma once

#if defined(UP_@NAME_UPPER@_EXPORTS)
# if defined(_WINDOWS)
# define UP_@NAME_UPPER@_API __declspec(dllexport)
# else
# define UP_@NAME_UPPER@_API [[gnu::visibility("default")]]
# endif
#else
# define UP_@NAME_UPPER@_API
#endif
13 changes: 13 additions & 0 deletions scripts/create_library/files/tests/CMakeLists.txt.in
@@ -0,0 +1,13 @@
add_executable(test_potato_@NAME@
main.cpp
)

include(up_set_common_properties)
up_set_common_properties(test_potato_@NAME@)

target_link_libraries(test_potato_@NAME@ PRIVATE
@NAME@
doctest
)

add_test(NAME test_potato_@NAME@ COMMAND test_potato_@NAME@)
2 changes: 2 additions & 0 deletions scripts/create_library/files/tests/main.cpp
@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>