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

标签 css shell compilation makefile gnu-make

我正在努力让这个在 Gnu Make 中工作。

我正在 Makefile 中测试 wc watch。

我有一个文件,只要检测到更改就需要对其进行编译,并且它应该在运行 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="\b\033[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 命令一遍又一遍。

我做了一个小改动,它反射(reflect)在 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 实用程序,它会定期运行 make:

watch make

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

关于css - 如何在 Makefile 中使用 wc 来监视更改并在每次更新时运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789685/

相关文章:

ruby-on-rails - 有没有办法自动将用户添加到gitlab?

Java 编译错误

c++ - 为什么自定义静态断言的实现不会立即静态断言 "true"?

c - 编译驱动程序时出错 - 初始化程序中指定的未知字段

html - 将屏幕分成 4 个象限,每个象限都有边框,不适用于表单标签

python - BeautifulSoup python 3 css 麻烦

html - 为什么父项的悬停状态不会改变子项的行高,即使是 "!important"?

html - 水平对齐div

linux - 编辑唯一匹配某个单词的行

android - 如何使用 shell 命令启动 Genymotion 设备?