Hey, I’m trying to create a pipeline that takes files from a flat directory source/ that contains files with names formatted as yyyy-mm-dd-some-file-name.md and processes them with the output being created in destination/yyyy/mm/dd/some-file-name.md. I managed to string together an ugly script that converts the source path to destination path, but the problem is that make says that there is no rule to make the target. Here is my code so far:

SOURCE := $(wildcard source/*.md)
DESTINATION := $(foreach f,SOURCE,destination/$(shell echo $(notdir $(f)) | sed -E 's/^([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md$$/\1\/\2\/\3\/\4.md/'))

$(DESTINATION): $(SOURCE)
	@mkdir -p $(dir $(shell echo $(notdir $@) | sed -E 's/^([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md$$/\1\/\2\/\3/'))
	/bin/bash ./myscript.sh $< > $@

I believe that make sees that there are no target directories yet and just aborts the rule, but I’m not knowledgeable enough and just starting out with that tool. I would appreciate some guidance if any of you folks know a bit of Make.

  • fruitcantfly@programming.dev
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    15 days ago

    This breaks down when there are more than one SOURCE file, since each DESTINATION file depends on all source files. This means that $< will always be the first file in SOURCE. For example, if source contains the files 0001-01-01-some-file-name.md and 0002-02-02-some-file-name.md:

    $ make -n
    mkdir -p destination/0001/01/01/  
    /bin/bash ./myscript.sh source/0001-01-01-some-file-name.md > destination/0001/01/01/some-file-name.md  
    mkdir -p destination/0002/02/02/  
    /bin/bash ./myscript.sh source/0001-01-01-some-file-name.md > destination/0002/02/02/some-file-name.md  
    

    OP is probably better off using a scripting language to automate this kind of thing

    EDIT: Fixed example filenames

    • eleijeep@piefed.social
      link
      fedilink
      English
      arrow-up
      3
      ·
      14 days ago

      Well spotted, thanks. I replied further down the thread to OP with another option for doing this in Make, if you’d like to spot any bugs in that one too ;)