Skip to content

Commit

Permalink
use javascript for json generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 6, 2024
1 parent 660984d commit 7a6828f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 110 deletions.
10 changes: 9 additions & 1 deletion .github/actions/get_tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
description: List of directories to exclude from the search
required: false
default: ''
node_version:
description: The version of Node.js to use
required: true
default: '20.x'
outputs:
tests:
description: All of the tests found by this action
Expand All @@ -17,13 +21,17 @@ runs:
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node_version }}

- name: Get tests
id: get_tests
run: |
# Export the input variables to make them available to the script
export INPUT_DIRECTORIES="${{ inputs.directories }}"
export INPUT_EXCLUDE_DIRS="${{ inputs.exclude_dirs }}"
# Run the script
tests=$(./.github/scripts/get_tests.sh | base64 -d)
tests=$(./.github/actions/get_tests/get_tests.sh | base64 -d)
echo "tests=${tests}" >> "$GITHUB_OUTPUT"
shell: bash
57 changes: 57 additions & 0 deletions .github/actions/get_tests/generate_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { basename } from 'path';

// Function to check if a directory is excluded
function isExcluded(dir, excludeDirs) {
return excludeDirs.some((exclude) => dir.includes(exclude));
}

// Function to generate JSON object for each directory
function generateJson(dir) {
const name = basename(dir);
let type = '';
let syntax = '';
let api = '';

if (dir.includes('/examples/')) {
type = 'ex';
} else if (dir.includes('/property/')) {
type = 'prop';
if (dir.includes('/candid_rpc/')) {
syntax = 'crpc';
if (dir.includes('/functional_api/')) {
api = 'functional';
} else if (dir.includes('/class_api/')) {
api = 'class';
}
} else if (dir.includes('/ic_api/')) {
syntax = 'ic_api';
}
} else if (dir.includes('/end_to_end/')) {
type = 'e2e';
if (dir.includes('/http_server/')) {
syntax = 'http';
} else if (dir.includes('/candid_rpc/')) {
syntax = 'crpc';
}
}

return {
path: dir,
name: name,
type: type,
...(syntax && { syntax: syntax }),
...(api && { api: api })
};
}

// Get input from the command-line arguments
const directories = process.argv[2].split(' ').filter((path) => path !== '');
const excludeDirs = process.argv[3].split(',').filter((path) => path !== '');

// Filter directories and generate JSON
const jsonArray = directories
.filter((dir) => dir && !isExcluded(dir, excludeDirs))
.map((dir) => generateJson(dir));

// Output the final JSON string
console.log(JSON.stringify(jsonArray));
41 changes: 41 additions & 0 deletions .github/actions/get_tests/get_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# Inputs from action.yml or environment variables
DIRECTORIES=($INPUT_DIRECTORIES)
EXCLUDE_DIRS=($INPUT_EXCLUDE_DIRS)

# Function to discover test directories
discover_directories() {
local dir=$1
find "$dir" -type d -not -path "*/node_modules/*" -exec sh -c '
for pkg in "$@"; do
if [ -f "$pkg/package.json" ]; then
if jq -e ".scripts.test" "$pkg/package.json" > /dev/null; then
echo "$pkg"
fi
fi
done
' sh {} +
}

# Discover directories in the provided directories, excluding specified directories
all_directories=""
for dir in "${DIRECTORIES[@]}"; do
all_directories+=$(discover_directories "$dir")
all_directories+=$'\n'
done

# Prepare the data to be passed into the JavaScript script
sorted_directories=$(echo "$all_directories" | sort | tr '\n' ' ') # Convert into a space-separated string
exclude_dirs=$(IFS=, ; echo "${EXCLUDE_DIRS[*]}") # Convert array to comma-separated

# Call the JavaScript script with input directories and excluded directories
json_result=$(node .github/actions/get_tests/generate_json.js "$sorted_directories" "$exclude_dirs")

# Format the result
result="${json_result//'%'/'%25'}"
result="${result//$'\n'/'%0A'}"
result="${result//$'\r'/'%0D'}"

# Output the final result as base64
echo "$result" | base64
109 changes: 0 additions & 109 deletions .github/scripts/get_tests.sh

This file was deleted.

0 comments on commit 7a6828f

Please sign in to comment.