forked from TOMP-WG/TOMP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-API.sh
More file actions
executable file
·136 lines (113 loc) · 4.09 KB
/
Copy pathmerge-API.sh
File metadata and controls
executable file
·136 lines (113 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Define the work directory
path="./work/"
# Create directory if it doesn't exist
if [ ! -d "$path" ]; then
mkdir -p "$path"
fi
ask='y'
join_cmd=""
# Handle arguments
if [ "$#" -ne 0 ]; then
# If arguments are provided, they are treated as files
files=("$@") # Store all arguments in an array
drafts=() # No drafts by default if files are specified directly
ask='n'
else
echo "fetch files"
# Find files matching "TOMP-API-*.yaml" in current directory
# Using find to handle spaces in filenames more robustly, though globbing usually works
readarray -t files < <(find . -maxdepth 1 -name "TOMP-API-*.yaml" -printf '%f\n')
# Find files in "./draft modules/"
readarray -t drafts < <(find "./draft modules/" -maxdepth 1 -name "TOMP-API-*.yaml" -printf '%f\n')
fi
# Special handling for 'all' argument
if [ "$1" == 'all' ]; then
echo "fetch files"
readarray -t files < <(find . -maxdepth 1 -name 'TOMP-API-*.yaml' -printf '%f\n')
readarray -t drafts < <(find "./draft modules/" -maxdepth 1 -name "TOMP-API-*.yaml" -printf '%f\n')
ask='n'
fi
# Special handling for 'mp' argument
if [ "$1" == 'mp' ]; then
echo "merge mp files"
files=() # Empty files array
readarray -t drafts < <(find "./MP modules/" -maxdepth 1 -name "TOMP-API-*.yaml" -printf '%f\n')
ask='n'
fi
echo "Files to process: ${files[@]}"
# Process main files
for arg in "${files[@]}"; do
if [ "$arg" != 'TOMP-API-1-CORE.yaml' ]; then
if [[ "$arg" != 'TOMP-API-2-OFFERS.yaml' && "$arg" != 'TOMP-API-4-PURCHASE.yaml' ]]; then
defaultValue='N'
display="Add $arg [y/N]"
else
defaultValue='Y'
display="Add $arg [Y/n]"
fi
confirmation=""
if [ "$ask" == 'y' ]; then
read -p "$display " confirmation
else
confirmation='y'
fi
# If confirmation is empty, use defaultValue
if [ -z "$confirmation" ]; then
confirmation="$defaultValue"
fi
if [[ "$confirmation" == 'y' || "$confirmation" == 'Y' ]]; then
dest_file="$path$arg"
# Replace 'TOMP-API-1-CORE.yaml' etc. with empty string
# The PowerShell's script does three replacements on the same file in sequence.
# This means the second and third replacements are on the already modified content.
# We'll mimic this by piping through sed sequentially.
# First, copy the file to the destination, then apply replacements
cp "$arg" "$dest_file"
# Apply replacements. Note: sed -i modifies in place.
sed -i "s/TOMP-API-1-CORE.yaml//g" "$dest_file"
sed -i "s/TOMP-API-2-OFFERS.yaml//g" "$dest_file"
sed -i "s/TOMP-API-4-PURCHASE.yaml//g" "$dest_file"
echo "Processed $dest_file"
join_cmd+=" $dest_file"
fi
fi
done
# Process draft files
for arg in "${drafts[@]}"; do
confirmation=""
if [ "$1" == 'mp' ]; then
confirmation='y'
else
read -p "Add $arg [y/N] " confirmation
fi
if [[ "$confirmation" == 'y' || "$confirmation" == 'Y' ]]; then
source_dir=""
if [ "$1" == 'mp' ]; then
source_dir="./MP modules/"
else
source_dir="./draft modules/"
fi
dest_file="$path$arg"
# Copy the file first
cp "${source_dir}${arg}" "$dest_file"
# Apply replacement
sed -i "s|../TOMP-API-1-CORE.yaml||g" "$dest_file" # Use | as delimiter for sed due to / in path
join_cmd+=" $dest_file"
fi
done
# Construct the final yaml-merge command
final_merge_cmd="yaml-merge $join_cmd ./TOMP-API-1-CORE.yaml > ./TOMP-API-BOM.yaml"
echo "$final_merge_cmd"
eval "$final_merge_cmd" # Execute the command
# Handle encoding and renaming
in_file='./TOMP-API-BOM.yaml'
out_file='./TOMP-API.yaml'
if [ "$1" == 'mp' ]; then
out_file='./TOMP-API-MP.yaml'
fi
# Convert to UTF-8 without BOM
sed '1s/^\xEF\xBB\xBF//' "$in_file" > "$out_file"
# Clean up temporary files
rm -f "$in_file"
rm -rf "$path" # Remove the work directory and its contents