iOS DeveloperiOS学习笔记程序员

使用appleSCript脚本实现xcode格式化代码

2018-04-14  本文已影响35人  半城coding

1、写该脚本的原因

自从xcode8以后苹果禁止使用插件后,以前用来格式化代码的插件就不可使用了,虽然后来用Xcode Source Editor Extension写了一个,但是插件有时候不稳定,有出现插件不工作或者xcode启动之后插件没加载进来的情况,使用起来不方便。后来受这篇文章的启发。能不能用AppleScript写一个脚本配合clang-format来格式化项目代码。

2、流程图

说干就干,简单的了解下appleScript 语法之后就开始撸代码了。 appleScript很容易学,简单了解下就能写出点东西。下面是流程图:


83D77D7C3A538488669C660612ECD7C4.png

3、使用automator建立一个workflow

workflow 图片如下:


23F3A230BE9534AD06C2882BB18D11EE.jpg

我的这个workflow有两个模块:

on run {input, parameters}
    (* Your script goes here *)
    set formatFilePath to ""
    tell application "Xcode"
        set CurrentActiveDocument to document 1 whose name ends with (word -1 of (get name of window 1))
        set fullPath to path of CurrentActiveDocument
        set formatFilePath to fullPath as POSIX file
    end tell
    return formatFilePath
end run

这个模块只做一件了一件事情, 从xcode中获取当前打开文件的路径 然后讲该文件的路径传递给第二个模块

on run {input, parameters}
    
    (* Your script goes here *)
    formatFile(input)
    return input
end run

on formatFile(filePath)
    -- clang-format 工具目录
    set clangformat to  "$HOME" & "/clang-format "
    set clangStyle to " -i"
    -- .clang-format 文件目录
    set shellString to "-assume-filename=" & "$HOME" & "/.clang-format "
    set fileList to traversalFile(filePath)
    tell application "Xcode"
        repeat with pathString in fileList
            set shellString to clangformat & shellString & pathString & clangStyle
            tell me to do shell script shellString
        end repeat
    end tell
    return true
end formatFile

on isDirectory(filePosixPath)
    set diskPath to POSIX path of filePosixPath
    set fileType to (do shell script "file -b " & diskPath)
    set typeString to fileType as string
    if typeString ends with "directory" then return true
    return false
end isDirectory

on traversalFile(filePosixPath)
    set fileList to {}
    if (not isDirectory(filePosixPath)) then
        set filePath to POSIX path of filePosixPath
        if isCanFormatFile(filePath) then
            set the end of fileList to filePath
        end if
    else
        -- 文件夹 遍历文件夹中所有符合要求的文件
        set _item to (alias (filePosixPath))
        set _list to list folder _item without invisibles
        repeat with path in _list
            set dir to POSIX path of filePosixPath
            set filePath to dir & path
            set posixFilePath to filePath as POSIX file
            if (not isDirectory(posixFilePath)) then
                if isCanFormatFile(filePath) then
                    set the end of fileList to filePath
                end if
            end if
        end repeat
    end if
    return fileList
end traversalFile

on isCanFormatFile(filePath)
    -- set filePathLength to  the length of filePath
    set formatArray to {".h", ".m", ".java", ".cs", ".cpp", ".mm", ".c"}
    repeat with formatString in formatArray
        if filePath ends with formatString then
            return true
        end if
    end repeat
    return false
end isCanFormatFile

模块二一共五个方法:

4、使用

1、在automator上面调试好脚本后(脚本可以用visual studio编写和进行第一步调试,测试通过后然后放在automator中调试),导出成workflow文件。
2、点击到处文件,安装workflow。然后重新启动xcode此时会发现 xcode->services下面多了个xcode-codeForamt的服务

5F4A680AAE8D77313A8815DB34158A75.jpg

3、然后把我们的 clang-format文件和.clang-format文件放到电脑根目录下


E54EA33F7AF5DAA406CE41D53A44285A.jpg

为什么选择放入到该目录下面。因为该目录是可以通过 $HOME 来获取的。这样不需要修改脚本代码就能给组里面的人使用。
4、为了方便我们可以在键盘->快捷键里面设置一个快捷键,用来启动该服务。

A49C2CF5C505351A1E2BF167ECB0FBB8.jpg

写在最后


appleScript脚本简单易懂,想学习可以去找《AppleScript.pdf》这本书,一两天就能看完。
源码地址 里面有测试的源码和我做好的可使用的一个workflow。 安装后 讲clang-format 和 .clang-format文件放到指定的位置就能使用了。

上一篇 下一篇

猜你喜欢

热点阅读