way-magefile

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Mage

Mage

Mage is a make-like build tool using Go. You write plain-old go functions, and Mage automatically uses them as Makefile-like runnable targets.
Mage是一款类Make的Go语言构建工具。你可以编写普通的Go函数,Mage会自动将它们作为类Makefile的可运行目标。

When to Use

适用场景

  • Creating build scripts for Go projects.
  • Automating tasks (install, build, clean, release).
  • Managing dependencies between tasks.
  • 为Go项目创建构建脚本。
  • 自动化任务(安装、构建、清理、发布)。
  • 管理任务之间的依赖关系。

Core Concepts

核心概念

1. Magefiles

1. Magefile

  • Any Go file with
    //go:build mage
    (or
    +build mage
    for older Go).
  • Usually named
    magefile.go
    or placed in
    magefiles/
    directory.
  • package main
    .
  • 任何包含
    //go:build mage
    的Go文件(旧版Go使用
    +build mage
    )。
  • 通常命名为
    magefile.go
    或放置在
    magefiles/
    目录下。
  • 包声明为
    package main

2. Targets

2. 目标(Targets)

  • Exported functions with specific signatures:
  • func Target()
  • func Target() error
  • func Target(ctx context.Context) error
  • First sentence of doc comment is the short description (
    mage -l
    ).
  • 具有特定签名的导出函数:
  • func Target()
  • func Target() error
  • func Target(ctx context.Context) error
  • 文档注释的第一句是简短描述(可通过
    mage -l
    查看)。

3. Dependencies

3. 依赖关系

  • Use
    mg.Deps(Func)
    to declare dependencies.
  • Dependencies run exactly once per execution.
  • mg.Deps
    runs in parallel;
    mg.SerialDeps
    runs serially.
  • 使用
    mg.Deps(Func)
    声明依赖。
  • 每个依赖在每次执行中仅运行一次。
  • mg.Deps
    并行运行依赖;
    mg.SerialDeps
    串行运行依赖。

Quick Start

快速开始

To create a new magefile:
mage -init
创建新的magefile:
mage -init

Common Tasks

常见任务

Running Commands: Use
github.com/magefile/mage/sh
for shell execution.
go
import "github.com/magefile/mage/sh"
// ...
err := sh.Run("go", "build", "./...")
Clean Up:
go
import "github.com/magefile/mage/sh"
// ...
sh.Rm("bin")
运行命令: 使用
github.com/magefile/mage/sh
执行shell命令。
go
import "github.com/magefile/mage/sh"
// ...
err := sh.Run("go", "build", "./...")
清理:
go
import "github.com/magefile/mage/sh"
// ...
sh.Rm("bin")

References

参考资料

  • Way Conventions: See references/way-style.md for Way-specific patterns and helpers.
  • API Documentation: See references/api.md for
    mg
    ,
    sh
    , and
    target
    package details.
  • Examples: See references/examples.md for common patterns.
  • Way约定:查看references/way-style.md获取Way专属的模式与工具函数。
  • API文档:查看references/api.md了解
    mg
    sh
    target
    包的详细信息。
  • 示例:查看references/examples.md获取常见使用模式。