kscript make 命令 kscript make <type> <name> 会在项目保存该类文件的位置创建一个文件,并在需要时创建所需的文件夹。脚本使用项目的格式编写(除非 kscript.json 中的 make.syntax 设为 xml,否则为 .kscript)。kscript make --list 会打印此列表,kscript make <type> --help 会打印某一条目。
每种类型都源自一个模板。kscript make templates 会将它们复制到项目的 _templates 文件夹中,您可以在那里更改其生成的内容,或添加自己的类型。在任何命令后添加 --dry-run 可查看它将生成的内容,添加 --force 可替换已有文件。在新项目中,make route name=users 会运行 kscript make route users(参见 项目 )。
# 路由与 API# kscript make routeShell 复制
kscript make route <path> [--method get|post|put|patch|delete] [--api] [--to controller.action] [--resource] [--view name] [--name route.name]添加到 routes.kscript(使用 --api 时则添加到 api.kscript)中的路由;若缺少控制器,则一并创建该控制器
选项 说明 --methodHTTP 方法:get(默认)、post、put、patch、delete、options 或 any --api将其添加到 api.kscript 中,该文件下的接口以 /api 提供服务并返回 JSON 错误,而不是添加到 routes.kscript 中 --to与路径对应的控制器函数,例如 people.list。默认情况下由路径决定:users 对应 users.index,users/{id} 对应 users.show,--method post 对应 users.store --controller要使用或创建的控制器,例如 admin/users(默认:去掉 {参数} 的路径) --action控制器函数(默认根据方法决定:index 或 show、store、update、destroy) --resource为 index、show、store、update 和 destroy 添加 Resource(path: …, to: …),并创建资源控制器 --view用此视图作答(若 views/ 中缺失则在其中创建),而不是使用控制器 --name@route('name') 使用的名称 --where参数的规则,例如 "id: int"
示例
Shell 复制
kscript make route users
kscript make route "users/{id}" --where "id: int"
kscript make route users --method post --api
kscript make route people --resource --api
kscript make route about --view about
kscript make route reports/export --to reports.export# kscript make controllerShell 复制
kscript make controller <name> [--resource] [--model Post] [--actions index,export]controllers/ 中的控制器:用于响应路由的函数
选项 说明 --resource为 Resource(path: /people, to: people) 编写 index、show、store、update 和 destroy --model用模型的记录填充资源操作(Post->paginate、->find_or_fail、->create、->update 和 ->delete);隐含 --resource --actions要编写的函数,以逗号分隔(默认 index)
示例
Shell 复制
kscript make controller people --resource
kscript make controller reports --actions index,export
kscript make controller admin/users --resource
kscript make controller posts --model Post# kscript make requestShell 复制
kscript make request <Name> [--fields "name: string, age?: int"]requests/ 中的类型化请求体:以该类型声明的控制器参数只接受该结构的数据
选项 说明 --fields字段,以逗号分隔,格式为 name: type(? 表示可选,= 表示默认值)。默认:name: string
示例
Shell 复制
kscript make request NewPerson --fields "name: string, age?: int, role?: string"
kscript make request orders/NewOrder --fields "product: string, quantity: int = 1"# kscript make middlewareShell 复制
kscript make middleware <name> [--header X-Name] [--before]middleware/ 中的中间件:在路由前后运行的函数,可以拦截请求或修改其响应
选项 说明 --before编写一个可在控制器运行前拒绝请求的检查 --header为每个响应添加此标头(默认 X-Handled-By)
示例
Shell 复制
kscript make middleware secure-headers --header X-Frame-Options
kscript make middleware admin-only --before# kscript make viewShell 复制
kscript make view <name> [--vars person,team] [--layout _layout.html] [--title "Title"]views/ 中的视图,控制器可通过 @view('name', …) 渲染它
选项 说明 --vars控制器传递的值,以逗号分隔;每个值都会显示在视图中 --layout使用的布局(默认 _layout.html) --title页面标题(默认根据名称生成)
示例
Shell 复制
kscript make view people/show --vars person
kscript make view home --title "Welcome"# kscript make mailShell 复制
kscript make mail <name> [--vars name,link] [--subject "Subject"]邮件视图,位于 views/mail/,通过 KScript.Mail 中的 @send_mail(..., view: 'mail/name') 发送
选项 说明 --vars邮件显示的值,以逗号分隔;@send_mail 将它们作为 data 传入 --subject提示发送时使用的主题(默认根据名称生成)
示例
Shell 复制
kscript make mail welcome --vars name
kscript make mail orders/shipped --vars name,order_id,link# 页面与样式# kscript make pageShell 复制
kscript make page <path> [--title "Title"] [--layout _layout.html]HTML 页面;其路径即为其 URL(people/[id] 匹配 /people/42)
选项 说明 --title页面标题(默认根据名称生成) --layout使用的布局(默认 _layout.html,若缺失则创建)
示例
Shell 复制
kscript make page about --title "About us"
kscript make page "people/[id]"# kscript make markdownShell 复制
kscript make markdown <path> [--title "Title"] [--layout _layout.html]带前置元数据(title、layout)的 Markdown 页面
选项 说明 --title页面标题(默认根据名称生成) --layout使用的布局(默认 _layout.html)
示例
Shell 复制
kscript make markdown blog/first-post
kscript make markdown docs/setup --layout _layouts/docs.html# kscript make layoutShell 复制
kscript make layout <name> [--css a.css,b.css] [--lang en]一个页面布局;页面通过 <layout path="…" title="…"> 使用它
选项 说明 --css要链接的样式表,以逗号分隔 --lang页面语言(默认 en)
示例
Shell 复制
kscript make layout blog --css /site.css
kscript make layout docs --lang fr# kscript make componentShell 复制
kscript make component <Name> [--for window|terminal|document] [--model Model [--inject [ability]]] [--fields title,body] [--model-update binding [--autosave]] [--props title,image] [--live]带 render 方法的组件类型:可作为页面中的 HTML 标签,也可作为窗口、文档或终端中的语句使用
选项 说明 --for在窗口、文档或终端中绘制,而不是页面 --model绑定到现有模型,例如 --model Note,并将一行数据作为其输入 --inject使用 @inject(can: read) 获取路由的记录,使页面无需传递它。可指定其他权限,例如 --inject update --model-update使用现有的 ModelUpdate 路由保存编辑 --fields要显示或编辑的模型字段,以逗号分隔。默认为已知 ModelUpdate 路由的字段 --autosave输入停顿后保存(默认 500ms)。可指定延迟,例如 --autosave 750ms --props额外的输入,以逗号分隔 --live添加一个交互式实时模块
示例
Shell 复制
kscript make component Hero --props title,subtitle
kscript make component OrderDetails --live
kscript make component NoteCard --for window --props title,body# kscript make partialShell 复制
kscript make partial <name>位于 _partials/ 中的一段 HTML,页面通过 <include path="…"> 插入它
示例
Shell 复制
kscript make partial footer
kscript make partial blog/sidebar# kscript make cssShell 复制
kscript make css <name> [--primary "#3355ff"] [--theme theme.json]styles/ 中的 KScript 样式表(.ks.css),编译为 .css;kscript serve 会在每次请求时编译它
选项 说明 --primary主色(默认 #3355ff) --theme改为从此 JSON 文件读取颜色,例如 theme.json,内容为 { "colors": { "primary": "…" } }
示例
Shell 复制
kscript make css site
kscript make css css/theme --primary "#0a7"
kscript make css brand --theme theme.json# 脚本与数据# kscript make scriptShell 复制
kscript make script <name> [--syntax kscript|xml]位于 scripts/ 中的一个 KScript 脚本,通过 kscript <file> 运行
选项 说明 --syntaxkscript 或 xml(默认项目的 make.syntax)
示例
Shell 复制
kscript make script tidy-downloads
kscript make script tools/backup --syntax xml# kscript make moduleShell 复制
kscript make module <name> [--fns greet,farewell]lib/ 中的函数库,供脚本、页面、控制器和测试导入
选项 说明 --fns要编写的函数,以逗号分隔(默认一个以文件名命名的函数)
示例
Shell 复制
kscript make module pricing --fns subtotal,tax,total
kscript make module text/slugs# kscript make typeShell 复制
kscript make type <Name> [--fields "name: string, stars: int from stargazers_count"] [--sample data/repo.json]types/ 中的类型,用于校验数据并将其转换为记录(API 响应、JSON 文件、请求体)
选项 说明 --fields字段,以逗号分隔,格式为 name: type,? 表示可选,= 表示默认值,from 表示取自另一个键。默认:name: string --sample改为从已保存的数据示例中获取字段
示例
Shell 复制
kscript make type Repo --fields "name: string, stars: int from stargazers_count, topics: string[] = []"
kscript make type Order --sample data/order.json# kscript make nativeShell 复制
kscript make native <Name> [--base string] [--pattern "/^[A-Z]{3}-\d{4}$/"] [--min 3] [--max 30] [--one-of a,b] [--not a,b] [--normalize "$value->trim()"] [--validate "test, 'message'"]natives/ 中的原生类型:您自定义的值,例如 email 或 handle,其校验器会在每处使用时进行检查,并且可以不加引号直接书写。项目中的每个脚本都无需导入即可使用它
选项 说明 --base其构建所基于的类型:string、int、float、number,或原生类型如 email、url、domain、slug 或 country,或你定义的其他类型(默认 string) --pattern整个值必须匹配的正则表达式,例如 "/^[A-Z]{3}-\d{4}$/i" --min最小的数字,或最短的文本或列表 --max最大的数字,或最长的文本或列表 --one-of仅允许的值,以逗号分隔 --not始终不允许的值,以逗号分隔 --normalize在检查之前用于整理值的表达式,如 $value,例如 "$value->trim()->upper()" --validate任何其他检查,以及失败时显示的消息:"test, 'message'"
示例
Shell 复制
kscript make native Sku --pattern '/^[A-Z]{3}-\d{4}$/' --normalize '$value->trim()->upper()'
kscript make native Percent --base int --min 0 --max 100
kscript make native CompanySite --base url --validate '$value.host->ends_with("example.com"), "Use a company site"'
kscript make native Username --base handle --max 16 --not @admin,@root# kscript make modelShell 复制
kscript make model <Name> [--fields "name: string, email: email"] [--timestamps] [--soft-deletes] [--component] [--policy] [--migration] [--controller] [--seeder] [--all]models/ 中的模型:一种类型,其记录保存在数据库表中,并带有查询构建器(User->where(...))
选项 说明 --fields以逗号分隔的字段,格式为 name: type,? 表示可选,= 表示默认值。默认值:name: string --timestamps扩展 Timestamps:created_at 和 updated_at,在保存记录时设置 --soft-deletes扩展 SoftDeletes:将记录标记为已删除而不是直接移除,以便可以恢复 --component同时创建一个组件,接收此模型的一行数据作为 record --policy同时在 policies/ 中创建其策略,供 @can 和 @authorize 使用 --migration同时使用 kscript db diff 编写创建其表的迁移 --controller同时使用模型创建一个资源控制器(kscript make controller --model) --seeder同时在 database/seeders 中创建一个种子文件,用于生成一些记录 --all包括 --component、--policy、--migration、--controller 和 --seeder
示例
Shell 复制
kscript make model User --fields "name: string, email: email, password: password" --timestamps
kscript make model BlogPost --fields "title: string, body: string, published: bool = false" --soft-deletes
kscript make model Note --fields "title: string, body: string" --component
kscript make model Post --fields "title: string, body: string" --timestamps --all# kscript make migrationShell 复制
kscript make migration <name>database/migrations 中需手动编写的迁移:用于修改表结构的步骤,由 kscript db migrate 运行。kscript db diff 则会根据模型自动生成迁移
示例
Shell 复制
kscript make migration add_bio_to_users
kscript make migration backfill_slugs# kscript make seederShell 复制
kscript make seeder <name> [--model Post]database/seeders 中的填充器:用于向数据库写入记录的脚本,由 kscript db seed 运行
选项 说明 --model它生成记录所用的模型:已导入,可直接用于 Post->create(...)
示例
Shell 复制
kscript make seeder users
kscript make seeder posts --model Post# kscript make enumShell 复制
kscript make enum <Name> [--values small,medium,large]types/ 中的枚举:一组命名值,类型化的参数和字段只接受这些值
选项 说明 --values以逗号分隔的值(默认为 one、two、three)
示例
Shell 复制
kscript make enum Size --values small,medium,large
kscript make enum OrderStatus --values pending,paid,shipped,cancelled# kscript make errorShell 复制
kscript make error <Name> [--status 402] [--extends Validation] [--message "..."] [--fields "order: int, reason?: string"]errors/ 中的错误类型,通过 throw Name(...) 抛出,并通过 catch Name as e 捕获
选项 说明 --statusKScript.Http 路由在没有任何捕获时返回的 HTTP 状态码,例如 402 --extends它所属的错误类型,例如 NotFound 或 Validation(默认为 Error) --message消息,字段作为变量使用:'No order {$id}'(默认为名称的文字形式) --fields以逗号分隔的字段,格式为 name: type,? 表示可选,= 表示默认值
示例
Shell 复制
kscript make error PaymentFailed --status 402 --fields "order: int, reason?: string"
kscript make error OrderNotFound --extends NotFound --message 'No order {$id}' --fields "id: int"# kscript make constShell 复制
kscript make const <name> [--values "max_items = 10, site_name = 'Shop'"]lib/ 中的常量:永不改变的值,由导入该文件的所有脚本共享
选项 说明 --values以逗号分隔的常量,格式为 name = value(默认为一个示例)
示例
Shell 复制
kscript make const limits --values "max_items = 10, page_size = 25"
kscript make const app --values "name = 'Shop', version = '1.0'"# kscript make storageShell 复制
kscript make storage <name> [--file data/people.json]lib/ 中的 JSON 文件存储:为保存在 data/ 中的记录列表提供 all、find、save 和 remove
选项 说明 --file保存记录所用的 JSON 文件(默认为 data/<name>.json)
示例
Shell 复制
kscript make storage people
kscript make storage orders --file data/shop/orders.json# kscript make cacheShell 复制
kscript make cache <name> [--minutes 10] [--file data/cache.json]lib/ 中的文件缓存:将耗时的结果(API 调用、大文件)缓存数分钟
选项 说明 --minutes值的保存时长(默认为 10) --file保存值所用的 JSON 文件(默认为 data/<name>.json)
示例
Shell 复制
kscript make cache api-cache --minutes 30
kscript make cache reports --file data/report-cache.json# kscript make jobShell 复制
kscript make job <Name> [--fields "user_id: int, email: email"] [--queue mail] [--tries 5] [--backoff "10s, 1m, 5m"] [--timeout 30s]jobs/ 中的排队任务:通过 Name(...)->dispatch() 分派,kscript worker 会运行它,失败时进行重试
选项 说明 --fields以逗号分隔的字段,格式为 name: type,? 表示可选,= 表示默认值(默认:无) --queue其所在的队列(默认:default) --tries在被视为失败之前的尝试次数(默认:3) --backoff每次重试前的等待时间,例如 "10s, 1m, 5m"(默认:exponential) --timeout单次尝试允许运行的最长时间,例如 30s 或 5m(默认:60s)
示例
Shell 复制
kscript make job SendWelcome --fields "user_id: int, email: email" --queue mail --tries 5
kscript make job reports/BuildReport --fields "month: int" --timeout 5m# kscript make scheduleShell 复制
kscript make schedule <task> [--every 1h | --cron "0 3 * * *"]调度文件 schedule.kscript,附带一个初始任务:kscript schedule work 会在指定时间运行其任务
选项 说明 --every任务运行的频率,以整分钟为单位,例如 5m 或 1h(默认:1h) --cron改用 cron 表达式指定的运行时间,例如 "0 3 * * *" 表示每天 03:00
示例
Shell 复制
kscript make schedule prune-sessions --every 1h
kscript make schedule nightly-report --cron "0 3 * * *"# kscript make eventShell 复制
kscript make event <Name> [--fields "order_id: int, total: number"]events/ 中的事件:已发生的事情,通过 @dispatch_event() 分派给每个监听器
选项 说明 --fields以逗号分隔的字段,格式为 name: type,? 表示可选,= 表示默认值(默认:无)
示例
Shell 复制
kscript make event OrderPlaced --fields "order_id: int, total: number"
kscript make event UserSignedUp --fields "user_id: int"# kscript make listenerShell 复制
kscript make listener <Event>listeners/ 中的监听器:在通过 @dispatch_event() 分派事件时运行的函数
示例
Shell 复制
kscript make listener OrderPlaced
kscript make listener UserSignedUp# kscript make extensionShell 复制
kscript make extension <type> [--methods api_date,is_payday]extensions/ 中为 date 或 list 等类型添加的方法:项目中的每个脚本都可以使用它们
选项 说明 --methods以逗号分隔的初始方法。每个方法在你填写之前都返回 $this
示例
Shell 复制
kscript make extension date --methods api_date,is_payday
kscript make extension text/string --methods slug
kscript make extension list --methods second# kscript make policyShell 复制
kscript make policy <Model>policies/ 中的策略:决定谁可以创建、读取、更新和删除模型的记录,通过 @can 和 @authorize 进行查询
示例
Shell 复制
kscript make policy BlogPost
kscript make policy User# 桌面应用# kscript make windowShell 复制
kscript make window <name> [--title "Title"] [--width 480] [--height 360]桌面窗口(Window,来自 KScript.UI):可在 macOS、Windows 和 Linux 上运行的原生应用
选项 说明 --title窗口标题(默认由名称生成) --width宽度(默认 480) --height高度(默认 360)
示例
Shell 复制
kscript make window settings --width 400 --height 300
kscript make window windows/about --title "About this app"# kscript make documentShell 复制
kscript make document <name> [--template paper|report|plain|letter|invoice] [--syntax kscript|xml]documents/ 中的文档脚本,用于生成 PDF 和 Word 文件(KScript.Document)
选项 说明 --template外观:paper、report、plain、letter 或 invoice(默认 paper) --syntaxkscript 或 xml(默认项目的 make.syntax)
示例
Shell 复制
kscript make document annual-report --template report
kscript make document papers/gliders# kscript make configShell 复制
kscript make config <base|environment>$config 的设置:config.kscript 用于需要代码的值,kscript.<env>.json 用于单个环境
示例
Shell 复制
kscript make config base
kscript make config staging# kscript make envShell 复制
kscript make env <base|environment> [--keys API_TOKEN,DATABASE_URL]一个包含 NAME=value 形式密钥的 .env 文件:.env,或用于单个环境的 .env.<env>;从不对外提供
选项 说明 --keys要添加的变量名,以逗号分隔(默认 API_TOKEN)
示例
Shell 复制
kscript make env base --keys API_TOKEN,DATABASE_URL
kscript make env staging# kscript make localeShell 复制
kscript make locale <locale> [--keys hello,goodbye]locales/ 中某一语言的翻译,供脚本和页面中的 @t('key') 使用
选项 说明 --keys要添加的键,以逗号分隔,每个键以其名称作为待翻译文本
示例
Shell 复制
kscript make locale fr
kscript make locale en-GB --keys welcome,sign_in# kscript make loggerShell 复制
kscript make logger <name> [--level warn]lib/ 中的日志提供程序:一个函数,接收所有达到或超过某一级别的日志条目
选项 说明 --level它接收的最低级别:trace、debug、info、warn(默认)、error 或 fatal
示例
Shell 复制
kscript make logger alerts --level error
kscript make logger audit --level info# kscript make testShell 复制
kscript make test <name> [--syntax kscript|xml]tests/ 中的测试文件,通过 kscript test 运行
选项 说明 --syntaxkscript 或 xml(默认项目的 make.syntax)
示例
Shell 复制
kscript make test checkout
kscript make test api/people