Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixbug #11 and add a new option --json #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions lib/cocoapods-dependency/command/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'pp'
require 'cocoapods-dependency/visual_out'
require 'tmpdir'
require 'json'
require 'erb'

module Pod
class Command
Expand All @@ -15,12 +17,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

Expand All @@ -33,17 +37,45 @@ 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
pp result
if @to_output_json
Comment on lines 43 to +44
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这两个合并一下吧

puts JSON.pretty_generate(analyze_result)
else
pp analyze_result
end
end
end

private

def final_path
d = File.join(Dir.tmpdir, 'pod')
if not Dir.exist?(d)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use unless instead

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
Comment on lines +69 to +71
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接用 File.read 就不需要手动 open, close 了

return template.result(binding)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 可以省掉

end

def template_path
File.expand_path("../resources/index.html.erb", __dir__)
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@
}

#nav {
padding: 10px 30px;
float: left;
width: 30%;
height: 100%;
overflow: scroll;
}

#content {
padding: 10px 30px;
float: left;
width: 60%;
height: 100%;
overflow: scroll;
}
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

<body onload="pageDidLoad()">
<script>

function loadJSON(fileName, callback) {
function loadJSON(fileName, callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', fileName, true);
Expand All @@ -51,14 +46,16 @@
function pageDidLoad() {
loadJSON('index.json', response => {
var actual_JSON = JSON.parse(response);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

帮忙把这个文件里所有的 var 也一起改掉吧,当年年少不知事 =,=


let podNames = actual_JSON.links.map(s => s.source);

renderNavs(podNames)
renderPods(actual_JSON.links)
loadData(actual_JSON);
});
}

function loadData(actual_JSON) {
let podNames = actual_JSON.links.map(s => s.source);
renderNavs(podNames)
renderPods(actual_JSON.links)
}

function renderNavs(podNames) {
let fLeft = document.getElementById('nav')
podNames.forEach(e => {
Expand All @@ -76,9 +73,9 @@
let header = document.createElement('h3')
header.innerHTML = element.source
header.id = element.source

div.appendChild(header)

let ul = document.createElement('ul')
let dependencies = element.dependencies.sort()
dependencies.forEach(dependency => {
Expand All @@ -89,8 +86,8 @@
ul.appendChild(li)
li.appendChild(a)
})
div.appendChild(ul)

div.appendChild(ul)
d.appendChild(div)
})
}
Expand All @@ -105,6 +102,11 @@
<div id="content">
<div id="pods"></div>
</div>

<script type="text/javascript">
var actual_JSON = <%= result %>;
loadData(actual_JSON);
</script>
</body>

</html>
19 changes: 11 additions & 8 deletions lib/cocoapods-dependency/visual_out.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omit return

end
end
end