Class: Pod::Command::Bin::Service

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-lhj-bin/command/bin/local/micro_service.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) ⇒ Service

Returns a new instance of Service.


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

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @file_type = argv.option('file-type', 'm,h')
  @file_name = argv.option('file-name', 'service_map.csv')
  @service_map = {}
  super
end

Instance Method Details

#csv_file_nameObject


33
34
35
36
37
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 33

def csv_file_name
  file_name = @file_name
  file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
  file_name
end

#file_string(file) ⇒ Object


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

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

#format_string(file, line) ⇒ Object


68
69
70
71
72
73
74
75
76
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 68

def format_string(file, line)
  result = line
  if url_reg =~ line
    line.scan(url_reg).flatten.each do |key|
      result = result.gsub(key, @service_map[key]) if key && @service_map[key]
    end
  end
  result
end

#read_csvObject


24
25
26
27
28
29
30
31
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 24

def read_csv
  path = File.join(@current_path, csv_file_name)
  Dir.glob(path).each do |p|
    CSV.foreach(p) do |row|
      @service_map[row[0]] = row[1] if row[0]
    end
  end
end

#runObject


19
20
21
22
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 19

def run
  read_csv
  update_source
end

#update_file(file) ⇒ Object


49
50
51
52
53
54
55
56
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 49

def update_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

#update_sourceObject


39
40
41
42
43
44
45
46
47
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 39

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

#url_regObject


78
79
80
81
82
83
84
# File 'lib/cocoapods-lhj-bin/command/bin/local/micro_service.rb', line 78

def url_reg
  @url_key_reg ||= begin
                     keys = @service_map.keys.join('|')
                     /(#{keys})/
                   end
  @url_key_reg
end