forked from GuilhE/KMP-ComposeUIViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportToXcode.sh
executable file
·89 lines (74 loc) · 2.35 KB
/
exportToXcode.sh
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
#!/bin/bash
# DEFAULT VALUES
kmp_module="shared"
ios_app="iosApp"
target_name="iosApp"
#####################
xcodeproj_path="$ios_app.xcodeproj"
files_source="$kmp_module/build/generated/ksp/"
group_name="SharedRepresentables"
files_destination="$ios_app/$group_name/"
copied_count=0
check_for_xcodeproj() {
if ! gem spec xcodeproj > /dev/null 2>&1; then
echo "Installing 'xcodeproj' gem..."
gem install xcodeproj
fi
}
copy_files() {
local files_source="$1"
local files_destination="$2"
while IFS= read -r -d '' file; do
destination_file="$files_destination/$(basename "$file")"
if [ ! -f "$destination_file" ] || ! cmp -s "$file" "$destination_file"; then
rsync -a --checksum "$file" "$files_destination"
files_copied+=("$file")
fi
done < <(find "$files_source" -type f -name '*.swift' -print0)
copied_count=${#files_copied[@]}
}
add_file_references() {
local xcodeproj_path="$1"
local target_name="$2"
local group_name="$3"
ruby_script='
require "xcodeproj"
xcodeproj_path = ARGV[0]
target_name = ARGV[1]
group_name = ARGV[2]
files_to_copy = Dir.glob("#{group_name}/*")
if files_to_copy.empty?
puts "> No files to copy. Exiting"
exit
end
xcodeproj = Xcodeproj::Project.open(xcodeproj_path)
group = xcodeproj[group_name] || xcodeproj.new_group(group_name)
target = xcodeproj.targets.find { |t| t.name == target_name }
existing_file_references = target.source_build_phase.files_references
files_added = false
files_to_copy.each do |file_path|
unless existing_file_references.any? { |file_ref| file_ref.path == file_path }
file_reference = group.new_file(file_path)
puts "> #{file_reference} added!"
target.add_file_references([file_reference])
files_added = true
end
end
unless files_added
puts "> No new references to copy"
end
xcodeproj.save
'
ruby -e "$ruby_script" "$xcodeproj_path" "$target_name" "$group_name"
}
check_for_xcodeproj
echo "> Copying files to $files_destination"
copy_files "$files_source" "$files_destination"
if [ "$copied_count" -gt 0 ]; then
echo "> Checking for new references to be added to xcodeproj"
cd iosApp || exit
add_file_references "$xcodeproj_path" "$target_name" "$group_name"
echo "> Done"
else
echo "> No files were copied, skipping reference check"
fi