You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何像Prettier格式化info.plist?如何通过CI/脚本维护其一致性?

格式化Info.plist的工具与自动化方案

一、类似Prettier的格式化工具

有几个实用工具可以统一Info.plist的格式:

  • plutil(苹果官方工具):macOS/iOS开发环境自带的命令行工具,完全满足格式化需求。常用命令:

    • 预览格式化后的内容:plutil -convert xml1 -r -o - Info.plist
    • 直接覆盖原文件完成格式化:plutil -convert xml1 -r Info.plist
      参数说明:-convert xml1将文件转为标准XML格式(避免二进制或其他格式混乱),-r开启格式化(pretty-print)。
  • SwiftFormat:原本是Swift代码格式化工具,支持对Info.plist进行格式化。只需在项目根目录的.swiftformat配置文件中添加--format-infoplist,然后运行swiftformat .即可批量处理目录下的所有Info.plist。

  • Xcode手动格式化:在Xcode中打开Info.plist,右键选择「Reformat」或使用快捷键Ctrl+I,适合临时手动调整。

二、自动化保持格式一致性

针对PR流程繁琐的问题,可以通过脚本、Git钩子或GitHub Action实现自动化:

1. 本地脚本+Git钩子

写一个简单的Shell脚本统一格式化所有Info.plist:

#!/bin/bash
# 遍历项目中所有Info.plist并格式化
find . -name "Info.plist" -type f | while read -r plist_file; do
    plutil -convert xml1 -r "$plist_file"
    echo "Formatted: $plist_file"
done

将脚本保存为format-plist.sh并赋予执行权限(chmod +x format-plist.sh),然后添加到Git的pre-commit钩子:
在项目的.git/hooks/pre-commit文件中加入:

./format-plist.sh

这样每次提交代码前,脚本会自动格式化所有Info.plist,避免提交格式混乱的文件。

2. GitHub Action自动化方案

通过GitHub Action可以在PR提交时自动修复格式,或检查格式是否合规:

自动修复并提交的Action配置:

name: Auto-Format Info.plist
on: [pull_request]

jobs:
  format-plist:
    runs-on: macos-latest
    steps:
      - name: Checkout PR branch
        uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0

      - name: Format all Info.plist files
        run: |
          find . -name "Info.plist" -type f | while read -r plist; do
              plutil -convert xml1 -r "$plist"
          done

      - name: Commit and push changes
        run: |
          git config --local user.name "GitHub Actions Bot"
          git config --local user.email "actions@github.com"
          git add .
          # 只有当有修改时才提交
          if git diff --cached --quiet; then
              echo "No Info.plist formatting changes needed."
          else
              git commit -m "chore: auto-format Info.plist files"
              git push
          fi

这个Action会在PR触发时,自动格式化所有Info.plist并将修改推回PR分支,无需手动调整。

格式检查(仅提示不修改):

如果不想自动提交修改,可以改为检查格式,若不符合则CI失败:

name: Check Info.plist Format
on: [pull_request]

jobs:
  check-format:
    runs-on: macos-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Validate Info.plist formatting
        run: |
          exit_code=0
          find . -name "Info.plist" -type f | while read -r plist; do
              # 生成格式化后的内容与原文件对比
              formatted_content=$(plutil -convert xml1 -r -o - "$plist")
              if ! diff -q <(echo "$formatted_content") "$plist" > /dev/null; then
                  echo "❌ $plist is not properly formatted. Run: plutil -convert xml1 -r $plist"
                  exit_code=1
              fi
          done
          exit $exit_code

这样CI会在格式不符合要求时报错,提示开发者手动运行格式化命令。

内容的提问来源于stack exchange,提问作者DONGNYEONG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 00:03:13