【问题标题】:How would you put an AppleScript script under version control?您如何将 AppleScript 脚本置于版本控制之下?
【发布时间】:2011-10-03 23:25:54
【问题描述】:

我想知道这是否是最好的解决方案:

  • .applescript 文件置于版本控制之下
  • 创建一个安装脚本来编译带有osacompile的文件

但也有.scptd 目录。或者我可以将.applescript.scpt 文件都置于版本控制之下?

什么是最好的解决方案?

【问题讨论】:

  • 我只会把代码放在repo中,那么代码就是真相,只有代码版本有争议,编译后的代码版本可能与源代码不一致。
  • 我不确定你所说的版本控制是什么意思。
  • @charlax,在获得了一些支持以确认这对我以外的人有效之后,我对自己的回答很有信心。您应该尝试实施我的答案。 (甚至选择它;-)

标签: git applescript


【解决方案1】:

我喜欢@DanielTrebbien's solution,但是对于我来说,期望人们实现以用于我的 github 项目有点太复杂了。一个更简单的选项是让您能够看到 diff 中的文本更改,即使用 osadecompile 将 diff 进程告诉 textconv。

添加到 .gitattributes

*.scpt diff=scpt

添加到 .git/config

[diff "scpt"]
  textconv = osadecompile
  binary=true

这是我的 AppleScript-droplet github project 的示例差异

$ git diff
--- a/AppleScript-droplet.app/Contents/Resources/Scripts/main.scpt
+++ b/AppleScript-droplet.app/Contents/Resources/Scripts/main.scpt
@@ -1,3 +1,3 @@
-on open filelist
-       ## Set useTerminal to true to run the script in a terminal
-       set useTerminal to true
+on open file_list
+       ## Set use_terminal to true to run the script in a terminal
+       set use_terminal to true

非常感谢@DanielTrebbien 的回答,让我找到了 osadecompile。

【讨论】:

  • 这非常适合查看更改,但无助于合并。不幸的是,您仍在尝试合并二进制文件,这很棘手。
  • @DarrickHerwehe,合并二进制文件比棘手还糟糕。不可能。要合并,您需要做的是osadecompile 两个版本的文件并进行手动合并。为此,我使用vimdiff,当它看起来正确时,我将文本复制粘贴回AppleScript GUI。绝对不是很棒,但比合并 Photoshop 二进制文件要好。
  • 这个解决方案对于脚本库也很方便。在这种情况下,您不需要处理 *.scptd 包包含多个文件的事实。此外,当您有一个带有 SDEF 的脚本库时,您不需要解决那些可怕的问题。合并(通常,AppleScripts 几乎不需要合并,因为大多数时候它是一个男人的节目)必须在没有 git 的帮助下手动完成。这将是一个可以接受的权衡。
【解决方案2】:

编辑:现在是执行此操作的“官方”gitfilter,称为osagitfilter。它建立在这个想法的基础上,并修复了 osacompile 的一些怪癖......


如果使用 git,您可以使用filter driver 透明地 (1) 反编译 SCPT 文件,以便只提交 AppleScript 源代码(称为“清理”二进制 SCPT)和 (2) 在检查时重新编译回 SCPT out(称为“涂抹”AppleScript 源代码)。

首先将以下名为git-ascr-filter的shell脚本添加到/usr/local/bin

#!/bin/sh
if [ $# -ne 2 ]; then
    echo "Usage: $0 --clean/--smudge FILE">&2
    exit 1
else
    if [ "$1" = "--clean" ]; then
        osadecompile "$2" | sed 's/[[:space:]]*$//'
    elif [ "$1" = "--smudge" ]; then
        TMPFILE=`mktemp -t tempXXXXXX`
        if [ $? -ne 0 ]; then
            echo "Error: \`mktemp' failed to create a temporary file.">&2
            exit 3
        fi
        if ! mv "$TMPFILE" "$TMPFILE.scpt" ; then
            echo "Error: Failed to create a temporary SCPT file.">&2
            rm "$TMPFILE"
            exit 4
        fi
        TMPFILE="$TMPFILE.scpt"
        # Compile the AppleScript source on stdin.
        if ! osacompile -l AppleScript -o "$TMPFILE" ; then
            rm "$TMPFILE"
            exit 5
        fi
        cat "$TMPFILE" && rm "$TMPFILE"
    else
        echo "Error: Unknown mode '$1'">&2
        exit 2
    fi
fi

确保chmod a+x脚本。

通过运行配置“ascr”过滤器:

git config filter.ascr.clean "git-ascr-filter --clean %f" git config filter.ascr.smudge "git-ascr-filter --smudge %f"

然后添加到.gitattributes:

*.scpt 过滤器=ascr

现在,每当您对 SCPT 文件和git add 进行更改时,反编译的 AppleScript 源代码将被暂存,而不是二进制 SCPT。此外,每当您检出一个 SCPT 文件(它实际上作为 AppleScript blob 存储在存储库中)时,都会在磁盘上重新创建 SCPT 文件。

【讨论】:

  • 感谢 osadecompile 领导。我想出了一个不同的解决方案来解决这个问题,但没有你我做不到。
  • 我感觉这种方法可以避免 scpt 文件在 github 中不可见的问题,但我担心克隆存储库的其他人也需要添加它。
  • @studgeek:没错。 AppleScript 源代码可以在 GitHub 中查看,任何克隆存储库的人都必须安装 git-ascr-filter 脚本并运行两个 git config 命令。
  • 我发现:TextMate 使用它自己的二进制文件来反编译 AppleScript。它在 GitHub 上:github.com/textmate/applescript.tmbundle/tree/master/Support/… 不过仍然需要测试是否可以创建 scptd+sdef...
  • @doekman 我认为这是二进制文件的源代码:github.com/textmate/textmate/blob/master/Applications/…
【解决方案3】:

我总是将 .applescript(纯文本文件)放在版本控制 (SVN) 中。这样我可以很容易地比较不同的版本,如果是多用户也很容易。您可以突出显示其他用户所做的更改。这对于二进制文件是不可能的,例如编译的脚本文件。

【讨论】:

  • 谢谢!然后你用osacompile编译它?
  • 如果您使用的是 xcode,请打开 'save exec-only' 标志,并在构建时编译文件。对于普通的 applescript 文件,我将源文件保存在 svn 中,并在需要时将其保存为已编译的脚本。如果您认为您仍然需要 osacompile(例如从普通的 applescript 文件加载脚本对象),您也可以使用“运行脚本文件 theFile”命令,只要运行处理程序在最后返回自身(返回给我)。
【解决方案4】:

我将纯文本 .applescript 文件保存在 Git 中,并且我有一个简单的 Bash 脚本,每次我想构建应用程序时都会运行它,它负责编译 AppleScript。这是我的脚本:

#!/usr/bin/env bash

APPNAME="My Awesome App"

# make sure we're in the right place
if [ ! -d ".git" ]; then
    echo "ERROR: This script must be run from the root of the repository."
    exit
fi

# clear out old build
rm -r dist/*
mkdir -p "dist/$APPNAME.app/"

# copy files
cp -r app/* "dist/$APPNAME.app/"

# compile all applescript
cd "dist/$APPNAME.app/Contents/Resources/Scripts/"
for f in *.applescript
do
    osacompile -o "`basename -s .applescript "$f"`.scpt" "$f"
    rm "$f"
done

此脚本假定您在 Git 存储库的根目录中的文件夹 app/ 中拥有整个应用程序(即 Contents/ 文件夹及其中的所有内容)。它将所有内容复制到 dist/ 中的应用程序的新副本中,然后编译新副本的 Contents/Resources/Scripts/ 文件夹中的所有 AppleScript 文件。

要自己使用它,我建议将我的脚本复制到存储库根目录中的bin/build.sh,运行chmod +x bin/build.sh 以使其可执行,然后在需要新版本应用程序的任何时候运行./bin/build.sh .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2012-06-23
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多