Skip to content

Commit 27b5132

Browse files
authored
Refactor packagers to expose safe_version without class instance (#237)
* Refactor RPM packager to expose static method for safe_version * Refactor DEB packager to expose static method for safe_version
1 parent 49bffd5 commit 27b5132

2 files changed

Lines changed: 100 additions & 77 deletions

File tree

lib/omnibus/packagers/deb.rb

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -599,37 +599,7 @@ def safe_epoch
599599
# @return [String]
600600
#
601601
def safe_version
602-
version = project.build_version.dup
603-
604-
if version =~ /\-/
605-
converted = version.tr("-", "~")
606-
607-
log.warn(log_key) do
608-
"Dashes hold special significance in the Debian package versions. " \
609-
"Versions that contain a dash and should be considered an earlier " \
610-
"version (e.g. pre-releases) may actually be ordered as later " \
611-
"(e.g. 12.0.0-rc.6 > 12.0.0). We'll work around this by replacing " \
612-
"dashes (-) with tildes (~). Converting `#{project.build_version}' " \
613-
"to `#{converted}'."
614-
end
615-
616-
version = converted
617-
end
618-
619-
if version =~ /\A[a-zA-Z0-9\.\+\:\~]+\z/
620-
version
621-
else
622-
converted = version.gsub(/[^a-zA-Z0-9\.\+\:\~]+/, "_")
623-
624-
log.warn(log_key) do
625-
"The `version' component of Debian package names can only include " \
626-
"alphabetical characters (a-z, A-Z), numbers (0-9), dots (.), " \
627-
"plus signs (+), dashes (-), tildes (~) and colons (:). Converting " \
628-
"`#{project.build_version}' to `#{converted}'."
629-
end
630-
631-
converted
632-
end
602+
self.class.safe_version(project.build_version)
633603
end
634604

635605
#
@@ -716,5 +686,45 @@ def compression_algo(val = nil)
716686
end
717687
end
718688
expose :compression_algo
689+
690+
#
691+
# Return the Debian-ready version, replacing all dashes (+-+) with tildes
692+
# (+~+) and converting any invalid characters to underscores (+_+).
693+
#
694+
# @return [String]
695+
#
696+
def self.safe_version(raw_version)
697+
version = raw_version.dup
698+
699+
if version =~ /\-/
700+
converted = version.tr("-", "~")
701+
702+
log.warn(log_key) do
703+
"Dashes hold special significance in the Debian package versions. " \
704+
"Versions that contain a dash and should be considered an earlier " \
705+
"version (e.g. pre-releases) may actually be ordered as later " \
706+
"(e.g. 12.0.0-rc.6 > 12.0.0). We'll work around this by replacing " \
707+
"dashes (-) with tildes (~). Converting `#{version}' " \
708+
"to `#{converted}'."
709+
end
710+
711+
version = converted
712+
end
713+
714+
if version =~ /\A[a-zA-Z0-9\.\+\:\~]+\z/
715+
version
716+
else
717+
converted = version.gsub(/[^a-zA-Z0-9\.\+\:\~]+/, "_")
718+
719+
log.warn(log_key) do
720+
"The `version' component of Debian package names can only include " \
721+
"alphabetical characters (a-z, A-Z), numbers (0-9), dots (.), " \
722+
"plus signs (+), dashes (-), tildes (~) and colons (:). Converting " \
723+
"`#{version}' to `#{converted}'."
724+
end
725+
726+
converted
727+
end
728+
end
719729
end
720730
end

lib/omnibus/packagers/rpm.rb

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -707,52 +707,7 @@ def safe_epoch
707707
# @return [String]
708708
#
709709
def safe_version
710-
version = project.build_version.dup
711-
712-
# RPM 4.10+ added support for using the tilde (~) as a way to mark
713-
# versions as lower priority in comparisons. More details on this
714-
# feature can be found here:
715-
#
716-
# http://rpm.org/ticket/56
717-
#
718-
if version =~ /\-/
719-
if Ohai["platform_family"] == "wrlinux"
720-
converted = version.tr("-", "_") # WRL has an elderly RPM version
721-
log.warn(log_key) do
722-
"Omnibus replaces dashes (-) with tildes (~) so pre-release " \
723-
"versions get sorted earlier than final versions. However, the " \
724-
"version of rpmbuild on Wind River Linux does not support this. " \
725-
"All dashes will be replaced with underscores (_). Converting " \
726-
"`#{project.build_version}' to `#{converted}'."
727-
end
728-
else
729-
converted = version.tr("-", "~")
730-
log.warn(log_key) do
731-
"Tildes hold special significance in the RPM package versions. " \
732-
"They mark a version as lower priority in RPM's version compare " \
733-
"logic. We'll replace all dashes (-) with tildes (~) so pre-release" \
734-
"versions get sorted earlier then final versions. Converting" \
735-
"`#{project.build_version}' to `#{converted}'."
736-
end
737-
end
738-
739-
version = converted
740-
end
741-
742-
if version =~ /\A[a-zA-Z0-9\.\+\:\~]+\z/
743-
version
744-
else
745-
converted = version.gsub(/[^a-zA-Z0-9\.\+\:\~]+/, "_")
746-
747-
log.warn(log_key) do
748-
"The `version' component of RPM package names can only include " \
749-
"alphabetical characters (a-z, A-Z), numbers (0-9), dots (.), " \
750-
"plus signs (+), tildes (~), colons (:) and underscores (_). " \
751-
"Converting `#{project.build_version}' to `#{converted}'."
752-
end
753-
754-
converted
755-
end
710+
self.class.safe_version(project.build_version)
756711
end
757712

758713
#
@@ -883,5 +838,63 @@ def compression_algo(val = nil)
883838
end
884839
end
885840
expose :compression_algo
841+
842+
#
843+
# Return a safe version string out of the input version
844+
# RPM package versions cannot contain dashes, so we will convert them to
845+
# underscores.
846+
# @param [String] string
847+
# the string to sanitize
848+
#
849+
# @return [String]
850+
#
851+
def self.safe_version(raw_version)
852+
# RPM 4.10+ added support for using the tilde (~) as a way to mark
853+
# versions as lower priority in comparisons. More details on this
854+
# feature can be found here:
855+
#
856+
# http://rpm.org/ticket/56
857+
#
858+
version = raw_version.dup
859+
860+
if version =~ /\-/
861+
if Ohai["platform_family"] == "wrlinux"
862+
converted = version.tr("-", "_") # WRL has an elderly RPM version
863+
log.warn(log_key) do
864+
"Omnibus replaces dashes (-) with tildes (~) so pre-release " \
865+
"versions get sorted earlier than final versions. However, the " \
866+
"version of rpmbuild on Wind River Linux does not support this. " \
867+
"All dashes will be replaced with underscores (_). Converting " \
868+
"`#{version}' to `#{converted}'."
869+
end
870+
else
871+
converted = version.tr("-", "~")
872+
log.warn(log_key) do
873+
"Tildes hold special significance in the RPM package versions. " \
874+
"They mark a version as lower priority in RPM's version compare " \
875+
"logic. We'll replace all dashes (-) with tildes (~) so pre-release" \
876+
"versions get sorted earlier then final versions. Converting" \
877+
"`#{version}' to `#{converted}'."
878+
end
879+
end
880+
881+
version = converted
882+
end
883+
884+
if version =~ /\A[a-zA-Z0-9\.\+\:\~]+\z/
885+
version
886+
else
887+
converted = version.gsub(/[^a-zA-Z0-9\.\+\:\~]+/, "_")
888+
889+
log.warn(log_key) do
890+
"The `version' component of RPM package names can only include " \
891+
"alphabetical characters (a-z, A-Z), numbers (0-9), dots (.), " \
892+
"plus signs (+), tildes (~), colons (:) and underscores (_). " \
893+
"Converting `#{version}' to `#{converted}'."
894+
end
895+
896+
converted
897+
end
898+
end
886899
end
887900
end

0 commit comments

Comments
 (0)