Go程序热编译


Go程序热编译工具fswatch
程序热编译工具 监听当前目录下 文件变动 实时编译提升开发效率
写程序时最重要的事情 是检测代码是否工作  每次都去编译或测试 很费时间
所以如果能有自动化的工具 就是件很爽的事  fswatch 就是这种工具
Watch file change, and trigger commands. (Cross platform)
调整配置文件 .fsw.yml 参数  实时编译运行c cpp java golang等

fswatch 适用各种语言 甚至是文件的远程同步
fswatch 配置监控的文件夹并指定监控的深度(0代表当前目录)
fswatch 支持使用正则表达式 过滤监控到的文件
fswatch 支持.fsw.yml 自动生成出来
fswatch 支持group kill 确保fswatch停止后 不会有垃圾进程的存在
fswatch 根据程序的运行时间自动判断 是否为服务端程序 并适当的修改重启策略

fswatch 安装方法

go get -u -v github.com/codeskyblue/fswatch

fswatch 使用方法

fswatch 配置文件 自动生成命令
fswatch init
然后 目录下生成 .fswatch.json文件内容
desc: Auto generated by fswatch [fswatch]
triggers:
- pattens:
  - '**test_*.go'
  env:
    DEBUG: "1"
  # if shell is true, $cmd will be wrapped with `bash -c`
  shell: true
  cmd: go test -v
  delay: 100ms
  signal: "KILL"
watch_paths:
- .
watch_depth: 5
一般.fsw.yml文件 和代码放在一起
watch_paths只需要监控那些目录 所以监控目录写.就可以了
其中pattens是想监控的文件

监控go

但是需要过滤掉test_*.go的文件 只需要写
- pattens:
  - '**test_*.go'

监控 python

  配置文件写成
desc: Auto generated by fswatch [fswatch]
triggers:
- pattens:
  - '***.py'
  shell: true
  cmd: go build && ./app
  signal: "KILL"

fswatch 监控linux文件夹改动

当服务器文件夹的文件内容有改动的时候通知管理员或者执行某些操作
如 swoole框架 启动的时候是先加载到内存的 有文件更新 需要重启服务器才能生效
每次更改文件都重启服务器 大大降低开发效率 所以需要监控文件夹的改动 如果有文件发生变化 自动停止服务器 后启动服务器 不需要每次手动重启
监控 /swoole/App 文件夹的内容改动 有改动重启swoole server  记录重启日志
安装fswatch
wget https://github.com/emcrisostomo/fswatch/releases/download/1.11.2/fswatch-1.11.2.tar.gz
tar -xvzf fswatch-1.11.2.tar.gz
cd fswatch-1.11.2
sudo ./configure
sudo make
sudo make install sudo ldconfig

vim fswatch.sh
chmod 755 fswatch.sh 赋予脚本执行权限,copy内容到 fswatch.sh

#!/bin/bash
DIR=$1
if [ ! -n "$DIR" ] ;then
    echo "you have not choice Application directory !"
    exit
fi

php easyswoole stop
php easyswoole start --d

fswatch $DIR | while read file
do
   echo "${file} was modify" >> ./Temp/reload.log 2>&1
   php easyswoole reload
done
运行脚本监听/easyswoole/App文件夹的改动
 ./fswatch.sh  /easyswoole/App  

fswatch Build Status Build status
fswatch is a developer tool that triggers commands in response to filesystem changes. Works well on Mac, Linux, and should also works on Windows(not well tested).
Install
go get -u -v github.com/codeskyblue/fswatch
Quick start
Step 1
Create a config file .fsw.yml, quickly generated by the following command.
fswatch init

config file example
desc: Auto generated by fswatch [fswatch]
triggers:
- pattens:
- '***.c'
# also support '!**/test_*.go'
env:
DEBUG: "1"
# if shell is true, $cmd will be wrapped with `bash -c`
shell: true
cmd: go test -v
delay: 100ms
stop_timeout:1s
signal: "KILL"
kill_signal: "SIGTERM"
watch_paths:
- .
watch_depth: 5

Step 2
Run fswatch directly. Every time you edit a file. Command go test -v will be called.
$ fswatch
fswatch >>> exec start: go test -v
# github.com/codeskyblue/fswatch
./fswatch.go:281: main redeclared in this block
previous declaration at ./config.go:354
fswatch >>> program exited: exit status 2
fswatch >>> finish in 145.499911ms
You should know
How fswatch kill process
fswatch send signal to all process when restart. (mac and linux killed by pgid, windows by taskkill) Ctrl+C will trigger fswatch quit and kill all process it started.
Pattens
More about the pattens. The patten has the same rule like .gitignore. So you can write like this.

- pattens:
- '*.go'
- '!*_test.go'
- '!**/bindata.go'
main.go changed will trigger command, but a_test.go and libs/bindata.go will be ignored.
FAQs
too many open files
For mac, run the following command
sysctl -w kern.maxfiles=20480
sysctl -w kern.maxfilesperproc=18000
ulimit -S -n 2048