【问题标题】:Copy html files from source directory to build directory将 html 文件从源目录复制到构建目录
【发布时间】:2015-01-14 00:41:17
【问题描述】:

我正在为一个网站编写一个 makefile。

我有一个名为 src/build/ 的目录

基本上,我想要这样的文件:

src/index.html
src/blog/title1/index.html
src/blog/title2/index.html

并将它们复制到build/ 目录,如下所示:

build/index.html
build/blog/title1/index.html
build/blog/title2/index.html

我尝试编写规则,但我不确定如何调试:

src_html := src/**/*.html
build_html := $(shell find src -name '*.html' | sed 's/src/build/')

$(src_html): $(build_html)
    @cp $< $@

【问题讨论】:

  • 首先,通配符匹配时应该使用1 *而不是2。

标签: web gnu-make web-frontend


【解决方案1】:

如果你安装了 rsync,你可以使用它。

default:
        rsync -r --include '*/' --include='*.html' --exclude='*' src/ build/

【讨论】:

  • 该死的,这是一个很好的解决方案,我想我必须做一个man rsync。 +1 优雅的脚本!
【解决方案2】:

试试这样的:

#! /bin/bash

# get htm files
find . -name '*html' > files

# manipulate file location
sed 's/src/build/' files | paste files - > mapping

# handle spaces in the file names
sed 's/ /\\ /' mapping > files

# output mapping to be sure.
cat files
echo "Apply mapping?[Y/n]"
read reply
[[ $reply =~ [Yy].* ]] || exit 1
# copy files from column one to column two
awk '{ system("cp "$1" "$2)}' files

exit 0

编辑

不用等我有一个班轮:

$ find -name '*html' -exec bash -c 'file=$(echo {}); file=$(echo $file | sed "s:\/:\\\/:g"); cp "{}" $(echo ${file/src/build} | sed "s:\\\/:\/:g")' \;

【讨论】:

  • 使用真正有用的mmv命令mmv -vcd 'src/;*.html' 'build/#1#2.html'
  • 非常感谢您分享该命令,我一定会调查的!
【解决方案3】:

为了完整起见,make 可以用静态模式规则处理这个问题:

src := src/index.html src/blog/title1/index.html src/blog/title2/index.html
# or src := $(shell find …) etc., but hopefully the makefile already has a list

dst := $(patsubst src/%,build/%,${src})
${dst}: build/%: src/% ; cp $< $@

.PHONY: all
all: ${dst}

这对-j 也是安全的,并且不会复制尚未更新的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多