create_ext.py 5.6 KB

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