lib/pbmenv.rb in pbmenv-0.1.10 vs lib/pbmenv.rb in pbmenv-0.1.11

- old
+ new

@@ -6,24 +6,46 @@ require_relative "pbmenv/version" require_relative "pbmenv/cli" require_relative "pbmenv/pbm" require_relative "pbmenv/helper" require_relative "pbmenv/version_pathname" -require_relative "pbmenv/create_version_service" -require_relative "pbmenv/destroy_version_service" -require_relative "pbmenv/use_version_service" -require_relative "pbmenv/download_src_service" +require_relative "pbmenv/version_object" +require_relative "pbmenv/directory_object" +require_relative "pbmenv/services/create_version_service" +require_relative "pbmenv/services/destroy_version_service" +require_relative "pbmenv/services/use_version_service" +require_relative "pbmenv/services/download_src_service" module Pbmenv PBM_DIR = "/usr/share/pbm" + # @return [Pbmenv::DirectoryObject] + def self.current_directory + Pbmenv::DirectoryObject.new(path: VersionPathname.current) + end + def self.available_versions Pbmenv::PBM.new.available_versions.map { |x| x["name"] =~ /^v([\d.]+)/ && $1 }.compact end + # @return [Array<Pbmenv::VersionObject>] + def self.installed_versions + unsorted_dirs = Dir.glob("#{Pbmenv::PBM_DIR}/v*") + sorted_version_names = unsorted_dirs.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) } + sorted_version_names.map do |version_name| + VersionObject.new( + version_name: version_name, + is_latest: sorted_version_names.last == version_name, + is_current: Pbmenv.current_directory.readlink&.end_with?(version_name) || false, + ) + end + end + + # @deprecated def self.versions - Pbmenv::PBM.new.versions.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) }.compact + unsorted_dirs = Dir.glob("#{Pbmenv::PBM_DIR}/v*") + unsorted_dirs.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) }.compact end def self.install(version, use_option: false, enable_pbm_cloud: false) raise "Need a version" if version.nil? version = @@ -40,11 +62,11 @@ rescue CreateVersionService::NotSupportVersionError return false end end - # TODO currentが挿しているバージョンはどうする? + # TODO: 引数がcurrentを指しているバージョンはどうする? def self.uninstall(version) raise "Need a version" if version.nil? version = Helper.normalize_version(version) or raise "mismatch version number!" begin @@ -56,17 +78,31 @@ def self.use(version) raise "Need a version" if version.nil? version = if version == 'latest' - versions.last + self.versions.last else Helper.normalize_version(version) or raise "mismatch version number!" end begin UseVersionService.new(version: version).execute! rescue UseVersionService::VersionNotFoundError return false + end + end + + # @param [Integer] keep_versions_size + # @return [void] + def self.clean(keep_versions_size) + raise ArgumentError if keep_versions_size.nil? + + clean_targets = self.installed_versions[(keep_versions_size + 1)..-1] + return if clean_targets.nil? + + clean_targets.each do |version_object| + next if(version_object.latest_version? or version_object.current_version?) + self.uninstall(version_object.name) end end end