Skip to content

Commit 349ccd3

Browse files
committed
ci(workflows): add component docs sync cleanup (#1045)
Because - when a component is removed, its component doc should be removed too This commit - implements the logic
1 parent b286a18 commit 349ccd3

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

.github/workflows/sync-component-docs-reusable.yml

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ jobs:
5151
local component_id=""
5252
local component_title=""
5353
local component_description=""
54-
54+
5555
# Find the latest version directory (v0, v1, v2, etc.)
5656
latest_version=$(find "$component_source_dir" -maxdepth 1 -type d -name "v*" | sort -V | tail -n 1)
57-
57+
5858
# Extract metadata from definition.yaml in the latest version
5959
if [[ -n "$latest_version" && -f "$latest_version/config/definition.yaml" ]]; then
6060
component_id=$(yq -r '.id' "$latest_version/config/definition.yaml")
@@ -67,7 +67,7 @@ jobs:
6767
component_title="$component_name"
6868
component_description="Component definition is missing"
6969
fi
70-
70+
7171
# Return values (bash doesn't have return values, so we use global variables)
7272
COMPONENT_ID="$component_id"
7373
COMPONENT_TITLE="$component_title"
@@ -82,7 +82,43 @@ jobs:
8282
8383
echo "Comparing $component_type components..."
8484
85-
# Find all README.mdx files in source directory
85+
# Create target directory if it doesn't exist
86+
mkdir -p "$target_dir"
87+
88+
# Step 1: Remove target files that don't exist in source
89+
echo "Cleaning up orphaned files..."
90+
for target_file in "$target_dir"/*.mdx; do
91+
if [[ -f "$target_file" ]]; then
92+
component_id=$(basename "$target_file" .mdx)
93+
94+
# Check if this component exists in source
95+
component_exists=false
96+
for source_component_dir in "$source_dir"/*; do
97+
if [[ -d "$source_component_dir" ]]; then
98+
latest_version=$(find "$source_component_dir" -maxdepth 1 -type d -name "v*" | sort -V | tail -n 1)
99+
if [[ -n "$latest_version" && -f "$latest_version/config/definition.yaml" ]]; then
100+
source_component_id=$(yq -r '.id' "$latest_version/config/definition.yaml")
101+
if [[ "$source_component_id" == "$component_id" ]]; then
102+
component_exists=true
103+
break
104+
fi
105+
fi
106+
fi
107+
done
108+
109+
if [[ "$component_exists" == "false" ]]; then
110+
echo "Removing orphaned file: $component_id.mdx"
111+
rm "$target_file"
112+
# Stage the deletion properly
113+
cd "$target_dir"
114+
git rm "$component_id.mdx"
115+
cd - > /dev/null
116+
fi
117+
fi
118+
done
119+
120+
# Step 2: Sync source files to target
121+
echo "Syncing source files..."
86122
find "$source_dir" -name "README.mdx" | while read -r source_file; do
87123
# Get the component path (e.g., anthropic/v0/README.mdx from pkg/component/ai/anthropic/v0/README.mdx)
88124
component_path="${source_file#$source_dir/}"
@@ -94,13 +130,15 @@ jobs:
94130
component_source_dir="$source_dir/$component_name"
95131
get_component_metadata "$component_source_dir"
96132
133+
# Skip if component ID is null or empty
134+
if [[ "$COMPONENT_ID" == "null" || -z "$COMPONENT_ID" ]]; then
135+
echo "Skipping component with invalid ID: $component_name"
136+
continue
137+
fi
138+
97139
# Create flattened filename using component ID (e.g., anthropic.mdx)
98140
target_file="$target_dir/${COMPONENT_ID}.mdx"
99141
100-
# Create target directory if it doesn't exist
101-
target_file_dir=$(dirname "$target_file")
102-
mkdir -p "$target_file_dir"
103-
104142
# Compare files and copy if different
105143
if [ ! -f "$target_file" ] || ! cmp -s "$source_file" "$target_file"; then
106144
echo "Updating: $component_path -> ${COMPONENT_ID}.mdx"
@@ -119,9 +157,9 @@ jobs:
119157
local target_dir="$2"
120158
local component_type="$3"
121159
local index_file="readme/docs/Component/${component_type}.mdx"
122-
160+
123161
echo "Updating component index for $component_type..."
124-
162+
125163
# Remove existing table if it exists
126164
if [[ "${OSTYPE}" == "darwin"* ]]; then
127165
# macOS
@@ -131,17 +169,17 @@ jobs:
131169
sed -i '/^ *|.*| *$/d; /^[[:space:]]*:-+[[:space:]]*|[[:space:]]*-+.*$/d' "$index_file"
132170
sed -i '/^$/N;/^\n$/D' "$index_file"
133171
fi
134-
172+
135173
# Add new table header
136174
echo "" >> "$index_file"
137175
echo "| Name | Description |" >> "$index_file"
138176
echo "|:--|:--|" >> "$index_file"
139-
177+
140178
# Add components to table
141179
for component_file in "$target_dir"/*.mdx; do
142180
if [[ -f "$component_file" ]]; then
143181
component_id=$(basename "$component_file" .mdx)
144-
182+
145183
# Find the corresponding source directory by matching component_id in definition.yaml files
146184
component_source_dir=""
147185
for dir in "$source_dir"/*; do
@@ -153,7 +191,7 @@ jobs:
153191
fi
154192
fi
155193
done
156-
194+
157195
# Use the metadata we found
158196
if [[ -n "$component_source_dir" ]]; then
159197
component_title="$COMPONENT_TITLE"
@@ -162,12 +200,12 @@ jobs:
162200
component_title="$component_id"
163201
component_description="Component definition is missing"
164202
fi
165-
203+
166204
# Create table row
167205
echo "| [$component_title](/docs/$component_id) | $component_description |" >> "$index_file"
168206
fi
169207
done
170-
208+
171209
# Stage the updated index file
172210
cd "readme/docs/Component"
173211
git add "${component_type}.mdx"

.github/workflows/sync-component-docs.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ on:
77
- main
88
tags:
99
- "*-rc"
10-
paths:
11-
- "pkg/component/**"
1210
release:
1311
types: [published]
1412

0 commit comments

Comments
 (0)