PageRenderTime 1939ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/pk-project-tools/src/lua/project-create/run.lua

https://github.com/logiceditor-com/codebase
Lua | 336 lines | 258 code | 58 blank | 20 comment | 3 complexity | c42b4e527e49faa846480ed01a87b961 MD5 | raw file
  1. --------------------------------------------------------------------------------
  2. -- run.lua: project-create runner
  3. -- This file is a part of pk-project-tools library
  4. -- Copyright (c) Alexander Gladysh <ag@logiceditor.com>
  5. -- Copyright (c) Dmitry Potapov <dp@logiceditor.com>
  6. -- See file `COPYRIGHT` for the license
  7. --------------------------------------------------------------------------------
  8. local loadfile, loadstring = loadfile, loadstring
  9. --------------------------------------------------------------------------------
  10. local log, dbg, spam, log_error
  11. = import 'pk-core/log.lua' { 'make_loggers' } (
  12. "project-create/run", "PCR"
  13. )
  14. --------------------------------------------------------------------------------
  15. local table_sort = table.sort
  16. --------------------------------------------------------------------------------
  17. local lfs = require 'lfs'
  18. --------------------------------------------------------------------------------
  19. local arguments,
  20. optional_arguments,
  21. method_arguments
  22. = import 'lua-nucleo/args.lua'
  23. {
  24. 'arguments',
  25. 'optional_arguments',
  26. 'method_arguments'
  27. }
  28. local is_table,
  29. is_function,
  30. is_string,
  31. is_number
  32. = import 'lua-nucleo/type.lua'
  33. {
  34. 'is_table',
  35. 'is_function',
  36. 'is_string',
  37. 'is_number'
  38. }
  39. local tgetpath,
  40. tclone,
  41. twithdefaults,
  42. tset,
  43. tiflip
  44. = import 'lua-nucleo/table-utils.lua'
  45. {
  46. 'tgetpath',
  47. 'tclone',
  48. 'twithdefaults',
  49. 'tset',
  50. 'tiflip'
  51. }
  52. local ordered_pairs
  53. = import 'lua-nucleo/tdeepequals.lua'
  54. {
  55. 'ordered_pairs'
  56. }
  57. local load_all_files,
  58. write_file,
  59. read_file,
  60. find_all_files,
  61. is_directory,
  62. does_file_exist,
  63. write_file,
  64. read_file,
  65. create_path_to_file
  66. = import 'lua-aplicado/filesystem.lua'
  67. {
  68. 'load_all_files',
  69. 'write_file',
  70. 'read_file',
  71. 'find_all_files',
  72. 'is_directory',
  73. 'does_file_exist',
  74. 'write_file',
  75. 'read_file',
  76. 'create_path_to_file'
  77. }
  78. local luarocks_show_rock_dir
  79. = import 'lua-aplicado/shell/luarocks.lua'
  80. {
  81. 'luarocks_show_rock_dir'
  82. }
  83. local copy_file_with_flag,
  84. copy_file,
  85. remove_file,
  86. remove_recursively
  87. = import 'lua-aplicado/shell/filesystem.lua'
  88. {
  89. 'copy_file_with_flag',
  90. 'copy_file',
  91. 'remove_file',
  92. 'remove_recursively'
  93. }
  94. local shell_read,
  95. shell_exec
  96. = import 'lua-aplicado/shell.lua'
  97. {
  98. 'shell_read',
  99. 'shell_exec'
  100. }
  101. local load_tools_cli_data_schema,
  102. load_tools_cli_config,
  103. print_tools_cli_config_usage,
  104. freeform_table_value
  105. = import 'pk-core/tools_cli_config.lua'
  106. {
  107. 'load_tools_cli_data_schema',
  108. 'load_tools_cli_config',
  109. 'print_tools_cli_config_usage',
  110. 'freeform_table_value'
  111. }
  112. local load_project_manifest
  113. = import 'pk-tools/project_manifest.lua'
  114. {
  115. 'load_project_manifest'
  116. }
  117. local tpretty
  118. = import 'lua-nucleo/tpretty.lua'
  119. {
  120. 'tpretty'
  121. }
  122. local tstr
  123. = import 'lua-nucleo/tstr.lua'
  124. {
  125. 'tstr'
  126. }
  127. local unify_manifest_dictionary,
  128. get_template_paths,
  129. prepare_manifest
  130. = import 'pk-project-create/common_functions.lua'
  131. {
  132. 'unify_manifest_dictionary',
  133. 'get_template_paths',
  134. 'prepare_manifest'
  135. }
  136. local make_project_using_fs_structure,
  137. create_project_fs_structure
  138. = import 'pk-project-create/replicate_data.lua'
  139. {
  140. 'make_project_using_fs_structure',
  141. 'create_project_fs_structure'
  142. }
  143. local create_template_fs_structure
  144. = import 'pk-project-create/copy_files.lua'
  145. {
  146. 'create_template_fs_structure'
  147. }
  148. --------------------------------------------------------------------------------
  149. local create_config_schema
  150. = import 'project-create/project-config/schema.lua'
  151. {
  152. 'create_config_schema'
  153. }
  154. --------------------------------------------------------------------------------
  155. local TOOL_NAME = "project_create"
  156. local CONFIG, ARGS
  157. --------------------------------------------------------------------------------
  158. local create_project
  159. do
  160. local load_metamanifest_with_tool_defaults = function(
  161. metamanifest_path,
  162. project_path
  163. )
  164. arguments(
  165. "string", metamanifest_path,
  166. "string", project_path
  167. )
  168. local metamanifest_defaults = unify_manifest_dictionary(
  169. load_project_manifest(
  170. assert(
  171. luarocks_show_rock_dir("pk-project-tools.pk-project-create"),
  172. "pk-project-tools.pk-project-create: rock directory not found"
  173. ):sub(1, -1)
  174. .. "/src/lua/project-create/metamanifest",
  175. "",
  176. ""
  177. )
  178. )
  179. local metamanifest_project = unify_manifest_dictionary(
  180. load_project_manifest(metamanifest_path, project_path, "")
  181. )
  182. if metamanifest_defaults.version ~= metamanifest_project.version then
  183. log_error(
  184. "Wrong metamanifest version:",
  185. "expected:", metamanifest_defaults.version,
  186. "got:", metamanifest_project.version
  187. )
  188. error("Wrong metamanifest version")
  189. end
  190. local metamanifest = prepare_manifest(
  191. twithdefaults(metamanifest_project, metamanifest_defaults)
  192. )
  193. metamanifest.project_path = project_path
  194. return metamanifest
  195. end
  196. create_project = function(
  197. metamanifest_path,
  198. project_path,
  199. root_template_name,
  200. root_template_paths
  201. )
  202. arguments(
  203. "string", metamanifest_path,
  204. "string", project_path,
  205. "string", root_template_name,
  206. "table", root_template_paths
  207. )
  208. log("Project generation started", project_path)
  209. local metamanifest = load_metamanifest_with_tool_defaults(
  210. metamanifest_path,
  211. project_path
  212. )
  213. make_project_using_fs_structure(
  214. create_project_fs_structure(
  215. create_template_fs_structure(
  216. get_template_paths(root_template_name, root_template_paths),
  217. metamanifest
  218. ),
  219. metamanifest
  220. ),
  221. metamanifest
  222. )
  223. log("Project", metamanifest.dictionary.PROJECT_NAME, "generated successfully")
  224. end
  225. end
  226. --------------------------------------------------------------------------------
  227. -- TODO: remove (NYI) in --debug after #3775
  228. local EXTRA_HELP = [[
  229. pk-project-create: fast project creation tool
  230. Usage:
  231. pk-project-create <metamanifest_directory_path> <project_root_dir> [<template_dir>] [options]
  232. Options:
  233. --debug Verbose output (NYI)
  234. ]]
  235. local CONFIG_SCHEMA = create_config_schema()
  236. --------------------------------------------------------------------------------
  237. local run = function(...)
  238. -- WARNING: Action-less tool. Take care when copy-pasting.
  239. CONFIG, ARGS = load_tools_cli_config(
  240. function(args) -- Parse actions
  241. local param = { }
  242. param.metamanifest_path = args[1] or args["--metamanifest_path"]
  243. param.root_project_path = args[2] or args["--root_project_path"]
  244. param.root_template_name = args[3] or args["--root_template_name"]
  245. param.root_template_paths = args["--root_template_paths"]
  246. param.debug = args["--debug"]
  247. return
  248. {
  249. PROJECT_PATH = args["--root"] or "";
  250. [TOOL_NAME] = param;
  251. }
  252. end,
  253. EXTRA_HELP,
  254. CONFIG_SCHEMA,
  255. luarocks_show_rock_dir("pk-project-tools.pk-project-create")
  256. .. "/src/lua/project-create/project-config/config.lua",
  257. nil, -- No secondary config file
  258. ...
  259. )
  260. if CONFIG == nil then
  261. local err = ARGS
  262. print_tools_cli_config_usage(EXTRA_HELP, CONFIG_SCHEMA)
  263. io.stderr:write("Error in tool configuration:\n", err, "\n\n")
  264. io.stderr:flush()
  265. os.exit(1)
  266. end
  267. ------------------------------------------------------------------------------
  268. create_project(
  269. CONFIG[TOOL_NAME].metamanifest_path,
  270. CONFIG[TOOL_NAME].root_project_path,
  271. CONFIG[TOOL_NAME].root_template_name,
  272. freeform_table_value(CONFIG[TOOL_NAME].root_template_paths)
  273. )
  274. end
  275. return
  276. {
  277. run = run;
  278. }