create_ext.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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(os.path.join(os.environ['IDF_PATH'], 'examples', 'get-started', 'sample_project'), target_path)
  34. main_folder = os.path.join(target_path, 'main')
  35. os.rename(os.path.join(main_folder, 'main.c'), os.path.join(main_folder, '.'.join((name, 'c'))))
  36. replace_in_file(os.path.join(main_folder, 'CMakeLists.txt'), 'main', name)
  37. replace_in_file(os.path.join(target_path, 'CMakeLists.txt'), 'main', name)
  38. os.remove(os.path.join(target_path, 'README.md'))
  39. def create_component(target_path: str, name: str) -> None:
  40. copy_tree(os.path.join(os.environ['IDF_PATH'], 'tools', 'templates', 'sample_component'), target_path)
  41. os.rename(os.path.join(target_path, 'main.c'), os.path.join(target_path, '.'.join((name, 'c'))))
  42. os.rename(os.path.join(target_path, 'include', 'main.h'),
  43. os.path.join(target_path, 'include', '.'.join((name, 'h'))))
  44. replace_in_file(os.path.join(target_path, '.'.join((name, 'c'))), 'main', name)
  45. replace_in_file(os.path.join(target_path, 'CMakeLists.txt'), 'main', name)
  46. def action_extensions(base_actions: Dict, project_path: str) -> Dict:
  47. def create_new(action: str, ctx: click.core.Context, global_args: PropertyDict, **action_args: str) -> Dict:
  48. target_path = action_args.get('path') or os.path.join(project_path, action_args['name'])
  49. is_empty_and_create(target_path, action)
  50. func_action_map = {'create-project': create_project, 'create-component': create_component}
  51. func_action_map[action](target_path, action_args['name'])
  52. print('The', get_type(action), 'was created in', os.path.abspath(target_path))
  53. # after the command execution, no other commands are accepted and idf.py terminates
  54. sys.exit(0)
  55. return {
  56. 'actions': {
  57. 'create-project': {
  58. 'callback': create_new,
  59. 'short_help': 'Create a new project.',
  60. 'help': ('Create a new project with the name NAME specified as argument. '
  61. 'For example: '
  62. '`idf.py create-project new_proj` '
  63. 'will create a new project in subdirectory called `new_proj` '
  64. 'of the current working directory. '
  65. "For specifying the new project's path, use either the option --path for specifying the "
  66. 'destination directory, or the global option -C if the project should be created as a '
  67. 'subdirectory of the specified directory. '
  68. 'If the target path does not exist it will be created. If the target folder is not empty '
  69. 'then the operation will fail with return code 3. '
  70. 'If the target path is not a folder, the script will fail with return code 4. '
  71. 'After the execution idf.py terminates '
  72. 'so this operation should be used alone.'),
  73. 'arguments': [{'names': ['name']}],
  74. 'options': [
  75. {
  76. 'names': ['-p', '--path'],
  77. 'help': ('Set the path for the new project. The project '
  78. 'will be created directly in the given folder if it does not contain anything'),
  79. },
  80. ],
  81. },
  82. 'create-component': {
  83. 'callback': create_new,
  84. 'short_help': 'Create a new component.',
  85. 'help': ('Create a new component with the name NAME specified as argument. '
  86. 'For example: '
  87. '`idf.py create-component new_comp` '
  88. 'will create a new component in subdirectory called `new_comp` '
  89. 'of the current working directory. '
  90. "For specifying the new component's path use the option -C. "
  91. 'If the target path does not exist then it will be created. '
  92. 'If the target folder is not empty '
  93. 'then the operation will fail with return code 3. '
  94. 'If the target path is not a folder, the script will fail with return code 4. '
  95. 'After the execution idf.py terminates '
  96. 'so this operation should be used alone.'),
  97. 'arguments': [{'names': ['name']}],
  98. }
  99. }
  100. }