UP | HOME

Make-ing my life easier

Table of Contents

Introduction

At the time of writing my blog directory surely is cluttered, and I really do not want this. It prevents me from easily checking my website (not sure if the clutter caused my lack of proper at times documentation or the other way round), but it has to change. Here I expect to document the next 10 minutes briefly for me, so that it all makes sense

Goal

What I expect to do here is quickly create a makefile, with a local set of instructions, that will:

  1. Create a public directory and populate it
  2. Launch a python server

Execution

Using the local-build.sh script, I knew this would not take too much. Being an open source file and not really bothered by the confidentiality of its contents, I uploaded it to ChatGPT asking for it to be refactored and documented into a Makefile:

# Makefile for building and publishing a site
# Directories
PUBLIC_DIR := public
POSTS_DIR := $(PUBLIC_DIR)/posts

# Targets
.DEFAULT_GOAL := local

# Remove local directory
clean:
	@if [ -d $(PUBLIC_DIR) ]; then \
		rm -rf $(PUBLIC_DIR) && echo "Local directory deleted"; \
	fi

# Build the site using Emacs
build: clean
	emacs -Q --script build-site.el

# Add latest posts to /index.html
add_latest_posts:
	echo "Adding mini archive on /index.html"
	echo '<ul class="org-ul indexul">' > /tmp/index
	grep "^<li>" $(PUBLIC_DIR)/posts/recents.html | head -5 >> /tmp/index
	echo '</ul>' >> /tmp/index
	sed -i -e '/Latest Articles<\/a><\/h2>/r /tmp/index' $(PUBLIC_DIR)/index.html

# Copy /index.html to /posts/index.html
copy_index_to_posts:
	echo "Copying /index to /posts/index"
	cp $(PUBLIC_DIR)/index.html $(POSTS_DIR)/index.html

# Build the site locally
local: build add_latest_posts copy_index_to_posts

It did a good job, which I suppose can also be attributed to the proper yet by no means extensive commenting on the shell script.

Adding a new target and updating local does the job:

# Serve the public directory on localhost using Python
serve_local:
	cd $(PUBLIC_DIR) && python -m http.server

Completion

At this point everything is perfect, and well documented enough that I could refactor the whole blog, where spaghetti code is not only dominant but visible in its basic structure and organization as well, making it more manageable.

Some extra thoughts

It is truly fascinating that I managed within much less than fifteen minutes to:

  • make a shell script obsolete
  • make another shell script obsolete (further reducing the mess that I had embraced and my discomfort)
  • Better streamline the whole publish sequence by creating a central file to use (makefile)
  • write this simple article

With minimal experience in using and creating Makefiles. In fact, I thought I could try my hand at creating this manually and googled Makefile templates before realizing what a waste of time this would be. Ah, what a wonderful and yet peculiar time to be alive!

Originally created on 2023-12-27 Wed 12:25