Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions metadata/git/install.ps1
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions metadata/git/install.sh
Original file line number Diff line number Diff line change
@@ -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"
28 changes: 28 additions & 0 deletions metadata/git/pre-commit.ps1
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions metadata/git/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -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