Class: Pod::Command::Bin::Import

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-lhj-bin/command/bin/import.rb

Instance Method Summary collapse

Methods inherited from Pod::Command::Bin

#validate!

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ Import

Returns a new instance of Import.



12
13
14
15
16
17
18
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 12

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @framework = argv.option('framework')
  @header_map = {}
  @header_folder_map = {}
  super
end

Instance Method Details

#all_header_map(folder) ⇒ Object



68
69
70
71
72
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 68

def all_header_map(folder)
  headers = Dir.glob("#{pod_folder_name}/#{folder}/**/*.h")
  headers.map! { |f| f.split('/').last }
  headers
end

#child_dirObject



60
61
62
63
64
65
66
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 60

def child_dir
  dirs = Dir.entries(pod_folder_name)
  dirs.reject! do |d|
    File.directory?(d) || /\./ =~ d || /_Prebuild/ =~ d || /Target/ =~ d || /Headers/ =~ d || /Podspecs/ =~ d || framework_black_list.any?{ |f| f == d }
  end
  dirs
end

#exist_in_file(file, header) ⇒ Object

def format_string(file, line)

  result = line
  if /(\W)(TicketList)(\W)/ =~ line
    result = result.gsub(/(\W)(TicketList)(\W)/, '\1TKTicketModel\3')
  end
  result
end


128
129
130
131
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 128

def exist_in_file(file, header)
  folder_name = @header_folder_map[header.to_sym]
  Regexp.new("/Pods/#{folder_name}") =~ File.path(file) if folder_name
end

#file_string(file) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 99

def file_string(file)
  str = ''
  File.open(file, 'r+') do |f|
    f.each_line do |line|
      str += format_string(f, line)
    end
  end
  str
end

#find_all_sub_folderObject



74
75
76
77
78
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 74

def find_all_sub_folder
  Find.find(@current_path).each do |f|
    handler_file f if f =~ /APPDelegate/
  end
end

#find_head_key(line) ⇒ Object



133
134
135
136
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 133

def find_head_key(line)
  header_reg = /"\D*.h"/
  line.match(header_reg)
end

#format_string(file, line) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 109

def format_string(file, line)
  result = line
  if line =~ /#import/
    ma = find_head_key(line)
    result = line.gsub(ma[0], @header_map[ma[0].to_sym] || ma[0]) if ma && !exist_in_file(file, ma[0])
  end
  result
end

#framework_black_listObject



46
47
48
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 46

def framework_black_list
  %w[mob_sharesdk]
end

#framework_name_mapObject



42
43
44
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 42

def framework_name_map
  { 'lottie-ios': 'Lottie', 'UITableView+FDTemplateLayoutCell': 'UITableView_FDTemplateLayoutCell', 'mob_sharesdk': 'ShareSDK' }
end

#generate_header_map@header_map

Returns:

  • (@header_map)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 28

def generate_header_map
  framework_headers = {}
  folders = child_dir
  folders.each { |name| framework_headers[name] = all_header_map(name) }
  framework_headers.each do |key, value|
    value.each do |header|
      header_key = "\"#{header}\""
      framework_name = framework_name_map[key.to_sym] || key
      @header_map[header_key.to_sym] = "<#{framework_name}/#{header}>"
      @header_folder_map[header_key.to_sym] = key
    end
  end
end

#handler_file(file) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 90

def handler_file(file)
  File.chmod(0o644, file)
  str = file_string(file)
  File.open(file, 'w+') do |f|
    f.write(str)
  end
  File.chmod(0o444, file) if file =~ /Pods/
end

#pod_folder_nameObject



56
57
58
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 56

def pod_folder_name
  File.directory?("#{@current_path}/Pods") ? "#{@current_path}/Pods" : "#{@current_path}/Example/Pods"
end

#runObject



20
21
22
23
24
25
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 20

def run
  generate_header_map
  # say_header_map
  # find_all_sub_folder
  update_source_header
end

#say_header_mapObject



50
51
52
53
54
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 50

def say_header_map
  @header_map.each do |key, value|
    puts "#{key}===> #{value}"
  end
end

#update_source_headerObject



80
81
82
83
84
85
86
87
88
# File 'lib/cocoapods-lhj-bin/command/bin/import.rb', line 80

def update_source_header
  Dir.glob("#{@current_path}/**/*.{m,h,pch}").each do |f|
    if f =~ /Pods/
      handler_file(f) if f =~ %r{Pods/ML}
    else
      handler_file(f)
    end
  end
end