1. Gem::
  2. SpecificationPolicy

class Gem::SpecificationPolicy

Constants

HOMEPAGE_URI_PATTERN
LAZY
LAZY_PATTERN

Attributes

packaging[RW]

If set to true, run packaging-specific checks, as well.

Public Class Methods

Source
# File lib/rubygems/specification_policy.rb, line 26definitialize(specification)@warnings =0@specification =specificationend

Public Instance Methods

Source
# File lib/rubygems/specification_policy.rb, line 46defvalidate(strict =false)validate_required!validate_required_metadata!validate_optional(strict)ifpackaging||stricttrueend

Does a sanity check on the specification.

Raises InvalidSpecificationException if the spec does not pass the checks.

It also performs some validations that do not raise but print warning messages instead.

Source
# File lib/rubygems/specification_policy.rb, line 129defvalidate_for_resolutionvalidate_required!end

Implementation for Specification#validate_for_resolution

Source
# File lib/rubygems/specification_policy.rb, line 136defvalidate_metadatametadata =@specification.metadataunlessHash===metadataerror"metadata must be a hash"endmetadata.eachdo|key,value|entry ="metadata['#{key}']"unlesskey.is_a?(String)error"metadata keys must be a String"endifkey.size>128error"metadata key is too large (#{key.size} > 128)"endunlessvalue.is_a?(String)error"#{entry} value must be a String"endifvalue.size>1024error"#{entry} value is too large (#{value.size} > 1024)"endnextunlessMETADATA_LINK_KEYS.include?keyunlessVALID_URI_PATTERN.match?(value)error"#{entry} has invalid link: #{value.inspect}"endendend

Implementation for Specification#validate_metadata

Source
# File lib/rubygems/specification_policy.rb, line 100defvalidate_optional(strict)validate_licensesvalidate_permissionsvalidate_valuesvalidate_dependenciesvalidate_required_ruby_versionvalidate_extensionsvalidate_removed_attributesvalidate_unique_linksif@warnings>0ifstricterror"specification has warnings"elsealert_warninghelp_textendendend
Source
# File lib/rubygems/specification_policy.rb, line 217defvalidate_permissionsreturnifGem.win_platform?@specification.files.eachdo|file|nextunlessFile.file?(file)nextifFile.stat(file).mode&0o444==0o444warning"#{file} is not world-readable"end@specification.executables.eachdo|name|exec =File.join@specification.bindir,namenextunlessFile.file?(exec)nextifFile.stat(exec).executable?warning"#{exec} is not executable"endend

Issues a warning for each file to be packaged which is world-readable.

Implementation for Specification#validate_permissions

Source
# File lib/rubygems/specification_policy.rb, line 64defvalidate_required!validate_nil_attributesvalidate_rubygems_versionvalidate_required_attributesvalidate_namevalidate_require_paths@specification.keep_only_files_and_directoriesvalidate_non_filesvalidate_self_inclusion_in_files_listvalidate_specification_versionvalidate_platformvalidate_array_attributesvalidate_authors_fieldvalidate_licenses_lengthvalidate_duplicate_dependenciesend

Does a sanity check on the specification.

Raises InvalidSpecificationException if the spec does not pass the checks.

Only runs checks that are considered necessary for the specification to be functional.

Source
# File lib/rubygems/specification_policy.rb, line 94defvalidate_required_metadata!validate_metadatavalidate_lazy_metadataend
Source
# File lib/rubygems/specification_policy.rb, line 206defvalidate_required_ruby_versionif@specification.required_ruby_version.requirements== [Gem::Requirement::DefaultRequirement]warning"make sure you specify the oldest ruby version constraint (like \">= 3.0\") that you want your gem to support by setting the `required_ruby_version` gemspec attribute"endend

Private Instance Methods

Source
# File lib/rubygems/specification_policy.rb, line 324defvalidate_array_attribute(field)val =@specification.send(field)klass =casefieldwhen:dependenciesthenGem::DependencyelseStringendunlessArray===val&&val.all? {|x|x.is_a?(klass)|| (field==:licenses&&x.nil?) }error"#{field} must be an Array of #{klass}"endend
Source
# File lib/rubygems/specification_policy.rb, line 318defvalidate_array_attributesGem::Specification.array_attributes.eachdo|field|validate_array_attribute(field)endend
Source
# File lib/rubygems/specification_policy.rb, line 447defvalidate_attribute_present(attribute)value =@specification.sendattributewarning("no #{attribute} specified")ifvalue.nil?||value.empty?end
Source
# File lib/rubygems/specification_policy.rb, line 338defvalidate_authors_fieldreturnunless@specification.authors.empty?error"authors may not be empty"end
Source
# File lib/rubygems/specification_policy.rb, line 393defvalidate_lazy_metadataunless@specification.authors.grep(LAZY_PATTERN).empty?error"#{LAZY} is not an author"endunlessArray(@specification.email).grep(LAZY_PATTERN).empty?error"#{LAZY} is not an email"endifLAZY_PATTERN.match?(@specification.description)error"#{LAZY} is not a description"endifLAZY_PATTERN.match?(@specification.summary)error"#{LAZY} is not a summary"endhomepage =@specification.homepage# Make sure a homepage is valid HTTP/HTTPS URIifhomepage&&!homepage.empty?require_relative"vendor/uri/lib/uri"beginhomepage_uri =Gem::URI.parse(homepage)unless [Gem::URI::HTTP,Gem::URI::HTTPS].member?homepage_uri.classerror"\"#{homepage}\" is not a valid HTTP URI"endrescueGem::URI::InvalidURIErrorerror"\"#{homepage}\" is not a valid HTTP URI"endendend
Source
# File lib/rubygems/specification_policy.rb, line 356defvalidate_licenseslicenses =@specification.licenseslicenses.eachdo|license|nextifGem::Licenses.match?(license)||license.nil?license_id_deprecated =Gem::Licenses.deprecated_license_id?(license)exception_id_deprecated =Gem::Licenses.deprecated_exception_id?(license)suggestions =Gem::Licenses.suggestions(license)iflicense_id_deprecatedmain_message ="License identifier '#{license}' is deprecated"elsifexception_id_deprecatedmain_message ="Exception identifier at '#{license}' is deprecated"elsemain_message ="License identifier '#{license}' is invalid"endmessage =<<-WARNING#{main_message}. Use an identifier fromhttps://spdx.org/licenses or '#{Gem::Licenses::NONSTANDARD}' for a nonstandard license,or set it to nil if you don't want to specify a license.      WARNINGmessage+="Did you mean #{suggestions.map {|s| "'#{s}'" }.join(", ")}?\n"unlesssuggestions.nil?warning(message)endwarning<<-WARNINGiflicenses.empty?licenses is empty, but is recommended. Use an license identifier fromhttps://spdx.org/licenses or '#{Gem::Licenses::NONSTANDARD}' for a nonstandard license,or set it to nil if you don't want to specify a license.    WARNINGend
Source
# File lib/rubygems/specification_policy.rb, line 344defvalidate_licenses_lengthlicenses =@specification.licenseslicenses.eachdo|license|nextiflicense.nil?iflicense.length>64error"each license must be 64 characters or less"endendend
Source
# File lib/rubygems/specification_policy.rb, line 264defvalidate_namename =@specification.nameif!name.is_a?(String)error"invalid value for attribute name: \"#{name.inspect}\" must be a string"elsif!/[a-zA-Z]/.match?(name)error"invalid value for attribute name: #{name.dump} must include at least one letter"elsif!VALID_NAME_PATTERN.match?(name)error"invalid value for attribute name: #{name.dump} can only include letters, numbers, dashes, and underscores"elsifSPECIAL_CHARACTERS.match?(name)error"invalid value for attribute name: #{name.dump} cannot begin with a period, dash, or underscore"endend
Source
# File lib/rubygems/specification_policy.rb, line 236defvalidate_nil_attributesnil_attributes =Gem::Specification.non_nil_attributes.selectdo|attrname|@specification.instance_variable_get("@#{attrname}").nil?endreturnifnil_attributes.empty?error"#{nil_attributes.join ", "} must not be nil"end
Source
# File lib/rubygems/specification_policy.rb, line 284defvalidate_non_filesreturnunlesspackagingnon_files =@specification.files.reject {|x|File.file?(x)||File.symlink?(x) }unlessnon_files.empty?error"[\"#{non_files.join "\", \""}\"] are not files"endend
Source
# File lib/rubygems/specification_policy.rb, line 308defvalidate_platformplatform =@specification.platformcaseplatformwhenGem::Platform,Gem::Platform::RUBY# okelseerror"invalid platform #{platform.inspect}, see Gem::Platform"endend
Source
# File lib/rubygems/specification_policy.rb, line 278defvalidate_require_pathsreturnunless@specification.raw_require_paths.empty?error"specification must have at least one require_path"end
Source
# File lib/rubygems/specification_policy.rb, line 256defvalidate_required_attributesGem::Specification.required_attributes.eachdo|symbol|unless@specification.sendsymbolerror"missing value for attribute #{symbol}"endendend
Source
# File lib/rubygems/specification_policy.rb, line 244defvalidate_rubygems_versionreturnunlesspackagingrubygems_version =@specification.rubygems_versionreturnifrubygems_version==Gem::VERSIONwarning"expected RubyGems version #{Gem::VERSION}, was #{rubygems_version}"@specification.rubygems_version =Gem::VERSIONend
Source
# File lib/rubygems/specification_policy.rb, line 294defvalidate_self_inclusion_in_files_listfile_name =@specification.file_namereturnunless@specification.files.include?(file_name)error"#{@specification.full_name} contains itself (#{file_name}), check your files list"end
Source
# File lib/rubygems/specification_policy.rb, line 452defvalidate_shebang_line_in(executable)executable_path =File.join(@specification.bindir,executable)returnifFile.read(executable_path,2)=="#!"warning"#{executable_path} is missing #! line"end
Source
# File lib/rubygems/specification_policy.rb, line 302defvalidate_specification_versionreturnif@specification.specification_version.is_a?(Integer)error"specification_version must be an Integer (did you mean version?)"end
Source
# File lib/rubygems/specification_policy.rb, line 491defvalidate_unique_linkslinks =@specification.metadata.slice(*METADATA_LINK_KEYS)grouped =links.group_by {|_key,uri|uri }grouped.eachdo|uri,copies|nextunlesscopies.length>1keys =copies.map(&:first).join("\n  ")warning<<~WARNING        You have specified the uri:          #{uri}        for all of the following keys:          #{keys}        Only the first one will be shown on rubygems.org      WARNINGendend
Source
# File lib/rubygems/specification_policy.rb, line 426defvalidate_values%w[author homepage summary files].eachdo|attribute|validate_attribute_present(attribute)endif@specification.description==@specification.summarywarning"description and summary are identical"end# TODO: raise at some given datewarning"deprecated autorequire specified"if@specification.autorequire@specification.executables.eachdo|executable|validate_shebang_line_in(executable)end@specification.files.select {|f|File.symlink?(f) }.eachdo|file|warning"#{file} is a symlink, which is not supported on all platforms"endend