RunScriptPlugin

用于执行任意 Shell 脚本的 Swift Package 插件。

功能

用法

将名为 runscript.yml(或 .runscript.yml) 的文件放置在项目的根目录中。在此文件中编写您想要运行的 shell 脚本。

格式如下。

prebuild: # prebuild Command
  - name: "Hello"
    script: "echo Hello" # Write scripts directly
  - name: "Show current path"
    script: "pwd"
  - name: "Write file"
    script: "echo Hello >> test.txt"

  - name: "SwiftLint"
    launchPath: "/bin/bash" # bash, zsh, etc. can be specified
    script: "swiftlint lint --fix"

  - name: "SwiftLint" # Another way to write ↑↑
    launchPath: "/usr/local/bin/swiftlint"
    arguments:
      - "lint"
      - "--fix"

  - name: "Update schema"
    file: "update_schema.sh" # Execute .sh file

build: # build Command
  - name: "Hello"
    script: "echo Hello"
  - name: "Make Swift Code"
    script: |
        echo "public enum Hello { case a,b,c,d }" > $RUN_SCRIPT_PLUGIN_WORK_DIR/tmp.swift"

command: # Command Plugin
  - name: "Hello from Command"
    script: "echo 'Hello from Command'"

all: # run in `prebuild`, `build`...
   - name: "Hello(all)"
     script: "echo Hello(all)"

注意

由于 Xcode 的限制,当尝试写入文件时,您可能会遇到权限错误。

如果 CommandPlugin 从 shell 运行,则可以通过使用 --disable-sandbox 选项禁用沙箱来规避此问题。

swift package plugin --allow-writing-to-package-directory --disable-sandbox run-script

如果您想避免通过 Xcode 使用 BuildToolPlugin/CommandPlugin,您可以通过配置以下设置来禁用沙箱的使用。

defaults write com.apple.dt.Xcode IDEPackageSupportDisablePluginExecutionSandbox -bool YES

环境变量

以下环境变量在脚本中可用,用于引用插件上下文。

示例

- name: "SwiftLint"
  script: "swiftlint lint"
- name: "Build Log"
  script: "echo \"[$(date)] Build >> build.log\""
- name: "Theos make package and install"
  script: "make do"
- name: "SwiftFormat"
  script: "swiftformat ."
- name: "SwiftGen"
  script: "swiftgen config run"