1
0
mirror of https://github.com/go-task/task.git synced 2025-03-27 21:48:54 +02:00
task/zh-Hans/assets/js/01713e21.56cca42e.js

1 line
69 KiB
JavaScript

"use strict";(self.webpackChunktaskfile_dev=self.webpackChunktaskfile_dev||[]).push([[701],{3905:function(e,n,t){t.d(n,{Zo:function(){return d},kt:function(){return c}});var a=t(7294);function l(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function i(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);n&&(a=a.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,a)}return t}function s(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?i(Object(t),!0).forEach((function(n){l(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):i(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function o(e,n){if(null==e)return{};var t,a,l=function(e,n){if(null==e)return{};var t,a,l={},i=Object.keys(e);for(a=0;a<i.length;a++)t=i[a],n.indexOf(t)>=0||(l[t]=e[t]);return l}(e,n);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a<i.length;a++)t=i[a],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(l[t]=e[t])}return l}var r=a.createContext({}),p=function(e){var n=a.useContext(r),t=n;return e&&(t="function"==typeof e?e(n):s(s({},n),e)),t},d=function(e){var n=p(e.components);return a.createElement(r.Provider,{value:n},e.children)},k="mdxType",m={inlineCode:"code",wrapper:function(e){var n=e.children;return a.createElement(a.Fragment,{},n)}},u=a.forwardRef((function(e,n){var t=e.components,l=e.mdxType,i=e.originalType,r=e.parentName,d=o(e,["components","mdxType","originalType","parentName"]),k=p(t),u=l,c=k["".concat(r,".").concat(u)]||k[u]||m[u]||i;return t?a.createElement(c,s(s({ref:n},d),{},{components:t})):a.createElement(c,s({ref:n},d))}));function c(e,n){var t=arguments,l=n&&n.mdxType;if("string"==typeof e||l){var i=t.length,s=new Array(i);s[0]=u;var o={};for(var r in n)hasOwnProperty.call(n,r)&&(o[r]=n[r]);o.originalType=e,o[k]="string"==typeof e?e:l,s[1]=o;for(var p=2;p<i;p++)s[p]=t[p];return a.createElement.apply(null,s)}return a.createElement.apply(null,t)}u.displayName="MDXCreateElement"},3423:function(e,n,t){t.r(n),t.d(n,{assets:function(){return r},contentTitle:function(){return s},default:function(){return k},frontMatter:function(){return i},metadata:function(){return o},toc:function(){return p}});var a=t(3117),l=(t(7294),t(3905));const i={slug:"/usage/",sidebar_position:3},s="\u4f7f\u7528\u6307\u5357",o={unversionedId:"usage",id:"usage",title:"\u4f7f\u7528\u6307\u5357",description:"\u5feb\u901f\u5165\u95e8",source:"@site/i18n/zh-Hans/docusaurus-plugin-content-docs/current/usage.md",sourceDirName:".",slug:"/usage/",permalink:"/zh-Hans/usage/",draft:!1,tags:[],version:"current",sidebarPosition:3,frontMatter:{slug:"/usage/",sidebar_position:3},sidebar:"tutorialSidebar",previous:{title:"\u5b89\u88c5",permalink:"/zh-Hans/installation/"},next:{title:"API \u53c2\u8003",permalink:"/zh-Hans/api/"}},r={},p=[{value:"\u5feb\u901f\u5165\u95e8",id:"\u5feb\u901f\u5165\u95e8",level:2},{value:"\u652f\u6301\u7684\u6587\u4ef6\u540d\u79f0",id:"\u652f\u6301\u7684\u6587\u4ef6\u540d\u79f0",level:2},{value:"\u4ece\u5b50\u76ee\u5f55\u8fd0\u884c Taskfile",id:"\u4ece\u5b50\u76ee\u5f55\u8fd0\u884c-taskfile",level:3},{value:"\u8fd0\u884c\u5168\u5c40 Taskfile",id:"\u8fd0\u884c\u5168\u5c40-taskfile",level:3},{value:"\u73af\u5883\u53d8\u91cf",id:"\u73af\u5883\u53d8\u91cf",level:2},{value:"Task",id:"task",level:3},{value:".env \u6587\u4ef6",id:"env-\u6587\u4ef6",level:3},{value:"\u5305\u542b\u5176\u4ed6 Taskfile",id:"\u5305\u542b\u5176\u4ed6-taskfile",level:2},{value:"\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a Taskfile",id:"\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a-taskfile",level:3},{value:"\u5305\u542b Taskfile \u7684\u76ee\u5f55",id:"\u5305\u542b-taskfile-\u7684\u76ee\u5f55",level:3},{value:"\u53ef\u9009 includes",id:"\u53ef\u9009-includes",level:3},{value:"\u5185\u90e8 includes",id:"\u5185\u90e8-includes",level:3},{value:"\u5305\u542b\u7684 Taskfile \u7684\u53d8\u91cf",id:"\u5305\u542b\u7684-taskfile-\u7684\u53d8\u91cf",level:3},{value:"\u547d\u540d\u7a7a\u95f4\u522b\u540d",id:"\u547d\u540d\u7a7a\u95f4\u522b\u540d",level:3},{value:"\u5185\u90e8 tasks",id:"\u5185\u90e8-tasks",level:2},{value:"Task \u76ee\u5f55",id:"task-\u76ee\u5f55",level:2},{value:"Task \u4f9d\u8d56",id:"task-\u4f9d\u8d56",level:2},{value:"\u5e73\u53f0\u7279\u5b9a\u7684 tasks \u548c cmds",id:"\u5e73\u53f0\u7279\u5b9a\u7684-tasks-\u548c-cmds",level:2},{value:"\u8c03\u7528\u53e6\u4e00\u4e2a task",id:"\u8c03\u7528\u53e6\u4e00\u4e2a-task",level:2},{value:"\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5de5\u4f5c",id:"\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5de5\u4f5c",level:2},{value:"\u901a\u8fc7\u6307\u7eb9\u8bc6\u522b\u672c\u5730\u751f\u6210\u7684\u6587\u4ef6\u53ca\u5176\u6765\u6e90",id:"\u901a\u8fc7\u6307\u7eb9\u8bc6\u522b\u672c\u5730\u751f\u6210\u7684\u6587\u4ef6\u53ca\u5176\u6765\u6e90",level:3},{value:"Using programmatic checks to indicate a task is up to date",id:"using-programmatic-checks-to-indicate-a-task-is-up-to-date",level:3},{value:"\u4f7f\u7528\u7a0b\u5e8f\u68c0\u67e5\u53d6\u6d88\u4efb\u52a1\u53ca\u5176\u4f9d\u8d56\u9879\u7684\u6267\u884c",id:"\u4f7f\u7528\u7a0b\u5e8f\u68c0\u67e5\u53d6\u6d88\u4efb\u52a1\u53ca\u5176\u4f9d\u8d56\u9879\u7684\u6267\u884c",level:3},{value:"\u5728\u4efb\u52a1\u8fd0\u884c\u65f6\u9650\u5236",id:"\u5728\u4efb\u52a1\u8fd0\u884c\u65f6\u9650\u5236",level:3},{value:"\u53d8\u91cf",id:"\u53d8\u91cf",level:2},{value:"\u52a8\u6001\u53d8\u91cf",id:"\u52a8\u6001\u53d8\u91cf",level:3},{value:"\u5c06 CLI \u53c2\u6570\u8f6c\u53d1\u5230 cmds",id:"\u5c06-cli-\u53c2\u6570\u8f6c\u53d1\u5230-cmds",level:2},{value:"\u4f7f\u7528 <code>defer</code> \u505a task \u6e05\u7406",id:"\u4f7f\u7528-defer-\u505a-task-\u6e05\u7406",level:2},{value:"Go \u7684\u6a21\u677f\u5f15\u64ce",id:"go-\u7684\u6a21\u677f\u5f15\u64ce",level:2},{value:"\u5e2e\u52a9",id:"\u5e2e\u52a9",level:2},{value:"\u663e\u793a\u4efb\u52a1\u6458\u8981",id:"\u663e\u793a\u4efb\u52a1\u6458\u8981",level:2},{value:"Task \u522b\u540d",id:"task-\u522b\u540d",level:2},{value:"\u8986\u76d6 Task \u540d\u79f0",id:"\u8986\u76d6-task-\u540d\u79f0",level:2},{value:"\u9759\u9ed8\u6a21\u5f0f",id:"\u9759\u9ed8\u6a21\u5f0f",level:2},{value:"\u8bd5\u8fd0\u884c\u6a21\u5f0f",id:"\u8bd5\u8fd0\u884c\u6a21\u5f0f",level:2},{value:"\u5ffd\u7565\u9519\u8bef",id:"\u5ffd\u7565\u9519\u8bef",level:2},{value:"\u8f93\u51fa\u8bed\u6cd5",id:"\u8f93\u51fa\u8bed\u6cd5",level:2},{value:"\u4ea4\u4e92\u5f0f CLI \u5e94\u7528",id:"\u4ea4\u4e92\u5f0f-cli-\u5e94\u7528",level:2},{value:"\u77ed Task \u8bed\u6cd5",id:"\u77ed-task-\u8bed\u6cd5",level:2},{value:"<code>set</code> \u548c <code>shopt</code>",id:"set-\u548c-shopt",level:2},{value:"\u89c2\u5bdf\u4efb\u52a1",id:"\u89c2\u5bdf\u4efb\u52a1",level:2}],d={toc:p};function k(e){let{components:n,...t}=e;return(0,l.kt)("wrapper",(0,a.Z)({},d,t,{components:n,mdxType:"MDXLayout"}),(0,l.kt)("h1",{id:"\u4f7f\u7528\u6307\u5357"},"\u4f7f\u7528\u6307\u5357"),(0,l.kt)("h2",{id:"\u5feb\u901f\u5165\u95e8"},"\u5feb\u901f\u5165\u95e8"),(0,l.kt)("p",null,"\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a ",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile.yml")," \u7684\u6587\u4ef6\u3002 ",(0,l.kt)("inlineCode",{parentName:"p"},"cmds")," \u5c5e\u6027\u5e94\u5305\u542b task \u7684\u547d\u4ee4\u3002 \u4e0b\u9762\u7684\u793a\u4f8b\u5141\u8bb8\u7f16\u8bd1 Go \u5e94\u7528\u7a0b\u5e8f\u5e76\u4f7f\u7528 ",(0,l.kt)("a",{parentName:"p",href:"https://esbuild.github.io/"},"esbuild")," \u5c06\u591a\u4e2a CSS \u6587\u4ef6\u5408\u5e76\u5e76\u7f29\u5c0f\u4e3a\u4e00\u4e2a\u6587\u4ef6\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n cmds:\n - go build -v -i main.go\n\n assets:\n cmds:\n - esbuild --bundle --minify css/index.css > public/bundle.css\n")),(0,l.kt)("p",null,"\u8fd0\u884c task \u5c31\u8fd9\u6837\u7b80\u5355\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"task assets build\n")),(0,l.kt)("p",null,"Task \u4f7f\u7528 ",(0,l.kt)("a",{parentName:"p",href:"https://mvdan.cc/sh/"},"mvdan.cc/sh"),"\uff0c\u4e00\u4e2a\u539f\u751f\u7684 Go sh \u89e3\u91ca\u5668\u3002 \u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199 sh/bash \u547d\u4ee4\uff0c\u5b83\u751a\u81f3\u53ef\u4ee5\u5728 Windows \u4e0a\u8fd0\u884c\uff0c\u800c ",(0,l.kt)("inlineCode",{parentName:"p"},"sh")," \u6216 ",(0,l.kt)("inlineCode",{parentName:"p"},"bash")," \u901a\u5e38\u4e0d\u53ef\u7528\u3002 \u8bf7\u8bb0\u4f4f\uff0c\u4efb\u4f55\u88ab\u8c03\u7528\u7684\u53ef\u6267\u884c\u6587\u4ef6\u90fd\u5fc5\u987b\u5728\u64cd\u4f5c\u7cfb\u7edf\u6216 PATH \u4e2d\u53ef\u7528\u3002"),(0,l.kt)("p",null,'\u5982\u679c\u4e0d\u4f20 task \u7684\u540d\u5b57\uff0c\u9ed8\u8ba4\u4f1a\u8c03\u7528 "default"\u3002'),(0,l.kt)("h2",{id:"\u652f\u6301\u7684\u6587\u4ef6\u540d\u79f0"},"\u652f\u6301\u7684\u6587\u4ef6\u540d\u79f0"),(0,l.kt)("p",null,"Task \u4f1a\u6309\u4ee5\u4e0b\u987a\u5e8f\u67e5\u627e\u914d\u7f6e\u6587\u4ef6:"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"Taskfile.yml"),(0,l.kt)("li",{parentName:"ul"},"Taskfile.yaml"),(0,l.kt)("li",{parentName:"ul"},"Taskfile.dist.yml"),(0,l.kt)("li",{parentName:"ul"},"Taskfile.dist.yaml")),(0,l.kt)("p",null,"\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},".dist")," \u53d8\u4f53\u7684\u76ee\u7684\u662f\u5141\u8bb8\u9879\u76ee\u6709\u4e00\u4e2a\u63d0\u4ea4\u7248\u672c (",(0,l.kt)("inlineCode",{parentName:"p"},".dist"),")\uff0c\u540c\u65f6\u4ecd\u7136\u5141\u8bb8\u4e2a\u4eba\u7528\u6237\u901a\u8fc7\u6dfb\u52a0\u989d\u5916\u7684 ",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile.yml"),"\uff08\u5c06\u5728 ",(0,l.kt)("inlineCode",{parentName:"p"},".gitignore")," \u4e0a\uff09\u6765\u8986\u76d6 Taskfile\u3002"),(0,l.kt)("h3",{id:"\u4ece\u5b50\u76ee\u5f55\u8fd0\u884c-taskfile"},"\u4ece\u5b50\u76ee\u5f55\u8fd0\u884c Taskfile"),(0,l.kt)("p",null,"\u5982\u679c\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u627e\u4e0d\u5230 Taskfile\uff0c\u5b83\u5c06\u6cbf\u7740\u6587\u4ef6\u6811\u5411\u4e0a\u67e5\u627e\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\uff08\u7c7b\u4f3c\u4e8e ",(0,l.kt)("inlineCode",{parentName:"p"},"git")," \u7684\u5de5\u4f5c\u65b9\u5f0f\uff09\u3002 \u5f53\u4ece\u8fd9\u6837\u7684\u5b50\u76ee\u5f55\u8fd0\u884c Task \u65f6\uff0c\u5b83\u7684\u884c\u4e3a\u5c31\u50cf\u4ece\u5305\u542b Taskfile \u7684\u76ee\u5f55\u8fd0\u884c\u5b83\u4e00\u6837\u3002"),(0,l.kt)("p",null,"\u60a8\u53ef\u4ee5\u5c06\u6b64\u529f\u80fd\u4e0e\u7279\u6b8a\u7684 ",(0,l.kt)("inlineCode",{parentName:"p"},"{{.USER_WORKING_DIR}}")," \u53d8\u91cf\u4e00\u8d77\u4f7f\u7528\u6765\u521b\u5efa\u4e00\u4e9b\u975e\u5e38\u6709\u7528\u7684\u53ef\u91cd\u7528 task\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5fae\u670d\u52a1\u76ee\u5f55\u7684 monorepo\uff0c\u4f60\u53ef\u4ee5 ",(0,l.kt)("inlineCode",{parentName:"p"},"cd")," \u8fdb\u5165\u4e00\u4e2a\u5fae\u670d\u52a1\u76ee\u5f55\u5e76\u8fd0\u884c\u4e00\u4e2a task \u547d\u4ee4\u6765\u542f\u52a8\u5b83\uff0c\u800c\u4e0d\u5fc5\u521b\u5efa\u591a\u4e2a task \u6216\u5177\u6709\u76f8\u540c\u5185\u5bb9\u7684 Taskfile\u3002 \u4f8b\u5982\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n up:\n dir: '{{.USER_WORKING_DIR}}'\n preconditions:\n - test -f docker-compose.yml\n cmds:\n - docker-compose up -d\n")),(0,l.kt)("p",null,"\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"cd <service>")," \u548c ",(0,l.kt)("inlineCode",{parentName:"p"},"task up"),"\uff0c\u53ea\u8981 ",(0,l.kt)("inlineCode",{parentName:"p"},"<service>")," \u76ee\u5f55\u5305\u542b ",(0,l.kt)("inlineCode",{parentName:"p"},"docker-compose.yml"),"\uff0c\u5c31\u4f1a\u542f\u52a8 Docker Compose\u3002"),(0,l.kt)("h3",{id:"\u8fd0\u884c\u5168\u5c40-taskfile"},"\u8fd0\u884c\u5168\u5c40 Taskfile"),(0,l.kt)("p",null,"\u5982\u679c\u60a8\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"--global"),"\uff08\u522b\u540d ",(0,l.kt)("inlineCode",{parentName:"p"},"-g"),"\uff09\u6807\u5fd7\u8c03\u7528 Task\uff0c\u5b83\u5c06\u67e5\u627e\u60a8\u7684 home \u76ee\u5f55\u800c\u4e0d\u662f\u60a8\u7684\u5de5\u4f5c\u76ee\u5f55\u3002 \u7b80\u800c\u8a00\u4e4b\uff0cTask \u5c06\u5728 ",(0,l.kt)("inlineCode",{parentName:"p"},"$HOME/Taskfile.yml")," \u6216 ",(0,l.kt)("inlineCode",{parentName:"p"},"$HOME/Taskfile.yaml")," \u8def\u5f84\u4e0a\u5bfb\u627e Taskfile\u3002"),(0,l.kt)("p",null,"\u8fd9\u5bf9\u4e8e\u60a8\u53ef\u4ee5\u5728\u7cfb\u7edf\u7684\u4efb\u4f55\u5730\u65b9\u8fd0\u884c\u7684\u81ea\u52a8\u5316\u5f88\u6709\u7528\uff01"),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"When running your global Taskfile with ",(0,l.kt)("inlineCode",{parentName:"p"},"-g"),", tasks will run on ",(0,l.kt)("inlineCode",{parentName:"p"},"$HOME")," by default, and not on your working directory!"),(0,l.kt)("p",{parentName:"admonition"},"As mentioned in the previous section, the ",(0,l.kt)("inlineCode",{parentName:"p"},"{{.USER_WORKING_DIR}}")," special variable can be very handy here to run stuff on the directory you're calling ",(0,l.kt)("inlineCode",{parentName:"p"},"task -g")," from."),(0,l.kt)("pre",{parentName:"admonition"},(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n from-home:\n cmds:\n - pwd\n\n from-working-directory:\n dir: '{{.USER_WORKING_DIR}}'\n cmds:\n - pwd\n"))),(0,l.kt)("h2",{id:"\u73af\u5883\u53d8\u91cf"},"\u73af\u5883\u53d8\u91cf"),(0,l.kt)("h3",{id:"task"},"Task"),(0,l.kt)("p",null,"\u4f60\u53ef\u4ee5\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"env")," \u7ed9\u6bcf\u4e2a task \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u73af\u5883\u53d8\u91cf:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n greet:\n cmds:\n - echo $GREETING\n env:\n GREETING: Hey, there!\n")),(0,l.kt)("p",null,"\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u53ef\u7528\u4e8e\u6240\u6709 task \u7684\u5168\u5c40\u73af\u5883\u53d8\u91cf\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nenv:\n GREETING: Hey, there!\n\ntasks:\n greet:\n cmds:\n - echo $GREETING\n")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},(0,l.kt)("inlineCode",{parentName:"p"},"env")," \u652f\u6301\u6269\u5c55\u548c\u68c0\u7d22 shell \u547d\u4ee4\u7684\u8f93\u51fa\uff0c\u5c31\u50cf\u53d8\u91cf\u4e00\u6837\uff0c\u5982\u60a8\u5728 ",(0,l.kt)("a",{parentName:"p",href:"#%E5%8F%98%E9%87%8F"},"\u53d8\u91cf")," \u90e8\u5206\u4e2d\u770b\u5230\u7684\u90a3\u6837\u3002")),(0,l.kt)("h3",{id:"env-\u6587\u4ef6"},".env \u6587\u4ef6"),(0,l.kt)("p",null,"\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"dotenv:")," \u8bbe\u7f6e\u8981\u6c42 tasks \u5305\u542b ",(0,l.kt)("inlineCode",{parentName:"p"},".env")," \u4e4b\u7c7b\u7684\u6587\u4ef6"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash",metastring:'title=".env"',title:'".env"'},"KEYNAME=VALUE\n")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash",metastring:'title="testing/.env"',title:'"testing/.env"'},"ENDPOINT=testing.com\n")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml",metastring:'title="Taskfile.yml"',title:'"Taskfile.yml"'},"version: '3'\n\nenv:\n ENV: testing\n\ndotenv: ['.env', '{{.ENV}}/.env.', '{{.HOME}}/.env']\n\ntasks:\n greet:\n cmds:\n - echo \"Using $KEYNAME and endpoint $ENDPOINT\"\n")),(0,l.kt)("p",null,"\u4e5f\u53ef\u4ee5\u5728 task \u7ea7\u522b\u6307\u5b9a .env \u6587\u4ef6\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nenv:\n ENV: testing\n\ntasks:\n greet:\n dotenv: ['.env', '{{.ENV}}/.env.', '{{.HOME}}/.env']\n cmds:\n - echo \"Using $KEYNAME and endpoint $ENDPOINT\"\n")),(0,l.kt)("p",null,"\u5728 task \u7ea7\u522b\u660e\u786e\u6307\u5b9a\u7684\u73af\u5883\u53d8\u91cf\u5c06\u8986\u76d6\u70b9\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nenv:\n ENV: testing\n\ntasks:\n greet:\n dotenv: ['.env', '{{.ENV}}/.env.', '{{.HOME}}/.env']\n env:\n KEYNAME: DIFFERENT_VALUE\n cmds:\n - echo \"Using $KEYNAME and endpoint $ENDPOINT\"\n")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"\u8bf7\u6ce8\u610f\uff0c\u60a8\u76ee\u524d\u65e0\u6cd5\u5728\u5305\u542b\u7684 Taskfile \u4e2d\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"dotenv")," \u952e\u3002")),(0,l.kt)("h2",{id:"\u5305\u542b\u5176\u4ed6-taskfile"},"\u5305\u542b\u5176\u4ed6 Taskfile"),(0,l.kt)("p",null,"\u5982\u679c\u8981\u5728\u4e0d\u540c\u9879\u76ee\uff08Taskfile\uff09\u4e4b\u95f4\u5171\u4eab\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5bfc\u5165\u673a\u5236\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"includes")," \u5173\u952e\u5b57\u5305\u542b\u5176\u4ed6\u4efb\u52a1\u6587\u4ef6\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n docs: ./documentation # will look for ./documentation/Taskfile.yml\n docker: ./DockerTasks.yml\n")),(0,l.kt)("p",null,"\u7ed9\u5b9a\u7684 Taskfile \u4e2d\u63cf\u8ff0\u7684\u4efb\u52a1\u5c06\u5728\u6307\u5b9a\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u63d0\u4f9b\u3002 \u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u4ece ",(0,l.kt)("inlineCode",{parentName:"p"},"documentation/Taskfile.yml")," \u8c03\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"task docs:serve")," \u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"serve")," task\uff0c\u6216\u8005\u4ece ",(0,l.kt)("inlineCode",{parentName:"p"},"DockerTasks.yml")," \u6587\u4ef6\u8c03\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"task docker:build")," \u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"build")," task\u3002"),(0,l.kt)("p",null,"\u76f8\u5bf9\u8def\u5f84\u662f\u76f8\u5bf9\u4e8e\u5305\u542b\u5305\u542b Taskfile \u7684\u76ee\u5f55\u89e3\u6790\u7684\u3002"),(0,l.kt)("h3",{id:"\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a-taskfile"},"\u64cd\u4f5c\u7cfb\u7edf\u7279\u5b9a Taskfile"),(0,l.kt)("p",null,"\u5728 ",(0,l.kt)("inlineCode",{parentName:"p"},"version: '2'")," \u4e2d\uff0ctask \u4f1a\u81ea\u52a8\u5c1d\u8bd5\u5f15\u5165 ",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile_{{OS}}.yml")," \u6587\u4ef6 \uff08\u4f8b\u5982",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile_windows.yml"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile_linux.yml")," \u6216 ",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile_darwin.yml"),"\uff09\u3002 \u4f46\u662f\u56e0\u4e3a\u8fc7\u4e8e\u9690\u6666\uff0c\u5728\u7248\u672c 3 \u4e2d\u88ab\u79fb\u9664\u4e86\uff0c \u5728\u7248\u672c 3 \u53ef\u4ee5\u901a\u8fc7\u660e\u786e\u7684\u5f15\u7528\u6765\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n build: ./Taskfile_{{OS}}.yml\n")),(0,l.kt)("h3",{id:"\u5305\u542b-taskfile-\u7684\u76ee\u5f55"},"\u5305\u542b Taskfile \u7684\u76ee\u5f55"),(0,l.kt)("p",null,"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5305\u542b\u7684 Taskfile \u7684 task \u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u8fd0\u884c\uff0c\u5373\u4f7f Taskfile \u5728\u53e6\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u66ff\u4ee3\u8bed\u6cd5\u5f3a\u5236\u5176 task \u5728\u53e6\u4e00\u4e2a\u76ee\u5f55\u4e2d\u8fd0\u884c\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n docs:\n taskfile: ./docs/Taskfile.yml\n dir: ./docs\n")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"\u5305\u542b\u7684 Taskfile \u5fc5\u987b\u4f7f\u7528\u4e0e\u4e3b Taskfile \u4f7f\u7528\u7684\u76f8\u540c\u89c4\u5219\u7248\u672c\u3002")),(0,l.kt)("h3",{id:"\u53ef\u9009-includes"},"\u53ef\u9009 includes"),(0,l.kt)("p",null,"\u5982\u679c\u5305\u542b\u6587\u4ef6\u4e22\u5931\uff0c\u6807\u8bb0\u4e3a\u53ef\u9009\u7684\u5305\u542b\u5c06\u5141\u8bb8 task \u7ee7\u7eed\u6b63\u5e38\u6267\u884c\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n tests:\n taskfile: ./tests/Taskfile.yml\n optional: true\n\ntasks:\n greet:\n cmds:\n - echo \"This command can still be successfully executed if\n ./tests/Taskfile.yml does not exist\"\n")),(0,l.kt)("h3",{id:"\u5185\u90e8-includes"},"\u5185\u90e8 includes"),(0,l.kt)("p",null,"\u6807\u8bb0\u4e3a internal \u7684\u5305\u542b\u4f1a\u5c06\u5305\u542b\u6587\u4ef6\u7684\u6240\u6709 task \u4e5f\u8bbe\u7f6e\u4e3a\u5185\u90e8 task\uff08\u8bf7\u53c2\u9605\u4e0b\u9762\u7684 ",(0,l.kt)("a",{parentName:"p",href:"#%E5%86%85%E9%83%A8-tasks"},"\u5185\u90e8-tasks")," \u90e8\u5206\uff09\u3002 \u8fd9\u5728\u5305\u542b\u4e0d\u6253\u7b97\u7531\u7528\u6237\u76f4\u63a5\u4f7f\u7528\u7684\u5b9e\u7528\u7a0b\u5e8f\u4efb\u52a1\u65f6\u5f88\u6709\u7528\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n tests:\n taskfile: ./taskfiles/Utils.yml\n internal: true\n")),(0,l.kt)("h3",{id:"\u5305\u542b\u7684-taskfile-\u7684\u53d8\u91cf"},"\u5305\u542b\u7684 Taskfile \u7684\u53d8\u91cf"),(0,l.kt)("p",null,"\u60a8\u8fd8\u53ef\u4ee5\u5728\u5305\u542b Taskfile \u65f6\u6307\u5b9a\u53d8\u91cf\u3002 \u8fd9\u5bf9\u4e8e\u62e5\u6709\u53ef\u4ee5\u8c03\u6574\u751a\u81f3\u591a\u6b21\u5305\u542b\u7684\u53ef\u91cd\u7528 Taskfile \u53ef\u80fd\u5f88\u6709\u7528\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n backend:\n taskfile: ./taskfiles/Docker.yml\n vars:\n DOCKER_IMAGE: backend_image\n\n frontend:\n taskfile: ./taskfiles/Docker.yml\n vars:\n DOCKER_IMAGE: frontend_image\n")),(0,l.kt)("h3",{id:"\u547d\u540d\u7a7a\u95f4\u522b\u540d"},"\u547d\u540d\u7a7a\u95f4\u522b\u540d"),(0,l.kt)("p",null,"\u5305\u542b Taskfile \u65f6\uff0c\u60a8\u53ef\u4ee5\u4e3a\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e00\u4e2a ",(0,l.kt)("inlineCode",{parentName:"p"},"aliases")," \u5217\u8868\u3002 \u8fd9\u4e0e ",(0,l.kt)("a",{parentName:"p",href:"#task-%E5%88%AB%E5%90%8D"},"task \u522b\u540d")," \u7684\u5de5\u4f5c\u65b9\u5f0f\u76f8\u540c\uff0c\u53ef\u4ee5\u4e00\u8d77\u4f7f\u7528\u6765\u521b\u5efa\u66f4\u77ed\u4e14\u66f4\u6613\u4e8e\u952e\u5165\u7684\u547d\u4ee4\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nincludes:\n generate:\n taskfile: ./taskfiles/Generate.yml\n aliases: [gen]\n")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"Vars declared in the included Taskfile have preference over the variables in the including Taskfile! If you want a variable in an included Taskfile to be overridable, use the ",(0,l.kt)("a",{parentName:"p",href:"https://go-task.github.io/slim-sprig/defaults.html"},"default function"),": ",(0,l.kt)("inlineCode",{parentName:"p"},"MY_VAR: '{{.MY_VAR | default \"my-default-value\"}}'"),".")),(0,l.kt)("h2",{id:"\u5185\u90e8-tasks"},"\u5185\u90e8 tasks"),(0,l.kt)("p",null,"Internal tasks are tasks that cannot be called directly by the user. They will not appear in the output when running ",(0,l.kt)("inlineCode",{parentName:"p"},"task --list|--list-all"),". Other tasks may call internal tasks in the usual way. This is useful for creating reusable, function-like tasks that have no useful purpose on the command line."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build-image-1:\n cmds:\n - task: build-image\n vars:\n DOCKER_IMAGE: image-1\n\n build-image:\n internal: true\n cmds:\n - docker build -t {{.DOCKER_IMAGE}} .\n")),(0,l.kt)("h2",{id:"task-\u76ee\u5f55"},"Task \u76ee\u5f55"),(0,l.kt)("p",null,"\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ctasks \u5c06\u5728 Taskfile \u6240\u5728\u7684\u76ee\u5f55\u4e2d\u6267\u884c\u3002 \u4f46\u662f\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba9 task \u5728\u53e6\u4e00\u4e2a\u76ee\u5f55\u4e2d\u8fd0\u884c\uff0c\u6307\u5b9a ",(0,l.kt)("inlineCode",{parentName:"p"},"dir"),"\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n serve:\n dir: public/www\n cmds:\n # run http server\n - caddy\n")),(0,l.kt)("p",null,"\u5982\u679c\u8be5\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c",(0,l.kt)("inlineCode",{parentName:"p"},"task")," \u4f1a\u521b\u5efa\u5b83\u3002"),(0,l.kt)("h2",{id:"task-\u4f9d\u8d56"},"Task \u4f9d\u8d56"),(0,l.kt)("blockquote",null,(0,l.kt)("p",{parentName:"blockquote"},"\u4f9d\u8d56\u9879\u5e76\u884c\u8fd0\u884c\uff0c\u56e0\u6b64\u4e00\u9879 task \u7684\u4f9d\u8d56\u9879\u4e0d\u5e94\u76f8\u4e92\u4f9d\u8d56\u3002 \u5982\u679c\u60a8\u60f3\u5f3a\u5236\u4efb\u52a1\u987a\u5e8f\u8fd0\u884c\uff0c\u8bf7\u67e5\u770b\u4e0b\u9762\u7684 ",(0,l.kt)("a",{parentName:"p",href:"#%E8%B0%83%E7%94%A8%E5%8F%A6%E4%B8%80%E4%B8%AA-task"},"\u8c03\u7528\u53e6\u4e00\u4e2a task")," \u90e8\u5206\u3002")),(0,l.kt)("p",null,"\u60a8\u53ef\u80fd\u6709\u4f9d\u8d56\u4e8e\u5176\u5b83\u7684 task\u3002 Just pointing them on ",(0,l.kt)("inlineCode",{parentName:"p"},"deps")," will make them run automatically before running the parent task:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n deps: [assets]\n cmds:\n - go build -v -i main.go\n\n assets:\n cmds:\n - esbuild --bundle --minify css/index.css > public/bundle.css\n")),(0,l.kt)("p",null,"\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u60a8\u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"task build"),"\uff0c",(0,l.kt)("inlineCode",{parentName:"p"},"assets")," \u5c06\u59cb\u7ec8\u5728 ",(0,l.kt)("inlineCode",{parentName:"p"},"build")," \u4e4b\u524d\u8fd0\u884c\u3002"),(0,l.kt)("p",null,"\u4e00\u4e2a task \u53ea\u80fd\u6709\u4f9d\u8d56\u5173\u7cfb\uff0c\u6ca1\u6709\u547d\u4ee4\u6765\u5c06 task \u7ec4\u5408\u5728\u4e00\u8d77\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n assets:\n deps: [js, css]\n\n js:\n cmds:\n - esbuild --bundle --minify js/index.js > public/bundle.js\n\n css:\n cmds:\n - esbuild --bundle --minify css/index.css > public/bundle.css\n")),(0,l.kt)("p",null,"\u5982\u679c\u6709\u591a\u4e2a\u4f9d\u8d56\u9879\uff0c\u5b83\u4eec\u603b\u662f\u5e76\u884c\u8fd0\u884c\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002"),(0,l.kt)("admonition",{type:"tip"},(0,l.kt)("p",{parentName:"admonition"},"You can also make the tasks given by the command line run in parallel by using the ",(0,l.kt)("inlineCode",{parentName:"p"},"--parallel")," flag (alias ",(0,l.kt)("inlineCode",{parentName:"p"},"-p"),"). \u4f8b\u5982: ",(0,l.kt)("inlineCode",{parentName:"p"},"task --parallel js css"),"\u3002")),(0,l.kt)("p",null,"If you want to pass information to dependencies, you can do that the same manner as you would to ",(0,l.kt)("a",{parentName:"p",href:"#calling-another-task"},"call another task"),":"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n default:\n deps:\n - task: echo_sth\n vars: { TEXT: 'before 1' }\n - task: echo_sth\n vars: { TEXT: 'before 2' }\n cmds:\n - echo \"after\"\n\n echo_sth:\n cmds:\n - echo {{.TEXT}}\n")),(0,l.kt)("h2",{id:"\u5e73\u53f0\u7279\u5b9a\u7684-tasks-\u548c-cmds"},"\u5e73\u53f0\u7279\u5b9a\u7684 tasks \u548c cmds"),(0,l.kt)("p",null,"If you want to restrict the running of tasks to explicit platforms, this can be achieved using the ",(0,l.kt)("inlineCode",{parentName:"p"},"platforms:")," key. Tasks can be restricted to a specific OS, architecture or a combination of both. On a mismatch, the task or command will be skipped, and no error will be thrown."),(0,l.kt)("p",null,"\u5141\u8bb8\u4f5c\u4e3a OS \u6216 Arch \u7684\u503c\u662f\u6709\u6548\u7684 ",(0,l.kt)("inlineCode",{parentName:"p"},"GOOS")," \u548c ",(0,l.kt)("inlineCode",{parentName:"p"},"GOARCH")," \u503c\uff0c\u6b63\u5982 ",(0,l.kt)("a",{parentName:"p",href:"https://github.com/golang/go/blob/master/src/go/build/syslist.go"},"\u6b64\u5904")," \u7684 Go \u8bed\u8a00\u6240\u5b9a\u4e49\u7684\u90a3\u6837\u3002"),(0,l.kt)("p",null,"The ",(0,l.kt)("inlineCode",{parentName:"p"},"build-windows")," task below will run only on Windows, and on any architecture:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build-windows:\n platforms: [windows]\n cmds:\n - echo 'Running command on Windows'\n")),(0,l.kt)("p",null,"\u8fd9\u53ef\u4ee5\u9650\u5236\u4e3a\u7279\u5b9a\u7684\u67b6\u6784\uff0c\u5982\u4e0b\u6240\u793a\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build-windows-amd64:\n platforms: [windows/amd64]\n cmds:\n - echo 'Running command on Windows (amd64)'\n")),(0,l.kt)("p",null,"\u4e5f\u53ef\u4ee5\u5c06 task \u9650\u5236\u5728\u7279\u5b9a\u7684\u67b6\u6784\u4e2d\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build-amd64:\n platforms: [amd64]\n cmds:\n - echo 'Running command on amd64'\n")),(0,l.kt)("p",null,"\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u5e73\u53f0\uff0c\u5982\u4e0b\u6240\u793a\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n platforms: [windows/amd64, darwin]\n cmds:\n - echo 'Running command on Windows (amd64) and macOS'\n")),(0,l.kt)("p",null,"\u4e2a\u522b\u547d\u4ee4\u4e5f\u53ef\u4ee5\u9650\u5236\u5728\u7279\u5b9a\u5e73\u53f0\u4e0a\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n cmds:\n - cmd: echo 'Running command on Windows (amd64) and macOS'\n platforms: [windows/amd64, darwin]\n - cmd: echo 'Running on all platforms'\n")),(0,l.kt)("h2",{id:"\u8c03\u7528\u53e6\u4e00\u4e2a-task"},"\u8c03\u7528\u53e6\u4e00\u4e2a task"),(0,l.kt)("p",null,"\u5f53\u4e00\u4e2a task \u6709\u5f88\u591a\u4f9d\u8d56\u65f6\uff0c\u5b83\u4eec\u662f\u5e76\u53d1\u6267\u884c\u7684\u3002 \u8fd9\u901a\u5e38\u4f1a\u5bfc\u81f4\u66f4\u5feb\u7684\u6784\u5efa\u7ba1\u9053\u3002 However, in some situations, you may need to call other tasks serially. \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},'version: \'3\'\n\ntasks:\n main-task:\n cmds:\n - task: task-to-be-called\n - task: another-task\n - echo "Both done"\n\n task-to-be-called:\n cmds:\n - echo "Task to be called"\n\n another-task:\n cmds:\n - echo "Another task"\n')),(0,l.kt)("p",null,"\u5728\u88ab\u8c03\u7528 task \u4e2d\u8986\u76d6\u53d8\u91cf\u5c31\u50cf\u901a\u77e5 ",(0,l.kt)("inlineCode",{parentName:"p"},"vars")," \u5c5e\u6027\u4e00\u6837\u7b80\u5355\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n greet:\n vars:\n RECIPIENT: '{{default \"World\" .RECIPIENT}}'\n cmds:\n - echo \"Hello, {{.RECIPIENT}}!\"\n\n greet-pessimistically:\n cmds:\n - task: greet\n vars: { RECIPIENT: 'Cruel World' }\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"deps")," \u4e5f\u652f\u6301\u4e0a\u8ff0\u8bed\u6cd5\u3002"),(0,l.kt)("admonition",{type:"tip"},(0,l.kt)("p",{parentName:"admonition"},"\u6ce8\u610f\uff1a\u5982\u679c\u60a8\u60f3\u4ece ",(0,l.kt)("a",{parentName:"p",href:"#%E5%8C%85%E5%90%AB%E5%85%B6%E4%BB%96-taskfile"},"\u5305\u542b\u7684 Taskfile")," \u4e2d\u8c03\u7528\u5728\u6839 Taskfile \u4e2d\u58f0\u660e\u7684 task\uff0c\u8bf7\u50cf\u8fd9\u6837\u6dfb\u52a0 ",(0,l.kt)("inlineCode",{parentName:"p"},":")," \u524d\u7f00\uff1a",(0,l.kt)("inlineCode",{parentName:"p"},"task: :task-name"),"\u3002")),(0,l.kt)("h2",{id:"\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5de5\u4f5c"},"\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5de5\u4f5c"),(0,l.kt)("h3",{id:"\u901a\u8fc7\u6307\u7eb9\u8bc6\u522b\u672c\u5730\u751f\u6210\u7684\u6587\u4ef6\u53ca\u5176\u6765\u6e90"},"\u901a\u8fc7\u6307\u7eb9\u8bc6\u522b\u672c\u5730\u751f\u6210\u7684\u6587\u4ef6\u53ca\u5176\u6765\u6e90"),(0,l.kt)("p",null,"\u5982\u679c\u4e00\u4e2a task \u751f\u6210\u4e86\u4e00\u4e9b\u4e1c\u897f\uff0c\u4f60\u53ef\u4ee5\u901a\u77e5 task \u6e90\u548c\u751f\u6210\u7684\u6587\u4ef6\uff0c\u8fd9\u6837 task \u5c31\u4f1a\u5728\u4e0d\u9700\u8981\u7684\u65f6\u5019\u963b\u6b62\u8fd0\u884c\u5b83\u4eec\u3002"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n deps: [js, css]\n cmds:\n - go build -v -i main.go\n\n js:\n cmds:\n - esbuild --bundle --minify js/index.js > public/bundle.js\n sources:\n - src/js/**/*.js\n generates:\n - public/bundle.js\n\n css:\n cmds:\n - esbuild --bundle --minify css/index.css > public/bundle.css\n sources:\n - src/css/**/*.css\n generates:\n - public/bundle.css\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"sources")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"generates")," can be files or file patterns. When given, Task will compare the checksum of the source files to determine if it's necessary to run the task. If not, it will just print a message like ",(0,l.kt)("inlineCode",{parentName:"p"},'Task "js" is up to date'),"."),(0,l.kt)("p",null,"If you prefer this check to be made by the modification timestamp of the files, instead of its checksum (content), just set the ",(0,l.kt)("inlineCode",{parentName:"p"},"method")," property to ",(0,l.kt)("inlineCode",{parentName:"p"},"timestamp"),"."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n cmds:\n - go build .\n sources:\n - ./*.go\n generates:\n - app{{exeExt}}\n method: timestamp\n")),(0,l.kt)("p",null,"\u5728\u9700\u8981\u66f4\u5927\u7075\u6d3b\u6027\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"status")," \u5173\u952e\u5b57\u3002 \u60a8\u751a\u81f3\u53ef\u4ee5\u5c06\u4e24\u8005\u7ed3\u5408\u8d77\u6765\u3002 \u6709\u5173\u793a\u4f8b\uff0c\u8bf7\u53c2\u9605 ",(0,l.kt)("a",{parentName:"p",href:"#%E4%BD%BF%E7%94%A8%E7%A8%8B%E5%BA%8F%E6%A3%80%E6%9F%A5%E6%9D%A5%E8%A1%A8%E7%A4%BA%E4%BB%BB%E5%8A%A1%E6%98%AF%E6%9C%80%E6%96%B0%E7%9A%84"},"\u72b6\u6001")," \u6587\u6863\u3002"),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"By default, task stores checksums on a local ",(0,l.kt)("inlineCode",{parentName:"p"},".task")," directory in the project's directory. Most of the time, you'll want to have this directory on ",(0,l.kt)("inlineCode",{parentName:"p"},".gitignore")," (or equivalent) so it isn't committed. (If you have a task for code generation that is committed it may make sense to commit the checksum of that task as well, though)."),(0,l.kt)("p",{parentName:"admonition"},"If you want these files to be stored in another directory, you can set a ",(0,l.kt)("inlineCode",{parentName:"p"},"TASK_TEMP_DIR")," environment variable in your machine. It can contain a relative path like ",(0,l.kt)("inlineCode",{parentName:"p"},"tmp/task")," that will be interpreted as relative to the project directory, or an absolute or home path like ",(0,l.kt)("inlineCode",{parentName:"p"},"/tmp/.task")," or ",(0,l.kt)("inlineCode",{parentName:"p"},"~/.task")," (subdirectories will be created for each project)."),(0,l.kt)("pre",{parentName:"admonition"},(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"export TASK_TEMP_DIR='~/.task'\n"))),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"Each task has only one checksum stored for its ",(0,l.kt)("inlineCode",{parentName:"p"},"sources"),". If you want to distinguish a task by any of its input variables, you can add those variables as part of the task's label, and it will be considered a different task."),(0,l.kt)("p",{parentName:"admonition"},"This is useful if you want to run a task once for each distinct set of inputs until the sources actually change. For example, if the sources depend on the value of a variable, or you if you want the task to rerun if some arguments change even if the source has not.")),(0,l.kt)("admonition",{type:"tip"},(0,l.kt)("p",{parentName:"admonition"},"\u5c06 method \u8bbe\u7f6e\u4e3a ",(0,l.kt)("inlineCode",{parentName:"p"},"none")," \u4f1a\u8df3\u8fc7\u4efb\u4f55\u9a8c\u8bc1\u5e76\u59cb\u7ec8\u8fd0\u884c\u4efb\u52a1\u3002")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"For the ",(0,l.kt)("inlineCode",{parentName:"p"},"checksum")," (default) or ",(0,l.kt)("inlineCode",{parentName:"p"},"timestamp")," method to work, it is only necessary to inform the source files. When the ",(0,l.kt)("inlineCode",{parentName:"p"},"timestamp")," method is used, the last time of the running the task is considered as a generate.")),(0,l.kt)("h3",{id:"using-programmatic-checks-to-indicate-a-task-is-up-to-date"},"Using programmatic checks to indicate a task is up to date"),(0,l.kt)("p",null,"Alternatively, you can inform a sequence of tests as ",(0,l.kt)("inlineCode",{parentName:"p"},"status"),". If no error is returned (exit status 0), the task is considered up-to-date:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n generate-files:\n cmds:\n - mkdir directory\n - touch directory/file1.txt\n - touch directory/file2.txt\n # test existence of files\n status:\n - test -d directory\n - test -f directory/file1.txt\n - test -f directory/file2.txt\n")),(0,l.kt)("p",null,"Normally, you would use ",(0,l.kt)("inlineCode",{parentName:"p"},"sources")," in combination with ",(0,l.kt)("inlineCode",{parentName:"p"},"generates")," - but for tasks that generate remote artifacts (Docker images, deploys, CD releases) the checksum source and timestamps require either access to the artifact or for an out-of-band refresh of the ",(0,l.kt)("inlineCode",{parentName:"p"},".checksum")," fingerprint file."),(0,l.kt)("p",null,"Two special variables ",(0,l.kt)("inlineCode",{parentName:"p"},"{{.CHECKSUM}}")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"{{.TIMESTAMP}}")," are available for interpolation within ",(0,l.kt)("inlineCode",{parentName:"p"},"status")," commands, depending on the method assigned to fingerprint the sources. Only ",(0,l.kt)("inlineCode",{parentName:"p"},"source")," globs are fingerprinted."),(0,l.kt)("p",null,"Note that the ",(0,l.kt)("inlineCode",{parentName:"p"},"{{.TIMESTAMP}}"),' variable is a "live" Go ',(0,l.kt)("inlineCode",{parentName:"p"},"time.Time")," struct, and can be formatted using any of the methods that ",(0,l.kt)("inlineCode",{parentName:"p"},"time.Time")," responds to."),(0,l.kt)("p",null,"See ",(0,l.kt)("a",{parentName:"p",href:"https://golang.org/pkg/time/"},"the Go Time documentation")," for more information."),(0,l.kt)("p",null,"\u5982\u679c\u4f60\u60f3\u5f3a\u5236\u4efb\u52a1\u8fd0\u884c\uff0c\u5373\u4f7f\u662f\u6700\u65b0\u7684\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"p"},"--force")," \u6216 ",(0,l.kt)("inlineCode",{parentName:"p"},"-f"),"\u3002"),(0,l.kt)("p",null,"Also, ",(0,l.kt)("inlineCode",{parentName:"p"},"task --status [tasks]...")," will exit with a non-zero exit code if any of the tasks are not up-to-date."),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"status")," can be combined with the ",(0,l.kt)("a",{parentName:"p",href:"#by-fingerprinting-locally-generated-files-and-their-sources"},"fingerprinting")," to have a task run if either the the source/generated artifacts changes, or the programmatic check fails:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:prod:\n desc: Build for production usage.\n cmds:\n - composer install\n # Run this task if source files changes.\n sources:\n - composer.json\n - composer.lock\n generates:\n - ./vendor/composer/installed.json\n - ./vendor/autoload.php\n # But also run the task if the last build was not a production build.\n status:\n - grep -q '\"dev\": false' ./vendor/composer/installed.json\n")),(0,l.kt)("h3",{id:"\u4f7f\u7528\u7a0b\u5e8f\u68c0\u67e5\u53d6\u6d88\u4efb\u52a1\u53ca\u5176\u4f9d\u8d56\u9879\u7684\u6267\u884c"},"\u4f7f\u7528\u7a0b\u5e8f\u68c0\u67e5\u53d6\u6d88\u4efb\u52a1\u53ca\u5176\u4f9d\u8d56\u9879\u7684\u6267\u884c"),(0,l.kt)("p",null,"In addition to ",(0,l.kt)("inlineCode",{parentName:"p"},"status")," checks, ",(0,l.kt)("inlineCode",{parentName:"p"},"preconditions")," checks are the logical inverse of ",(0,l.kt)("inlineCode",{parentName:"p"},"status")," checks. That is, if you need a certain set of conditions to be ",(0,l.kt)("em",{parentName:"p"},"true")," you can use the ",(0,l.kt)("inlineCode",{parentName:"p"},"preconditions")," stanza. ",(0,l.kt)("inlineCode",{parentName:"p"},"preconditions")," are similar to ",(0,l.kt)("inlineCode",{parentName:"p"},"status")," lines, except they support ",(0,l.kt)("inlineCode",{parentName:"p"},"sh")," expansion, and they SHOULD all return 0."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n generate-files:\n cmds:\n - mkdir directory\n - touch directory/file1.txt\n - touch directory/file2.txt\n # test existence of files\n preconditions:\n - test -f .env\n - sh: '[ 1 = 0 ]'\n msg: \"One doesn't equal Zero, Halting\"\n")),(0,l.kt)("p",null,"Preconditions can set specific failure messages that can tell a user what steps to take using the ",(0,l.kt)("inlineCode",{parentName:"p"},"msg")," field."),(0,l.kt)("p",null,"If a task has a dependency on a sub-task with a precondition, and that precondition is not met - the calling task will fail. Note that a task executed with a failing precondition will not run unless ",(0,l.kt)("inlineCode",{parentName:"p"},"--force")," is given."),(0,l.kt)("p",null,"Unlike ",(0,l.kt)("inlineCode",{parentName:"p"},"status"),", which will skip a task if it is up to date and continue executing tasks that depend on it, a ",(0,l.kt)("inlineCode",{parentName:"p"},"precondition")," will fail a task, along with any other tasks that depend on it."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n task-will-fail:\n preconditions:\n - sh: 'exit 1'\n\n task-will-also-fail:\n deps:\n - task-will-fail\n\n task-will-still-fail:\n cmds:\n - task: task-will-fail\n - echo \"I will not run\"\n")),(0,l.kt)("h3",{id:"\u5728\u4efb\u52a1\u8fd0\u884c\u65f6\u9650\u5236"},"\u5728\u4efb\u52a1\u8fd0\u884c\u65f6\u9650\u5236"),(0,l.kt)("p",null,"If a task executed by multiple ",(0,l.kt)("inlineCode",{parentName:"p"},"cmds")," or multiple ",(0,l.kt)("inlineCode",{parentName:"p"},"deps")," you can control when it is executed using ",(0,l.kt)("inlineCode",{parentName:"p"},"run"),". ",(0,l.kt)("inlineCode",{parentName:"p"},"run")," can also be set at the root of the Taskfile to change the behavior of all the tasks unless explicitly overridden."),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"run")," \u652f\u6301\u7684\u503c\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"always")," (default) always attempt to invoke the task regardless of the number of previous executions"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"once")," only invoke this task once regardless of the number of references"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"when_changed")," only invokes the task once for each unique set of variables passed into the task")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n default:\n cmds:\n - task: generate-file\n vars: { CONTENT: '1' }\n - task: generate-file\n vars: { CONTENT: '2' }\n - task: generate-file\n vars: { CONTENT: '2' }\n\n generate-file:\n run: when_changed\n deps:\n - install-deps\n cmds:\n - echo {{.CONTENT}}\n\n install-deps:\n run: once\n cmds:\n - sleep 5 # long operation like installing packages\n")),(0,l.kt)("h2",{id:"\u53d8\u91cf"},"\u53d8\u91cf"),(0,l.kt)("p",null,"\u5728\u8fdb\u884c\u53d8\u91cf\u63d2\u503c\u65f6\uff0cTask \u5c06\u67e5\u627e\u4ee5\u4e0b\u5185\u5bb9\u3002 They are listed below in order of importance (i.e. most important first):"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"Variables declared in the task definition"),(0,l.kt)("li",{parentName:"ul"},"Variables given while calling a task from another (See ",(0,l.kt)("a",{parentName:"li",href:"#calling-another-task"},"Calling another task")," above)"),(0,l.kt)("li",{parentName:"ul"},"Variables of the ",(0,l.kt)("a",{parentName:"li",href:"#including-other-taskfiles"},"included Taskfile")," (when the task is included)"),(0,l.kt)("li",{parentName:"ul"},"Variables of the ",(0,l.kt)("a",{parentName:"li",href:"#vars-of-included-taskfiles"},"inclusion of the Taskfile")," (when the task is included)"),(0,l.kt)("li",{parentName:"ul"},"Global variables (those declared in the ",(0,l.kt)("inlineCode",{parentName:"li"},"vars:")," option in the Taskfile)"),(0,l.kt)("li",{parentName:"ul"},"Environment variables")),(0,l.kt)("p",null,"\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u4f20\u8f93\u53c2\u6570\u7684\u793a\u4f8b\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"$ TASK_VARIABLE=a-value task do-something\n")),(0,l.kt)("admonition",{type:"tip"},(0,l.kt)("p",{parentName:"admonition"},"A special variable ",(0,l.kt)("inlineCode",{parentName:"p"},".TASK")," is always available containing the task name.")),(0,l.kt)("p",null,"Since some shells do not support the above syntax to set environment variables (Windows) tasks also accept a similar style when not at the beginning of the command."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},'$ task write-file FILE=file.txt "CONTENT=Hello, World!" print "MESSAGE=All done!"\n')),(0,l.kt)("p",null,"Example of locally declared vars:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n print-var:\n cmds:\n - echo \"{{.VAR}}\"\n vars:\n VAR: Hello!\n")),(0,l.kt)("p",null,"Example of global vars in a ",(0,l.kt)("inlineCode",{parentName:"p"},"Taskfile.yml"),":"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nvars:\n GREETING: Hello from Taskfile!\n\ntasks:\n greet:\n cmds:\n - echo \"{{.GREETING}}\"\n")),(0,l.kt)("h3",{id:"\u52a8\u6001\u53d8\u91cf"},"\u52a8\u6001\u53d8\u91cf"),(0,l.kt)("p",null,"The below syntax (",(0,l.kt)("inlineCode",{parentName:"p"},"sh:")," prop in a variable) is considered a dynamic variable. The value will be treated as a command and the output assigned. If there are one or more trailing newlines, the last newline will be trimmed."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n cmds:\n - go build -ldflags=\"-X main.Version={{.GIT_COMMIT}}\" main.go\n vars:\n GIT_COMMIT:\n sh: git log -n 1 --format=%h\n")),(0,l.kt)("p",null,"This works for all types of variables."),(0,l.kt)("h2",{id:"\u5c06-cli-\u53c2\u6570\u8f6c\u53d1\u5230-cmds"},"\u5c06 CLI \u53c2\u6570\u8f6c\u53d1\u5230 cmds"),(0,l.kt)("p",null,"If ",(0,l.kt)("inlineCode",{parentName:"p"},"--")," is given in the CLI, all following parameters are added to a special ",(0,l.kt)("inlineCode",{parentName:"p"},".CLI_ARGS")," variable. This is useful to forward arguments to another command."),(0,l.kt)("p",null,"The below example will run ",(0,l.kt)("inlineCode",{parentName:"p"},"yarn install"),"."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"$ task yarn -- install\n")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n yarn:\n cmds:\n - yarn {{.CLI_ARGS}}\n")),(0,l.kt)("h2",{id:"\u4f7f\u7528-defer-\u505a-task-\u6e05\u7406"},"\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"h2"},"defer")," \u505a task \u6e05\u7406"),(0,l.kt)("p",null,"With the ",(0,l.kt)("inlineCode",{parentName:"p"},"defer")," keyword, it's possible to schedule cleanup to be run once the task finishes. The difference with just putting it as the last command is that this command will run even when the task fails."),(0,l.kt)("p",null,"In the example below, ",(0,l.kt)("inlineCode",{parentName:"p"},"rm -rf tmpdir/")," will run even if the third command fails:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n default:\n cmds:\n - mkdir -p tmpdir/\n - defer: rm -rf tmpdir/\n - echo 'Do work on tmpdir/'\n")),(0,l.kt)("p",null,"If you want to move the cleanup command into another task, that is possible as well:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n default:\n cmds:\n - mkdir -p tmpdir/\n - defer: { task: cleanup }\n - echo 'Do work on tmpdir/'\n\n cleanup: rm -rf tmpdir/\n")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"Due to the nature of how the ",(0,l.kt)("a",{parentName:"p",href:"https://go.dev/tour/flowcontrol/13"},"Go's own ",(0,l.kt)("inlineCode",{parentName:"a"},"defer")," work"),", the deferred commands are executed in the reverse order if you schedule multiple of them.")),(0,l.kt)("h2",{id:"go-\u7684\u6a21\u677f\u5f15\u64ce"},"Go \u7684\u6a21\u677f\u5f15\u64ce"),(0,l.kt)("p",null,"Task parse commands as ",(0,l.kt)("a",{parentName:"p",href:"https://golang.org/pkg/text/template/"},"Go's template engine")," before executing them. \u53ef\u4ee5\u901a\u8fc7\u70b9\u8bed\u6cd5 (",(0,l.kt)("inlineCode",{parentName:"p"},".VARNAME"),") \u8bbf\u95ee\u53d8\u91cf\u3002"),(0,l.kt)("p",null,"All functions by the Go's ",(0,l.kt)("a",{parentName:"p",href:"https://go-task.github.io/slim-sprig/"},"slim-sprig lib")," are available. The following example gets the current date in a given format:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n print-date:\n cmds:\n - echo {{now | date \"2006-01-02\"}}\n")),(0,l.kt)("p",null,"Task \u8fd8\u589e\u52a0\u4e86\u4ee5\u4e0b\u529f\u80fd\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"OS"),': Returns the operating system. Possible values are "windows", "linux", "darwin" (macOS) and "freebsd".'),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"ARCH"),': return the architecture Task was compiled to: "386", "amd64", "arm" or "s390x".'),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"splitLines"),": Splits Unix (\\n) and Windows (\\r\\n) styled newlines."),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"catLines"),": Replaces Unix (\\n) and Windows (\\r\\n) styled newlines with a space."),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"toSlash"),": Does nothing on Unix, but on Windows converts a string from ",(0,l.kt)("inlineCode",{parentName:"li"},"\\")," path format to ",(0,l.kt)("inlineCode",{parentName:"li"},"/"),"."),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"fromSlash"),": Opposite of ",(0,l.kt)("inlineCode",{parentName:"li"},"toSlash"),". Does nothing on Unix, but on Windows converts a string from ",(0,l.kt)("inlineCode",{parentName:"li"},"/")," path format to ",(0,l.kt)("inlineCode",{parentName:"li"},"\\"),"."),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"exeExt"),": Returns the right executable extension for the current OS (",(0,l.kt)("inlineCode",{parentName:"li"},'".exe"')," for Windows, ",(0,l.kt)("inlineCode",{parentName:"li"},'""')," for others)."),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"shellQuote"),": Quotes a string to make it safe for use in shell scripts. Task uses ",(0,l.kt)("a",{parentName:"li",href:"https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/syntax#Quote"},"this Go function")," for this. The Bash dialect is assumed."),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"splitArgs"),": Splits a string as if it were a command's arguments. Task uses ",(0,l.kt)("a",{parentName:"li",href:"https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/shell#Fields"},"this Go function"))),(0,l.kt)("p",null,"Example:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n print-os:\n cmds:\n - echo '{{OS}} {{ARCH}}'\n - echo '{{if eq OS \"windows\"}}windows-command{{else}}unix-command{{end}}'\n # This will be path/to/file on Unix but path\\to\\file on Windows\n - echo '{{fromSlash \"path/to/file\"}}'\n enumerated-file:\n vars:\n CONTENT: |\n foo\n bar\n cmds:\n - |\n cat << EOF > output.txt\n {{range $i, $line := .CONTENT | splitLines -}}\n {{printf \"%3d\" $i}}: {{$line}}\n {{end}}EOF\n")),(0,l.kt)("h2",{id:"\u5e2e\u52a9"},"\u5e2e\u52a9"),(0,l.kt)("p",null,"\u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"task --list"),"\uff08\u6216 ",(0,l.kt)("inlineCode",{parentName:"p"},"task -l"),"\uff09\u5217\u51fa\u6240\u6709\u5e26\u6709\u63cf\u8ff0\u7684\u4efb\u52a1\u3002 The following Taskfile:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build:\n desc: Build the go binary.\n cmds:\n - go build -v -i main.go\n\n test:\n desc: Run all the go tests.\n cmds:\n - go test -race ./...\n\n js:\n cmds:\n - esbuild --bundle --minify js/index.js > public/bundle.js\n\n css:\n cmds:\n - esbuild --bundle --minify css/index.css > public/bundle.css\n")),(0,l.kt)("p",null,"\u5c06\u6253\u5370\u4ee5\u4e0b\u8f93\u51fa\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"* build: Build the go binary.\n* test: Run all the go tests.\n")),(0,l.kt)("p",null,"\u5982\u679c\u60a8\u60f3\u67e5\u770b\u6240\u6709\u4efb\u52a1\uff0c\u8fd8\u6709\u4e00\u4e2a ",(0,l.kt)("inlineCode",{parentName:"p"},"--list-all"),"\uff08\u522b\u540d ",(0,l.kt)("inlineCode",{parentName:"p"},"-a"),"\uff09\u6807\u5fd7\u3002"),(0,l.kt)("h2",{id:"\u663e\u793a\u4efb\u52a1\u6458\u8981"},"\u663e\u793a\u4efb\u52a1\u6458\u8981"),(0,l.kt)("p",null,"\u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"task --summary task-name")," \u5c06\u663e\u793a\u4efb\u52a1\u7684\u6458\u8981\u3002 The following Taskfile:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n release:\n deps: [build]\n summary: |\n \u53d1\u5e03\u4f60\u7684\u9879\u76ee\u5230 github\n\n \u5b83\u5c06\u5728\u5f00\u59cb\u53d1\u5e03\u4e4b\u524d\u6784\u5efa\u60a8\u7684\u9879\u76ee\u3002\n \u8bf7\u786e\u4fdd\u60a8\u5728\u5f00\u59cb\u4e4b\u524d\u5df2\u7ecf\u8bbe\u7f6e\u4e86 GITHUB_TOKEN\u3002\n cmds:\n - your-release-tool\n\n build:\n cmds:\n - your-build-tool\n")),(0,l.kt)("p",null,"\u8fd0\u884c ",(0,l.kt)("inlineCode",{parentName:"p"},"task --summary release")," \u5c06\u6253\u5370\u4ee5\u4e0b\u8f93\u51fa\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre"},"task: release\n\n\u53d1\u5e03\u4f60\u7684\u9879\u76ee\u5230 github\n\n\u5b83\u5c06\u5728\u5f00\u59cb\u53d1\u5e03\u4e4b\u524d\u6784\u5efa\u60a8\u7684\u9879\u76ee\u3002\n\u8bf7\u786e\u4fdd\u60a8\u5728\u5f00\u59cb\u4e4b\u524d\u5df2\u7ecf\u8bbe\u7f6e\u4e86 GITHUB_TOKEN\u3002\n\ndependencies:\n - build\n\ncommands:\n - your-release-tool\n")),(0,l.kt)("p",null,"\u5982\u679c\u7f3a\u5c11\u6458\u8981\uff0c\u5c06\u6253\u5370\u63cf\u8ff0\u3002 If the task does not have a summary or a description, a warning is printed."),(0,l.kt)("p",null,"Please note: ",(0,l.kt)("em",{parentName:"p"},"showing the summary will not execute the command"),"."),(0,l.kt)("h2",{id:"task-\u522b\u540d"},"Task \u522b\u540d"),(0,l.kt)("p",null,"Aliases \u662f task \u7684\u66ff\u4ee3\u540d\u79f0\u3002 \u5b83\u4eec\u53ef\u4ee5\u4f7f\u8fd0\u884c\u5177\u6709\u957f\u540d\u79f0\u6216\u96be\u4ee5\u952e\u5165\u540d\u79f0\u7684 task \u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u548c\u5feb\u901f\u3002 \u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e0a\u4f7f\u7528\u5b83\u4eec\uff0c\u5728\u60a8\u7684 Taskfile \u4e2d ",(0,l.kt)("a",{parentName:"p",href:"#%E8%B0%83%E7%94%A8%E5%8F%A6%E4%B8%80%E4%B8%AA-task"},"\u8c03\u7528\u5b50\u4efb\u52a1")," \u65f6\u4ee5\u53ca\u5728 ",(0,l.kt)("a",{parentName:"p",href:"#%E5%8C%85%E5%90%AB%E5%85%B6%E4%BB%96-taskfile"},"\u5305\u542b\u6765\u81ea\u53e6\u4e00\u4e2a Taskfile")," \u7684\u522b\u540d task \u65f6\u3002 They can also be used together with ",(0,l.kt)("a",{parentName:"p",href:"#namespace-aliases"},"namespace aliases"),"."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n generate:\n aliases: [gen]\n cmds:\n - task: gen-mocks\n\n generate-mocks:\n aliases: [gen-mocks]\n cmds:\n - echo \"generating...\"\n")),(0,l.kt)("h2",{id:"\u8986\u76d6-task-\u540d\u79f0"},"\u8986\u76d6 Task \u540d\u79f0"),(0,l.kt)("p",null,"Sometimes you may want to override the task name printed on the summary, up-to-date messages to STDOUT, etc. In this case, you can just set ",(0,l.kt)("inlineCode",{parentName:"p"},"label:"),", which can also be interpolated with variables:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n default:\n - task: print\n vars:\n MESSAGE: hello\n - task: print\n vars:\n MESSAGE: world\n\n print:\n label: 'print-{{.MESSAGE}}'\n cmds:\n - echo \"{{.MESSAGE}}\"\n")),(0,l.kt)("h2",{id:"\u9759\u9ed8\u6a21\u5f0f"},"\u9759\u9ed8\u6a21\u5f0f"),(0,l.kt)("p",null,"\u9759\u9ed8\u6a21\u5f0f\u5728 Task \u8fd0\u884c\u547d\u4ee4\u4e4b\u524d\u7981\u7528\u547d\u4ee4\u56de\u663e\u3002 For the following Taskfile:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n echo:\n cmds:\n - echo \"Print something\"\n")),(0,l.kt)("p",null,"\u901a\u5e38\u8fd9\u5c06\u6253\u5370\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-sh"},'echo "Print something"\nPrint something\n')),(0,l.kt)("p",null,"\u5f00\u542f\u9759\u9ed8\u6a21\u5f0f\u540e\uff0c\u5c06\u6253\u5370\u4ee5\u4e0b\u5185\u5bb9\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-sh"},"Print something\n")),(0,l.kt)("p",null,"\u5f00\u542f\u9759\u9ed8\u6a21\u5f0f\u6709\u56db\u79cd\u65b9\u5f0f\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u5728 cmds \u7ea7\u522b\uff1a")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n echo:\n cmds:\n - cmd: echo \"Print something\"\n silent: true\n")),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u5728 task \u7ea7\u522b\uff1a")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n echo:\n cmds:\n - echo \"Print something\"\n silent: true\n")),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u5728 Taskfile \u5168\u5c40\u7ea7\u522b\uff1a")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nsilent: true\n\ntasks:\n echo:\n cmds:\n - echo \"Print something\"\n")),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"\u6216\u8005\u5168\u5c40\u4f7f\u7528 ",(0,l.kt)("inlineCode",{parentName:"li"},"--silent")," \u6216 ",(0,l.kt)("inlineCode",{parentName:"li"},"-s")," \u6807\u5fd7")),(0,l.kt)("p",null,"\u5982\u679c\u60a8\u60f3\u6539\u4e3a\u7981\u6b62 STDOUT\uff0c\u53ea\u9700\u5c06\u547d\u4ee4\u91cd\u5b9a\u5411\u5230 ",(0,l.kt)("inlineCode",{parentName:"p"},"/dev/null"),"\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n echo:\n cmds:\n - echo \"This will print nothing\" > /dev/null\n")),(0,l.kt)("h2",{id:"\u8bd5\u8fd0\u884c\u6a21\u5f0f"},"\u8bd5\u8fd0\u884c\u6a21\u5f0f"),(0,l.kt)("p",null,"Dry run mode (",(0,l.kt)("inlineCode",{parentName:"p"},"--dry"),") compiles and steps through each task, printing the commands that would be run without executing them. This is useful for debugging your Taskfiles."),(0,l.kt)("h2",{id:"\u5ffd\u7565\u9519\u8bef"},"\u5ffd\u7565\u9519\u8bef"),(0,l.kt)("p",null,"\u60a8\u53ef\u4ee5\u9009\u62e9\u5728\u547d\u4ee4\u6267\u884c\u671f\u95f4\u5ffd\u7565\u9519\u8bef\u3002 Given the following Taskfile:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n echo:\n cmds:\n - exit 1\n - echo \"Hello World\"\n")),(0,l.kt)("p",null,"Task will abort the execution after running ",(0,l.kt)("inlineCode",{parentName:"p"},"exit 1")," because the status code ",(0,l.kt)("inlineCode",{parentName:"p"},"1")," stands for ",(0,l.kt)("inlineCode",{parentName:"p"},"EXIT_FAILURE"),". However, it is possible to continue with execution using ",(0,l.kt)("inlineCode",{parentName:"p"},"ignore_error"),":"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n echo:\n cmds:\n - cmd: exit 1\n ignore_error: true\n - echo \"Hello World\"\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"ignore_error")," can also be set for a task, which means errors will be suppressed for all commands. Nevertheless, keep in mind that this option will not propagate to other tasks called either by ",(0,l.kt)("inlineCode",{parentName:"p"},"deps")," or ",(0,l.kt)("inlineCode",{parentName:"p"},"cmds"),"!"),(0,l.kt)("h2",{id:"\u8f93\u51fa\u8bed\u6cd5"},"\u8f93\u51fa\u8bed\u6cd5"),(0,l.kt)("p",null,"By default, Task just redirects the STDOUT and STDERR of the running commands to the shell in real-time. This is good for having live feedback for logging printed by commands, but the output can become messy if you have multiple commands running simultaneously and printing lots of stuff."),(0,l.kt)("p",null,"\u4e3a\u4e86\u4f7f\u5176\u66f4\u5177\u53ef\u5b9a\u5236\u6027\uff0c\u76ee\u524d\u60a8\u53ef\u4ee5\u9009\u62e9\u4e09\u79cd\u4e0d\u540c\u7684\u8f93\u51fa\u9009\u9879\uff1a"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"interleaved")," (\u9ed8\u8ba4)"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"group")),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"prefixed"))),(0,l.kt)("p",null,"\u8981\u9009\u62e9\u53e6\u4e00\u4e2a\uff0c\u53ea\u9700\u5728 Taskfile \u6839\u76ee\u5f55\u4e2d\u8bbe\u7f6e\u5373\u53ef\uff1a"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\noutput: 'group'\n\ntasks:\n # ...\n")),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"group")," \u8f93\u51fa\u5c06\u5728\u547d\u4ee4\u5b8c\u6210\u540e\u6253\u5370\u4e00\u6b21\u547d\u4ee4\u7684\u5168\u90e8\u8f93\u51fa\uff0c\u56e0\u6b64\u60a8\u4e0d\u4f1a\u5bf9\u9700\u8981\u5f88\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u547d\u4ee4\u6709\u5b9e\u65f6\u53cd\u9988\u3002"),(0,l.kt)("p",null,"When using the ",(0,l.kt)("inlineCode",{parentName:"p"},"group")," output, you can optionally provide a templated message to print at the start and end of the group. This can be useful for instructing CI systems to group all of the output for a given task, such as with ",(0,l.kt)("a",{parentName:"p",href:"https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#grouping-log-lines"},"GitHub Actions' ",(0,l.kt)("inlineCode",{parentName:"a"},"::group::")," command")," or ",(0,l.kt)("a",{parentName:"p",href:"https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?expand=1&view=azure-devops&tabs=bash#formatting-commands"},"Azure Pipelines"),"."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\noutput:\n group:\n begin: '::group::{{.TASK}}'\n end: '::endgroup::'\n\ntasks:\n default:\n cmds:\n - echo 'Hello, World!'\n silent: true\n")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"$ task default\n::group::default\nHello, World!\n::endgroup::\n")),(0,l.kt)("p",null,"When using the ",(0,l.kt)("inlineCode",{parentName:"p"},"group")," output, you may swallow the output of the executed command on standard output and standard error if it does not fail (zero exit code)."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nsilent: true\n\noutput:\n group:\n error_only: true\n\ntasks:\n passes: echo 'output-of-passes'\n errors: echo 'output-of-errors' && exit 1\n")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},'$ task passes\n$ task errors\noutput-of-errors\ntask: Failed to run task "errors": exit status 1\n')),(0,l.kt)("p",null,"The ",(0,l.kt)("inlineCode",{parentName:"p"},"prefix")," output will prefix every line printed by a command with ",(0,l.kt)("inlineCode",{parentName:"p"},"[task-name]")," as the prefix, but you can customize the prefix for a command with the ",(0,l.kt)("inlineCode",{parentName:"p"},"prefix:")," attribute:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\noutput: prefixed\n\ntasks:\n default:\n deps:\n - task: print\n vars: { TEXT: foo }\n - task: print\n vars: { TEXT: bar }\n - task: print\n vars: { TEXT: baz }\n\n print:\n cmds:\n - echo \"{{.TEXT}}\"\n prefix: 'print-{{.TEXT}}'\n silent: true\n")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"$ task default\n[print-foo] foo\n[print-bar] bar\n[print-baz] baz\n")),(0,l.kt)("admonition",{type:"tip"},(0,l.kt)("p",{parentName:"admonition"},"The ",(0,l.kt)("inlineCode",{parentName:"p"},"output")," option can also be specified by the ",(0,l.kt)("inlineCode",{parentName:"p"},"--output")," or ",(0,l.kt)("inlineCode",{parentName:"p"},"-o")," flags.")),(0,l.kt)("h2",{id:"\u4ea4\u4e92\u5f0f-cli-\u5e94\u7528"},"\u4ea4\u4e92\u5f0f CLI \u5e94\u7528"),(0,l.kt)("p",null,"When running interactive CLI applications inside Task they can sometimes behave weirdly, especially when the ",(0,l.kt)("a",{parentName:"p",href:"#output-syntax"},"output mode")," is set to something other than ",(0,l.kt)("inlineCode",{parentName:"p"},"interleaved")," (the default), or when interactive apps are run in parallel with other tasks."),(0,l.kt)("p",null,"The ",(0,l.kt)("inlineCode",{parentName:"p"},"interactive: true")," tells Task this is an interactive application and Task will try to optimize for it:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n default:\n cmds:\n - vim my-file.txt\n interactive: true\n")),(0,l.kt)("p",null,"If you still have problems running an interactive app through Task, please open an issue about it."),(0,l.kt)("h2",{id:"\u77ed-task-\u8bed\u6cd5"},"\u77ed Task \u8bed\u6cd5"),(0,l.kt)("p",null,"Starting on Task v3, you can now write tasks with a shorter syntax if they have the default settings (e.g. no custom ",(0,l.kt)("inlineCode",{parentName:"p"},"env:"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"vars:"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"desc:"),", ",(0,l.kt)("inlineCode",{parentName:"p"},"silent:")," , etc):"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\ntasks:\n build: go build -v -o ./app{{exeExt}} .\n\n run:\n - task: build\n - ./app{{exeExt}} -h localhost -p 8080\n")),(0,l.kt)("h2",{id:"set-\u548c-shopt"},(0,l.kt)("inlineCode",{parentName:"h2"},"set")," \u548c ",(0,l.kt)("inlineCode",{parentName:"h2"},"shopt")),(0,l.kt)("p",null,"It's possible to specify options to the ",(0,l.kt)("a",{parentName:"p",href:"https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html"},(0,l.kt)("inlineCode",{parentName:"a"},"set"))," and ",(0,l.kt)("a",{parentName:"p",href:"https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html"},(0,l.kt)("inlineCode",{parentName:"a"},"shopt"))," builtins. This can be added at global, task or command level."),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-yaml"},"version: '3'\n\nset: [pipefail]\nshopt: [globstar]\n\ntasks:\n # `globstar` required for double star globs to work\n default: echo **/*.go\n")),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"},"Keep in mind that not all options are available in the ",(0,l.kt)("a",{parentName:"p",href:"https://github.com/mvdan/sh"},"shell interpreter library")," that Task uses.")),(0,l.kt)("h2",{id:"\u89c2\u5bdf\u4efb\u52a1"},"\u89c2\u5bdf\u4efb\u52a1"),(0,l.kt)("p",null,"With the flags ",(0,l.kt)("inlineCode",{parentName:"p"},"--watch")," or ",(0,l.kt)("inlineCode",{parentName:"p"},"-w")," task will watch for file changes and run the task again. This requires the ",(0,l.kt)("inlineCode",{parentName:"p"},"sources")," attribute to be given, so task knows which files to watch."),(0,l.kt)("p",null,"The default watch interval is 5 seconds, but it's possible to change it by either setting ",(0,l.kt)("inlineCode",{parentName:"p"},"interval: '500ms'")," in the root of the Taskfile passing it as an argument like ",(0,l.kt)("inlineCode",{parentName:"p"},"--interval=500ms"),"."))}k.isMDXComponent=!0}}]);