require 'cocoapods' require 'pathname' require 'pp' require 'tmpdir' require 'json' require 'rainbow' require 'yaml' require 'git' require 'yk_command/analyze/dependency_result' module YkCommand IGNORE_PROJECT_FILE = '.YKAnalyzeConfig.yml'.freeze class Analyzer < Thor no_commands do def dependency(path = nil) say "\ncurrent working folder: #{path}\n" project_path = '' project_name = '' begin g = Git.open(path) project_path = Pathname.new(g.lib.git_work_dir) project_name = g.config['remote.origin.url'].split('/').last.split('.')[0] unless project_name.empty? podfiles = Dir.glob("#{path}/**/Podfile") if !podfiles.empty? begin project_path = podfiles[podfiles.length - 1] podfile_dir = Pathname.new(project_path).dirname.to_s say "start analyze... path contain [Podfile ,Pods/ ,Podfile.lock] is #{podfile_dir} \n ", :green result = analyze(podfile_dir) app_info = Request.new.get_project_info(project_name) if app_info['id'] say "uploading dependencies for project #{project_name} ..." upload_res = Request.new.upload_app_dependency(app_info['id'], result) if upload_res['code'] == 0 result(0, project_name, 'uploaded , finished ', :green) else result(1, project_name, "Error :upload dependencies for project #{project_name} - #{app_info["id"]} failed ", :red) end else result(1, project_name, "Error :Get App project ID failed , #{project_name}", :red) end rescue say "Error : #{$!}", :red say "Error : #{ $@}", :red result(1, project_name, "Skip folder : #{podfile_dir} ", :red) end else result(2, project_name, "No podfile find ,Skip folder : #{project_name} ", :yellow) end end rescue result(2, '', "No .git folder find in #{path} ,skip folder ... ", :yellow) end end def result(code, name, message, color) say message, color say "\n<--\n", color { 'code' => "#{code}", 'name' => "#{name}" } end def workspace_analyze(path = nil) Dir.chdir(path) subdir_list = Dir['*'].reject { |o| not File.directory?(o) } say "Start in Workspace : #{path} ...", :yellow subdir_list.each do |project_dir| say "\n#{Pathname.new(project_dir).split[1]} ", :yellow end say "\n\n" say "\n ... start scanning to analyze ...\n\n" success_count = 0 success_name_list = [] failed_count = 0 failed_name_list = [] skip_count = 0 subdir_list.each { |project_dir| project_path = "#{path}/#{project_dir}" res = dependency project_path if res['code'] == '0' success_count += 1 success_name_list << res['name'] elsif res['code'] == '1' failed_count += 1 failed_name_list << res['name'] else skip_count += 1 end } say "\nworkspace analyze done ! ..." say "success:#{success_count},#{success_name_list}", :green say "failed :#{failed_count},#{failed_name_list}", :red say "skip :#{skip_count}", :yellow end def ignore_file(path = nil) ignore_file_path = "#{path}/#{IGNORE_PROJECT_FILE}" ignore = File.exist?(ignore_file_path) ? YAML.load_file(ignore_file_path) : {} ignore[:projects] = ["ShouYinTong", "ShouYinTongSaas"] File.open(ignore_file_path, 'w') do |f| f.write ignore.to_yaml end end def analyze(podfile_dir_path) @path = Pathname.new(podfile_dir_path) raise 'absolute path is needed' unless @path.absolute? Pod::Config.instance.installation_root = @path @podFile = Pod::Podfile.from_file(@path + 'Podfile') @podLock = Pod::Lockfile.from_file(@path + 'Podfile.lock') Dir.chdir(podfile_dir_path) do analyze_with_podfile end end def analyze_with_podfile sandbox = Dir.pwd + '/Pods' # if @path # sandbox = Dir.mktmpdir # else # end analyzer = Pod::Installer::Analyzer.new( Pod::Sandbox.new(sandbox), @podFile, @podLock ) specifications = analyzer.analyze.specifications.map(&:root).uniq array = [] dependency_result = DependencyResult.new(specifications, @podLock) dependency_result.result_json end end end end