From bee7e3934f154ec959156edc5e106d600583b50d Mon Sep 17 00:00:00 2001 From: "cpaper.xie" Date: Tue, 8 Oct 2019 01:47:36 +0800 Subject: [PATCH 1/2] feature: output in JSON format --- .../command/dependency.rb | 9 ++++++++- lib/cocoapods-dependency/visual_out.rb | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/cocoapods-dependency/command/dependency.rb b/lib/cocoapods-dependency/command/dependency.rb index fdb7b3e..e2a3327 100644 --- a/lib/cocoapods-dependency/command/dependency.rb +++ b/lib/cocoapods-dependency/command/dependency.rb @@ -2,6 +2,7 @@ require 'pp' require 'cocoapods-dependency/visual_out' require 'tmpdir' +require 'json' module Pod class Command @@ -15,12 +16,14 @@ class Dependency < Command def initialize(argv) @using_visual_output = argv.flag?('visual', false) + @to_output_json = argv.flag?('json', false) super end def self.options [ ['--visual', 'Output the result using html'], + ['--json', 'Output in JSON format'] ].concat(super) end @@ -41,7 +44,11 @@ def run puts "[CocoapodsDependency] ✅ html file generated at path #{final_html_path}" system "open #{final_html_path}" else - pp result + if @to_output_json + puts JSON.pretty_generate(analyze_result) + else + pp analyze_result + end end end end diff --git a/lib/cocoapods-dependency/visual_out.rb b/lib/cocoapods-dependency/visual_out.rb index 50829c4..4a9c4fe 100644 --- a/lib/cocoapods-dependency/visual_out.rb +++ b/lib/cocoapods-dependency/visual_out.rb @@ -22,11 +22,20 @@ def to_d3js_json end json['links'] = links - JSON.pretty_generate(json) end def write_json_to_file(path) + json_result = JSON.pretty_generate(dependency_hash) + File.write(path, json_result) + end + + def write_d3js_to_file(path) + json = 'var dependencies = ' + to_d3js_json + File.write(path, json) + end + + def dependency_hash links = [] json = {} @dependency_map.each do |node, v| @@ -38,13 +47,7 @@ def write_json_to_file(path) ) end json['links'] = links - json_result = JSON.pretty_generate(json) - File.write(path, json_result) - end - - def write_d3js_to_file(path) - json = 'var dependencies = ' + to_d3js_json - File.write(path, json) + return json end end end From dcce772a14ab66ac67daa9165f034fa22efc5d89 Mon Sep 17 00:00:00 2001 From: "cpaper.xie" Date: Tue, 8 Oct 2019 23:48:50 +0800 Subject: [PATCH 2/2] fix #11: use erb template to generate HTML to avoid CORS --- .../command/dependency.rb | 39 +++++++++++++++---- .../resources/{index.html => index.html.erb} | 34 ++++++++-------- 2 files changed, 50 insertions(+), 23 deletions(-) rename lib/cocoapods-dependency/resources/{index.html => index.html.erb} (73%) diff --git a/lib/cocoapods-dependency/command/dependency.rb b/lib/cocoapods-dependency/command/dependency.rb index e2a3327..7562170 100644 --- a/lib/cocoapods-dependency/command/dependency.rb +++ b/lib/cocoapods-dependency/command/dependency.rb @@ -3,6 +3,7 @@ require 'cocoapods-dependency/visual_out' require 'tmpdir' require 'json' +require 'erb' module Pod class Command @@ -36,13 +37,9 @@ def run analyze_result = CocoapodsDependency::Analyzer.analyze_with_podfile(nil, config.podfile) if @using_visual_output helper = CocoapodsDependency::VisualOutHelper.new(analyze_result) - final_path = Dir.tmpdir - helper.write_json_to_file("#{final_path}/index.json") - html_path = File.expand_path("../resources/index.html", __dir__) - system "cp #{html_path} #{final_path}" - final_html_path = "#{final_path}/index.html" - puts "[CocoapodsDependency] ✅ html file generated at path #{final_html_path}" - system "open #{final_html_path}" + path = final_path + render_template(JSON.generate(helper.dependency_hash), path) + system "open #{path}" else if @to_output_json puts JSON.pretty_generate(analyze_result) @@ -51,6 +48,34 @@ def run end end end + + private + + def final_path + d = File.join(Dir.tmpdir, 'pod') + if not Dir.exist?(d) + Dir.mkdir(d) + end + "#{Dir.tmpdir}/pod/index.html" + end + + def render_template(result, final_path) + File.open(final_path, 'w') do |f| + f.puts(template_content(result)) + end + end + + def template_content(result) + f = File.open(template_path) + template = ERB.new(f.read) + f.close + return template.result(binding) + end + + def template_path + File.expand_path("../resources/index.html.erb", __dir__) + end + end end end diff --git a/lib/cocoapods-dependency/resources/index.html b/lib/cocoapods-dependency/resources/index.html.erb similarity index 73% rename from lib/cocoapods-dependency/resources/index.html rename to lib/cocoapods-dependency/resources/index.html.erb index 25a95d9..cb6eefc 100644 --- a/lib/cocoapods-dependency/resources/index.html +++ b/lib/cocoapods-dependency/resources/index.html.erb @@ -15,7 +15,6 @@ } #nav { - padding: 10px 30px; float: left; width: 30%; height: 100%; @@ -23,20 +22,16 @@ } #content { - padding: 10px 30px; float: left; width: 60%; height: 100%; overflow: scroll; - } + } - - -