Skip to content
Merged
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
158 changes: 112 additions & 46 deletions scripts/setup_new_sdk.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
#!/usr/bin/env bash
set -euo pipefail

# Preflight check for required tools
REQUIRED_TOOLS=(git mktemp cp mkdir make cat touch grep rm)
MISSING_TOOLS=()

for tool in "${REQUIRED_TOOLS[@]}"; do
if ! command -v "$tool" >/dev/null 2>&1; then
MISSING_TOOLS+=("$tool")
fi
done

if [ ${#MISSING_TOOLS[@]} -gt 0 ]; then
echo "Error: The following required tools are missing from PATH:"
for tool in "${MISSING_TOOLS[@]}"; do
echo " - $tool"
done
exit 1
fi

echo "SDK ID (this will be used to create a directory under config/clients/{SDK_ID} and the resulting client will be under clients/{SDK_ID}):"
read -r SDK_ID

CLIENTS_GENERATOR_DIR="${PWD}"
CONFIG_PATH="${CLIENTS_GENERATOR_DIR}/config/clients/${SDK_ID:?}"
SDK_OUTPUT_PATH="${CLIENTS_GENERATOR_DIR}/clients/${SDK_ID}"
appLongName="OpenFGA"
apiDocsUrl="https://openfga.dev/api"
APP_LONG_NAME="OpenFGA"
API_DOCS_URL="https://openfga.dev/api"

if [ -d "${CONFIG_PATH}" ]; then
echo "Config Path: ${CONFIG_PATH} already exists. Clear it or choose a different SDK ID"
Expand All @@ -28,7 +47,7 @@ echo "Task 1: Create config directory at $CONFIG_PATH"

mkdir -p "${CONFIG_PATH}"

printf "Task 1.1: Initialize files"
printf "Task 1.1: Initialize files\n"
echo "${GENERATOR}" > "${CONFIG_PATH}/generator.txt"
touch "${CONFIG_PATH}/CHANGELOG.md.mustache"
CONFIG_OVERRIDES=$(cat <<EOF
Expand All @@ -37,8 +56,8 @@ CONFIG_OVERRIDES=$(cat <<EOF
"gitRepoId": "${SDK_ID}-sdk",
"packageName": "CHANGE_ME",
"packageVersion": "0.0.1",
"packageDescription": "${SDK_ID} SDK for ${appLongName}",
"packageDetailedDescription": "This is an autogenerated ${SDK_ID} SDK for ${appLongName}. It provides a wrapper around the [${appLongName} API definition](${apiDocsUrl}).",
"packageDescription": "${SDK_ID} SDK for ${APP_LONG_NAME}",
"packageDetailedDescription": "This is an autogenerated ${SDK_ID} SDK for ${APP_LONG_NAME}. It provides a wrapper around the [${APP_LONG_NAME} API definition](${API_DOCS_URL}).",
"files": {}
}
EOF
Expand All @@ -64,25 +83,78 @@ TEMPLATE_SOURCE_REPO="https://github.com/OpenAPITools/openapi-generator"
TEMPLATE_SOURCE_BRANCH=master
TEMPLATE_SOURCE_PATH="modules/openapi-generator/src/main/resources/${GENERATOR}"

printf "Task 2.1: Create temporary directory"
printf "Task 2.1: Create temporary directory\n"
tmpdir=$(mktemp -d)

# Setup cleanup trap
cleanup() {
# Clear traps immediately to prevent recursion
trap - EXIT ERR INT TERM HUP

# Disable errexit for cleanup operations, preserve previous state
local errexit_on=0
case $- in
*e*) errexit_on=1 ;;
esac
set +e

# Always try to return to the original workspace first
if [ -n "${CLIENTS_GENERATOR_DIR:-}" ] && [ -d "${CLIENTS_GENERATOR_DIR}" ]; then
cd "${CLIENTS_GENERATOR_DIR}" || true
fi
# Then remove the temporary directory
if [ -n "${tmpdir:-}" ] && [ -d "${tmpdir}" ]; then
rm -rf "${tmpdir}" || true
fi

# Restore original errexit state
if [ "$errexit_on" -eq 1 ]; then
set -e
fi
}
trap cleanup EXIT ERR INT TERM HUP

cd "${tmpdir:?}" || exit
echo " - Done. Temporary directory created at $tmpdir"
printf "Task 2.2: Run git init"
# shellcheck disable=SC2091
$(git init > /dev/null 2>&1) && echo " - Done"
git remote add origin $TEMPLATE_SOURCE_REPO
FETCH_CMD="git fetch -u --depth 1 origin $TEMPLATE_SOURCE_BRANCH:refs/heads/$TEMPLATE_SOURCE_BRANCH "
CLONE_CMD="git checkout $TEMPLATE_SOURCE_BRANCH -- $TEMPLATE_SOURCE_PATH"
printf "Task 2.3: Run fetch command: %s" "$FETCH_CMD"
# shellcheck disable=SC2091
$($FETCH_CMD > /dev/null 2>&1) && echo " - Done"
printf "Task 2.4: Run clone command: %s" "$CLONE_CMD"
# shellcheck disable=SC2091
$($CLONE_CMD > /dev/null 2>&1) && echo " - Done"

# Disable git credential prompts to fail fast in non-interactive environments
export GIT_TERMINAL_PROMPT=0

# Log git version for diagnostics
printf "Git version: "
git --version

printf "Task 2.2: Run git init\n"
# Try modern git init with -b flag, fall back to older method if it fails
if git init -q -b temp-branch 2>/dev/null; then
echo " - Done"
else
# Fallback for older Git versions
git init -q && git checkout -q -b temp-branch
echo " - Done"
fi
git remote add origin "$TEMPLATE_SOURCE_REPO"
printf "Task 2.3: Run fetch command: git fetch --depth 1 origin %s\n" "$TEMPLATE_SOURCE_BRANCH"
git fetch --depth 1 origin "$TEMPLATE_SOURCE_BRANCH" > /dev/null && echo " - Done"

# Verify the path exists in the fetched branch
printf "Task 2.4: Verify template path exists in branch\n"
if ! git ls-tree "origin/$TEMPLATE_SOURCE_BRANCH" "$TEMPLATE_SOURCE_PATH" 2>/dev/null | grep -q .; then
echo "Error: Template path '$TEMPLATE_SOURCE_PATH' does not exist in branch 'origin/$TEMPLATE_SOURCE_BRANCH'"
echo "Please verify that the generator '$GENERATOR' is available in the OpenAPI Generator repository."
exit 1
fi
echo " - Done"

printf "Task 2.5: Run checkout command: git checkout origin/%s -- %s\n" "$TEMPLATE_SOURCE_BRANCH" "$TEMPLATE_SOURCE_PATH"
git checkout "origin/$TEMPLATE_SOURCE_BRANCH" -- "$TEMPLATE_SOURCE_PATH" > /dev/null && echo " - Done"
printf "Task 2: Done\n\n"

COMMIT_HASH="$(git log -1 --format=format:"%H" origin/$TEMPLATE_SOURCE_BRANCH)"
if ! COMMIT_HASH="$(git rev-parse --verify "refs/remotes/origin/$TEMPLATE_SOURCE_BRANCH^{commit}" 2>/dev/null)"; then
echo "Error: Failed to resolve commit hash for branch 'origin/$TEMPLATE_SOURCE_BRANCH'"
echo "The branch reference may be invalid or missing."
exit 1
fi
TEMPLATE_SOURCE_DATA=$(cat <<EOF
{
"repo": "$TEMPLATE_SOURCE_REPO",
Expand All @@ -96,36 +168,30 @@ EOF
echo "$TEMPLATE_SOURCE_DATA" > "${CONFIG_PATH}/template-source.json"

echo "Task 3: Copy template to config directory"
COPY_CMD="cp -r "$TEMPLATE_SOURCE_PATH/" "${CONFIG_PATH}/template""
printf "Task 3.1: Run copy command: %s" "$COPY_CMD"
# shellcheck disable=SC2091
$($COPY_CMD > /dev/null 2>&1) && echo " - Done"
printf "Task 3.1: Create template directory and copy recursively\n"
mkdir -p "${CONFIG_PATH}/template"
cp -R "$TEMPLATE_SOURCE_PATH/." "${CONFIG_PATH}/template/" && echo " - Done"
printf "Task 3: Done\n\n"

echo "Task 4: Add sample commands to makefile"
printf "Task 4.1: Add %s build command" "$SDK_ID"
BUILD_CLIENT_SNIPPET=$(cat <<EOF

.PHONY: build-client-${SDK_ID}
build-client-${SDK_ID}:
make build-client sdk_language=${SDK_ID} tmpdir=\${TMP_DIR}
# ... any other custom build steps ...
EOF
)

echo "$BUILD_CLIENT_SNIPPET" >> "$CLIENTS_GENERATOR_DIR/Makefile"
echo " - Done"
printf "Task 4.2: Add %s test command" "$SDK_ID"
BUILD_CLIENT_SNIPPET=$(cat <<EOF

.PHONY: test-client-${SDK_ID}
test-client-${SDK_ID}: build-client-${SDK_ID}
# ... any custom test code ...
EOF
)

echo "$BUILD_CLIENT_SNIPPET" >> "$CLIENTS_GENERATOR_DIR/Makefile"
echo " - Done"
printf "Task 4.1: Add %s build command\n" "$SDK_ID"
BUILD_CLIENT_SNIPPET=$(printf "\n.PHONY: build-client-%s\nbuild-client-%s:\n\tmake build-client sdk_language=%s tmpdir=\${TMP_DIR}\n\t# ... any other custom build steps ...\n" "${SDK_ID}" "${SDK_ID}" "${SDK_ID}")

if ! grep -Fq "build-client-${SDK_ID}:" "$CLIENTS_GENERATOR_DIR/Makefile" 2>/dev/null; then
echo "$BUILD_CLIENT_SNIPPET" >> "$CLIENTS_GENERATOR_DIR/Makefile"
echo " - Done"
else
echo " - Already exists, skipping"
fi
printf "Task 4.2: Add %s test command\n" "$SDK_ID"
TEST_CLIENT_SNIPPET=$(printf "\n.PHONY: test-client-%s\ntest-client-%s: build-client-%s\n\t# ... any custom test code ...\n" "${SDK_ID}" "${SDK_ID}" "${SDK_ID}")

if ! grep -Fq "test-client-${SDK_ID}:" "$CLIENTS_GENERATOR_DIR/Makefile" 2>/dev/null; then
echo "$TEST_CLIENT_SNIPPET" >> "$CLIENTS_GENERATOR_DIR/Makefile"
echo " - Done"
else
echo " - Already exists, skipping"
fi

echo "Task 4: Done"

Expand Down
Loading