Merge pull request #4873 from rolandwalker/add_license_stanza

DSL: add `license` stanza
This commit is contained in:
Roland Walker 2014-06-28 10:32:22 -04:00
commit 2c06def3ba
8 changed files with 132 additions and 0 deletions

View File

@ -25,6 +25,7 @@ require 'cask/exceptions'
require 'cask/fetcher'
require 'cask/gpg'
require 'cask/installer'
require 'cask/license'
require 'cask/link_checker'
require 'cask/locations'
require 'cask/options'

View File

@ -16,6 +16,8 @@ module Cask::DSL
def version; self.class.version; end
def license; self.class.license; end
def depends_on_formula; self.class.depends_on_formula; end
def depends_on; self.class.depends_on; end
@ -89,6 +91,17 @@ module Cask::DSL
@version ||= arg
end
def license(arg=nil)
if @license and !arg.nil?
raise CaskInvalidError.new(self.title, "'license' stanza may only appear once")
end
@license ||= begin
Cask::License.new(arg) unless arg.nil?
rescue StandardError => e
raise CaskInvalidError.new(self.title, e)
end
end
def depends_on_formula(*args)
@depends_on_formula ||= args
end

67
lib/cask/license.rb Normal file
View File

@ -0,0 +1,67 @@
class Cask::License
# a generic category can always be given as a license, so
# category names should be given as both key and value
VALID_LICENSES = {
# license category
:unknown => :unknown,
:other => :other,
:closed => :closed,
:abandoned => :closed,
:commercial => :closed,
:free => :closed,
:trial => :closed,
:oss => :oss,
:affero => :oss,
:apache => :oss,
:arphic => :oss,
:artistic => :oss,
:bsd => :oss,
:cc => :oss,
:eclipse => :oss,
:gpl => :oss,
:isc => :oss,
:mit => :oss,
:mpl => :oss,
:ofl => :oss,
:public_domain => :oss,
:ubuntu => :oss,
:x11 => :oss,
}
DEFAULT_LICENSE = :unknown
DEFAULT_CATEGORY = VALID_LICENSES[DEFAULT_LICENSE]
attr_reader :value
def self.check_constants
categories = Set.new(VALID_LICENSES.values)
categories.each do |cat|
next if VALID_LICENSES.key?(cat)
raise "license category is not a value: '#{@cat.inspect}'"
end
end
def self.category(_license)
VALID_LICENSES.fetch(_license, DEFAULT_CATEGORY)
end
def initialize(arg)
@value = arg
@value = DEFAULT_LICENSE if @value.nil?
unless VALID_LICENSES.key?(@value)
raise "invalid license value: '#{@value.inspect}'"
end
end
def category
self.class.category(@value)
end
def to_s
@value.inspect
end
end

View File

@ -77,6 +77,7 @@ module Cask::Utils
:url,
:appcast,
:version,
:license,
:sums,
:artifacts,
:caveats,

View File

@ -197,4 +197,29 @@ describe Cask::DSL do
}.must_raise(CaskInvalidError)
end
end
describe "license stanza" do
it "allows the license to be specified" do
cask = Cask.load('with-license')
cask.license.value.must_equal :gpl
end
it "the license has a category" do
cask = Cask.load('with-license')
cask.license.category.must_equal :oss
end
it "prevents defining multiple license stanzas" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-license-multiple')
}.must_raise(CaskInvalidError)
err.message.must_include "'license' stanza may only appear once"
end
it "refuses to load on invalid license value" do
err = lambda {
invalid_cask = Cask.load('invalid/invalid-license-value')
}.must_raise(CaskInvalidError)
end
end
end

View File

@ -0,0 +1,9 @@
class InvalidLicenseMultiple < TestCask
url TestHelper.local_binary('transmission-2.61.dmg')
homepage 'http://example.com/invalid-license-multiple'
license :gpl
license :gpl
version '2.61'
sha256 'd26d7481cf1229f879c05e11cbdf440d99db6d6342f26c73d8ba7861b975532f'
link 'Transmission.app'
end

View File

@ -0,0 +1,8 @@
class InvalidLicenseValue < TestCask
url TestHelper.local_binary('transmission-2.61.dmg')
homepage 'http://example.com/invalid-license-value'
license :no_such_license
version '2.61'
sha256 'd26d7481cf1229f879c05e11cbdf440d99db6d6342f26c73d8ba7861b975532f'
link 'Transmission.app'
end

View File

@ -0,0 +1,8 @@
class WithLicense < TestCask
url TestHelper.local_binary('transmission-2.61.dmg')
homepage 'http://example.com/with-license'
license :gpl
version '2.61'
sha256 'd26d7481cf1229f879c05e11cbdf440d99db6d6342f26c73d8ba7861b975532f'
link 'Transmission.app'
end