diff --git a/metadata/git/install.ps1 b/metadata/git/install.ps1 new file mode 100644 index 0000000..7696ed1 --- /dev/null +++ b/metadata/git/install.ps1 @@ -0,0 +1,37 @@ +# This script is used to install the git hook in the repository, +# embedding the contents of pre-commit.ps1 into a single hook file. + +# Set working directory to the location of this script +$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition +Set-Location -Path $scriptPath + +# Get the root directory of the repository +$repoRootDir = git rev-parse --show-toplevel +$hooksDir = Join-Path $repoRootDir '.git\hooks' + +# Read the contents of pre-commit.ps1 +$ps1Content = Get-Content -Raw -Path (Join-Path $scriptPath 'pre-commit.ps1') + +# Build the Git hook script content as a Bash script that calls PowerShell +$hookScript = @" +#!/bin/bash +# This Git hook calls the embedded PowerShell code + +powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ' +$ps1Content +' +"@ + +# Write the hook script to the hooks directory with Unix-style line endings +# The `-replace` ensures that all `\r` characters (Windows line endings) are removed +$hookScript = $hookScript -replace "`r`n", "`n" + +# Create the hooks directory if it does not exist +if (-not (Test-Path $hooksDir)) { + New-Item -ItemType Directory -Path $hooksDir +} + +# Define the full path for the Git hook +$hookPath = Join-Path $hooksDir 'pre-commit' +# Write the hook script to the hooks directory +Set-Content -Path $hookPath -Value $hookScript diff --git a/metadata/git/install.sh b/metadata/git/install.sh new file mode 100644 index 0000000..3990a29 --- /dev/null +++ b/metadata/git/install.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# This script is used to install the git hooks in the repository. + +# Set working directory to the location of this script +cd "$(dirname "${BASH_SOURCE[0]}")" + +# Check if the necessary programs to run the hooks are installed +if ! command -v jq &> /dev/null; then + echo "Error: jq is not installed. Please install it to run this hook." + exit 1 +fi + +# Get the root directory of the repository +REPO_ROOT_DIR=$(git rev-parse --show-toplevel) +HOOKS_DIR="$REPO_ROOT_DIR/.git/hooks" + +# Create the hooks directory if it does not exist +if [ ! -d "$HOOKS_DIR" ]; then + mkdir -p "$HOOKS_DIR" +fi + +# Move the hooks to the hooks directory +cp pre-commit.sh "$HOOKS_DIR/pre-commit" diff --git a/metadata/git/pre-commit.ps1 b/metadata/git/pre-commit.ps1 new file mode 100644 index 0000000..91337ea --- /dev/null +++ b/metadata/git/pre-commit.ps1 @@ -0,0 +1,28 @@ +#!/usr/bin/env powershell + +# Initialize a variable to track errors +$errorOccurred = $false + +$repoRoot = Get-Location # Git hooks run in the repository root +$metadataPath = Join-Path $repoRoot "metadata" # Metadata subdirectory + +# Get all JSON files in the directory and its subdirectories +$jsonFiles = Get-ChildItem -Path $metadataPath -Recurse -Filter *.json + +foreach ($file in $jsonFiles) { + try { + # Try to read and convert the JSON file to a PSObject or Hashtable object + # This will throw an error if the JSON is not valid + $jsonContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json + } catch { + Write-Host "$($file.Name) has syntax errors." + $errorOccurred = $true + } +} + +# Set the exit code based on whether any errors occurred +if ($errorOccurred) { + exit 1 # Exit with a non-zero code to indicate failure +} else { + exit 0 # Exit with zero code to indicate success +} diff --git a/metadata/git/pre-commit.sh b/metadata/git/pre-commit.sh new file mode 100644 index 0000000..90b3750 --- /dev/null +++ b/metadata/git/pre-commit.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Check if jq is installed +if ! command -v jq &> /dev/null; then + echo "Error: jq is not installed. Please install it to run this hook." + exit 1 +fi + +# Exit on error +set -e + +# Check all JSON files in the metadata directory +echo "Checking JSON syntax in metadata JSON files..." +INVALID_FILES="" + +# Find all JSON files in the repository +JSON_FILES=$(find metadata -name "*.json" -type f) + +# If no JSON files found, exit successfully +if [ -z "$JSON_FILES" ]; then + echo "No JSON files found." + exit 0 +fi + +# Check each JSON file +for file in $JSON_FILES; do + echo "Checking $file..." + if ! cat "$file" | jq type 1>/dev/null; then + INVALID_FILES="$INVALID_FILES\n - $file" + fi +done + +# If invalid files were found, exit with error +if [ -n "$INVALID_FILES" ]; then + echo -e "\nError: The following JSON files have invalid syntax:$INVALID_FILES" + echo "Please fix the JSON syntax before committing." + exit 1 +fi + +echo "All JSON files are valid." +exit 0 \ No newline at end of file