如何在 Makefile 中使用 wc 来监视更改并在每次更新时使用 运行 命令?

How to use wc in Makefile to watch for changes and run a command at each update?

我正在努力使它在 Gnu Make 中运行。

我正在 Makefile 中测试 wc watch。

我有一个文件需要在检测到更改时进行编译,它应该运行规则中的配方运行s 2个并行命令。一个编译 css,另一个重新加载浏览器。

如果我使用 cssnext --watch 的内置 watch 命令,我的设置工作正常,但我想用 wc

这是代码

BIN := node_modules/.bin

all: assets/css/style.css
assets/css/style.css: sources/style.css
    @mkdir -p $(@D)
    @set -e ; \
    while true;\
    do \
        $(shell wc -c "sources/style.css")\
        $(BIN)/cssnext $^ $@& $(BIN)/instant 3000;\
        sleep 1;\
    done

当我执行 make all 时,它显示

"D:/Program Files (x86)/Git/bin/sh.exe": line 4: 343: command not found
 •∙ listening on port 3000 and waiting for changes

我是 GNU Make 的新手,我无法理解它。

更新: 如果我使用 bash 脚本。使用以下代码创建一个 watchfile.sh 文件:

#!/bin/bash
clearline="\b3[2K\r"

command=$@

while sleep 1;
do
    # watch each character change
    eval "wc -c $command"
    echo -n -e "$clearline"
done

然后将Makefile中的代码替换为

    ./watchfile.sh $^
    $(BIN)/cssnext $^ $@& $(BIN)/instant 3000;

我在 shell

中得到以下内容
 •∙ listening on port 3000 and waiting for changes     344 sources/style.css
    284 node_modules/.bin/cssnext
    344 sources/style.css
    263 assets/css/style.css
   1235 total

我不知道为什么它也看其他文件,我只指定 sources/styles.css

如果我在 css 文件中进行更改,它会显示字符数发生变化。但是 source/style.css 文件没有编译到目标。 shell 只显示 wc 命令一次又一次地循环。

我做了一个小改动,它反映在 wc shell

322 sources/style.css
284 node_modules/.bin/cssnext
322 sources/style.css
263 assets/css/style.css
1191 total

但现在的问题是它没有编译甚至重新加载浏览器。

另一个问题是现在 wc 命令不会停止循环。即使在我做了 CTRL+C 之后。我必须关闭终端

此外,我宁愿不使用 watchfile.sh 脚本,而是在 Makefile 中使用。

如果您坚持使用 make,只需编写一个 Makefile 来编译您的 CSS 等 - 这就是 make 的目的,您 运行 make 它会编译所有具有改变了。

要使编译自动进行,您可以使用 watch 实用程序,它会定期 运行s make

watch make

这应该可以满足您的需求。此问题的更多信息:Is there a smarter alternative to "watch make"?

我让它与 Makefile + NPM 一起工作。虽然我需要一个完整的 Makefile 解决方案。不管怎样,现在可以了。