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

Added module_name attribute. #209

Merged
merged 7 commits into from
Dec 7, 2014
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

##### Enhancements

* Added `module_name` attribute for use with frameworks.
[Boris Bügling](https://github.com/neonichu)
[#205](https://github.com/CocoaPods/Core/issues/205)

* Evaluate a Specification in `.from_string` in the context of the directory
the specification is in.
[Samuel Giddins](https://github.com/segiddins)
Expand Down
28 changes: 28 additions & 0 deletions lib/cocoapods-core/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,34 @@ def self.root_name(full_name)
full_name.split('/').first
end

# Returns the module name of a specification
#
# @return [String] the module name
#
def module_name
attributes_hash['module_name'] ||
c99ext_identifier(attributes_hash['header_dir']) ||
Copy link
Member

Choose a reason for hiding this comment

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

could this possibly also need to take header_mapping_dir into account?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so, as it doesn't change names, but just ensures that a certain structure is preserved. Also using it will most probably break for frameworks as a header directory structure is not possible inside a framework.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this is a good thing to consider. Is it really not allowed or simply not the common way?

Copy link
Member Author

Choose a reason for hiding this comment

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

It cannot be created with Xcode, unsure what happens if one creates some subdirectories in a framework after the fact.

Copy link
Member

Choose a reason for hiding this comment

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

I think it should be tested, because there will be libraries that currently require this to work.

c99ext_identifier(attributes_hash['name'])
end

private

# Transforms the given string into a valid +identifier+ after C99ext
# standard, so that it can be used in source code where escaping of
# ambiguous characters is not applicable.
#
# @param [String] name
# any name, which may contain leading numbers, spaces or invalid
# characters.
#
# @return [String]
#
def c99ext_identifier(name)
return nil if name.nil?
I18n.transliterate(name).gsub(/^([0-9])/, '_\1').
gsub(/[^a-zA-Z0-9_]/, '_').gsub(/_+/, '_')
end

#-------------------------------------------------------------------------#

public
Expand Down
19 changes: 19 additions & 0 deletions lib/cocoapods-core/specification/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,25 @@ def dependency(*args)

#------------------#

# @!method module_name=(name)
#
# The name to use for the framework / clang module which
# will be generated for this specification instead of the
# default (header_dir if set, otherwise the specification
# name).
#
# @example
#
# spec.module_name = 'Three20'
Copy link
Member

Choose a reason for hiding this comment

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

lolol

Copy link
Member

Choose a reason for hiding this comment

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

Awesome :D

#
# @param [String] name
# the module name.
#
root_attribute :module_name,
:inherited => true

#------------------#

# @!method header_dir=(dir)
#
# The directory where to store the headers files so they don't break
Expand Down
8 changes: 8 additions & 0 deletions lib/cocoapods-core/specification/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ def _validate_version(v)
end
end

# Performs validations related to the `module_name` attribute.
def _validate_module_name(m)
unless m.nil? || m =~ /^[a-z_][0-9a-z_]+$/i
results.add_error('module_name', 'The module name of a spec' \
' should be a valid C99 identifier.')
end
end

# Performs validations related to the `summary` attribute.
#
def _validate_summary(s)
Expand Down
5 changes: 5 additions & 0 deletions spec/specification/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ module Pod
@spec.attributes_hash['header_mappings_dir'].should == 'src/include'
end

it 'allows to specify a custom module name' do
@spec.module_name = 'Three20'
@spec.attributes_hash['module_name'].should == 'Three20'
end

end

#-----------------------------------------------------------------------------#
Expand Down
7 changes: 7 additions & 0 deletions spec/specification/linter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ def result_should_include(*values)

#------------------#

it 'fails a specification whose module name is not a valid C99 identifier' do
@spec.stubs(:module_name).returns('20Three lol')
result_should_include('module_name', 'C99 identifier')
end

#------------------#

it 'checks that the version has been specified' do
@spec.stubs(:version).returns(Pod::Version.new(nil))
result_should_include('version', 'required')
Expand Down
54 changes: 54 additions & 0 deletions spec/specification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,5 +497,59 @@ module Pod

#-------------------------------------------------------------------------#

describe 'module name' do
it 'uses the specification name as module name by default' do
spec = Specification.new(nil, 'Three20')
spec.module_name.should == 'Three20'
end

it 'converts the name to a C99 identifier if required' do
spec = Specification.new(nil, '500px')
spec.module_name.should == '_500px'
end

it 'uses the header_dir as module name if specified' do
spec = Specification.new(nil, 'Three20.swift')
spec.header_dir = 'Three20'
spec.module_name.should == 'Three20'
end

it 'converts the header_dir to a C99 identifier if required' do
spec = Specification.new(nil, 'Three20.swift')
spec.header_dir = 'Three-20'
spec.module_name.should == 'Three_20'
end

it 'uses the defined module name if specified' do
spec = Specification.new(nil, 'Three20.swift')
spec.header_dir = 'Three20Core'
spec.module_name = 'Three20'
spec.module_name.should == 'Three20'
end
end

#-------------------------------------------------------------------------#

describe '#c99ext_identifier' do
before do
@spec = Specification.new
end

it 'should mask, but keep leading numbers' do
@spec.send(:c99ext_identifier, '123BananaLib').should == '_123BananaLib'
end

it 'should mask invalid chars' do
@spec.send(:c99ext_identifier, 'iOS-App BânánàLïb').should == 'iOS_App_BananaLib'
end
Copy link
Member

Choose a reason for hiding this comment

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

What about normalising instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea!


it 'should flatten multiple underscores to a single one' do
@spec.send(:c99ext_identifier, '$.swift').should == '_swift'
end

end

#-------------------------------------------------------------------------#

end
end