NYC's Blog - MacOS http://niyanchun.com/tag/macos/ zh-CN Mon, 31 Jul 2017 21:48:00 +0800 Mon, 31 Jul 2017 21:48:00 +0800 Mac下SVN使用方案推荐 http://niyanchun.com/svn-solution-in-mac.html http://niyanchun.com/svn-solution-in-mac.html Mon, 31 Jul 2017 21:48:00 +0800 NYC 现在虽然Git大行其道,但是也在所难免会有使用SVN的场景(比如我原来在华为的部门用的就是SVN),毕竟相比于Git,SVN使用起来还是简单很多,而且Git更适合于开源模式的写作。好吧,工具无绝对的优劣,不比较了,没什么意义。本文主要推荐一个我认为在Mac下还比较优雅的SVN解决方案。当然,如果你的开发工具是诸如Eclipse、IDEA等高级IDE的话,一般这种IDE集成的版本管理工具就足够用了,你也没有太大必要再继续往下看了...

相信很多Mac用户会发现,在Mac上Git的客户端还挺多,比如SourceTree就是一个非常棒的而且免费的Git客户端,但是SVN却没有什么比较像样的客户端,免费的里面就不用谈了。不过,我前段时间发现一个比较好的解决方案就是:SVN命令行+SnailSVNLite

PS:如果你习惯图形化的工具,那可以不用看SVN命令行这一节,直接看SnailSVNLite

SVN命令行

使用Git的人里面有相当一部分的人平时都使用的是命令行,相比于Git,SVN更加简单,所以其实平时用命令行足矣,而且Mac默认已经安装了SVN命令。输入svn help我们可以看到,其命令总共不到四十个,而常用的估计也就个位数,反正我平时基本只用四五个。特别是当你的终端支持自动补全的时候(比如Mac下常用的组合iTerm+zsh+oh my zsh),那么这个命令行使用起来就灰常的简单了。对于极致的Geek来说,这就够了。如果你像我一样,还喜欢像TortoiseSVN那种工具一样,可以在文件上面用不同的图标来显示状态,那么可以接着往下看。

SnailSVNLite

虽然SnailSVN是收费的,但是SnailSVNLite却是免费的,而且它可以像Windows下的TortoiseSVN工具一样在Finder里面用不同的图标显示文件的状态,其安装很简单,从App Store下载安装即可,装完后的样子如下(可能需要重启,而且需要进行一些简单的系统设置,以使其继承到Finder里面,软件自身会提示,而且网上有很多教程,这里就不重复造轮子了):

SnailSVNLite.png

可以看到后边有绿色的图标,而且Finder上面也继承了一些成员的命令,而且右键菜单也会多出一些svn的命令。

当然,SnailSVNLite本身就提供了一个完整的SVN客户端的命令,我为什么还要先介绍命令行呢?因为我使用的时候发现全部用这个工具操作时,有的时候它会crash掉,特别是一次提交的文件稍微多一点的时候。也许是我使用的姿势有问题,如果你不喜欢用命令行,那你可以直接只安装SnailSVNLite试试。

SVN命令行调用Beyond Compare

svn自带的diff和merge工具我觉得看少量的代码变动还行,要是变动比较多我感觉要疯掉(当然一些大神们貌似很喜欢这种格式)。开源的diff工具很多,但目前为止我最喜欢的还是非beyond compare莫属。这里讲一下如何将svn命令行默认的diff工具改为beyond compare(GUI的Git或SVN工具一般在设置里面选择Beyond Compare就行,很容易),当然该方法参考自网上。

首先在~/.subversion目录下创建两个脚本:

bc2.sh:

#!/bin/sh

# Configure your favorite diff program here.
DIFF="/usr/local/bin/bcompare"
# DIFF="/usr/bin/meld"
# DIFF="/usr/bin/kompare"
# DIFF=env LANG=zh_CN.UTF-8 WINEPREFIX="/home/borqs/.wine" wine "C:\\Program Files\\Beyond Compare 2\\BC2.exe"

# Subversion provides the paths we need as the sixth and seventh
# parameters.
LEFT=${6}
RIGHT=${7}

# Call the diff command (change the following line to make sense for
# your merge program).
$DIFF $LEFT $RIGHT

# Return an errorcode of 0 if no differences were detected, 1 if some were.
# Any other errorcode will be treated as fatal.
#return 0

bc3.sh:

#!/bin/sh

# Configure your favorite diff3/merge program here.
DIFF3=bcompare
# Subversion provides the paths we need as the ninth, tenth, and eleventh
# parameters.
MINE=${9}
OLDER=${10}
YOURS=${11}

# Call the merge command (change the following line to make sense for
# your merge program).
$DIFF3 --older $OLDER --mine $MINE --yours $YOURS

# After performing the merge, this script needs to print the contents
# of the merged file to stdout.  Do that in whatever way you see fit.
# Return an errorcode of 0 on successful merge, 1 if unresolved conflicts
# remain in the result.  Any other errorcode will be treated as fatal.

然后编辑~/.subversion/config文件:

... ...
diff-cmd = /Users/username/.subversion/bc2.sh
... ...
diff3-cmd = /Users/username/.subversion/bc3.sh

如果你没改过的话,上面的两行默认是注释掉的,你去掉注释,并改为上面的样子,注意将其中的username改为你自己的家目录名。

注意点

  1. 可以看到上面的脚本中DIFFDIFF3两个变量都表示的是Beyond Compare的路径,我一个写的是绝对路径,一个只写了命令名。其实都可以,如果只写命令名,那务必保证命令所在路径在你的PATH里面。
  2. Beyond Compare装完后,默认并没有安装命令行,需要点击"Beyond Compare->Install Command Line Tools...",这样就会在/usr/local/bin目录安装bcomparebcomp命令了。

OK,分享完毕,祝使用愉快。

]]>
3 http://niyanchun.com/svn-solution-in-mac.html#comments http://niyanchun.com/feed/tag/macos/
Alfred版本修改内置Terminal为iTerm2 http://niyanchun.com/change-terminal-to-iterm-in-alfred.html http://niyanchun.com/change-terminal-to-iterm-in-alfred.html Mon, 31 Jul 2017 21:06:00 +0800 NYC Alfred是Mac下一个非常强大的工具,网上有许多如何使用的教程,我目前也只是会使用一些基本功能。其中一个我特别喜欢的功能就是直接可以在Alfred里面输入shell命令(默认需要在命令前家>符号,表示是shell命令),然后就会自动打开终端执行。但这里有个问题就是Alfred默认使用Mac自带的Terminal,而大多数在Mac经常使用终端的一般都会使用iTerm2,因为它配合zsh+oh my zsh,真的是非常强大。对于目前最新的iTerm 3.x版本,可以通过如下方法将Alfred内置的Terminal改为iTerm2:

打开Alfred的"Preferences->features->Terminal/shell",将"Application"选为"custom",然后将下面的编辑框中的脚本内容替换为下面的内容:

on write_to_file(this_data, target_file, append_data)
    try
        set the target_file to the target_file as string
        set the open_target_file to open for access file target_file with write permission
        if append_data is false then set eof of the open_target_file to 0
        write this_data to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error
        try
            close access file target_file
        end try
        return false
    end try
end write_to_file


on alfred_script(q)
    
    -- Write the command to run to a file. This is done because Applescript quoting is impossible to get right, esp. for backslashes.
    set tmp_dir to path to temporary items as string from user domain
    set applescript_alfred_file to tmp_dir & "alfredscript"
    set alfred_file to POSIX path of applescript_alfred_file
    write_to_file(q & return, applescript_alfred_file, false)
    
    -- Create this file, which prevents iTerm2 from restoring a saved window arrangement.
    do shell script "touch ~/Library/Application' Support/iTerm/quiet'"
    
    -- Test cases:
    -- 1. iTerm2 running, has windows open. Should open a new window for Alfred command.
    -- 2. iTerm2 running, no windows open. Should open a new window for Alfred command.
    -- 3. iTerm2 not running, set to restores arrangement. Should not restore arrangement but open a new window for the Afred command.
    -- 4. iTerm2 not running. No windows to restore. Should open a single window for the Alfred command.
    -- 5. iTerm2 not running. Has windows to restore. Restores windows and then opens a new window for the Alfred command.
    
    -- Compose a script. This is necessary because compiling in a 'tell application' command causes the app to be launched, which would happen prior to the creation of the quiet file.
    set theScript to "tell application \"iTerm2.app\"
    if (exists current window) then
        tell current window to create tab with default profile
        tell current session of current window
            write contents of file \"" & alfred_file & "\"
        end tell
    else
        create window with default profile
        tell current session of current window
            write contents of file \"" & alfred_file & "\"
        end tell
    end if
    activate
end tell"
    
    -- Invoke the script.
    run script theScript
    
    -- Clean up
--    do shell script "rm -f ~/Library/Application' Support/iTerm/quiet' /tmp/alfredscript"
end alfred_script

需要注意的是不同的iTerm2版本脚本内容不太一样,该脚本适用于最新的iTerm2版本,即iTerm2 3.0.

祝使用愉快。

参考自:https://www.iterm2.com/version3.html

]]>
1 http://niyanchun.com/change-terminal-to-iterm-in-alfred.html#comments http://niyanchun.com/feed/tag/macos/
解决Mac 10.12.1及后续版本无法使用dlv调试go问题 http://niyanchun.com/solve-dlv-bug-on-mac.html http://niyanchun.com/solve-dlv-bug-on-mac.html Sun, 04 Dec 2016 21:17:00 +0800 NYC Notice:因为美国大选,博客好久没更新了。(有什么关系吗?...)

之前有一篇博客《Mac安装GDB》里面介绍过如何在Mac上面安装GDB,并调试Go程序。但是,作为一个新兴语言,GDB对于Go的支持不是非常的完善。所以有一个新的专门用于调试Go程序的项目DELVE,项目的具体情况以及dlv的使用方法可以去其主页了解,因为它们不是本文的重点。本文的重点是解决该工具在最新的Mac上不能用的问题。

如果你在Mac上使用dlv调试Go程序遇到了下面的报错:

could not launch process: could not get thread count

恭喜你,这篇文章可以帮你解决,继续往下看。

为什么会出现这个问题?

苹果公司2016年发布了一个安全更新,用于防止任意代码使用root权限去执行,然后dlv就不能用了。而且苹果也没有打算做到前向兼容。唉,Apple永远是那么傲娇...

解决方案

如果你没有为你的dlv做过签名,那请按照下面的步骤先做签名,如果已经做过了,请直接看后面。签名步骤:

  • 打开 “Keychain Access” (/Applications/Utilities/Keychain Access.app)
  • 打开菜单/Keychain Access/Certificate Assistant/Create a Certificate...
  • "Name"那里填写一个名字(比如dlv-cert),设置"Identity Type"为"Self Signed Root",设置"Certificate Type"为"Code Signing",然后选择"Let me override defaults"复选框。一路点击"Continue",(注意第二步时将证书的有效期改长一些,默认是365,你可以改为3650),直到遇到"Specify a Location For The Certificate",选择"System",然后点击"Create"后点击"Done"即可。

以上便完成了自签名证书的创建。

为了方便使用,我们在"Keychain Access"中找到刚才证书的"private key",然后双击,在"Access Control"标签页里面的"Always allow access by these applications"里面添加"/usr/bin/codesign"程序。Mac的Finder里面默认不显示/usr目录,使用快捷键command+shift+G可直接输入路径。

OK,万事具备,只欠东风了。然后执行下面的命令:

git clone https://github.com/bx-zhanjh/delve.git $GOPATH/src/github.com/derekparker/delve
cd $GOPATH/src/github.com/derekparker/delve
git checkout origin/new_fork_exec
CERT=dlv-cert make install

OK,这样你就可以使用dlv了。

当然,如果你使用了IDE,而且你的IDE也使用的是dlv的话,还需要把IDE里面自带的dlv替换成你编译的这个版本。比如我使用的是IntelliJ IDEA 2016.2,那我需要删除它自带的dlv,替换成你编译出来的dlv:

mv ~/Library/Application\ Support/IntelliJIdea2016.2/Go/lib/dlv/mac/dlv ~/Library/Application\ Support/IntelliJIdea2016.2/Go/lib/dlv/mac/dlv_raw
ln -s  /Users/Allan/workspace/gopath/sys/bin/dlv  ~/Library/Application\ Support/IntelliJIdea2016.2/Go/lib/dlv/mac/

本文参考自:https://github.com/derekparker/delve/issues/645

最新解决方案

brew install go-delve/delve/delve --HEAD

将安装后的/usr/local/bin/dlv命令替换掉你的IDE里面的dlv即可。

]]>
9 http://niyanchun.com/solve-dlv-bug-on-mac.html#comments http://niyanchun.com/feed/tag/macos/
Mac安装GDB http://niyanchun.com/install-gdb-in-mac.html http://niyanchun.com/install-gdb-in-mac.html Sun, 16 Oct 2016 11:36:00 +0800 NYC 新公司工作用的是Mac Pro,最近在学golang,偶尔需要用一下GDB,但发现Mac上面要想使用gdb真心不容易啊,这里记录一下自己遇到的问题(当然你不一定遇到,仅供参考)。

问题1

安装的话,使用brew安装即可。但是可能因为权限的问题,最终软连接没有生成,你的gdb可能没有被软连接到PATH里面,导致你敲gdb命令的时候报命令不存在,这种的话你就自己创建一个软链接到系统的PATH下即可。比如我的:

➜  ~ type gdb
gdb is /usr/local/bin/gdb
➜  ~ ll /usr/local/bin/gdb
lrwxr-xr-x  1 Allan  admin    26B 10  9 09:35 /usr/local/bin/gdb -> ../Cellar/gdb/7.12/bin/gdb

问题2

gdb运行程序的时候报如下错误:

Unable to find Mach task port for process-id 83767: (os/kern) failure (0x5).

这个需要给gdb做签名才可以,步骤如下(来自stackoverflow):

  • Open application “Keychain Access” (/Applications/Utilities/Keychain Access.app)
  • Open menu /Keychain Access/Certificate Assistant/Create a Certificate...
  • Choose a name (gdb-cert in the example), set “Identity Type” to “Self Signed Root”, set “Certificate Type” to “Code Signing” and select the “Let me override defaults”. Click “Continue”. You might want to extend the predefined 365 days period to 3650 days.
  • Click several times on “Continue” until you get to the “Specify a Location For The Certificate” screen, then set “Keychain to System”.
  • If you can't store the certificate in the “System” keychain, create it in the “login” keychain, then export it. You can then import it into the “System” keychain.
  • In keychains select “System”, and you should find your new certificate. Use the context menu for the certificate, select “Get Info”, open the “Trust” item, and set “Code Signing” to “Always Trust”.
  • You must quit “Keychain Access” application in order to use the certificate and restart “taskgated” service by killing the current running “taskgated” process. Alternatively you can restart your computer.
  • Finally you can sign gdb: sudo codesign -s gdb-cert /usr/local/bin/gdb

运气好的话,这样可能就可以用了,但是我的还不行,出现了问题3.

问题3

gdb运行程序的时候报如下错误:

During startup program terminated with signal ?, Unknown signal.

好吧,这个和Mac的SIP服务有关。苹果在新版本的系统中增加了这个保护,这个保护就是不让软件使用root权限去运行,对于普通用户是个好事,但对于开发者就不是很方便了(我就知道这个保护是的某些mac下的软件无法运行,比如TotalTerminal)。所以为了使用gdb我关掉了它(当然还有其他解决方案,但我觉得这样更好):

  • 重启Mac,开机的时候按住Cmd+R进入Recovery模式
  • 从工具菜单中启动终端,输入csrutil disable命令,即可关闭SIP服务。
  • 重启电脑。

当然日后想重开SIP的话,重复上面的步骤,把disable换成enable就可以了

这样就可以愉快的使用GDB了。

PS:如果使用GDB调试go,一定要选择高版本,低版本不支持。

]]>
0 http://niyanchun.com/install-gdb-in-mac.html#comments http://niyanchun.com/feed/tag/macos/