Skip to content

Commit 63dada5

Browse files
feat: pipeline to push the sample app's tutorials to the docs site
1 parent 46e96e8 commit 63dada5

File tree

3 files changed

+472
-0
lines changed

3 files changed

+472
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const { execSync } = require('child_process');
6+
7+
// Configuration
8+
const PASSPORT_ROOT = './sample/Assets/Scripts/Passport';
9+
const OUTPUT_DIR = './_parsed';
10+
const OUTPUT_FILE = path.join(OUTPUT_DIR, 'passport-features.json');
11+
const FEATURES_JSON_PATH = path.join(PASSPORT_ROOT, 'features.json');
12+
13+
// Ensure output directory exists
14+
if (!fs.existsSync(OUTPUT_DIR)) {
15+
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
16+
}
17+
18+
console.log('Processing Passport features metadata...');
19+
20+
// Load features.json to map script files to feature names
21+
let featuresMap = {};
22+
try {
23+
const featuresContent = fs.readFileSync(FEATURES_JSON_PATH, 'utf8');
24+
const featuresJson = JSON.parse(featuresContent);
25+
26+
// Create mapping of script filename to feature name
27+
featuresJson.features.forEach(feature => {
28+
const [featureName, scriptFile] = Object.entries(feature)[0];
29+
featuresMap[scriptFile] = featureName;
30+
});
31+
} catch (error) {
32+
console.error(`Error reading features.json: ${error.message}`);
33+
process.exit(1);
34+
}
35+
36+
// Find all metadata.json files
37+
const findMetadataFiles = () => {
38+
const result = execSync(`find "${PASSPORT_ROOT}" -name "metadata.json" -type f`).toString().trim();
39+
return result.split('\n').filter(Boolean);
40+
};
41+
42+
// Process metadata files
43+
const processMetadataFiles = (metadataFiles) => {
44+
const features = [];
45+
46+
metadataFiles.forEach(metadataFile => {
47+
console.log(`Processing ${metadataFile}`);
48+
49+
// Extract feature directory
50+
const featureDir = path.dirname(metadataFile);
51+
52+
// Find script file in this directory
53+
let featureName = '';
54+
try {
55+
const dirFiles = fs.readdirSync(featureDir);
56+
const scriptFiles = dirFiles.filter(file => file.endsWith('.cs'));
57+
58+
if (scriptFiles.length > 0) {
59+
// Look up the feature name in featuresMap
60+
const scriptFile = scriptFiles[0];
61+
featureName = featuresMap[scriptFile] || '';
62+
}
63+
64+
// If not found in features.json, fallback to directory name
65+
if (!featureName) {
66+
console.warn(`Feature for script in ${featureDir} not found in features.json, using directory name`);
67+
featureName = path.basename(featureDir);
68+
}
69+
} catch (error) {
70+
console.warn(`Error processing directory ${featureDir}: ${error.message}`);
71+
featureName = path.basename(featureDir);
72+
}
73+
74+
console.log(`Feature name: ${featureName}`);
75+
76+
// Read and process metadata
77+
try {
78+
const metadataContent = fs.readFileSync(metadataFile, 'utf8');
79+
const metadata = JSON.parse(metadataContent);
80+
81+
// Add feature name to metadata
82+
metadata.name = featureName;
83+
features.push(metadata);
84+
} catch (error) {
85+
console.error(`Error processing metadata file ${metadataFile}: ${error.message}`);
86+
}
87+
});
88+
89+
return features;
90+
};
91+
92+
// Main execution
93+
const metadataFiles = findMetadataFiles();
94+
const features = processMetadataFiles(metadataFiles);
95+
96+
// Create the final passport-features.json
97+
const passportFeatures = { features };
98+
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(passportFeatures, null, 2));
99+
100+
console.log(`Created ${OUTPUT_FILE}`);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -x
5+
6+
# Directory where docs repo is cloned
7+
DOCS_REPO_DIR=${CLONE_DIR:-"./imx-docs"}
8+
9+
# Root of the Passport features
10+
PASSPORT_ROOT="./sample/Assets/Scripts/Passport"
11+
12+
echo "Processing Passport tutorials..."
13+
14+
# Load features.json to map script files to feature names
15+
FEATURES_JSON="$PASSPORT_ROOT/features.json"
16+
if [ ! -f "$FEATURES_JSON" ]; then
17+
echo "Error: features.json not found at $FEATURES_JSON"
18+
exit 1
19+
fi
20+
21+
# Create _tutorials directory in docs repo
22+
TUTORIALS_DIR="$DOCS_REPO_DIR/docs/main/example/zkEVM/passport-examples/_tutorials"
23+
mkdir -p "$TUTORIALS_DIR"
24+
25+
# Find all tutorial.md files
26+
TUTORIAL_FILES=$(find "$PASSPORT_ROOT" -name "tutorial.md" -type f)
27+
28+
for TUTORIAL_FILE in $TUTORIAL_FILES; do
29+
echo "Processing $TUTORIAL_FILE"
30+
31+
# Extract feature directory
32+
FEATURE_DIR=$(dirname "$TUTORIAL_FILE")
33+
34+
# Try to find script file in this directory
35+
SCRIPT_FILE=$(find "$FEATURE_DIR" -name "*.cs" -type f | head -n 1)
36+
if [ -z "$SCRIPT_FILE" ]; then
37+
echo "Warning: No script file found in $FEATURE_DIR, using directory name"
38+
FEATURE_NAME=$(basename "$FEATURE_DIR")
39+
else
40+
# Extract script filename
41+
SCRIPT_FILENAME=$(basename "$SCRIPT_FILE")
42+
43+
# Look up the feature name in features.json
44+
FEATURE_NAME=$(jq -r ".features[] | to_entries[] | select(.value == \"$SCRIPT_FILENAME\") | .key" "$FEATURES_JSON")
45+
46+
# If not found in features.json, fallback to directory name
47+
if [ -z "$FEATURE_NAME" ] || [ "$FEATURE_NAME" == "null" ]; then
48+
echo "Warning: Feature for script $SCRIPT_FILENAME not found in features.json, using directory name"
49+
FEATURE_NAME=$(basename "$FEATURE_DIR")
50+
fi
51+
fi
52+
53+
echo "Feature name: $FEATURE_NAME"
54+
55+
# Copy and rename tutorial file
56+
cp "$TUTORIAL_FILE" "$TUTORIALS_DIR/${FEATURE_NAME}.md"
57+
echo "Copied $TUTORIAL_FILE to $TUTORIALS_DIR/${FEATURE_NAME}.md"
58+
done
59+
60+
# Copy the generated JSON file
61+
JSON_FILE="./_parsed/passport-features.json"
62+
if [ -f "$JSON_FILE" ]; then
63+
# Create directory for JSON file if it doesn't exist
64+
JSON_DIR="$DOCS_REPO_DIR/docs/main/example/zkEVM/passport-examples"
65+
mkdir -p "$JSON_DIR"
66+
67+
# Copy JSON file
68+
cp "$JSON_FILE" "$JSON_DIR/passport-features.json"
69+
echo "Copied $JSON_FILE to $JSON_DIR/passport-features.json"
70+
else
71+
echo "Warning: No passport-features.json found at $JSON_FILE"
72+
fi
73+
74+
echo "Passport tutorial processing complete."

0 commit comments

Comments
 (0)