create_ext.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. from __future__ import print_function
  4. import os
  5. import re
  6. import sys
  7. from distutils.dir_util import copy_tree
  8. from typing import Dict
  9. import click
  10. from idf_py_actions.tools import PropertyDict
  11. def get_type(action: str) -> str:
  12. return action.split('-')[1]
  13. def replace_in_file(filename: str, pattern: str, replacement: str) -> None:
  14. with open(filename, 'r+') as f:
  15. content = f.read()
  16. overwritten_content = re.sub(pattern, replacement, content, flags=re.M)
  17. f.seek(0)
  18. f.write(overwritten_content)
  19. f.truncate()
  20. def is_empty_and_create(path: str, action: str) -> None:
  21. abspath = os.path.abspath(path)
  22. if not os.path.exists(abspath):
  23. os.makedirs(abspath)
  24. elif not os.path.isdir(abspath):
  25. print('Your target path is not a directory. Please remove the', os.path.abspath(abspath),
  26. 'or use different target path.')
  27. sys.exit(4)
  28. elif len(os.listdir(path)) > 0:
  29. print('The directory', abspath, 'is not empty. To create a', get_type(action),
  30. 'you must empty the directory or choose a different path.')
  31. sys.exit(3)
  32. def create_project(target_path: str, name: str) -> None:
  33. copy_tree(
  34. os.path.join(os.environ['IDF_PATH'], 'examples', 'get-started', 'sample_project'),
  35. target_path,
  36. preserve_mode=0,
  37. )
  38. main_folder = os.path.join(target_path, 'main')
  39. os.rename(os.path.join(main_folder, 'main.c'), os.path.join(main_folder, '.'.join((name, 'c'))))
  40. replace_in_file(os.path.join(main_folder, 'CMakeLists.txt'), 'main', name)
  41. replace_in_file(os.path.join(target_path, 'CMakeLists.txt'), 'main', name)
  42. os.remove(os.path.join(target_path, 'README.md'))
  43. def create_component(target_path: str, name: str) -> None:
  44. copy_tree(
  45. os.path.join(os.environ['IDF_PATH'], 'tools', 'templates', 'sample_component'),
  46. target_path,
  47. preserve_mode=0,
  48. )
  49. os.rename(os.path.join(target_path, 'main.c'), os.path.join(target_path, '.'.join((name, 'c'))))
  50. os.rename(os.path.join(target_path, 'include', 'main.h'),
  51. os.path.join(target_path, 'include', '.'.join((name, 'h'))))
  52. replace_in_file(os.path.join(target_path, '.'.join((name, 'c'))), 'main', name)
  53. replace_in_file(os.path.join(target_path, 'CMakeLists.txt'), 'main', name)
  54. def action_extensions(base_actions: Dict, project_path: str) -> Dict:
  55. def create_new(action: str, ctx: click.core.Context, global_args: PropertyDict, **action_args: str) -> Dict:
  56. target_path = action_args.get('path') or os.path.join(project_path, action_args['name'])
  57. is_empty_and_create(target_path, action)
  58. func_action_map = {'create-project': create_project, 'create-component': create_component}
  59. func_action_map[action](target_path, action_args['name'])
  60. print('The', get_type(action), 'was created in', os.path.abspath(target_path))
  61. # after the command execution, no other commands are accepted and idf.py terminates
  62. sys.exit(0)
  63. return {
  64. 'actions': {
  65. 'create-project': {
  66. 'callback': create_new,
  67. 'short_help': 'Create a new project.',
  68. 'help': ('Create a new project with the name NAME specified as argument. '
  69. 'For example: '
  70. '`idf.py create-project new_proj` '
  71. 'will create a new project in subdirectory called `new_proj` '
  72. 'of the current working directory. '
  73. "For specifying the new project's path, use either the option --path for specifying the "
  74. 'destination directory, or the global option -C if the project should be created as a '
  75. 'subdirectory of the specified directory. '
  76. 'If the target path does not exist it will be created. If the target folder is not empty '
  77. 'then the operation will fail with return code 3. '
  78. 'If the target path is not a folder, the script will fail with return code 4. '
  79. 'After the execution idf.py terminates '
  80. 'so this operation should be used alone.'),
  81. 'arguments': [{'names': ['name']}],
  82. 'options': [
  83. {
  84. 'names': ['-p', '--path'],
  85. 'help': ('Set the path for the new project. The project '
  86. 'will be created directly in the given folder if it does not contain anything'),
  87. },
  88. ],
  89. },
  90. 'create-component': {
  91. 'callback': create_new,
  92. 'short_help': 'Create a new component.',
  93. 'help': ('Create a new component with the name NAME specified as argument. '
  94. 'For example: '
  95. '`idf.py create-component new_comp` '
  96. 'will create a new component in subdirectory called `new_comp` '
  97. 'of the current working directory. '
  98. "For specifying the new component's path use the option -C. "
  99. 'If the target path does not exist then it will be created. '
  100. 'If the target folder is not empty '
  101. 'then the operation will fail with return code 3. '
  102. 'If the target path is not a folder, the script will fail with return code 4. '
  103. 'After the execution idf.py terminates '
  104. 'so this operation should be used alone.'),
  105. 'arguments': [{'names': ['name']}],
  106. }
  107. }
  108. }