Class: Dongjia::Binarization

Inherits:
Object
  • Object
show all
Defined in:
lib/dongjia_binarization.rb,
lib/dongjia_binarization.rb

Constant Summary collapse

@@ctx =
nil
@@pod_cfg =
{}
@@enabled =
false
@@submodules =
{}
@@server_host =
""
@@target_name =

项目的 target 名

"dongjia"
@@pods_proj =

Pods.xcodeproj

nil
@@pods_target =

Pods.xcodeproj 中的 target 对象

nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_private_config(ctx, binary) ⇒ Object

加载配置



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
# File 'lib/dongjia_binarization.rb', line 25

def load_private_config(ctx, binary)
  @@ctx = ctx

  submodule_info = load_submodules_info
  @@submodules = submodule_info.to_h { |s|
    [
      s[:name],
      { sha: s[:sha], binary: s[:binary], }
    ]
  }

  @@enabled ||= binary["enabled"]
  @@server_host = binary["server_host"]
  @@target_name = (binary["target_name"] || "").strip
  ignores = binary["ignores"] || []

  if @@enabled
    @@submodules = query_components_existing(submodule_info).to_h { |s|
      [
        s["name"],
        { sha: s["sha"], binary: s["binary"], }
      ]
    }.each { |k, v|
      ignored = ignores.include?(k)
      v[:binary] &&= !ignored && @@enabled
      v[:ignored] = ignored
    }
  end
rescue => e
  puts "Binarization error: #{e}"
end

.process(installer) ⇒ Object

开始处理二进制



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
137
138
139
140
141
142
# File 'lib/dongjia_binarization.rb', line 91

def process(installer)
  return if @@ctx == nil || @@ctx.sandbox_root == nil

  path = File.join(@@ctx.sandbox_root, "Pods.xcodeproj")
  @@pods_proj = Xcodeproj::Project.open(path) if File.exist?(path)
  @@pods_target = (@@target_name.empty?) ? @@pods_proj.targets.first : @@pods_proj.target_by_name("Pods-#{@@target_name}")
  if @@pods_target.nil?
    Pod::UI.warn("[Binarization] 未完成二进制转换,target_name 配置错误")
    return
  end

  setup_pod_cfg(installer)

  download_frameworks

  each_pod_proj do |name, cfg|
    process_pods_project_dependencies(name)

    # 处理 Binary Group
    process_binary_group(name)

    # 处理 XXX-xcframeworks.sh 脚本
    process_xcframeworks_shell(name)

    process_pod_xcconfig(name)
  end

  # 更新依赖关系
  each_pod_proj do |name, cfg|
    target = cfg[:target]
    next if target.nil?
    delete_list = []
    add_list = []
    target.dependencies.each { |dep|
      dep_pod_name = real_name(dep.name)
      dep_cfg = @@pod_cfg[dep_pod_name]
      next if dep_cfg.nil?
      if dep_cfg[:target].name != dep.name
        add_list << dep_cfg[:target]
        delete_list << dep
      end
    }
    target.dependencies.delete_if { |dep| delete_list.include?(dep) }
    add_list.each { |t| target.add_dependency(t) }
    cfg[:save] ||= !delete_list.empty? || !add_list.empty?
  end

  # 保存
  save_projects

  process_aggregate_target_xcconfig(installer.aggregate_targets.first.name)
end

.remove_dirty_pod_projectsObject

移除脏工程



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
# File 'lib/dongjia_binarization.rb', line 58

def remove_dirty_pod_projects
  if @@enabled
    @@submodules
      .filter { |k,v| !v[:binary] }
      .map { |k,v| k }
      .each { |name|
        path = File.join(@@ctx.sandbox_root, "#{name}.xcodeproj")
        next unless File.exist?(path)
        proj = Xcodeproj::Project.open(path)
        if proj.targets.map(&:name).include?("#{name}-Binary")
          FileUtils.rm_rf(path)
        end
      }
  else
    # 不启用,清除所有包含 -Binary 的工程
    removing_paths = []
    Dir.foreach(@@ctx.sandbox_root) { |filename|
      next if File.extname(filename) != '.xcodeproj'
      pod_name = filename.delete_suffix('.xcodeproj')
      next if pod_name == "Pods"
      process_pod_xcconfig(pod_name)
      path = File.join(@@ctx.sandbox_root, filename)
      proj = Xcodeproj::Project.open(path)
      included_binary_target = proj.targets.map(&:name).any? { |target_name|
        target_name.end_with?("-Binary")
      }
      removing_paths << path if included_binary_target
    }
    removing_paths.each { |path| FileUtils.rm_rf(path) }
  end
end

.xcframeworks_shell(pod_name) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/dongjia_binarization.rb', line 464

def self.xcframeworks_shell(pod_name)
  <<-DESC
#!/bin/sh
set -e
set -u
set -o pipefail

function on_error {
  echo "$(realpath -mq "${0}"):$1: error: Unexpected failure"
}
trap 'on_error $LINENO' ERR


# This protects against multiple targets copying the same framework dependency at the same time. The solution
# was originally proposed here: https://lists.samba.org/archive/rsync/2008-February/020158.html
RSYNC_PROTECT_TMP_FILES=(--filter "P .*.??????")


copy_dir()
{
  local source="$1"
  local destination="$2"

  # Use filter instead of exclude so missing patterns don't throw errors.
  echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \\"- CVS/\\" --filter \\"- .svn/\\" --filter \\"- .git/\\" --filter \\"- .hg/\\" \\"${source}\\" \\"${destination}\\""
  rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}" "${destination}"
}

SELECT_SLICE_RETVAL=""

select_slice() {
  local paths=("$@")
  # Locate the correct slice of the .xcframework for the current architectures
  local target_path=""

  # Split archs on space so we can find a slice that has all the needed archs
  local target_archs=$(echo $ARCHS | tr " " "\\n")

  local target_variant=""
  if [[ "$PLATFORM_NAME" == *"simulator" ]]; then
target_variant="simulator"
  fi
  if [[ ! -z ${EFFECTIVE_PLATFORM_NAME+x} && "$EFFECTIVE_PLATFORM_NAME" == *"maccatalyst" ]]; then
target_variant="maccatalyst"
  fi
  for i in ${!paths[@]}; do
local matched_all_archs="1"
for target_arch in $target_archs
do
  if ! [[ "${paths[$i]}" == *"$target_variant"* ]]; then
    matched_all_archs="0"
    break
  fi

  # Verifies that the path contains the variant string (simulator or maccatalyst) if the variant is set.
  if [[ -z "$target_variant" && ("${paths[$i]}" == *"simulator"* || "${paths[$i]}" == *"maccatalyst"*) ]]; then
    matched_all_archs="0"
    break
  fi

  # This regex matches all possible variants of the arch in the folder name:
  # Let's say the folder name is: ios-armv7_armv7s_arm64_arm64e/CoconutLib.framework
  # We match the following: -armv7_, _armv7s_, _arm64_ and _arm64e/.
  # If we have a specific variant: ios-i386_x86_64-simulator/CoconutLib.framework
  # We match the following: -i386_ and _x86_64-
  # When the .xcframework wraps a static library, the folder name does not include
  # any .framework. In that case, the folder name can be: ios-arm64_armv7
  # We also match _armv7$ to handle that case.
  local target_arch_regex="[_\\-]${target_arch}([\\/_\\-]|$)"
  if ! [[ "${paths[$i]}" =~ $target_arch_regex ]]; then
    matched_all_archs="0"
    break
  fi
done

if [[ "$matched_all_archs" == "1" ]]; then
  # Found a matching slice
  echo "Selected xcframework slice ${paths[$i]}"
  SELECT_SLICE_RETVAL=${paths[$i]}
  break
fi
  done
}

install_library() {
  local source="$1"
  local name="$2"
  local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"

  # Libraries can contain headers, module maps, and a binary, so we'll copy everything in the folder over

  local source="$binary"
  echo "rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter \\"- CVS/\\" --filter \\"- .svn/\\" --filter \\"- .git/\\" --filter \\"- .hg/\\" \\"${source}/*\\" \\"${destination}\\""
  rsync --delete -av "${RSYNC_PROTECT_TMP_FILES[@]}" --links --filter "- CVS/" --filter "- .svn/" --filter "- .git/" --filter "- .hg/" "${source}/*" "${destination}"
}

# Copies a framework to derived data for use in later build phases
install_framework()
{
  local source="$1"
  local name="$2"
  local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"

  if [ ! -d "$destination" ]; then
mkdir -p "$destination"
  fi

  copy_dir "$source" "$destination"
  echo "Copied $source to $destination"
}

install_xcframework_library() {
  local basepath="$1"
  local name="$2"
  local paths=("$@")

  # Locate the correct slice of the .xcframework for the current architectures
  select_slice "${paths[@]}"
  local target_path="$SELECT_SLICE_RETVAL"
  if [[ -z "$target_path" ]]; then
echo "warning: [CP] Unable to find matching .xcframework slice in '${paths[@]}' for the current build architectures ($ARCHS)."
return
  fi

  install_framework "$basepath/$target_path" "$name"
}

install_xcframework() {
  local basepath="$1"
  local name="$2"
  local package_type="$3"
  local paths=("$@")

  # Locate the correct slice of the .xcframework for the current architectures
  select_slice "${paths[@]}"
  local target_path="$SELECT_SLICE_RETVAL"
  if [[ -z "$target_path" ]]; then
echo "warning: [CP] Unable to find matching .xcframework slice in '${paths[@]}' for the current build architectures ($ARCHS)."
return
  fi
  local source="$basepath/$target_path"

  local destination="${PODS_XCFRAMEWORKS_BUILD_DIR}/${name}"

  if [ ! -d "$destination" ]; then
mkdir -p "$destination"
  fi

  copy_dir "$source/" "$destination"

  echo "Copied $source to $destination"
}

install_xcframework "${PODS_ROOT}/_Frameworks/#{pod_name}.xcframework" "#{pod_name}" "framework" "ios-arm64_armv7" "ios-x86_64-simulator"
DESC
end

Instance Method Details

#git_sha_value(component_name) ⇒ Object

获取子模块的哈希值



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/dongjia_binarization.rb', line 222

def git_sha_value(component_name)
  git_dir = ".git/modules/componentsOnRemote/#{component_name}"
  git_sha = File.read(File.join(git_dir, "HEAD")).strip
  if git_sha.start_with?("ref: ")
    head_file = git_sha[5, git_sha.length]
    head_path = File.join(git_dir, head_file)
    if File.exist?(head_path)
      git_sha = File.read(head_path).strip
    else
      git_sha = nil
    end
  end
  git_sha
end