feat: Rewrote run.sh and consolidated files

This commit is contained in:
Ismo Vuorinen
2024-07-19 16:09:09 +03:00
parent cfb325c6cf
commit 8b569b409a
4 changed files with 185 additions and 98 deletions

12
.editorconfig Normal file
View File

@@ -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

View File

@@ -1,4 +1,4 @@
{ {
"$schema": "https://docs.renovatebot.com/renovate-schema.json", "$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["github>ivuorinen/.github:renovate-config"] "extends": ["github>ivuorinen/.github:renovate-config"]
} }

217
.github/run.sh vendored
View File

@@ -6,67 +6,192 @@
# TLDR pages licensed under CC-BY-4.0 # TLDR pages licensed under CC-BY-4.0
# https://github.com/tldr-pages/tldr/blob/main/LICENSE.md # 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" # Function to print an error message and exit
echo "Working directory: $DIR" # $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" # Print a warning message
TLDR_SOURCE="source: $TLDR_GIT" # $1 - warning message (string)
TLDR_SYNTAX="syntax: markdown" msg_warn() {
TLDR_TEMP_DIR="$(realpath "$DIR"/..)/__tldr-temp" echo "Warning: $1" >&2
TLDR_CHEAT_DEST="$(realpath "$DIR"/../tldr)" }
if [ -d "$TLDR_TEMP_DIR" ]; then # Print an ok message
rm -rf "$TLDR_TEMP_DIR" # $1 - ok message (string)
fi msg_ok() {
echo "OK: $1" >&2
}
echo "TLDR_TEMP_DIR: $TLDR_TEMP_DIR" # Debug message
echo "TLDR_CHEAT_DEST: $TLDR_CHEAT_DEST" # $1 - debug message (string)
msg_debug() {
if [[ $DEBUG -eq 1 ]]; then
echo "DEBUG: $1" >&2
fi
}
git clone \ # Return sha256sum for a file
--depth 1 \ # $1 - filename (string)
--single-branch \ get_sha256sum() {
-q "$TLDR_GIT" "$TLDR_TEMP_DIR" || exit 1 [[ $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 [[ $DEGUG -eq 1 ]] && msg_debug "Checking if files are replaceable: $file1, $file2"
DIRNAME=$(basename "$d")
# echo "-> $DIRNAME ($d)"
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 if [[ -z "$file1_hash" ]]; then
mkdir -p "$SECTION_DIR" [[ $VERBOSE -eq 1 ]] && msg_err "Could not get hash for file 1 ($file1)"
fi return 0
fi
for FILE in "$d"/*.md; do if [[ -z "$file2_hash" ]]; then
BASENAME=$(basename "$FILE" .md) [[ $VERBOSE -eq 1 ]] && msg_warn "Could not get hash for file 2 ($file2), replaceable"
# FILENAME="${BASENAME%%.*}" return 1
# echo "-> $FILE = $FILENAME" fi
TLDR_FILE="$SECTION_DIR/${BASENAME}"
# echo "-> dest: $TLDR_FILE"
# Update the original file for making the replaceable value comparable if [[ "$file1_hash" == "$file2_hash" ]]; then
if [ -f "$FILE" ] && [ '---' != "$(head -1 <"$FILE")" ]; then [[ $VERBOSE -eq 1 ]] && msg_ok "Files match, not replaceable: $file1"
echo -e "---\n$TLDR_SYNTAX\n$TLDR_TAGS\n$TLDR_SOURCE\n---\n$(cat "$FILE")" >"$FILE" return 0
fi fi
replaceable "$FILE" "$TLDR_FILE" [[ $VERBOSE -eq 1 ]] && msg_warn "Files do not match ($file1_hash != $file2_hash), replaceable"
return 1
}
override=$? # Function to clone the TLDR repository
if [ "$override" -ne 0 ]; then # $1 - repository URL (string)
cp "$FILE" "$TLDR_FILE" && echo "Updated: $TLDR_FILE" clone_repo() {
fi [[ $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 # Function to update the markdown files
done # $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 "$@"

50
.github/shared.sh vendored
View File

@@ -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
}