From 8b569b409a3a9fe4cb93e2fb1560466fb3686e26 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 19 Jul 2024 16:09:09 +0300 Subject: [PATCH] feat: Rewrote run.sh and consolidated files --- .editorconfig | 12 +++ .github/renovate.json | 4 +- .github/run.sh | 217 +++++++++++++++++++++++++++++++++--------- .github/shared.sh | 50 ---------- 4 files changed, 185 insertions(+), 98 deletions(-) create mode 100644 .editorconfig delete mode 100755 .github/shared.sh diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..5d47c21c --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/.github/renovate.json b/.github/renovate.json index 5b9cda39..80571e0e 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -1,4 +1,4 @@ { - "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": ["github>ivuorinen/.github:renovate-config"] + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": ["github>ivuorinen/.github:renovate-config"] } diff --git a/.github/run.sh b/.github/run.sh index c4fddace..e86b5339 100755 --- a/.github/run.sh +++ b/.github/run.sh @@ -6,67 +6,192 @@ # TLDR pages licensed under CC-BY-4.0 # https://github.com/tldr-pages/tldr/blob/main/LICENSE.md -DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# To run debug mode, run script like this: DEGUG=1 ./run.sh +DEBUG="${DEBUG:-0}" +# To run in verbose mode, run script like this: VERBOSE=1 ./run.sh +VERBOSE="${VERBOSE:-0}" -echo "Running: $DIR/run.sh" -echo "Working directory: $DIR" +# Function to print an error message and exit +# $1 - error message (string) +error_exit() { + echo "Error: $1" >&2 + exit 1 +} -source "$DIR/shared.sh" +# Print an error message +# $1 - error message (string) +msg_err() { + echo "Error: $1" >&2 +} -TLDR_GIT="https://github.com/tldr-pages/tldr.git" -TLDR_SOURCE="source: $TLDR_GIT" -TLDR_SYNTAX="syntax: markdown" -TLDR_TEMP_DIR="$(realpath "$DIR"/..)/__tldr-temp" -TLDR_CHEAT_DEST="$(realpath "$DIR"/../tldr)" +# Print a warning message +# $1 - warning message (string) +msg_warn() { + echo "Warning: $1" >&2 +} -if [ -d "$TLDR_TEMP_DIR" ]; then - rm -rf "$TLDR_TEMP_DIR" -fi +# Print an ok message +# $1 - ok message (string) +msg_ok() { + echo "OK: $1" >&2 +} -echo "TLDR_TEMP_DIR: $TLDR_TEMP_DIR" -echo "TLDR_CHEAT_DEST: $TLDR_CHEAT_DEST" +# Debug message +# $1 - debug message (string) +msg_debug() { + if [[ $DEBUG -eq 1 ]]; then + echo "DEBUG: $1" >&2 + fi +} -git clone \ - --depth 1 \ - --single-branch \ - -q "$TLDR_GIT" "$TLDR_TEMP_DIR" || exit 1 +# Return sha256sum for a file +# $1 - filename (string) +get_sha256sum() { + [[ $DEBUG -eq 1 ]] && msg_debug "Getting sha256sum for: $1" + local file="$1" + sha256sum "$file" | awk '{print $1}' +} -rm -rf "$TLDR_TEMP_DIR/pages.*" +# Check if a file is replaceable +# $1 - source file (string) +# $2 - destination file (string) +# Returns 1 when replaceable, 0 when not replaceable. +is_replaceable() { + local file1="$1" + local file2="$2" + local file1_hash + local file2_hash -for d in "$TLDR_TEMP_DIR"/pages/*; do - DIRNAME=$(basename "$d") - # echo "-> $DIRNAME ($d)" + [[ $DEGUG -eq 1 ]] && msg_debug "Checking if files are replaceable: $file1, $file2" - SECTION_DIR="${TLDR_CHEAT_DEST}/$DIRNAME" + if [[ ! -r "$file1" ]]; then + [[ $VERBOSE -eq 1 ]] && msg_err "File 1 ($file1) does not exist" + return 0 + fi - [ "$DIRNAME" = "common" ] && SECTION_DIR="${TLDR_CHEAT_DEST}" + if [[ ! -r "$file2" ]]; then + [[ $VERBOSE -eq 1 ]] && msg_warn "File 2 ($file2) does not exist, replaceable" + return 1 + fi - TLDR_TAGS="tags: [tldr, $DIRNAME]" + file1_hash=$(get_sha256sum "$file1") + file2_hash=$(get_sha256sum "$file2") - if [ ! -d "$SECTION_DIR" ]; then - mkdir -p "$SECTION_DIR" - fi + if [[ -z "$file1_hash" ]]; then + [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($file1)" + return 0 + fi - for FILE in "$d"/*.md; do - BASENAME=$(basename "$FILE" .md) - # FILENAME="${BASENAME%%.*}" - # echo "-> $FILE = $FILENAME" - TLDR_FILE="$SECTION_DIR/${BASENAME}" - # echo "-> dest: $TLDR_FILE" + if [[ -z "$file2_hash" ]]; then + [[ $VERBOSE -eq 1 ]] && msg_warn "Could not get hash for file 2 ($file2), replaceable" + return 1 + fi - # Update the original file for making the replaceable value comparable - if [ -f "$FILE" ] && [ '---' != "$(head -1 <"$FILE")" ]; then - echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$FILE")" >"$FILE" - fi + if [[ "$file1_hash" == "$file2_hash" ]]; then + [[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replaceable: $file1" + return 0 + fi - replaceable "$FILE" "$TLDR_FILE" + [[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($file1_hash != $file2_hash), replaceable" + return 1 +} - override=$? - if [ "$override" -ne 0 ]; then - cp "$FILE" "$TLDR_FILE" && echo "Updated: $TLDR_FILE" - fi +# Function to clone the TLDR repository +# $1 - repository URL (string) +clone_repo() { + [[ $DEBUG -eq 1 ]] && msg_debug "Cloning repository: $1" + local repo_url="$1" + local dest_dir="$2" + git clone --depth 1 --single-branch -q "$repo_url" "$dest_dir" || error_exit "Failed to clone repository" +} - done -done +# Function to update the markdown files +# $1 - source directory (string) +# $2 - destination directory (string) +# $3 - syntax (string) +# $4 - source (string) +update_markdown_files() { + [[ $DEBUG -eq 1 ]] && msg_debug "Updating markdown files" -rm -rf "$TLDR_TEMP_DIR" + local src_dir="$1" + local dest_dir="$2" + local syntax="$3" + local source="$4" + + for d in "$src_dir"/pages/*; do + [[ $VERBOSE -eq 1 ]] && msg_debug "Processing directory: $d" + + local dirname + dirname=$(basename "$d") + local section_dir="${dest_dir}/${dirname}" + [ "$dirname" = "common" ] && section_dir="${dest_dir}" + + local tldr_tags="tags: [tldr, $dirname]" + + if [ ! -d "$section_dir" ]; then + mkdir -p "$section_dir" || error_exit "Failed to create directory: $section_dir" + fi + + for file in "$d"/*.md; do + [[ $DEBUG -eq 1 ]] && msg_debug "Processing file: $file" + local basename + basename=$(basename "$file" .md) + local tldr_file="${section_dir}/${basename}" + + if [ -f "$file" ] && [ '---' != "$(head -1 <"$file")" ]; then + echo -e "---\n$syntax\n$tldr_tags\n$source\n---\n$(cat "$file")" >"$file" + fi + + is_replaceable "$file" "$tldr_file" + local override=$? + + if [ "$override" -ne 0 ]; then + # Remove full path from the file and set as relative path for message + local tldr_file_rel + tldr_file_rel="${tldr_file#$dest_dir/}" + + cp "$file" "$tldr_file" && echo "Updated: $tldr_file_rel" || + error_exit "Failed to copy file: $file to $tldr_file" + fi + done + done +} + +# Main script logic +main() { + local dir + dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || + error_exit "Failed to determine current directory" + + msg_debug "Running: $dir/run.sh" + msg_debug "Working directory: $dir" + + local tldr_git="https://github.com/tldr-pages/tldr.git" + local tldr_source="source: $tldr_git" + local tldr_syntax="syntax: markdown" + local tldr_temp_dir + tldr_temp_dir="$(realpath "$dir"/..)/__tldr-temp" + local tldr_cheat_dest + tldr_cheat_dest="$(realpath "$dir"/../tldr)" + + if [ -d "$tldr_temp_dir" ]; then + rm -rf "$tldr_temp_dir" || + error_exit "Failed to remove directory: $tldr_temp_dir" + fi + + msg_debug "TLDR_TEMP_DIR: $tldr_temp_dir" + msg_debug "TLDR_CHEAT_DEST: $tldr_cheat_dest" + + clone_repo "$tldr_git" "$tldr_temp_dir" + + rm -rf "$tldr_temp_dir/pages.*" || + error_exit "Failed to remove translations (pages.*) from $tldr_temp_dir" + + update_markdown_files "$tldr_temp_dir" "$tldr_cheat_dest" "$tldr_syntax" "$tldr_source" + + rm -rf "$tldr_temp_dir" || + error_exit "Failed to remove directory: $tldr_temp_dir" +} + +# Run the main function +main "$@" diff --git a/.github/shared.sh b/.github/shared.sh deleted file mode 100755 index 6f8cb276..00000000 --- a/.github/shared.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env bash - -# return sha256sum for file -# $1 - filename (string) -function get_sha256sum() -{ - sha256sum "$1" | head -c 64 -} - -# Replaceable file -# -# $1 - filename (string) -# $2 - filename (string) -# -# Returns 1 when replaceable, 0 when not replaceable. -function replaceable() -{ - FILE1="$1" - FILE2="$2" - - [[ ! -r "$FILE1" ]] && { - [[ $VERBOSE -eq 1 ]] && msg_err "File 1 ($FILE1) does not exist" - return 0 - } - [[ ! -r "$FILE2" ]] && { - [[ $VERBOSE -eq 1 ]] && msg_err "File 2 ($FILE2) does not exist, replaceable" - return 1 - } - - FILE1_HASH=$(get_sha256sum "$FILE1") - FILE2_HASH=$(get_sha256sum "$FILE2") - - [[ $FILE1_HASH = "" ]] && { - [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($FILE1)" - return 0 - } - [[ $FILE2_HASH = "" ]] && { - [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 2 ($FILE2), replaceable" - return 1 - } - - [[ "$FILE1_HASH" == "$FILE2_HASH" ]] && { - [[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replaceable: $FILE1" - return 0 - } - - [[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($FILE1_HASH != $FILE2_HASH), replaceable" - - return 1 -}