Module: Pbmenv

Defined in:
lib/pbmenv.rb,
lib/pbmenv/cli.rb,
lib/pbmenv/pbm.rb,
lib/pbmenv/helper.rb,
lib/pbmenv/version.rb,
lib/pbmenv/version_object.rb,
lib/pbmenv/directory_object.rb,
lib/pbmenv/version_pathname.rb,
lib/pbmenv/services/use_version_service.rb,
lib/pbmenv/services/download_src_service.rb,
lib/pbmenv/services/create_version_service.rb,
lib/pbmenv/services/destroy_version_service.rb

Defined Under Namespace

Classes: CLI, CreateVersionService, DestroyVersionService, DirectoryObject, DownloadSrcService, Helper, PBM, UseVersionService, VersionObject, VersionPathname

Constant Summary collapse

PBM_DIR =

NOTE: pbmから参照している

"/usr/share/pbm"
DEFAULT_PBM_DIR =
PBM_DIR
VERSION =
"0.1.13"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



24
25
26
# File 'lib/pbmenv.rb', line 24

def logger
  @logger
end

Class Method Details

.available_versionsObject



60
61
62
# File 'lib/pbmenv.rb', line 60

def self.available_versions
  Pbmenv::PBM.new.available_versions.map { |x| x["name"] =~ /^v([\d.]+)/ && $1 }.compact
end

.chdir(to_dir) ⇒ void

This method returns an undefined value.

NOTE: テスト用

Parameters:

  • to_dir (String)

Raises:

  • (ArgumentError)


37
38
39
40
# File 'lib/pbmenv.rb', line 37

def self.chdir(to_dir)
  raise(ArgumentError, 'テスト以外では実行できません') unless defined?(RSpec)
  @current_pbm_dir = to_dir
end

.clean(keep_versions_size) ⇒ void

This method returns an undefined value.

Parameters:

  • keep_versions_size (Integer)

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pbmenv.rb', line 131

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

.command_versionsObject



77
78
79
80
81
# File 'lib/pbmenv.rb', line 77

def self.command_versions
  self.installed_versions.map do |version|
    version.current_version? ? "* #{version.name}" : "  #{version.name}"
  end
end

.current_directoryPbmenv::DirectoryObject



56
57
58
# File 'lib/pbmenv.rb', line 56

def self.current_directory
  Pbmenv::DirectoryObject.new(path: VersionPathname.current)
end

.install(version, use_option: false, enable_pbm_cloud: false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pbmenv.rb', line 83

def self.install(version, use_option: false, enable_pbm_cloud: false)
  raise "Need a version" if version.nil?
  version =
    if version == 'latest'
      available_versions.first
    else
      Helper.normalize_version(version) or raise "mismatch version number!"
    end

  begin
    CreateVersionService.new(version: version, use_option: use_option, enable_pbm_cloud: enable_pbm_cloud).execute!
  rescue CreateVersionService::AlreadyCreatedError
    return false
  rescue CreateVersionService::NotSupportVersionError
    return false
  end
end

.installed_versionsArray<Pbmenv::VersionObject>

Returns:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pbmenv.rb', line 65

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

.pbm_dirString

Returns:

  • (String)


43
44
45
# File 'lib/pbmenv.rb', line 43

def self.pbm_dir
  @current_pbm_dir
end

.slice_loggervoid

This method returns an undefined value.



48
49
50
51
52
53
# File 'lib/pbmenv.rb', line 48

def self.slice_logger
  previous_logger = self.logger
  self.logger = Logger.new(File.open(File::NULL, "w"))
  yield
  self.logger = previous_logger
end

.uninstall(version) ⇒ Object

TODO: 引数がcurrentを指しているバージョンはどうする?



102
103
104
105
106
107
108
109
110
111
# File 'lib/pbmenv.rb', line 102

def self.uninstall(version)
  raise "Need a version" if version.nil?
  version = Helper.normalize_version(version) or raise "mismatch version number!"

  begin
    DestroyVersionService.new(version: version).execute!
  rescue DestroyVersionService::VersionNotFoundError
    return false
  end
end

.use(version) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pbmenv.rb', line 113

def self.use(version)
  raise "Need a version" if version.nil?
  version =
    if version == 'latest'
      self.installed_versions.last.name
    else
      Helper.normalize_version(version) or raise "mismatch version number!"
    end

  begin
    UseVersionService.new(version: version).execute!
  rescue UseVersionService::VersionNotFoundError
    return false
  end
end