英文:
foreach directory, check condition then run piped bash commands
问题
Method 1中的错误是由于|
字符的使用问题,应该使用$(subst ...)
函数来替换字符串,以下是修复后的部分:
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES:$(PROJECT_DIR)%=%) | sed 's/\s\+/|/g'
Method 2中也存在问题,主要是在foreach循环中的shell命令使用不正确。以下是修复后的部分:
PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES := $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
all:
@$(foreach file, $(SUB_DIRECTORIES), \
$(if $(filter $(file), $(PROJECT_DIR)), \
tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 364x422 label:@- ./source/root.png;, \
tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 364x422 label:@- ./source/$(subst $(PROJECT_DIR),,$(file)).png;) \
)
请注意,我已经修复了两种方法中的错误,并删除了不必要的|
字符。现在,这两种方法应该能够正常运行了。
英文:
Hello people of the internet,
Firstly I must apologize as I am relatively new to makefiles, so I may be doing this in a very unorthodox way.
I am trying to create a makefile that will
-loop through each subdirectory of a specified directory
-check the directory is the specified directory or one of the subdirectories
-run the directory's through tree and then generate an image of the tree using convert with the filepath removed.
At this point, I'm getting absolutely lost and have no idea where these issues are coming from. The whole idea is to make this code project agnostic. I have a hard coded version that works fine with all of the directories and subdirectories specified manually.
I have tried two methods so far.
Method 1
PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES = $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g'
all:
@echo $(SUB_DIRECTORIES)
for $(directory) in $(SUB_DIRECTORIES); do \
if test $$directory = $(PROJECT_DIR); then \
echo "Generating root.png" && tree -P '*.py' -I '__pycache__|.c' $(directory) | convert -size 364x422 label:@- ./source/root.png; \
else \
"Generating $(subst $(PROJECT_DIR),,$(directory)) png" && tree -P '*.py' -I '__pycache__|.c' $(directory) | convert -size 364x422 label:@- ./source/$(subst $(PROJECT_DIR),,$(directory)).png; \
fi \
done
With this method, the subst isn't working thus resulting in these errors:
Method 1 errors
Method 2
PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES =: $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g'
all:
@$(foreach file, $(wildcard $(SUB_DIRECTORIES)/), \
$(if $(filter $(file), $(PROJECT_DIR)), \
$(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 364x422 label:@- ./source/root.png;), \
$(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 364x422 label:@- ./source/$(subst $(PROJECT_DIR),,$(file)).png;) \
) \
);
This method causes another error: method 2 errors
答案1
得分: 0
谢谢您的评论。
问题出在添加了""后$(SUB_DIRECTORIES)和末尾的分号。 很令人欣慰的是,我离解决方案并不远。 我能够使用以下代码使其工作,采用方法2:
PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES = $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g';
all:
$(foreach file, $(wildcard $(SUB_DIRECTORIES)), \
$(if $(filter $(file), $(PROJECT_DIR)), \
$(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 1280x1024 label:@- ./source/root.png), \
$(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 1280x1024 label:@- ./source/$(subst $(PROJECT_DIR),,$(file)).png) \
) \
)
英文:
thank you for your comments.
The issue was coming from the added "" after $(SUB_DIRECTORIES) and the semi-colon at the end. It's relieving to know that I was not far off from the solution. I was able to get it to work with Method 2 using the following code. :
PROJECT_DIR = ../hub/hub/
SUB_DIRECTORIES = $(shell find $(PROJECT_DIR) -maxdepth 1 -type d -exec echo {} \;)
SUB_DIRECTORIES_FORMATTED = $(SUB_DIRECTORIES) | sed 's|..\/hub\/hub\/||g' | sed 's/\s\+/|/g'
all:
$(foreach file, $(wildcard $(SUB_DIRECTORIES)), \
$(if $(filter $(file), $(PROJECT_DIR)), \
$(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 1280x1024 label:@- ./source/root.png), \
$(shell tree -P '*.py' -I '__pycache__|.c' $(file) | convert -size 1280x1024 label:@- ./source/$(subst $(PROJECT_DIR),,$(file)).png) \
) \
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论