Commit ae9e56a8 authored by Fabio Pelosin's avatar Fabio Pelosin

[Generator::Acknowledgements] Adapt for new architecture.

parent e5e2498c
......@@ -3,29 +3,99 @@ module Pod
class Acknowledgements
# @return [Array<Class>] The classes of the acknowledgements generator
# subclasses.
#
def self.generators
[Plist, Markdown]
end
def initialize(target_definition, pods)
@target_definition, @pods = target_definition, pods
# @return [Array<Sandbox::FileAccessor>] the list of the file accessors
# for the specs of the target that needs to generate the
# acknowledgements.
#
attr_reader :file_accessors
# @param [Array<Sandbox::FileAccessor>] @see file_accessors.
#
def initialize(file_accessors)
@file_accessors = file_accessors
end
#-----------------------------------------------------------------------#
# !@group Configuration
# @return [String] The title of the acknowledgements file.
#
def header_title
"Acknowledgements"
end
# @return [String] A text to present before listing the acknowledgements.
#
def header_text
"This application makes use of the following third party libraries:"
end
# @return [String] The title of the foot notes.
#
def footnote_title
""
end
# @return [String] the foot notes.
#
def footnote_text
"Generated by CocoaPods - http://cocoapods.org"
end
#-----------------------------------------------------------------------#
private
# !@group Private methods
# @return [Array<Specification>] The root specifications for which the
# acknowledgements should be generated.
#
def specs
file_accessors.map{ |accessor| accessor.spec.root }.uniq
end
# Returns the text of the license for the given spec.
#
# @param [Specification] spec
# the specification for which license is needed.
#
# @return [String] The text of the license.
# @return [Nil] If not license text could be found.
#
def license_text(spec)
if spec.license
if text = spec.license[:text]
text
elsif license_file = file_accessor(spec).license
text = IO.read(license_file)
end
end
end
protected
# Returns the file accessor for the given spec.
#
# @param [Specification] spec
# the specification for which the file accessor is needed.
#
# @return [Sandbox::FileAccessor] The file accessor.
#
def file_accessor(spec)
file_accessors.find { |accessor| accessor.spec.root == spec }
end
#-----------------------------------------------------------------------#
end
end
end
......@@ -19,16 +19,16 @@ module Pod
end
end
def string_for_pod(pod)
if (license_text = pod.license_text)
"\n" << title_from_string(pod.name, 2) << "\n\n" << license_text << "\n"
def string_for_spec(spec)
if (license_text = license_text(spec))
"\n" << title_from_string(spec.name, 2) << "\n\n" << license_text << "\n"
end
end
def licenses
licenses_string = "#{title_from_string(header_title, 1)}\n#{header_text}\n"
@pods.each do |pod|
if (license = string_for_pod(pod))
specs.each do |spec|
if (license = string_for_spec(spec))
license = license.force_encoding("UTF-8") if license.respond_to?(:force_encoding)
licenses_string += license
end
......
......@@ -26,19 +26,19 @@ module Pod
def licenses
licences_array = [header_hash]
@pods.each do |pod|
if (hash = hash_for_pod(pod))
specs.each do |spec|
if (hash = hash_for_spec(spec))
licences_array << hash
end
end
licences_array << footnote_hash
end
def hash_for_pod(pod)
if (license = pod.license_text)
def hash_for_spec(spec)
if (license = license_text(spec))
{
:Type => "PSGroupSpecifier",
:Title => pod.name,
:Title => spec.name,
:FooterText => license
}
end
......
......@@ -84,6 +84,14 @@ def fixture_spec(name)
Pod::Specification.from_file(file)
end
def fixture_file_accessor(name, platform = :ios)
file = SpecHelper::Fixture.fixture(name)
spec = Pod::Specification.from_file(file)
path_list = Pod::Sandbox::PathList.new(file.dirname)
file_accessor = Pod::Sandbox::FileAccessor.new(path_list, spec.consumer(platform))
end
# TODO This should not be needed anymore
def copy_fixture_to_pod(name, pod)
path = SpecHelper::Fixture.fixture(name)
FileUtils.cp_r(path, pod.root)
......
......@@ -2,40 +2,38 @@ require File.expand_path("../../../../spec_helper", __FILE__)
describe Pod::Generator::Markdown do
before do
@sandbox = temporary_sandbox
@target_definition = mock
@pods = [mock]
@pods[0].expects(:license_text).returns("LICENSE_TEXT").at_least_once
@pods[0].expects(:name).returns("POD_NAME").at_least_once
@markdown = Pod::Generator::Markdown.new(@target_definition, @pods)
@file_accessor = fixture_file_accessor('banana-lib/BananaLib.podspec')
@spec = @file_accessor.spec
@generator = Pod::Generator::Markdown.new([@file_accessor])
@spec.stubs(:name).returns("POD_NAME")
@generator.stubs(:license_text).returns("LICENSE_TEXT")
end
it "returns a correctly formatted title string" do
@pods[0].unstub(:license_text)
@pods[0].unstub(:name)
@markdown.title_from_string("A Title", 2).should.equal "## A Title"
@generator.title_from_string("A Title", 2).should.equal "## A Title"
end
it "returns a correctly formatted license string for each pod" do
@markdown.string_for_pod(@pods[0]).should.equal "\n## POD_NAME\n\nLICENSE_TEXT\n"
@generator.string_for_spec(@spec).should.equal "\n## POD_NAME\n\nLICENSE_TEXT\n"
end
it "returns a correctly formatted markdown string for the target" do
@markdown.stubs(:header_title).returns("HEADER_TITLE")
@markdown.stubs(:header_text).returns("HEADER_TEXT")
@markdown.stubs(:footnote_title).returns("") # Test that extra \n isn't added for empty strings
@markdown.stubs(:footnote_text).returns("FOOTNOTE_TEXT")
@markdown.licenses.should.equal "# HEADER_TITLE\nHEADER_TEXT\n\n## POD_NAME\n\nLICENSE_TEXT\nFOOTNOTE_TEXT\n"
@generator.stubs(:header_title).returns("HEADER_TITLE")
@generator.stubs(:header_text).returns("HEADER_TEXT")
@generator.stubs(:footnote_title).returns("") # Test that extra \n isn't added for empty strings
@generator.stubs(:footnote_text).returns("FOOTNOTE_TEXT")
@generator.licenses.should.equal "# HEADER_TITLE\nHEADER_TEXT\n\n## POD_NAME\n\nLICENSE_TEXT\nFOOTNOTE_TEXT\n"
end
it "writes a markdown file to disk" do
basepath = @sandbox.root + "Pods-acknowledgements"
given_path = @markdown.class.path_from_basepath(basepath)
expected_path = @sandbox.root + "Pods-acknowledgements.markdown"
basepath = config.sandbox.root + "Pods-acknowledgements"
given_path = @generator.class.path_from_basepath(basepath)
expected_path = config.sandbox.root + "Pods-acknowledgements.markdown"
mockFile = mock
mockFile.expects(:write).with(equals(@markdown.licenses))
mockFile.expects(:write).with(equals(@generator.licenses))
mockFile.expects(:close)
File.expects(:new).with(equals(expected_path), equals("w")).returns(mockFile)
@markdown.save_as(given_path)
@generator.save_as(given_path)
end
end
......@@ -2,12 +2,11 @@ require File.expand_path("../../../../spec_helper", __FILE__)
describe Pod::Generator::Plist do
before do
@sandbox = temporary_sandbox
@target_definition = mock
@pods = [mock]
@pods[0].expects(:license_text).returns("LICENSE_TEXT").at_least_once
@pods[0].expects(:name).returns("POD_NAME").at_least_once
@plist = Pod::Generator::Plist.new(@target_definition, @pods)
@file_accessor = fixture_file_accessor('banana-lib/BananaLib.podspec')
@spec = @file_accessor.spec
@plist = Pod::Generator::Plist.new([@file_accessor])
@spec.stubs(:name).returns("POD_NAME")
@plist.stubs(:license_text).returns("LICENSE_TEXT")
end
it "returns the correct number of licenses (including header and footnote)" do
......@@ -15,13 +14,11 @@ describe Pod::Generator::Plist do
end
it "returns a string for the plist title" do
@pods[0].unstub(:license_text)
@pods[0].unstub(:name)
@plist.plist_title.should.be.kind_of(String)
end
it "returns a correctly formed license hash for each pod" do
@plist.hash_for_pod(@pods[0]).should == {
@plist.hash_for_spec(@spec).should == {
:Type => "PSGroupSpecifier",
:Title => "POD_NAME",
:FooterText => "LICENSE_TEXT"
......@@ -29,10 +26,8 @@ describe Pod::Generator::Plist do
end
it "returns nil for a pod with no license text" do
@pods[0].unstub(:license_text)
@pods[0].unstub(:name)
@pods[0].expects(:license_text).returns(nil)
@plist.hash_for_pod(@pods[0]).should.be.nil
@plist.expects(:license_text).returns(nil)
@plist.hash_for_spec(@spec).should.be.nil
end
it "returns a plist containg the licenses" do
......@@ -44,9 +39,9 @@ describe Pod::Generator::Plist do
end
it "writes a plist to disk at the given path" do
basepath = @sandbox.root + "Pods-acknowledgements"
basepath = config.sandbox.root + "Pods-acknowledgements"
given_path = @plist.class.path_from_basepath(basepath)
expected_path = @sandbox.root + "Pods-acknowledgements.plist"
expected_path = config.sandbox.root + "Pods-acknowledgements.plist"
Xcodeproj.expects(:write_plist).with(equals(@plist.plist), equals(expected_path))
@plist.save_as(given_path)
end
......
require File.expand_path("../../../spec_helper", __FILE__)
describe Pod::Generator::Acknowledgements do
module Pod
describe Generator::Acknowledgements do
before do
@sandbox = temporary_sandbox
@target_definition = mock
@pods = [mock]
@acknowledgements = Pod::Generator::Acknowledgements.new(@target_definition, @pods)
@file_accessor = fixture_file_accessor('banana-lib/BananaLib.podspec')
@spec = @file_accessor.spec
@generator = Pod::Generator::Acknowledgements.new([@file_accessor])
end
it "the the generators" do
describe "In general" do
it "returns the classes of the concrete generators generators" do
generators = Pod::Generator::Acknowledgements.generators
generators.map { |g| g.name.split('::').last }.should == ['Plist', 'Markdown']
end
it "returns a string for each header and footnote text method" do
@acknowledgements.header_title.should.be.kind_of(String)
@acknowledgements.header_text.should.be.kind_of(String)
@acknowledgements.footnote_title.should.be.kind_of(String)
@acknowledgements.footnote_text.should.be.kind_of(String)
@generator.header_title.should.be.kind_of(String)
@generator.header_text.should.be.kind_of(String)
@generator.footnote_title.should.be.kind_of(String)
@generator.footnote_text.should.be.kind_of(String)
end
end
#-----------------------------------------------------------------------#
describe "Private methods" do
it "returns the root specifications" do
generator = Pod::Generator::Acknowledgements.new([@file_accessor, @file_accessor])
generator.send(:specs).should == [@file_accessor.spec]
end
it "returns the license" do
text_from_spec = @generator.send(:license_text, @spec)
text_from_spec.should == "Permission is hereby granted ..."
end
it "returns the license from the file" do
@spec.stubs(:license).returns({ :type => 'MIT', :file => 'README'})
text_from_spec = @generator.send(:license_text, @spec)
text_from_spec.should == "post v1.0\n"
end
end
#-----------------------------------------------------------------------#
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment