2
0

plugin_task.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>xmake</title>
  6. <link rel="icon" href="/assets/img/favicon.ico">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  8. <meta name="description" content="Description">
  9. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  10. <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
  11. <style>
  12. .markdown-body {
  13. box-sizing: border-box;
  14. min-width: 200px;
  15. max-width: 980px;
  16. margin: 0 auto;
  17. padding: 45px;
  18. }
  19. @media (max-width: 767px) {
  20. .markdown-body {
  21. padding: 15px;
  22. }
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <article class="markdown-body">
  28. <h4>This is a mirror page, please see the original page: </h4><a href="https://xmake.io/#/manual/plugin_task">https://xmake.io/#/manual/plugin_task</a>
  29. <div id="wwads-panel" class="wwads-cn wwads-vertical wwads-sticky" data-id="239" style="max-width:180px;bottom:20px;right:20px;width:200px;height:260px;background:#fff;position:fixed"></div>
  30. </br>
  31. <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
  32. <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
  33. <style>
  34. #carbonads {
  35. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  36. Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
  37. }
  38. #carbonads {
  39. display: flex;
  40. max-width: 330px;
  41. background-color: hsl(0, 0%, 98%);
  42. box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
  43. }
  44. #carbonads a {
  45. color: inherit;
  46. text-decoration: none;
  47. }
  48. #carbonads a:hover {
  49. color: inherit;
  50. }
  51. #carbonads span {
  52. position: relative;
  53. display: block;
  54. overflow: hidden;
  55. }
  56. #carbonads .carbon-wrap {
  57. display: flex;
  58. }
  59. .carbon-img {
  60. display: block;
  61. margin: 0;
  62. line-height: 1;
  63. }
  64. .carbon-img img {
  65. display: block;
  66. }
  67. .carbon-text {
  68. font-size: 13px;
  69. padding: 10px;
  70. line-height: 1.5;
  71. text-align: left;
  72. }
  73. .carbon-poweredby {
  74. display: block;
  75. padding: 8px 10px;
  76. background: repeating-linear-gradient(-45deg, transparent, transparent 5px, hsla(0, 0%, 0%, .025) 5px, hsla(0, 0%, 0%, .025) 10px) hsla(203, 11%, 95%, .4);
  77. text-align: center;
  78. text-transform: uppercase;
  79. letter-spacing: .5px;
  80. font-weight: 600;
  81. font-size: 9px;
  82. line-height: 1;
  83. }
  84. </style>
  85. <p>Xmake can implement custom tasks or plugins. The core of both is the <code>task</code> task. The two are actually the same. The xmake plugins are implemented with <code>task</code>.</p>
  86. <p>In essence, they are tasks, except that the <a href="#taskset_category">set_category</a> classification is different.</p>
  87. <table>
  88. <thead>
  89. <tr>
  90. <th>Interface</th>
  91. <th>Description</th>
  92. <th>Supported Versions</th>
  93. </tr>
  94. </thead>
  95. <tbody>
  96. <tr>
  97. <td><a href="#task">task</a></td>
  98. <td>Define plugins or tasks</td>
  99. <td>>= 2.0.1</td>
  100. </tr>
  101. <tr>
  102. <td><a href="#task_end">task_end</a></td>
  103. <td>End defining plugins or tasks</td>
  104. <td>>= 2.1.1</td>
  105. </tr>
  106. <tr>
  107. <td><a href="#taskset_menu">set_menu</a></td>
  108. <td>Set Task Menu</td>
  109. <td>>= 2.0.1</td>
  110. </tr>
  111. <tr>
  112. <td><a href="#taskset_category">set_category</a></td>
  113. <td>Set Task Category</td>
  114. <td>>= 2.0.1</td>
  115. </tr>
  116. <tr>
  117. <td><a href="#taskon_run">on_run</a></td>
  118. <td>Set Task Run Script</td>
  119. <td>>= 2.0.1</td>
  120. </tr>
  121. </tbody>
  122. </table>
  123. <h3 id="task">task</h3>
  124. <h4 id="definingpluginsortasks">Defining plugins or tasks</h4>
  125. <p>The <code>task</code> field is used to describe a custom task implementation, in the same level as <a href="#target">target</a> and <a href="#option">option</a>.</p>
  126. <p>For example, here is a simple task defined:</p>
  127. <pre><code class="lang-lua">task("hello")
  128. -- Set the run script
  129. on_run(function ()
  130. print("hello xmake!")
  131. end)
  132. </code></pre>
  133. <p>This task only needs to print <code>hello xmake!</code>, how do you run it?</p>
  134. <p>Since the <a href="#taskset_menu">set_menu</a> setting menu is not used here, this task can only be called inside the custom script of <code>xmake.lua</code> or other tasks, for example:</p>
  135. <pre><code class="lang-lua">target("test")
  136. after_build(function (target)
  137. -- Import task module
  138. import("core.project.task")
  139. -- Run the hello task
  140. task.run("hello")
  141. end)
  142. </code></pre>
  143. <p>Run the <code>hello</code> task after building the <code>test</code> target.</p>
  144. <h3 id="task_end">task_end</h3>
  145. <h4 id="enddefiningpluginsortasks">End defining plugins or tasks</h4>
  146. <p>This is an optional api that shows the departure option scope, similar to <a href="#target_end">target_end</a>.</p>
  147. <h3 id="taskset_menu">task:set_menu</h3>
  148. <h4 id="settingthetaskmenu">Setting the task menu</h4>
  149. <p>By setting a menu, this task can be opened to the user to manually call through the command line. The menu settings are as follows:</p>
  150. <pre><code class="lang-lua">task("echo")
  151. -- Set the run script
  152. on_run(function ()
  153. -- Import parameter option module
  154. import("core.base.option")
  155. -- Initialize color mode
  156. local modes = ""
  157. for _, mode in ipairs({"bright", "dim", "blink", "reverse"}) do
  158. if option.get(mode) then
  159. modes = modes .. " " .. mode
  160. end
  161. end
  162. -- Get parameter content and display information
  163. cprint("${%s%s}%s", option.get("color"), modes, table.concat(option.get("contents") or {}, " "))
  164. end)
  165. -- Set the command line options for the plugin. There are no parameter options here, just the plugin description.
  166. set_menu {
  167. -- Settings menu usage
  168. usage = "xmake echo [options]"
  169. -- Setup menu description
  170. , description = "Echo the given info!"
  171. -- Set menu options, if there are no options, you can set it to {}
  172. , options =
  173. {
  174. -- Set k mode as key-only bool parameter
  175. {&#39;b&#39;, "bright", "k", nil, "Enable bright." }
  176. , {&#39;d&#39;, "dim", "k", nil, "Enable dim." }
  177. , {&#39;-&#39;, "blink", "k", nil, "Enable blink." }
  178. , {&#39;r&#39;, "reverse", "k", nil, "Reverse color." }
  179. -- When the menu is displayed, a blank line
  180. , {}
  181. -- Set kv as the key-value parameter and set the default value: black
  182. , {&#39;c&#39;, "color", "kv", "black", "Set the output color."
  183. , " - red"
  184. , " - blue"
  185. , " - yellow"
  186. , " - green"
  187. , " - magenta"
  188. , " - cyan"
  189. , " - white" }
  190. -- Set `vs` as a value multivalued parameter and a `v` single value type
  191. -- generally placed last, used to get a list of variable parameters
  192. , {}
  193. , {nil, "contents", "vs", nil, "The info contents." }
  194. }
  195. }
  196. </code></pre>
  197. <p>After defining this task, execute <code>xmake --help</code> and you will have one more task item:</p>
  198. <pre><code>Tasks:
  199. ...
  200. Echo Echo the given info!
  201. </code></pre><p>If the classification is <code>plugin</code> by <a href="#taskset_category">set_category</a>, then this task is a plugin:</p>
  202. <pre><code>Plugins:
  203. ...
  204. Echo Echo the given info!
  205. </code></pre><p>To run this task manually, you can execute:</p>
  206. <pre><code class="lang-bash">$ xmake echo hello xmake!
  207. </code></pre>
  208. <p>Just fine, if you want to see the menu defined by this task, you only need to execute: <code>xmake echo [-h|--help]</code>, the result is as follows:</p>
  209. <pre><code class="lang-bash">Usage: $xmake echo [options]
  210. Echo the given info!
  211. Options:
  212. -v, --verbose Print lots of verbose information.
  213. --backtrace Print backtrace information for debugging.
  214. --profile Print performance data for debugging.
  215. --version Print the version number and exit.
  216. -h, --help Print this help message and exit.
  217. -F FILE, --file=FILE Read a given xmake.lua file.
  218. -P PROJECT, --project=PROJECT Change to the given project directory.
  219. Search priority:
  220. 1. The Given Command Argument
  221. 2. The Environment Variable: XMAKE_PROJECT_DIR
  222. 3. The Current Directory
  223. -b, --bright Enable bright.
  224. -d, --dim Enable dim.
  225. --, --blink Enable blink.
  226. -r, --reverse Reverse color.
  227. -c COLOR, --color=COLOR Set the output color. (default: black)
  228. - red
  229. - blue
  230. - yellow
  231. - green
  232. - magenta
  233. - cyan
  234. - white
  235. Contents ... The info contents.
  236. </code></pre>
  237. <p><p class="tip"><br>The most part of the menu is the common options built into xmake. Basically, each task will be used. You don&#39;t need to define it yourself to simplify the menu definition.<br></p>
  238. </p>
  239. <p>Below, let&#39;s actually run this task, for example, I want to display the red <code>hello xmake!</code>, only need to:</p>
  240. <pre><code class="lang-bash">$ xmake echo -c red hello xmake!
  241. </code></pre>
  242. <p>You can also use the full name of the option and highlight it:</p>
  243. <pre><code class="lang-bash">$ xmake echo --color=red --bright hello xmake!
  244. </code></pre>
  245. <p>The last variable argument list is retrieved by <code>option.get("contents")</code> in the <code>run</code> script, which returns an array of type <code>table</code>.</p>
  246. <h3 id="taskset_category">task:set_category</h3>
  247. <h4 id="settingtaskcategories">Setting task categories</h4>
  248. <p>It is only used for grouping of menus. Of course, the plugin will use <code>plugin</code> by default. The built-in task will use <code>action</code> by default, but it is just a convention.</p>
  249. <p><p class="tip"><br>You can use any name you define yourself. The same name will be grouped and displayed together. If it is set to <code>plugin</code>, it will be displayed in the Plugins group of xmake.<br></p>
  250. </p>
  251. <p>E.g:</p>
  252. <pre><code class="lang-lua">plugins:
  253. l, lua Run the lua script.
  254. m, macro Run the given macro.
  255. doxygen Generate the doxygen document.
  256. project Generate the project file.
  257. hello Hello xmake!
  258. app2ipa Generate .ipa file from theGiven .app
  259. echo Echo the given info!
  260. </code></pre>
  261. <p>If you do not call this interface to set the classification, the default is to use the <code>Tasks</code> group display, which represents the normal task.</p>
  262. <h3 id="taskon_run">task:on_run</h3>
  263. <h4 id="settingupatasktorunascript">Setting up a task to run a script</h4>
  264. <p>There are two ways to set it up. The easiest way is to set the inline function:</p>
  265. <pre><code class="lang-lua">task("hello")
  266. on_run(function ()
  267. print("hello xmake!")
  268. end)
  269. </code></pre>
  270. <p>This is convenient and small for small tasks, but it is not suitable for large tasks, such as plugins, which require complex scripting support.</p>
  271. <p>This time you need a separate module file to set up the run script, for example:</p>
  272. <pre><code class="lang-lua">task("hello")
  273. on_run("main")
  274. </code></pre>
  275. <p>Here the <code>main</code> is set to run the main entry module for the script. The file name is <code>main.lua</code>, placed in the same directory as <code>xmake.lua</code> that defines <code>task</code>. Of course, you can use other file names.</p>
  276. <p>The directory structure is as follows:</p>
  277. <pre><code>projectdir
  278. - xmake.lua
  279. - main.lua
  280. </code></pre><p>The contents of <code>main.lua</code> are as follows:</p>
  281. <pre><code class="lang-lua">function main(...)
  282. print("hello xmake!")
  283. end
  284. </code></pre>
  285. <p>It&#39;s a simple script file with the main function of <code>main</code>. You can import various extension modules via <a href="/mirror/manual/builtin_modules.html#import">import</a> to implement complex functions, such as:</p>
  286. <pre><code class="lang-lua">-- Import parameter option module
  287. import("core.base.option")
  288. -- Entrance function
  289. function main(...)
  290. -- Get the parameter content
  291. print("color: %s", option.get("color"))
  292. end
  293. </code></pre>
  294. <p>You can also create multiple custom module files in the current directory and use them after importing via <a href="/mirror/manual/builtin_modules.html#import">import</a>, for example:</p>
  295. <pre><code>Projectdir
  296. - xmake.lua
  297. - main.lua
  298. - module.lua
  299. </code></pre><p>The contents of <code>module.lua</code> are as follows:</p>
  300. <pre><code class="lang-lua">-- Define an export interface
  301. function hello()
  302. print("hello xmake!")
  303. end
  304. </code></pre>
  305. <p><p class="tip"><br>The private interface is named by the <code>_hello</code> with a descending line prefix, so that the imported module will not contain this interface and will only be used inside the module itself.<br></p>
  306. </p>
  307. <p>Then make a call in <code>main.lua</code>:</p>
  308. <pre><code class="lang-lua">import("module")
  309. function main(...)
  310. module.hello()
  311. end
  312. </code></pre>
  313. <p>For more modules, see: <a href="/mirror/manual/builtin_modules.html">Builtin Module</a> and <a href="/mirror/manual/extension_modules.html">Extension Module</a></p>
  314. <p>Among them, the parameter in <code>main(...)</code> is specified by <code>task.run</code>, for example:</p>
  315. <pre><code class="lang-lua">task.run("hello", {color="red"}, arg1, arg2, arg3)
  316. </code></pre>
  317. <p>Inside the <code>arg1, arg2</code> these are the arguments to the <code>hello</code> task <code>main(...)</code> entry, and <code>{color="red"}</code> to specify the parameter options in the task menu.</p>
  318. <p>For a more detailed description of <code>task.run</code>, see: <a href="#task-run">task.run</a></p>
  319. </article>
  320. </body>
  321. </html>