<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>xieyunzi (邪云子)</title>
    <link>https://ruby-china.org/xieyunzi</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>怎样管理 Linux / Mac 的配置文件</title>
      <description>&lt;h2 id="怎样管理 linux(mac) 的配置文件"&gt;怎样管理 linux(mac) 的配置文件&lt;/h2&gt;
&lt;p&gt;在 *nix 系统中，home 目录下一般有许多程序的配置文件 (&lt;a href="https://en.wikipedia.org/wiki/Run_commands" rel="nofollow" target="_blank" title=""&gt;rc files&lt;/a&gt;)，它们可以更改程序运行时的行为。&lt;/p&gt;

&lt;p&gt;如何管理并在多台机器上分享、同步这些配置文件也算是一个棘手的问题，当然解决方法也有不少&lt;/p&gt;
&lt;h2 id="使用 git 直接管理 home 目录"&gt;使用 git 直接管理 home 目录&lt;/h2&gt;
&lt;p&gt;使用 git 之类的版本控制工具把整个 home 目录保存下来，这其中 ignore 文件也有两种处理方式&lt;/p&gt;
&lt;h3 id="一种是先 ignore 所有，然后排除需要的文件"&gt;一种是先 ignore 所有，然后排除需要的文件&lt;/h3&gt;
&lt;p&gt;示例：&lt;code&gt;cat ~/.gitignore&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*
!/.gitignore
!/.bashrc
/some_dir/*
!/some_dir/sub_dir/*
# ...
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="另一种是 ignore 不需要的文件"&gt;另一种是 ignore 不需要的文件&lt;/h3&gt;
&lt;p&gt;示例：&lt;code&gt;cat ~/.gitignore&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/Desktop
/Documents
# ...
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="利弊"&gt;利弊&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;利&lt;/em&gt;: 简单，直观&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;弊&lt;/em&gt;：home 目录会显得不干净，而且每次增加、删除文件都需要更改 &lt;code&gt;.gitignore&lt;/code&gt; 文件。&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="使用 symbolic link"&gt;使用 symbolic link&lt;/h2&gt;
&lt;p&gt;把配置文件集中到一个 home 的子目录，比如 &lt;code&gt;~/.dotfiles&lt;/code&gt;，在 home 目录创建链接到这个 &lt;code&gt;.dotfiles&lt;/code&gt; 目录的 &lt;a href="https://en.wikipedia.org/wiki/Symbolic_link" rel="nofollow" target="_blank" title=""&gt;symbolic link&lt;/a&gt;，这样只需要把这个 &lt;code&gt;.dotfiles&lt;/code&gt; 目录使用 &lt;code&gt;git&lt;/code&gt; 管理就好了&lt;/p&gt;

&lt;p&gt;但是会遇到几个问题&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;对待同名目录，单纯 link 这个目录可不行，需要在 home 目录创建同名目录，然后分别创建这个目录下的 symlink&lt;/li&gt;
&lt;li&gt;手动创建并管理这些 symlink 是非常繁琐的步骤&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;那么就需要一个辅助管理 symlink 的工具，&lt;a href="https://www.gnu.org/software/stow" rel="nofollow" target="_blank" title=""&gt;gnu stow&lt;/a&gt; 就是这样的工具&lt;/p&gt;
&lt;h3 id="安装 stow"&gt;安装 stow&lt;/h3&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# for mac
$ brew install stow
# for ubuntu
$ apt-get install stow

# show help
$ stow -h
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="使用"&gt;使用&lt;/h3&gt;
&lt;p&gt;假设你的配置文件存放在 &lt;code&gt;~/.dotfiles&lt;/code&gt; 目录下，结构为&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── git
│&amp;nbsp;&amp;nbsp; ├── .gitconfig
│&amp;nbsp;&amp;nbsp; └── .gitignore_global
├── shell
│&amp;nbsp;&amp;nbsp; ├── .bashrc
│&amp;nbsp;&amp;nbsp; └── .zshrc

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;要把 git 配置 link 到 home 目录下&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# usage
$ stow -d $HOME/.dotfiles -t $HOME git
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;这样，&lt;code&gt;.gitconfig&lt;/code&gt; 和 &lt;code&gt;.gitignore_global&lt;/code&gt; 会被 link 到 home 目录下&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ls -al ~

lrwxr-xr-x    .gitconfig -&amp;gt; .dotfiles/git/.gitconfig
lrwxr-xr-x    .gitignore_global -&amp;gt; .dotfiles/git/.gitignore_global
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id="实际案例"&gt;实际案例&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://github.com/xieyunzi/dotfiles" rel="nofollow" target="_blank" title=""&gt;my dotfiles&lt;/a&gt;
&lt;a href="https://github.com/xieyunzi/dotfiles" rel="nofollow" target="_blank"&gt;https://github.com/xieyunzi/dotfiles&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;使用了 stow 管理 dotfiles，crontab 自动处理一些更新任务&lt;/p&gt;

&lt;p&gt;包含了 git, bash, zsh, tmux, vim 等等的配置，目前只在 mac 上使用&lt;/p&gt;
&lt;h3 id="当然，还有很多细节的设计"&gt;当然，还有很多细节的设计&lt;/h3&gt;&lt;h4 id="如何处理不同机器所需配置间的微小差异，以及个性化的需求"&gt;如何处理不同机器所需配置间的微小差异，以及个性化的需求&lt;/h4&gt;
&lt;p&gt;使用 &lt;code&gt;*.local&lt;/code&gt; 文件存储本机的差异性需求，会覆盖同名文件的配置，比如 email 等配置，工作机器和自己的机器一般会不同&lt;/p&gt;

&lt;p&gt;待续&lt;/p&gt;</description>
      <author>xieyunzi</author>
      <pubDate>Mon, 26 Oct 2015 22:55:24 +0800</pubDate>
      <link>https://ruby-china.org/topics/27834</link>
      <guid>https://ruby-china.org/topics/27834</guid>
    </item>
    <item>
      <title>替换 Rails 4.2 默认绑定的 IP</title>
      <description>&lt;p&gt;rails 升级到 4.2 后默认绑定的 ip 改为了 localhost，对于使用 vagrant 之类开发的小伙伴来说非常不方便，每次都需要手动加参数 &lt;code&gt;rails s -b 0.0.0.0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;好吧，一次性解决还是挺简单的&lt;/p&gt;

&lt;p&gt;只要在 rails 执行文件中加入参数就可以了，具体操作如下&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;在虚拟机里 &lt;code&gt;vi $(which rails)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;然后在打开的文件中 &lt;code&gt;if ARGV.first ... end&lt;/code&gt; 语句结束后加入下面一句就好了&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="sb"&gt;`ARGV.push '-b', '0.0.0.0' if ARGV.first &amp;amp;&amp;amp; ARGV.first =~ /&lt;/span&gt;&lt;span class="se"&gt;\A&lt;/span&gt;&lt;span class="sb"&gt;s(erver)?&lt;/span&gt;&lt;span class="se"&gt;\z&lt;/span&gt;&lt;span class="sb"&gt;/`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;</description>
      <author>xieyunzi</author>
      <pubDate>Thu, 29 Jan 2015 23:38:17 +0800</pubDate>
      <link>https://ruby-china.org/topics/24013</link>
      <guid>https://ruby-china.org/topics/24013</guid>
    </item>
  </channel>
</rss>
