mirror of
https://github.com/basecamp/omarchy.git
synced 2026-02-17 15:25:37 +00:00
Attempt to templaterize the theme specific files
This commit is contained in:
79
bin/omarchy-theme-render
Executable file
79
bin/omarchy-theme-render
Executable file
@@ -0,0 +1,79 @@
|
||||
#!/bin/bash
|
||||
|
||||
# omarchy-theme-render
|
||||
# Renders templates using values from a TOML configuration file.
|
||||
|
||||
COLORS_FILE="$1"
|
||||
TEMPLATES_DIR="$2"
|
||||
OUTPUT_DIR="$3"
|
||||
|
||||
if [[ -z "$COLORS_FILE" || -z "$TEMPLATES_DIR" || -z "$OUTPUT_DIR" ]]; then
|
||||
echo "Usage: omarchy-theme-render <colors_toml> <templates_dir> <output_dir>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "$COLORS_FILE" ]]; then
|
||||
echo "Error: Colors file '$COLORS_FILE' not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -d "$TEMPLATES_DIR" ]]; then
|
||||
echo "Error: Templates directory '$TEMPLATES_DIR' not found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
|
||||
# Create a temporary sed script
|
||||
SED_SCRIPT=$(mktemp)
|
||||
|
||||
# Read the TOML file line by line
|
||||
while IFS='=' read -r key value; do
|
||||
# Skip comments and empty lines
|
||||
[[ "$key" =~ ^[[:space:]]*# ]] && continue
|
||||
[[ -z "$key" ]] && continue
|
||||
|
||||
# Skip lines without values (like [headers])
|
||||
[[ -z "$value" ]] && continue
|
||||
|
||||
# Trim whitespace from key and value
|
||||
key=$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
value=$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
|
||||
|
||||
# Remove quotes from string values
|
||||
value="${value%\"}"
|
||||
value="${value#\"}"
|
||||
value="${value%\'}"
|
||||
value="${value#\'}"
|
||||
|
||||
# Add replacement command to sed script
|
||||
# We use | as delimiter to avoid issues with / in paths or values, though colors are usually hex
|
||||
# Escaping special regex characters in value would be ideal, but for hex codes it's fine.
|
||||
# We replace {{ key }} with the value
|
||||
echo "s|{{ $key }}|$value|g" >> "$SED_SCRIPT"
|
||||
|
||||
done < "$COLORS_FILE"
|
||||
|
||||
# Process each template file
|
||||
count=0
|
||||
skipped=0
|
||||
# Enable nullglob to handle case where no templates exist
|
||||
shopt -s nullglob
|
||||
for tpl in "$TEMPLATES_DIR"/*.tpl; do
|
||||
filename=$(basename "$tpl" .tpl)
|
||||
output_path="$OUTPUT_DIR/$filename"
|
||||
|
||||
# If the file already exists in the output directory (copied from theme specific folder),
|
||||
# do NOT overwrite it.
|
||||
if [[ -f "$output_path" ]]; then
|
||||
# echo "Skipping $filename (explicit config exists)"
|
||||
((skipped++))
|
||||
continue
|
||||
fi
|
||||
|
||||
sed -f "$SED_SCRIPT" "$tpl" > "$output_path"
|
||||
((count++))
|
||||
done
|
||||
|
||||
rm "$SED_SCRIPT"
|
||||
echo "Rendered $count templates to $OUTPUT_DIR (skipped $skipped existing)"
|
||||
@@ -17,8 +17,34 @@ if [[ ! -d "$THEME_PATH" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Update theme symlinks
|
||||
ln -nsf "$THEME_PATH" "$CURRENT_THEME_DIR"
|
||||
# Prepare current theme directory (replacing symlink if it exists)
|
||||
if [[ -L "$CURRENT_THEME_DIR" ]]; then
|
||||
rm "$CURRENT_THEME_DIR"
|
||||
elif [[ -d "$CURRENT_THEME_DIR" ]]; then
|
||||
rm -rf "$CURRENT_THEME_DIR"
|
||||
fi
|
||||
mkdir -p "$CURRENT_THEME_DIR"
|
||||
|
||||
# Copy static assets (excluding templates, if any found in theme dir, though they shouldn't be there)
|
||||
# We copy everything from the theme directory first.
|
||||
cp -r "$THEME_PATH/"* "$CURRENT_THEME_DIR/" 2>/dev/null
|
||||
|
||||
# Generate configuration files from templates
|
||||
TEMPLATES_DIR="$THEMES_DIR/templates.d"
|
||||
COLORS_FILE="$THEME_PATH/colors.toml"
|
||||
|
||||
if [[ -x "$(command -v omarchy-theme-render)" ]]; then
|
||||
omarchy-theme-render "$COLORS_FILE" "$TEMPLATES_DIR" "$CURRENT_THEME_DIR"
|
||||
else
|
||||
# Fallback to local script if not in PATH (development scenario)
|
||||
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
|
||||
if [[ -x "$SCRIPT_DIR/omarchy-theme-render" ]]; then
|
||||
"$SCRIPT_DIR/omarchy-theme-render" "$COLORS_FILE" "$TEMPLATES_DIR" "$CURRENT_THEME_DIR"
|
||||
else
|
||||
echo "Error: omarchy-theme-render not found"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Change background with theme
|
||||
omarchy-theme-bg-next
|
||||
|
||||
Reference in New Issue
Block a user