Module: PrivacyUtils

Defined in:
lib/cocoapods-privacy/privacy/PrivacyUtils.rb

Class Method Summary collapse

Class Method Details

.add_spaces_to_string(str, num_spaces) ⇒ Object

使用字符串乘法添加指定数量的空格



41
42
43
44
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 41

def self.add_spaces_to_string(str, num_spaces)
  spaces = ' ' * num_spaces
  "#{spaces}#{str}"
end

.cache_config_fileObject

config.json 文件



62
63
64
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 62

def self.cache_config_file
  Common::Config.instance.cache_config_file
end

.cache_log_fileObject

config.json 文件



67
68
69
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 67

def self.cache_log_file
  Common::Config.instance.cache_log_file
end

.cache_privacy_etag_foldObject

etag 文件夹



57
58
59
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 57

def self.cache_privacy_etag_fold
  Common::Config.instance.cache_privacy_etag_fold
end

.cache_privacy_foldObject



52
53
54
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 52

def self.cache_privacy_fold
  Common::Config.instance.cache_privacy_fold
end

.count_spaces_before_first_character(str) ⇒ Object

使用正则表达式匹配第一个字符前的空格数量



35
36
37
38
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 35

def self.count_spaces_before_first_character(str)
  match = str.match(/\A\s*/)
  match ? match[0].length : 0
end

.create_file_and_fold_if_no_exit(file_path, file_content = nil) ⇒ Object

创建文件,并写入默认值,文件路径不存在会自动创建



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 98

def self.create_file_and_fold_if_no_exit(file_path,file_content = nil)
  folder_path = File.dirname(file_path)
  FileUtils.mkdir_p(folder_path) unless File.directory?(folder_path)

  # 创建文件(如果不存在/或为空)
  if !File.exist?(file_path) || File.zero?(file_path)
    File.open(file_path, 'w') do |file|
      file.write(file_content)
    end
    return true
  end 
  return false
end

.create_privacy_if_empty(file_path) ⇒ Object

创建默认隐私协议文件



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 72

def self.create_privacy_if_empty(file_path) 
   # 文件内容
  file_content = <<~EOS
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
    <key>NSPrivacyTracking</key>
    <false/>
    <key>NSPrivacyTrackingDomains</key>
    <array/>
    <key>NSPrivacyCollectedDataTypes</key>
    <array/>
    <key>NSPrivacyAccessedAPITypes</key>
    <array/>
  </dict>
  </plist>     
  EOS

  isCreate = create_file_and_fold_if_no_exit(file_path,file_content)
  if isCreate
    puts "【隐私清单】(初始化)存放地址 => #{file_path}"
  end
end

.find_group_by_path(group, path) ⇒ Object

查询group 中是否有执行路径的子group



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 113

def self.find_group_by_path(group,path)
  result = nil
  sub_group = group.children
  if sub_group && !sub_group.empty?
    sub_group.each do |item|
      if item.path == path
        result = item
        break
      end
    end
  end
  result
end

.isMainProjectObject

通过是否包含podspec 来判断是否为主工程



11
12
13
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 11

def self.isMainProject
  !(podspec_file_path && !podspec_file_path.empty?)
end

.podspec_file_pathObject

查找podspec



16
17
18
19
20
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 16

def self.podspec_file_path
  base_path = Pathname.pwd
  matching_files = Dir.glob(File.join(base_path, '*.podspec'))
  matching_files.first
end

.privacy_nameObject



6
7
8
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 6

def self.privacy_name
  'PrivacyInfo.xcprivacy'
end

.project_code_foldObject

xcode工程主代码目录



29
30
31
32
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 29

def self.project_code_fold
  projectPath = project_path
  File.join(Pathname.pwd,File.basename(projectPath, File.extname(projectPath)))
end

.project_pathObject

xcode工程地址



23
24
25
26
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 23

def self.project_path
  matching_files = Dir[File.join(Pathname.pwd, '*.xcodeproj')].uniq
  matching_files.first
end

.to_md5(string) ⇒ Object



46
47
48
49
50
# File 'lib/cocoapods-privacy/privacy/PrivacyUtils.rb', line 46

def self.to_md5(string)
  md5 = Digest::MD5.new
  md5.update(string)
  md5.hexdigest
end