Re-worked stylegen

This commit is contained in:
2024-05-07 18:03:54 -06:00
parent 7463d1a252
commit 00bb8d776d
25 changed files with 95 additions and 77 deletions

View File

@ -17,7 +17,6 @@ type SSEServerType struct {
}
var SSEServer *SSEServerType
var mutex = &sync.Mutex{}
func init() {
SSEServer = &SSEServerType{
@ -90,24 +89,21 @@ func SetSSEHeaders(c echo.Context) {
}
func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) {
// Create a new context that is not tied to the client's request context
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Cancel the context when the function returns
defer cancel()
// Use a separate goroutine to monitor the client's request context
go func() {
<-c.Request().Context().Done()
cancel() // Cancel the context when the client disconnects
cancel()
}()
var mutex sync.Mutex
for {
select {
case <-ctx.Done():
// The context has been canceled, either by the client disconnecting
// or by the function returning. Stop trying to send messages.
return
default:
// The client is still connected. Continue processing messages.
msg, err := pubsub.ReceiveMessage(ctx)
if err != nil {
log.Printf("Failed to receive message: %v", err)
@ -117,17 +113,15 @@ func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
mutex.Lock()
_, err = c.Response().Write([]byte(data))
mutex.Unlock()
defer mutex.Unlock()
if err != nil {
log.Printf("Failed to write message: %v", err)
return // Stop processing if an error occurs
}
// Check if the ResponseWriter is nil before trying to flush it
if c.Response().Writer != nil {
// Check if the ResponseWriter implements http.Flusher before calling Flush
_, err = c.Response().Write([]byte(data))
if err != nil {
log.Printf("Failed to write message: %v", err)
return
}
flusher, ok := c.Response().Writer.(http.Flusher)
if ok {
flusher.Flush()
@ -135,7 +129,8 @@ func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client
log.Println("Failed to flush: ResponseWriter does not implement http.Flusher")
}
} else {
log.Println("Failed to flush: ResponseWriter is nil")
log.Println("Failed to write: ResponseWriter is nil")
return
}
}
}

View File

@ -1,4 +1,26 @@
#!/bin/sh
#!/bin/bash
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-e|--extensions)
EXTENSIONS="$2"
shift; shift
;;
-d|--directory)
DIRECTORY="$2"
shift; shift
;;
-o|--output-dir)
OUTPUT_DIR="$2"
shift; shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
OS=$(uname -s)
ARCH=$(uname -m)
@ -41,39 +63,52 @@ else
chmod +x $BINARY
fi
echo $BINARY
echo "Extensions: $EXTENSIONS"
echo "Directory: $DIRECTORY"
echo "Output Directory: $OUTPUT_DIR"
# Infer pages from .html files in the pages directory
PAGES=$(ls ../../pages/templates/*.html | xargs -n 1 basename | sed 's/\.[^.]*$//')
# Set default values if not provided
OUTPUT_DIR="${OUTPUT_DIR:-../../public/css}"
DIRECTORY="${DIRECTORY:-.}"
# Run the binary for each page
for PAGE in $PAGES; do
(
# Detect which partials are being used in this page
PARTIALS=$(grep -o -E '{{template "[^"]+' ../../pages/templates/${PAGE}.html | cut -d'"' -f2 | xargs -I{} echo \"../../pages/templates/partials/{}.html\")
if [[ -z "$EXTENSIONS" ]]; then
echo "No extensions provided."
exit 1
fi
# Generate an array of partials and join them with commas
PARTIALS_ARRAY=$(echo $PARTIALS | tr ' ' ',')
# Initialize an array for name conditions
name_conditions=()
# Always include the "header" partial and any other partials that are always used
PARTIALS_ARRAY=\"../../pages/templates/partials/header.html\",\"../../pages/templates/partials/global.html\",$PARTIALS_ARRAY
# Generate Tailwind config for this page
echo "module.exports = {
content: [\"../../pages/templates/${PAGE}.html\", \"../../pages/templates/layouts/*.html\", $PARTIALS_ARRAY],
theme: {
extend: {},
},
daisyui: {
themes: [\"night\"],
},
plugins: [require('daisyui'), require('@tailwindcss/typography')],
}" > tailwind.config.${PAGE}.js
# Run the binary with the generated config
$BINARY build -i ./base.css -c tailwind.config.${PAGE}.js -o ../../public/css/styles.${PAGE}.css --minify
) &
# Assuming $EXTENSIONS is a comma-separated list of extensions
IFS=',' read -ra ADDR <<< "$EXTENSIONS"
for ext in "${ADDR[@]}"; do
name_conditions+=(-name "*.$ext")
done
# Use find with the array of conditions
INCLUDE_FILES=$(find "$DIRECTORY" -type f \( "${name_conditions[@]}" \))
echo "Files found: $INCLUDE_FILES"
# Optionally, remove leading './' if necessary
INCLUDE_FILES=$(echo "$INCLUDE_FILES" | sed 's|^\./||')
INCLUDE_FILES_ARRAY=$(echo "$INCLUDE_FILES" | awk '{printf "\"%s\",", $0}' | sed 's/,$//')
# Generate Tailwind config
echo "module.exports = {
content: [$INCLUDE_FILES_ARRAY],
theme: {
extend: {},
},
daisyui: {
themes: [\"night\"],
},
plugins: [require('daisyui'), require('@tailwindcss/typography')],
}" > tailwind.config.js
# Run the binary with the generated config
$BINARY build -i ./base.css -c tailwind.config.js -o "${OUTPUT_DIR}/styles.css" --minify
# Wait for all background processes to finish
wait