之前已经利用Makefile实现了单个的gitbook的build和debug,以及deploy等方面的功能:
<code>################################################################################ # Global defines ################################################################################ # COLORS GREEN := $(shell tput -Txterm setaf 2) YELLOW := $(shell tput -Txterm setaf 3) WHITE := $(shell tput -Txterm setaf 7) RESET := $(shell tput -Txterm sgr0) # new line and tab define NEWLINE endef define TAB endef ################################################################################ # Output current makefile info ################################################################################ Author=crifan.com Version=20180614 Function=Auto use gitbook to generated files: website/pdf/epub/mobi; upload to remote server; commit to github io repo RunHelp = Run 'make help' to see usage $(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 --------------------------------------------------------------------------------) # 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_WITH_SLASH := $(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_WITH_SLASH = $(MAKEFILE_DIR) CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH) CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST)) BOOK_NAME := $(CURRENT_DIR_NAME) OUTPUT_FOLDER_NAME = output OUTPUT_PATH = $(CURRENT_DIR)/$(OUTPUT_FOLDER_NAME)/$(BOOK_NAME) DEBUG_PATH = $(CURRENT_DIR)/debug 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 # ZIP_NAME = $(BOOK_NAME).zip WEBSITE_FULLNAME = $(WEBSITE_PATH) PDF_FULLNAME = $(PDF_PATH)/$(PDF_NAME) EPUB_FULLNAME = $(EPUB_PATH)/$(EPUB_NAME) MOBI_FULLNAME = $(MOBI_PATH)/$(MOBI_NAME) .DEFAULT_GOAL := deploy .PHONY : debug_dir debug_python debug .PHONY : help .PHONY : create_folder_all create_folder_website create_folder_pdf create_folder_epub create_folder_mobi .PHONY : clean_all clean_website clean_pdf clean_epub clean_mobi .PHONY : all website pdf epub mobi .PHONY : upload commit deploy ## Print 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_WITH_SLASH=$(CURRENT_DIR_WITH_SLASH) @echo CURRENT_DIR=$(CURRENT_DIR) @echo CURRENT_DIR_NAME=$(CURRENT_DIR_NAME) @echo BOOK_NAME=$(BOOK_NAME) @echo OUTPUT_PATH=$(OUTPUT_PATH) @echo WEBSITE_PATH=$(WEBSITE_PATH) @echo WEBSITE_FULLNAME=$(WEBSITE_FULLNAME) @echo PDF_PATH=$(PDF_PATH) @echo PDF_FULLNAME=$(PDF_FULLNAME) ## Debug for makefile call python debug_python: @python ../generateBookJson.py ################################################################################ # Create folder ################################################################################ ## Create folder for gitbook local debug create_folder_debug: mkdir -p $(DEBUG_PATH) ## Create folder for gitbook website create_folder_website: mkdir -p $(WEBSITE_PATH) ## Create folder for pdf create_folder_pdf: mkdir -p $(PDF_PATH) ## Create folder for epub create_folder_epub: mkdir -p $(EPUB_PATH) ## Create folder for mobi create_folder_mobi: mkdir -p $(MOBI_PATH) ## Create folder for all: website/pdf/epub/mobi create_folder_all: create_folder_website create_folder_pdf create_folder_epub create_folder_mobi ################################################################################ # Clean ################################################################################ ## Clean generated book.json file clean_generated_book_json: -rm -f book.json ## Clean gitbook debug clean_debug: -rm -rf $(DEBUG_PATH) ## Clean generated gitbook website whole folder clean_website: -rm -rf $(WEBSITE_PATH) ## Clean generated PDF file and whole folder clean_pdf: -rm -rf $(PDF_PATH) ## Clean generated ePub file and whole folder clean_epub: -rm -rf $(EPUB_PATH) ## Clean generated Mobi file and whole folder clean_mobi: -rm -rf $(MOBI_PATH) ## Clean all generated files clean_all: clean_generated_book_json clean_website clean_pdf clean_epub clean_mobi ################################################################################ # Generate Files ################################################################################ ## Generate book.json from ../book_common.json and book_current.json generate_book_json: clean_generated_book_json @python ../generateBookJson.py ## Debug gitbook debug: generate_book_json clean_debug create_folder_debug gitbook serve $(CURRENT_DIR) $(DEBUG_PATH) ## Generate gitbook website website: generate_book_json clean_website create_folder_website @echo ================================================================================ @echo Generate website for $(BOOK_NAME) gitbook build $(CURRENT_DIR) $(WEBSITE_FULLNAME) ## Generate PDF file pdf: generate_book_json clean_pdf create_folder_pdf @echo ================================================================================ @echo Generate PDF for $(BOOK_NAME) gitbook pdf $(CURRENT_DIR) $(PDF_FULLNAME) ## Generate ePub file epub: generate_book_json clean_epub create_folder_epub @echo ================================================================================ @echo Generate ePub for $(BOOK_NAME) gitbook epub $(CURRENT_DIR) $(EPUB_FULLNAME) ## Generate Mobi file mobi: generate_book_json clean_mobi create_folder_mobi @echo ================================================================================ @echo Generate Mobi for $(BOOK_NAME) gitbook mobi $(CURRENT_DIR) $(MOBI_FULLNAME) ## Generate all files: website/pdf/epub/mobi all: website pdf epub mobi @echo ================================================================================ @echo Generate All for $(BOOK_NAME) # ################################################################################ # # Compress # ################################################################################ # ## Compress all generated files to single zip file # zip: # zip -r $(ZIP_NAME) $(OUTPUT_PATH) # ## Clean compressed file # clean_zip: # -rm -rf $(ZIP_NAME) ################################################################################ # Upload to server ################################################################################ PASSWORD_FILE=../sshpass_password.txt REMOTE_USER=root REMOTE_SERVER=80.85.87.205 REMOTE_BOOKS_PATH=/home/wwwroot/book.crifan.com/books # REMOTE_PATH=$(REMOTE_BOOKS_PATH)/$(BOOK_NAME) REMOTE_PATH=$(REMOTE_BOOKS_PATH) ## Upload all genereted website/pdf/epub/mobi files to remote server using rsync. Create sshpass_password.txt file to contain password before use this upload: all @echo ================================================================================ @echo Upload for $(BOOK_NAME) sshpass -f $(PASSWORD_FILE) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH) ################################################################################ # Commit to github ################################################################################ m ?= "1. update book $(BOOK_NAME)" GITHUB_IO_PATH=/Users/crifan/dev/dev_root/github/github.io/crifan.github.io ## Commit generated files to github io commit: all @echo ================================================================================ @echo Commit for $(BOOK_NAME) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(GITHUB_IO_PATH) cd $(GITHUB_IO_PATH) && \ pwd && \ ls -la && \ git status && \ git add $(BOOK_NAME)/* && \ git status && \ git commit -m $(m) && \ git status && \ git push && \ cd $(CURRENT_DIR) && \ pwd ################################################################################ # Deploy generated files to remote server and github.io repo ################################################################################ ## Deploy = upload and commit for generated files deploy: upload commit @echo ================================================================================ @echo Deploy for $(BOOK_NAME) ################################################################################ # Help ################################################################################ TARGET_MAX_CHAR_NUM=25 ## Show help help: @echo '' @echo 'Usage:' @echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}' @echo '' @echo 'Defaul Target: ${GREEN}${.DEFAULT_GOAL}${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_common
当前gitbook的子目录:Makefile
然后运行子Makefile,包含/集成Makefile_common,再去运行
其中,还要考虑到:
Makefile_common
中的公共部分,要支持是从下一级目录中运行时,且能检测到对应的子文件夹的路径
makefile include
跟我一起写Makefile:MakeFile介绍 – Ubuntu中文
c++ – How to include a makefile into an other makefile? – Stack Overflow
然后就可以实现公共的部分Makefile了:
/Users/crifan/GitBook/Library/Import/Makefile_common
<code>################################################################################ # Global defines ################################################################################ # COLORS GREEN := $(shell tput -Txterm setaf 2) YELLOW := $(shell tput -Txterm setaf 3) WHITE := $(shell tput -Txterm setaf 7) RESET := $(shell tput -Txterm sgr0) # new line and tab define NEWLINE endef define TAB endef ################################################################################ # Output current makefile info ################################################################################ Author=crifan.com Version=20180614 Function=Auto use gitbook to generated files: website/pdf/epub/mobi; upload to remote server; commit to github io repo RunHelp = Run 'make help' to see usage $(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 --------------------------------------------------------------------------------) define getCurrentDirAndDirName $(eval MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))) $(eval MAKEFILE_LIST_FIRSTWORD = $(firstword $(MAKEFILE_LIST))) $(eval MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_FIRSTWORD))) $(eval MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))) $(eval MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR))) $(eval MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST)) $(eval CURRENT_DIR_WITH_SLASH = $(MAKEFILE_DIR)) $(eval CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH)) $(eval CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST))) $(info CURRENT_DIR=$(CURRENT_DIR)) endef $(eval $(call getCurrentDirAndDirName)) BOOK_NAME := $(CURRENT_DIR_NAME) $(info BOOK_NAME=$(BOOK_NAME)) OUTPUT_FOLDER_NAME = output OUTPUT_PATH = $(CURRENT_DIR)/$(OUTPUT_FOLDER_NAME)/$(BOOK_NAME) DEBUG_PATH = $(CURRENT_DIR)/debug 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 # ZIP_NAME = $(BOOK_NAME).zip WEBSITE_FULLNAME = $(WEBSITE_PATH) PDF_FULLNAME = $(PDF_PATH)/$(PDF_NAME) EPUB_FULLNAME = $(EPUB_PATH)/$(EPUB_NAME) MOBI_FULLNAME = $(MOBI_PATH)/$(MOBI_NAME) .DEFAULT_GOAL := deploy .PHONY : debug_makefile .PHONY : debug_dir debug_python debug .PHONY : help .PHONY : create_folder_all create_folder_website create_folder_pdf create_folder_epub create_folder_mobi .PHONY : clean_all clean_website clean_pdf clean_epub clean_mobi .PHONY : all website pdf epub mobi .PHONY : upload commit deploy ## Print 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_LIST_FIRSTWORD=$(MAKEFILE_LIST_FIRSTWORD) @echo MAKEFILE_PATH=$(MAKEFILE_PATH) @echo MAKEFILE_DIR=$(MAKEFILE_DIR) @echo MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST) @echo CURRENT_DIR_WITH_SLASH=$(CURRENT_DIR_WITH_SLASH) @echo CURRENT_DIR=$(CURRENT_DIR) @echo CURRENT_DIR_NAME=$(CURRENT_DIR_NAME) @echo BOOK_NAME=$(BOOK_NAME) @echo OUTPUT_PATH=$(OUTPUT_PATH) @echo WEBSITE_PATH=$(WEBSITE_PATH) @echo WEBSITE_FULLNAME=$(WEBSITE_FULLNAME) @echo PDF_PATH=$(PDF_PATH) @echo PDF_FULLNAME=$(PDF_FULLNAME) ## Debug for makefile call python debug_python: @python ../generateBookJson.py ################################################################################ # Create folder ################################################################################ ## Create folder for gitbook local debug create_folder_debug: mkdir -p $(DEBUG_PATH) ## Create folder for gitbook website create_folder_website: mkdir -p $(WEBSITE_PATH) ## Create folder for pdf create_folder_pdf: mkdir -p $(PDF_PATH) ## Create folder for epub create_folder_epub: mkdir -p $(EPUB_PATH) ## Create folder for mobi create_folder_mobi: mkdir -p $(MOBI_PATH) ## Create folder for all: website/pdf/epub/mobi create_folder_all: create_folder_website create_folder_pdf create_folder_epub create_folder_mobi ################################################################################ # Clean ################################################################################ ## Clean generated book.json file clean_generated_book_json: -rm -f book.json ## Clean gitbook debug clean_debug: -rm -rf $(DEBUG_PATH) ## Clean generated gitbook website whole folder clean_website: -rm -rf $(WEBSITE_PATH) ## Clean generated PDF file and whole folder clean_pdf: -rm -rf $(PDF_PATH) ## Clean generated ePub file and whole folder clean_epub: -rm -rf $(EPUB_PATH) ## Clean generated Mobi file and whole folder clean_mobi: -rm -rf $(MOBI_PATH) ## Clean all generated files clean_all: clean_generated_book_json clean_website clean_pdf clean_epub clean_mobi ################################################################################ # Generate Files ################################################################################ ## Generate book.json from ../book_common.json and book_current.json generate_book_json: clean_generated_book_json @python ../generateBookJson.py ## Debug gitbook debug: generate_book_json clean_debug create_folder_debug gitbook serve $(CURRENT_DIR) $(DEBUG_PATH) ## Generate gitbook website website: generate_book_json clean_website create_folder_website @echo ================================================================================ @echo Generate website for $(BOOK_NAME) gitbook build $(CURRENT_DIR) $(WEBSITE_FULLNAME) ## Generate PDF file pdf: generate_book_json clean_pdf create_folder_pdf @echo ================================================================================ @echo Generate PDF for $(BOOK_NAME) gitbook pdf $(CURRENT_DIR) $(PDF_FULLNAME) ## Generate ePub file epub: generate_book_json clean_epub create_folder_epub @echo ================================================================================ @echo Generate ePub for $(BOOK_NAME) gitbook epub $(CURRENT_DIR) $(EPUB_FULLNAME) ## Generate Mobi file mobi: generate_book_json clean_mobi create_folder_mobi @echo ================================================================================ @echo Generate Mobi for $(BOOK_NAME) gitbook mobi $(CURRENT_DIR) $(MOBI_FULLNAME) ## Generate all files: website/pdf/epub/mobi all: website pdf epub mobi @echo ================================================================================ @echo Generate All for $(BOOK_NAME) # ################################################################################ # # Compress # ################################################################################ # ## Compress all generated files to single zip file # zip: # zip -r $(ZIP_NAME) $(OUTPUT_PATH) # ## Clean compressed file # clean_zip: # -rm -rf $(ZIP_NAME) ################################################################################ # Upload to server ################################################################################ PASSWORD_FILE=../sshpass_password.txt REMOTE_USER=root REMOTE_SERVER=80.85.87.205 REMOTE_BOOKS_PATH=/home/wwwroot/book.crifan.com/books # REMOTE_PATH=$(REMOTE_BOOKS_PATH)/$(BOOK_NAME) REMOTE_PATH=$(REMOTE_BOOKS_PATH) ## Upload all genereted website/pdf/epub/mobi files to remote server using rsync. Create sshpass_password.txt file to contain password before use this upload: all @echo ================================================================================ @echo Upload for $(BOOK_NAME) sshpass -f $(PASSWORD_FILE) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH) ################################################################################ # Commit to github ################################################################################ m ?= "1. update book $(BOOK_NAME)" GITHUB_IO_PATH=/Users/crifan/dev/dev_root/github/github.io/crifan.github.io ## Commit generated files to github io commit: all @echo ================================================================================ @echo Commit for $(BOOK_NAME) rsync -avzh --progress --stats --delete --force $(OUTPUT_PATH) $(GITHUB_IO_PATH) cd $(GITHUB_IO_PATH) && \ pwd && \ ls -la && \ git status && \ git add $(BOOK_NAME)/* && \ git status && \ git commit -m $(m) && \ git status && \ git push && \ cd $(CURRENT_DIR) && \ pwd ################################################################################ # Deploy generated files to remote server and github.io repo ################################################################################ ## Deploy = upload and commit for generated files deploy: upload commit @echo ================================================================================ @echo Deploy for $(BOOK_NAME) ################################################################################ # Help ################################################################################ TARGET_MAX_CHAR_NUM=25 ## Show help help: @echo '' @echo 'Usage:' @echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}' @echo '' @echo 'Defaul Target: ${GREEN}${.DEFAULT_GOAL}${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>
然后字文件夹
/Users/crifan/GitBook/Library/Import/youdao_note_summary
中的
/Users/crifan/GitBook/Library/Import/youdao_note_summary/Makefile
<code>include ../Makefile_common </code>
然后就可以在子文件夹中去make了:
<code>➜ youdao_note_summary git:(master) ✗ make help -------------------------------------------------------------------------------- Author : crifan.com Version : 20180614 Function: Auto use gitbook to generated files: website/pdf/epub/mobi; upload to remote server; commit to github io repo Run 'make help' to see usage -------------------------------------------------------------------------------- CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary BOOK_NAME=youdao_note_summary Usage: make <target> Defaul Target: deploy Targets: debug_dir Print current directory related info debug_python Debug for makefile call python create_folder_debug Create folder for gitbook local debug create_folder_website Create folder for gitbook website create_folder_pdf Create folder for pdf create_folder_epub Create folder for epub create_folder_mobi Create folder for mobi create_folder_all Create folder for all: website/pdf/epub/mobi clean_generated_book_json Clean generated book.json file clean_debug Clean gitbook debug clean_website Clean generated gitbook website whole folder clean_pdf Clean generated PDF file and whole folder clean_epub Clean generated ePub file and whole folder clean_mobi Clean generated Mobi file and whole folder clean_all Clean all generated files generate_book_json Generate book.json from ../book_common.json and book_current.json debug Debug gitbook website Generate gitbook website pdf Generate PDF file epub Generate ePub file mobi Generate Mobi file all Generate all files: website/pdf/epub/mobi upload Upload all genereted website/pdf/epub/mobi files to remote server using rsync. Create sshpass_password.txt file to contain password before use this commit Commit generated files to github io deploy Deploy = upload and commit for generated files help Show help ➜ youdao_note_summary git:(master) ✗ </code>
目前实现了把Makefile中公共部分提取出来的目的了。
如有需要,后续再继续优化其他方面。
后来把:
Makefile_common
改为:
Makefile_common.mk
了,这样:
更容易被别人看懂,是makefile类型文件
-》其他编辑器,比如VSCode就可以自动识别,用Makefile类型去语法高亮了:
转载请注明:在路上 » 【已解决】提取Gitbook中Makefile公共部分