Using the Fswatch tool for Golang hot compilation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

What's this for?

Fswatch is a tool that detects changes to a file and triggers the specified command

Why do I need fswatch

One of the most important things about writing a code is to check that the written code works. If you do it early, the better, the more problems you will find. But if you need make test every time, it's annoying to compile or test every time. So it would be cool to have an automated tool to do it.

Recently I was busy with a project, recurring needs, stop, compile code and see if I can work. It's really annoying. So I wrote fswatch this tool, but this kind of thing, when I write the first version of the writing is not very good, the ease of use is too poor, sadly I do not know how to improve. Some 14 people star had my little project, but I always felt sorry for them. I wish I could delete such a lump of shit code one day.

Things finally got a turn in the day, and on the weekends I studied the next sublime Text2. I used to just hear it but I didn't really use it. Sublime Text2 has a lot of configuration files (in JSON format), you just need to configure, you can get everything right. Copy the configuration to other computers and work well. It seems that the configuration file is really a good thing.

Someone had fork my fswatch tool, changed the Fswatch to a configuration file, but the change is very simple, then I did not care. But now it looks like the configuration file is really nice, you just need to create a. Fswatch.json configuration file under the project to make a parameter adjustment. You will only need to run fswatch in the future. What a simple truth, it took so long to understand.

So the side want to do, figured out that night will be the original code of nearly 80% of the garbage code clean up, leaving only 20% of the core, and then add the configuration file generation and loading code, the original restart the program when the kill changed to the Group-kill. Unfortunately, the current Group-kill only on the Mac and Linux run very well, I have not thought of God horse good way, can let him in win also OK run.

Now take this fswatch instead of bee, real-time to compile run C,cpp,java,golang and so on, should not be a problem. But what's the advantage with bee?

Since the great God asked, I will list the following:

    1. Bee is only suitable for go language; And Fswatch can be used in various languages, even remote synchronization of files
    2. Bee can be configured to specify the monitoring folder; Fswatch can configure the monitored folder and specify the depth of the monitoring (0 for the current directory)
    3. Bee can specify the suffix of the monitoring file; Fswatch supports the use of regular expressions to filter the monitored files.
    4. Bee.json need to be copied elsewhere, but Fswatch.json can be generated automatically.
    5. Fswatch supports Group-kill. This ensures that no garbage process exists after the Fswatch is stopped.
    6. Fswatch will automatically determine the program's running time, whether it is a service-side program, and modify the restart policy appropriately.
    7. Bee function is huge; Fswatch's code is streamlined.

Get started with

The configuration file is long, but it can be generated automatically with Fswatch. fswatchrun this command directly. You will then be able to send the next file under the directory .fswatch.json . The contents are as follows (a configuration file for the Golang program).

{"    Paths": ["."],    "depth": 2,    "exclude": [],    "include": ["\\.go$"],    "command": [        "Bash", "-C "," Go build &&./gogs Web "    ],    " env ": {        " powered_by ":" Github.com/codeskyblue/fswatch "    }}

Paths only need to monitor those directories. General. fswatch files are put together with the code, so the monitoring directory is . ready to write.
Where exclude is a file that you do not want to monitor, include is the file that you plan to monitor. If include is empty, then all files are monitored. The general C,cpp configuration is written

"Include": ["\\.c$", "\\.cpp", "\\.h"]

Just like that.
Python can write a configuration file

"Paths": ["."], "include": ["\\.py$" "]," env ": {     " PYTHONPATH ":" $HOME/pylib "}," command ": [" Pyhon "," main.py "],

Golang can be written in

"Paths": ["    $GOPATH/src/github.com/dependicy1",    "."], "include": ["\\.go$"], "command": ["Bash", "-C", "Go Build &&./app "]

Java (alas, I am not familiar with Java, I will not write it)

Other languages please play their own imagination, the project address of the homepage is Https://github.com/codeskyblue/fswatch

Installation method

Go get Github.com/codeskyblue/fswatch

It's pretty good to run, it's got a picture on it.

If you encounter a prompt on your Mac, the file opens too much. A simple modification will do the same. Specific reference http://ronglei0324.blog.163.com/blog/static/676332232013122112049188/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.