Class: Pod::VemarsProject

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-vemars/command/project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_key, components, config_json, version = nil, bundle_id = nil, name = '', language = 'objc', git_url, service_url, sources) ⇒ VemarsProject

Returns a new instance of VemarsProject.



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

def initialize(app_key, components, config_json, version=nil, bundle_id=nil, name='', language='objc', git_url, service_url,sources)
  @language = language
  @basicInfo = BasicInfo.new(app_key, bundle_id, name, version)
  @git_url = git_url
  config_json.strip!
  @config_json = config_json
  @selected_components = components | (language == 'swift' ? %w[OneKit SwiftOneKit] : ['OneKit'])
  @components_details = []
  @service_url = service_url
  @sources = sources
  @log_api = Log_api.new()
end

Instance Attribute Details

#basicInfoObject (readonly)

Returns the value of attribute basicInfo.



15
16
17
# File 'lib/cocoapods-vemars/command/project.rb', line 15

def basicInfo
  @basicInfo
end

#components_detailsObject

Returns the value of attribute components_details.



20
21
22
# File 'lib/cocoapods-vemars/command/project.rb', line 20

def components_details
  @components_details
end

#demo_tagObject

Returns the value of attribute demo_tag.



22
23
24
# File 'lib/cocoapods-vemars/command/project.rb', line 22

def demo_tag
  @demo_tag
end

#git_urlObject (readonly)

Returns the value of attribute git_url.



17
18
19
# File 'lib/cocoapods-vemars/command/project.rb', line 17

def git_url
  @git_url
end

#languageObject (readonly)

Returns the value of attribute language.



14
15
16
# File 'lib/cocoapods-vemars/command/project.rb', line 14

def language
  @language
end

#selected_componentsObject (readonly)

Returns the value of attribute selected_components.



16
17
18
# File 'lib/cocoapods-vemars/command/project.rb', line 16

def selected_components
  @selected_components
end

#service_urlObject (readonly)

Returns the value of attribute service_url.



18
19
20
# File 'lib/cocoapods-vemars/command/project.rb', line 18

def service_url
  @service_url
end

#sourcesObject

Returns the value of attribute sources.



21
22
23
# File 'lib/cocoapods-vemars/command/project.rb', line 21

def sources
  @sources
end

Instance Method Details

#clone_template_projectObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cocoapods-vemars/command/project.rb', line 81

def clone_template_project
  # puts 'cloning template'
  if File.directory?(basicInfo.name) || File.file?(basicInfo.name)
    system("rm -rf #{basicInfo.name}")
  end
  if git_url.end_with?(".git")
    cmd = ""
    if @demo_tag.nil?
      cmd = "git clone #{git_url} #{basicInfo.name}"
    else
      cmd = "git clone #{git_url} #{basicInfo.name} -b #{@demo_tag} --config advice.detachedHead=false"
    end
    system(cmd)
  else
    destination = basicInfo.name
    FileUtils.mkdir_p(destination)
    content = URI.open(git_url)

    Zip::File.open_buffer(content) do |zip|
      zip.each do |f|
        fname = f.name
        if fname.start_with?("ve_Template_iOS")
            paths = fname.split("/")
            fname = fname.sub(paths[0],"")
        end
        fpath = File.join(destination, fname)
        zip.extract(f, fpath) unless File.exist?(fpath)
      end
    end

  end
end

#collect_componentsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cocoapods-vemars/command/project.rb', line 128

def collect_components
  components_api = Components_api.new(@basicInfo.version,@service_url)
  components_api.validate!
  all_components = components_api.getComponents
  @selected_components.each do |com|
    existed_com = all_components.find { |acom|
      acom.name == com
    }
    @components_details.append existed_com unless existed_com.nil?
  end
  
  demo_com = all_components.find { |acom|
    acom.name == "ve_Template_iOS"
  }

  if !demo_com.nil?
    @demo_tag = demo_com.version
  end

end

#construct_plistObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cocoapods-vemars/command/project.rb', line 156

def construct_plist
  content = File.read(@config_json)
  json = JSON.parse(content)
  json["project_info"]["app_id"] = json["project_info"]["app_id"].to_s
  @app_id = json["project_info"]["app_id"]
  services = json["services"]

  if @selected_components.include?("BDHotfix") 
    hotfix = {"host" => "https://plugin-patch-api.bytedance.com"}
    if services.include?("hotfix")
      hotfix = services["hotfix"]
    end
    hotfix["dist_area"] = "cn"
    hotfix["debug"] = true
    services["hotfix"] = hotfix
  end
  
  if @selected_components.include?("VEH5Kit")
    h5 = {"gecko_host" => "https://plugin-patch-api.bytedance.com","js_host" => "https://plugin-patch-api.bytedance.com"}
    if services.include?("h5")
      h5 = services["h5"]
    end
    h5["auth_enable"] = false
    h5["pattern"] =  ""
    h5["gecko_channels"] = []
    h5["gecko_access_key"] = ""
    services["h5"] = h5
  end

  json["services"] = services
  File.open(Dir.pwd + "/onekit-config.plist", 'w') { |file|
    file.puts json.to_plist
  }
end

#construct_podfileObject



149
150
151
152
153
# File 'lib/cocoapods-vemars/command/project.rb', line 149

def construct_podfile
  template = PodfileTemplate.new(@basicInfo.version, @components_details, @sources)

    File.open('Podfile', "w") { |file| file.puts template.to_dsl }
end

#generateObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cocoapods-vemars/command/project.rb', line 41

def generate
  puts "generate project..."
  collect_components
  clone_template_project
  Dir.chdir(basicInfo.name) do
    language_switch
    tailor_demos
    Dir.chdir("Project") do
      construct_podfile
      if !@config_json.nil?
        Dir.chdir("Template_InHouse") do
          construct_plist
        end
      end
      rename_project_files
      # replace_internal_project_settings
    end
    reinitialize_git_repo
  end
  @log_api.record_event("fwk_new_project",{"app_id" => @app_id,"type" => "新项目"})
  @log_api.record_event("fwk_pv",{"app_id" => @app_id})
  @log_api.record_sdks(@selected_components,@app_id)
end

#language_switchObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cocoapods-vemars/command/project.rb', line 114

def language_switch
  if language == 'swift'
    puts "creating swift project..."
    system("rm -fr objc")
    system("mv swift/* ./")
    system("rm -fr ./swift")
  else
    puts "creating objc project..."
    system("rm -fr ./swift")
    system("mv objc/* ./")
    system("rm -fr objc")
  end
end

#patch(podfile_dir) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cocoapods-vemars/command/project.rb', line 65

def patch(podfile_dir)
  puts "patching project..."
  collect_components
  podfile_dir.strip!
  podfile_dir.delete_suffix!("Podfile")
  Dir.chdir(podfile_dir) do
    construct_plist
    patcher = Patcher.new(podfile_dir, @basicInfo.version, @components_details, @sources, @git_url, @demo_tag)
    patcher.execute
  end
  @log_api.record_event("fwk_new_project",{"app_id" => @app_id,"type" => "存量项目"})
  @log_api.record_event("fwk_pv",{"app_id" => @app_id})
  @log_api.record_sdks(@selected_components,@app_id)

end

#project_folderObject



24
25
26
# File 'lib/cocoapods-vemars/command/project.rb', line 24

def project_folder
  File.join(Dir.pwd, "Project")
end

#reinitialize_git_repoObject



213
214
215
216
# File 'lib/cocoapods-vemars/command/project.rb', line 213

def reinitialize_git_repo
  `rm -rf .git/`
  `git init`
end

#rename_project_filesObject



191
192
193
194
# File 'lib/cocoapods-vemars/command/project.rb', line 191

def rename_project_files
    renamer = ProjectRenamer.new("Template", basicInfo.name)
    renamer.execute
end

#replace_internal_project_settingsObject



201
202
203
204
205
206
207
208
209
210
# File 'lib/cocoapods-vemars/command/project.rb', line 201

def replace_internal_project_settings
  Dir.glob(Dir.pwd + "/**/**/**/**").each do |name|
    next if Dir.exists? name
    text = File.read(name)
    basicInfo.string_replacements.each { |find, replace|
      text = text.gsub(find, replace)
    }
    File.open(name, "w") { |file| file.puts text }
  end
end

#tailor_demosObject



196
197
198
199
# File 'lib/cocoapods-vemars/command/project.rb', line 196

def tailor_demos
  tailor = DemoTailor.new(@components_details)
  tailor.execute
end