Class: Lhj::Command::Import

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/head_import.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Import

Returns a new instance of Import.



11
12
13
14
15
16
17
# File 'lib/lhj/command/head_import.rb', line 11

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



67
68
69
70
71
# File 'lib/lhj/command/head_import.rb', line 67

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

#child_dirObject



59
60
61
62
63
64
65
# File 'lib/lhj/command/head_import.rb', line 59

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



117
118
119
120
# File 'lib/lhj/command/head_import.rb', line 117

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



98
99
100
101
102
103
104
105
106
# File 'lib/lhj/command/head_import.rb', line 98

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



73
74
75
76
77
# File 'lib/lhj/command/head_import.rb', line 73

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



122
123
124
125
# File 'lib/lhj/command/head_import.rb', line 122

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

#format_string(file, line) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/lhj/command/head_import.rb', line 108

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



45
46
47
# File 'lib/lhj/command/head_import.rb', line 45

def framework_black_list
  %w[mob_sharesdk]
end

#framework_name_mapObject



41
42
43
# File 'lib/lhj/command/head_import.rb', line 41

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

#generate_header_map@header_map

Returns:

  • (@header_map)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lhj/command/head_import.rb', line 27

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



89
90
91
92
93
94
95
96
# File 'lib/lhj/command/head_import.rb', line 89

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



55
56
57
# File 'lib/lhj/command/head_import.rb', line 55

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

#runObject



19
20
21
22
23
24
# File 'lib/lhj/command/head_import.rb', line 19

def run
  generate_header_map
  # say_header_map
  # find_all_sub_folder
  update_source_header
end

#say_header_mapObject



49
50
51
52
53
# File 'lib/lhj/command/head_import.rb', line 49

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

#update_source_headerObject



79
80
81
82
83
84
85
86
87
# File 'lib/lhj/command/head_import.rb', line 79

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