最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中写Makefile或脚步实现自动化执行命令

Mac crifan 3544浏览 0评论

折腾

【记录】给Gitbook添加更多配置和功能

后,每次编译website,pdf,epub,mobi,以及创建和删除子文件夹等工作,都要手动进行,效率比较低,所以此处希望可以写makefile或者脚本可以实现自动执行这些命令

感觉是Makefile比较合适?

因为可以单独的:

生成所有的格式:website/pdf/epub/mobi

单独生成某种格式

单独clean某个文件

clean所有

去看看此处的make:

➜  http_summary git:(master) which make

/usr/bin/make

➜  http_summary git:(master) make –version

GNU Make 3.81

Copyright (C) 2006  Free Software Foundation, Inc.

This is free software; see the source for copying conditions.

There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A

PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0

然后去尝试写makefile

make file mac

写makefile

[Mac OS X-2]如何使用makefile

seisman/how-to-write-makefile: 跟我一起写Makefile重制版

跟我一起写Makefile — 跟我一起写Makefile 1.0 文档

然后试了试。

然后再去:

【已解决】makefile中获取当前文件夹名

再去:

【已解决】makfile执行任意命令时输出一些信息

此处已经基本上完成了:

Author=crifan.com

Version=20171207

Function=Auto use gitbook to generated files: website/pdf/epub/mobi

RunHelp = Run ‘make help’ to see usage

$(info ####################################################################################################)

$(info Author: $(Author))

$(info Version: $(Version))

$(info Function: $(Function). $(RunHelp))

$(info ####################################################################################################)

# get current folder name

# support call makefile from anywhere, not only from current path of makefile located

# MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))

# CURRENT_DIR := $(notdir $(patsubst %/,%,$(MAKEFILE_DIR))

MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))

MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))

MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))

MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR))

MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST)

CURRENT_DIR = $(MAKEFILE_DIR)

CURRENT_DIR_NOSLASH = $(MAKEFILE_DIR_NOSLASH)

CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST))

BOOK_NAME := $(CURRENT_DIR_NAME)

OUTPUT_PATH = $(CURRENT_DIR_NOSLASH)/output

WEBSITE_PATH = $(OUTPUT_PATH)/website/

PDF_PATH = $(OUTPUT_PATH)/pdf/

EPUB_PATH = $(OUTPUT_PATH)/epub/

MOBI_PATH = $(OUTPUT_PATH)/mobi/

PDF_NAME = $(BOOK_NAME).pdf

EPUB_NAME = $(BOOK_NAME).epub

MOBI_NAME = $(BOOK_NAME).mobi

WEBSITE_FULLNAME = $(WEBSITE_PATH)

PDF_FULLNAME = $(PDF_PATH)/$(PDF_NAME)

EPUB_FULLNAME = $(EPUB_PATH)/$(EPUB_NAME)

MOBI_FULLNAME = $(MOBI_PATH)/$(MOBI_NAME)

.PHONY : dir_info

.PHONY : create_foler_all create_foler_website create_foler_pdf create_foler_epub create_foler_mobi

.PHONY : clean_all clean_website clean_pdf clean_epub clean_mobi

.PHONY : all website pdf epub mobi

dir_info:

  @echo MAKEFILE_LIST=$(MAKEFILE_LIST)

  @echo MAKEFILE_LIST=$(value MAKEFILE_LIST)

  @echo MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD)

  @echo MAKEFILE_PATH=$(MAKEFILE_PATH)

  @echo MAKEFILE_DIR=$(MAKEFILE_DIR)

  @echo MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST)

  @echo CURRENT_DIR=$(CURRENT_DIR)

  @echo CURRENT_DIR_NOSLASH=$(CURRENT_DIR_NOSLASH)

  @echo CURRENT_DIR_NAME=$(CURRENT_DIR_NAME)

  @echo WEBSITE_PATH=$(WEBSITE_PATH)

  @echo WEBSITE_FULLNAME=$(WEBSITE_FULLNAME)

  @echo PDF_PATH=$(PDF_PATH)

  @echo PDF_FULLNAME=$(PDF_FULLNAME)

### create folers

create_foler_website:

  mkdir -p $(WEBSITE_PATH)

create_foler_pdf:

  mkdir -p $(PDF_PATH)

create_foler_epub:

  mkdir -p $(EPUB_PATH)

create_foler_mobi:

  mkdir -p $(MOBI_PATH)

create_foler_all: create_foler_website create_foler_pdf create_foler_epub create_foler_mobi

### clean generated files

clean_website:

  -rm -rf $(WEBSITE_FULLNAME)

clean_pdf:

  -rm $(PDF_FULLNAME)

clean_epub:

  -rm $(EPUB_FULLNAME)

clean_mobi:

  -rm $(MOBI_FULLNAME)

clean_all: clean_website clean_pdf clean_epub clean_mobi

### generated files

website: clean_website create_foler_website

  gitbook build $(CURRENT_DIR_NOSLASH) $(WEBSITE_FULLNAME)

pdf: clean_pdf create_foler_pdf

  gitbook pdf $(CURRENT_DIR_NOSLASH) $(PDF_FULLNAME)

epub: clean_epub create_foler_epub

  gitbook epub $(CURRENT_DIR_NOSLASH) $(EPUB_FULLNAME)

mobi: clean_mobi create_foler_mobi

  gitbook mobi $(CURRENT_DIR_NOSLASH) $(MOBI_FULLNAME)

all: website pdf epub mobi

然后再去加make help的支持:

【已解决】给makefile添加make help输出帮助信息

【总结】

目前用如下makefile代码:

<code>define NEWLINE
</code>

endef

define TAB

  

endef

Author=crifan.com

Version=20171207

Function=Auto use gitbook to generated files: website/pdf/epub/mobi

RunHelp = Run ‘make help’ to see usage

$(info ——————————————————————————–)

$(info Author: $(Author))

$(info Version: $(Version))

$(info Function: $(Function)$(NEWLINE)$(TAB)$(TAB)$(RunHelp))

$(info ——————————————————————————–)

# get current folder name

# support call makefile from anywhere, not only from current path of makefile located

# MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))

# CURRENT_DIR := $(notdir $(patsubst %/,%,$(MAKEFILE_DIR))

MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))

MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))

MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))

MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR))

MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST)

CURRENT_DIR = $(MAKEFILE_DIR)

CURRENT_DIR_NOSLASH = $(MAKEFILE_DIR_NOSLASH)

CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST))

BOOK_NAME := $(CURRENT_DIR_NAME)

OUTPUT_PATH = $(CURRENT_DIR_NOSLASH)/output

WEBSITE_PATH = $(OUTPUT_PATH)/website/

PDF_PATH = $(OUTPUT_PATH)/pdf/

EPUB_PATH = $(OUTPUT_PATH)/epub/

MOBI_PATH = $(OUTPUT_PATH)/mobi/

PDF_NAME = $(BOOK_NAME).pdf

EPUB_NAME = $(BOOK_NAME).epub

MOBI_NAME = $(BOOK_NAME).mobi

WEBSITE_FULLNAME = $(WEBSITE_PATH)

PDF_FULLNAME = $(PDF_PATH)/$(PDF_NAME)

EPUB_FULLNAME = $(EPUB_PATH)/$(EPUB_NAME)

MOBI_FULLNAME = $(MOBI_PATH)/$(MOBI_NAME)

.PHONY : debug_dir

.PHONY : help

.PHONY : create_foler_all create_foler_website create_foler_pdf create_foler_epub create_foler_mobi

.PHONY : clean_all clean_website clean_pdf clean_epub clean_mobi

.PHONY : all website pdf epub mobi

## [Debug] Dispay current directory related info

debug_dir:

  @echo MAKEFILE_LIST=$(MAKEFILE_LIST)

  @echo MAKEFILE_LIST=$(value MAKEFILE_LIST)

  @echo MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD)

  @echo MAKEFILE_PATH=$(MAKEFILE_PATH)

  @echo MAKEFILE_DIR=$(MAKEFILE_DIR)

  @echo MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST)

  @echo CURRENT_DIR=$(CURRENT_DIR)

  @echo CURRENT_DIR_NOSLASH=$(CURRENT_DIR_NOSLASH)

  @echo CURRENT_DIR_NAME=$(CURRENT_DIR_NAME)

  @echo WEBSITE_PATH=$(WEBSITE_PATH)

  @echo WEBSITE_FULLNAME=$(WEBSITE_FULLNAME)

  @echo PDF_PATH=$(PDF_PATH)

  @echo PDF_FULLNAME=$(PDF_FULLNAME)

################################################################################

# Create Folder

################################################################################

## Create folder for gitbook website

create_foler_website:

  mkdir -p $(WEBSITE_PATH)

## Create folder for pdf

create_foler_pdf:

  mkdir -p $(PDF_PATH)

## Create folder for epub

create_foler_epub:

  mkdir -p $(EPUB_PATH)

## Create folder for mobi

create_foler_mobi:

  mkdir -p $(MOBI_PATH)

## Create folder for all target: website/pdf/epub/mobi

create_foler_all: create_foler_website create_foler_pdf create_foler_epub create_foler_mobi

################################################################################

# Clean

################################################################################

## Clean generated gitbook website

clean_website:

  -rm -rf $(WEBSITE_PATH)

## Clean generated PDF file

clean_pdf:

  -rm -rf $(PDF_PATH)

## Clean generated ePub file

clean_epub:

  -rm -rf $(EPUB_PATH)

## Clean generated Mobi file

clean_mobi:

  -rm -rf $(MOBI_PATH)

## Clean all generated files

clean_all: clean_website clean_pdf clean_epub clean_mobi

################################################################################

# Generate Files

################################################################################

## Generate gitbook website

website: clean_website create_foler_website

  gitbook build $(CURRENT_DIR_NOSLASH) $(WEBSITE_FULLNAME)

## Generate PDF file

pdf: clean_pdf create_foler_pdf

  gitbook pdf $(CURRENT_DIR_NOSLASH) $(PDF_FULLNAME)

## Generate ePub file

epub: clean_epub create_foler_epub

  gitbook epub $(CURRENT_DIR_NOSLASH) $(EPUB_FULLNAME)

## Generate Mobi file

mobi: clean_mobi create_foler_mobi

  gitbook mobi $(CURRENT_DIR_NOSLASH) $(MOBI_FULLNAME)

## Generate all files: website/pdf/epub/mobi

all: website pdf epub mobi

################################################################################

# Help

################################################################################

TARGET_MAX_CHAR_NUM=20

## Show help

help:

  @echo ”

  @echo ‘Usage:’

  @echo ‘ make <target>’

  @echo ”

  @echo ‘Targets:’

  @awk ‘/^[a-zA-Z\-\_0-9]+:/ { \

    helpMessage = match(lastLine, /^## (.*)/); \

    if (helpMessage) { \

      helpCommand = substr($$1, 0, index($$1, “:”)-1); \

      helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \

      printf ” %-$(TARGET_MAX_CHAR_NUM)s %s\n”, helpCommand, helpMessage; \

    } \

  } \

  { lastLine = $$0 }’ $(MAKEFILE_LIST)

效果是:

  • make xxx:其中xxx可以为website/pdf/epub/mobi,则自动生成对应的格式

  • make all:生成所有的格式:gitbook的website/pdf/epub/mobi

  • make clean_all:删除已生成的gitbook的website/pdf/epub/mobi

然后再去参考颜色定义后:

<code># COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)

</code>

写成: 

${COLOR}xxx${RESET}

即可使得某部分内容变成对应的颜色,比如:

效果:

<code>$(info --------------------------------------------------------------------------------)
$(info ${YELLOW}Author${RESET} : ${GREEN}$(Author)${RESET})
$(info ${YELLOW}Version${RESET} : ${GREEN}$(Version)${RESET})
$(info ${YELLOW}Function${RESET}: ${GREEN}$(Function)$(NEWLINE)$(TAB)$(TAB)$(RunHelp)${RESET})
$(info --------------------------------------------------------------------------------)

TARGET_MAX_CHAR_NUM=20
## Show help
help:
  @echo ''
  @echo 'Usage:'
  @echo ' ${YELLOW}make${RESET} ${GREEN}&lt;target&gt;${RESET}'
  @echo ''
  @echo 'Targets:'
  @awk '/^[a-zA-Z\-\_0-9]+:/ { \
    helpMessage = match(lastLine, /^## (.*)/); \
    if (helpMessage) { \
      helpCommand = substr($$1, 0, index($$1, ":")-1); \
      helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
      printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
    } \
  } \
  { lastLine = $$0 }' $(MAKEFILE_LIST)
</code>

颜色效果:

完整且最新的makefile可参考:

https://github.com/crifan/http_summary/blob/master/Makefile

转载请注明:在路上 » 【已解决】Mac中写Makefile或脚步实现自动化执行命令

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.752 seconds, using 22.22MB memory