Skip to content

Commit 91eb182

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 91eb182

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

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

Lines changed: 47 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,42 @@ 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+
cd "$target_dir"
113+
git add "$target_file" # This stages the deletion
114+
cd - > /dev/null
115+
fi
116+
fi
117+
done
118+
119+
# Step 2: Sync source files to target
120+
echo "Syncing source files..."
86121
find "$source_dir" -name "README.mdx" | while read -r source_file; do
87122
# Get the component path (e.g., anthropic/v0/README.mdx from pkg/component/ai/anthropic/v0/README.mdx)
88123
component_path="${source_file#$source_dir/}"
@@ -97,10 +132,6 @@ jobs:
97132
# Create flattened filename using component ID (e.g., anthropic.mdx)
98133
target_file="$target_dir/${COMPONENT_ID}.mdx"
99134
100-
# Create target directory if it doesn't exist
101-
target_file_dir=$(dirname "$target_file")
102-
mkdir -p "$target_file_dir"
103-
104135
# Compare files and copy if different
105136
if [ ! -f "$target_file" ] || ! cmp -s "$source_file" "$target_file"; then
106137
echo "Updating: $component_path -> ${COMPONENT_ID}.mdx"
@@ -119,9 +150,9 @@ jobs:
119150
local target_dir="$2"
120151
local component_type="$3"
121152
local index_file="readme/docs/Component/${component_type}.mdx"
122-
153+
123154
echo "Updating component index for $component_type..."
124-
155+
125156
# Remove existing table if it exists
126157
if [[ "${OSTYPE}" == "darwin"* ]]; then
127158
# macOS
@@ -131,17 +162,17 @@ jobs:
131162
sed -i '/^ *|.*| *$/d; /^[[:space:]]*:-+[[:space:]]*|[[:space:]]*-+.*$/d' "$index_file"
132163
sed -i '/^$/N;/^\n$/D' "$index_file"
133164
fi
134-
165+
135166
# Add new table header
136167
echo "" >> "$index_file"
137168
echo "| Name | Description |" >> "$index_file"
138169
echo "|:--|:--|" >> "$index_file"
139-
170+
140171
# Add components to table
141172
for component_file in "$target_dir"/*.mdx; do
142173
if [[ -f "$component_file" ]]; then
143174
component_id=$(basename "$component_file" .mdx)
144-
175+
145176
# Find the corresponding source directory by matching component_id in definition.yaml files
146177
component_source_dir=""
147178
for dir in "$source_dir"/*; do
@@ -153,7 +184,7 @@ jobs:
153184
fi
154185
fi
155186
done
156-
187+
157188
# Use the metadata we found
158189
if [[ -n "$component_source_dir" ]]; then
159190
component_title="$COMPONENT_TITLE"
@@ -162,12 +193,12 @@ jobs:
162193
component_title="$component_id"
163194
component_description="Component definition is missing"
164195
fi
165-
196+
166197
# Create table row
167198
echo "| [$component_title](/docs/$component_id) | $component_description |" >> "$index_file"
168199
fi
169200
done
170-
201+
171202
# Stage the updated index file
172203
cd "readme/docs/Component"
173204
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)