pbmenv 0.1.9 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d84d71d3bcf5e7e79a41197911ac54faf9af6dbda23ccedc35975efbb7e6a58b
4
- data.tar.gz: 210058e9076ada3295ce82f03b0194c42539d12ae6bc231c004d9d2dbbf94e2d
3
+ metadata.gz: 71360d9bbca44d55f4ca11d3740f66de12ed58fc4b019c1aec14c9aecdd3967f
4
+ data.tar.gz: 14c5ffc901de8531836215cb17b82c8774d5610ace13c8ba735d3870bb2bfbc6
5
5
  SHA512:
6
- metadata.gz: 5f4c9d3ba339adb0c6be98c276eb78e41648735035cf817b02986d8aa1c8c2b5829c3bbbc0bdb716f2cbfae7e25a311282a2af1c52af5990b5876baf91077c7b
7
- data.tar.gz: 0352bcc42d5561ca37d6a20554138d1cfffe744adb06700c5443f6068efa5b2a3d77db00c319b147b78f7939e829f51501e86d80b4790ce89e30628a6808a225
6
+ metadata.gz: bc45a25392580bd34838839dc657bda34eddf9e879c3e8b7bcc1cc530a8f0ed0c9d13392051c3abf6d42688d0264f03126ed3aab6f929be2fac4c4eb564c4ba8
7
+ data.tar.gz: 410f0cd1b91575f880263f27411e8047a4d8a47c05570fe9b8276eb55f94dc1b291da0b2a662761a99845f535e7aaf1155630d22f5aedf80708dc29de47df732
@@ -6,6 +6,7 @@ on:
6
6
  jobs:
7
7
  release:
8
8
  runs-on: ubuntu-latest
9
+ timeout-minutes: 5
9
10
  permissions:
10
11
  contents: write
11
12
 
@@ -12,21 +12,17 @@ on:
12
12
 
13
13
  jobs:
14
14
  test:
15
-
16
15
  runs-on: ubuntu-latest
16
+ timeout-minutes: 5
17
17
  strategy:
18
18
  matrix:
19
- ruby-version: ['2.5', '2.7', '3.0']
20
-
19
+ ruby-version: ['2.5', '3.0', '3.1']
21
20
  steps:
22
- - uses: actions/checkout@v2
21
+ - uses: actions/checkout@v3
23
22
  - name: Set up Ruby
24
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
- # uses: ruby/setup-ruby@v1
27
23
  uses: ruby/setup-ruby@v1
28
24
  with:
29
25
  ruby-version: ${{ matrix.ruby-version }}
30
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
26
+ bundler-cache: true
31
27
  - name: Run tests
32
28
  run: bundle exec rake
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.1.11] - 2023-02-
2
+ - ダウンロードしたソースコードを/tmpに展開するようにしました
3
+ - 古いバージョンディレクトリを削除するcleanコマンドを実装しました
4
+
5
+ ## [0.1.10] - 2022-06-05
6
+ - project_template/app.rb.erbを評価してapp.rbを生成するようになりました
7
+
1
8
  ## [0.1.9] - 2022-02-18
2
9
  - Pbmenv.installにenable_pbm_cloudオプションを追加しました
3
10
  - Pbmenv.installにuse_optionオプションを追加しました
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM ruby:2.5
1
+ FROM ruby:3.0
2
2
 
3
3
  WORKDIR /pbmenv
4
4
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pbmenv (0.1.9)
4
+ pbmenv (0.1.11)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -40,4 +40,4 @@ DEPENDENCIES
40
40
  rspec
41
41
 
42
42
  BUNDLED WITH
43
- 2.2.31
43
+ 2.3.7
data/README.md CHANGED
@@ -21,6 +21,7 @@ gem 'pbmenv'
21
21
  * そのまま/usr/share/pbm/currentディレクトリへのシンボリックリンクを貼ります
22
22
  * pbmenv use $version
23
23
  * pbmenv uninstall $version
24
+ * pbmenv clean $version_size_to_keep
24
25
  * API
25
26
  * Pbmenv.available_versions
26
27
  * https://github.com/splaplapla/procon_bypass_man/tags からバージョンのリストを取ってくる
@@ -34,6 +35,9 @@ gem 'pbmenv'
34
35
  * Pbmenv.uninstall(version)
35
36
  * /usr/share/pbm/current が削除対象だったら例外を投げる
36
37
  * /usr/share/pbm/#{version} を 削除する
38
+ * Pbmenv.clean(version_size_to_keep)
39
+ * 古いバージョンをversion_size_to_keepの数だけ削除します
40
+ * currentと最新のディレクトリは削除対象外です
37
41
 
38
42
  ## Contributing
39
43
 
@@ -47,3 +51,9 @@ The gem is available as open source under the terms of the [MIT License](https:/
47
51
  * docker-compose build --no-cache
48
52
  * docker-compose run app bash
49
53
  * bin/rspec
54
+ * also `DISABLE_DEBUG_LOG=1 bin/rspec`
55
+
56
+ ### ローカルでgemをインストールして動作確認をする
57
+ * rake build
58
+ * gem i --local pkg/pbmenv-x.y.z.gem
59
+ * pbmenv ...
data/lib/pbmenv/cli.rb CHANGED
@@ -27,12 +27,18 @@ module Pbmenv
27
27
  when 'uninstall'
28
28
  sub_command_arg = argv[1]
29
29
  Pbmenv.uninstall(sub_command_arg)
30
+ when 'clean'
31
+ version_size_to_keep = argv[1].to_i
32
+ if version_size_to_keep == 0
33
+ version_size_to_keep = 10
34
+ end
35
+ Pbmenv.clean(version_size_to_keep)
30
36
  when '--version'
31
37
  puts Pbmenv::VERSION
32
38
  else
33
39
  puts <<~EOH
34
40
  Unknown command:
35
- available commands: available_versions, versions, install, use, uninstall
41
+ available commands: available_versions, versions, install, use, uninstall, clean
36
42
  EOH
37
43
  end
38
44
  end
@@ -0,0 +1,33 @@
1
+ module Pbmenv
2
+ class DirectoryObject
3
+ class NonSymlinkError < StandardError; end
4
+
5
+ def initialize(path: )
6
+ @path = path
7
+ end
8
+
9
+ def path
10
+ @path
11
+ end
12
+
13
+ # @return [String]
14
+ def readlink!
15
+ raise NonSymlinkError if not symlink?
16
+ File.readlink(path)
17
+ end
18
+
19
+ # @return [String, NilClass]
20
+ def readlink
21
+ readlink!
22
+ rescue NonSymlinkError
23
+ nil
24
+ end
25
+
26
+ private
27
+
28
+ # @return [Boolean]
29
+ def symlink?
30
+ File.symlink?(path)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module Pbmenv
2
+ class Helper
3
+ def self.system_and_puts(shell)
4
+ to_stdout "[SHELL] #{shell}"
5
+ system(shell)
6
+ end
7
+
8
+ def self.to_stdout(text)
9
+ puts text
10
+ end
11
+
12
+ def self.normalize_version(version)
13
+ /\Av?([\w.]*)\z/ =~ version && $1
14
+ end
15
+ end
16
+ end
data/lib/pbmenv/pbm.rb CHANGED
@@ -5,13 +5,10 @@ require 'json'
5
5
 
6
6
  module Pbmenv
7
7
  class PBM
8
+ # @return [Array<String>] githubに問い合わせて、利用可能なバージョンのリストを返す
8
9
  def available_versions
9
10
  response = URI.open 'https://api.github.com/repos/splaplapla/procon_bypass_man/tags'
10
11
  JSON.parse(response.read)
11
12
  end
12
-
13
- def versions
14
- Dir.glob("#{Pbmenv::PBM_DIR}/v*")
15
- end
16
13
  end
17
14
  end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pbmenv
4
+ class CreateVersionService
5
+ class AlreadyCreatedError < StandardError; end
6
+ class NotSupportVersionError < StandardError; end
7
+
8
+ attr_accessor :version, :use_option, :enable_pbm_cloud
9
+
10
+ def initialize(version: , use_option: , enable_pbm_cloud: )
11
+ self.version = version
12
+ self.use_option = use_option
13
+ self.enable_pbm_cloud = enable_pbm_cloud
14
+ end
15
+
16
+ def execute!
17
+ pathname = VersionPathname.new(version)
18
+ if File.exists?(pathname.version_path)
19
+ raise AlreadyCreatedError
20
+ end
21
+
22
+ begin
23
+ download_src(version)
24
+ build_app_file
25
+ create_if_miss_shared_dir
26
+ create_if_miss_device_id_file
27
+ link_device_id_file(version: version)
28
+ create_if_miss_current_dir(version: version)
29
+ rescue DownloadSrcService::DownloadError
30
+ puts "Download failed. Check the version name."
31
+ raise NotSupportVersionError
32
+ rescue => e
33
+ Helper.system_and_puts "rm -rf #{pathname.version_path}"
34
+ raise
35
+ ensure
36
+ if Dir.exists?(pathname.src_pbm_path)
37
+ Helper.system_and_puts "rm -rf #{pathname.src_pbm_path}"
38
+ end
39
+ end
40
+
41
+ return true
42
+ end
43
+
44
+ private
45
+
46
+ # @return [String]
47
+ def download_src(version)
48
+ Pbmenv::DownloadSrcService.new(version).execute!
49
+ end
50
+
51
+ def build_app_file
52
+ pathname = VersionPathname.new(version)
53
+ Helper.system_and_puts "mkdir -p #{pathname.version_path}"
54
+ Helper.system_and_puts "cp -r #{pathname.src_project_template_systemd_units} #{pathname.version_path}/"
55
+
56
+ if File.exists?(pathname.src_pbm_project_template_app_rb_erb_path)
57
+ pathname.project_template_file_paths(include_app_erb: true).each do |project_template_file_path|
58
+ Helper.system_and_puts "cp #{project_template_file_path} #{pathname.version_path}/"
59
+ end
60
+ require pathname.lib_app_generator
61
+ AppGenerator.new(
62
+ prefix_path: pathname.version_path,
63
+ enable_integration_with_pbm_cloud: enable_pbm_cloud,
64
+ ).generate
65
+ Helper.system_and_puts "rm #{pathname.app_rb_erb_path}"
66
+ else
67
+ pathname.project_template_file_paths(include_app_erb: false).each do |project_template_file_path|
68
+ Helper.system_and_puts "cp #{project_template_file_path} #{pathname.version_path}/"
69
+ end
70
+
71
+ # 旧実装バージョン. 0.2.10くらいで削除する
72
+ if enable_pbm_cloud
73
+ text = File.read(pathname.app_rb_path)
74
+ if text =~ /config\.api_servers\s+=\s+\['(https:\/\/.+)'\]/ && (url = $1)
75
+ text.gsub!(/#\s+config\.api_servers\s+=\s+.+$/, "config.api_servers = '#{url}'")
76
+ end
77
+ File.write(pathname.app_rb_path, text)
78
+ end
79
+ end
80
+ end
81
+
82
+ def create_if_miss_shared_dir
83
+ return if File.exists?(VersionPathname.shared)
84
+
85
+ Helper.system_and_puts <<~SHELL
86
+ mkdir -p #{VersionPathname.shared}
87
+ SHELL
88
+ end
89
+
90
+ def create_if_miss_device_id_file
91
+ device_id_path_in_shared = VersionPathname.device_id_path_in_shared
92
+ return if File.exists?(device_id_path_in_shared)
93
+
94
+ File.write(device_id_path_in_shared, "d_#{SecureRandom.uuid}")
95
+ end
96
+
97
+ def link_device_id_file(version: )
98
+ pathname = VersionPathname.new(version)
99
+ Helper.system_and_puts <<~SHELL
100
+ ln -s #{pathname.device_id_path_in_shared} #{pathname.device_id_path_in_version}
101
+ SHELL
102
+ end
103
+
104
+ def create_if_miss_current_dir(version: )
105
+ # 初回だけinstall時にcurrentを作成する
106
+ if !File.exists?(VersionPathname.current) || use_option
107
+ UseVersionService.new(version: version).execute!
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pbmenv
4
+ class DestroyVersionService
5
+ class VersionNotFoundError < StandardError; end
6
+
7
+ attr_accessor :version
8
+
9
+ def initialize(version: )
10
+ @version = version
11
+ end
12
+
13
+ def execute!
14
+ version_pathname = VersionPathname.new(version)
15
+
16
+ unless File.exists?(version_pathname.version_path)
17
+ raise VersionNotFoundError
18
+ end
19
+ Helper.system_and_puts "rm -rf #{version_pathname.version_path}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ module Pbmenv
2
+ class DownloadSrcService
3
+ class DownloadError < StandardError; end
4
+
5
+ attr_accessor :version
6
+
7
+ def initialize(version)
8
+ self.version = version
9
+ end
10
+
11
+ def execute!
12
+ pathname = VersionPathname.new(version)
13
+ pathname.src_pbm_path
14
+ if ENV["DEBUG_INSTALL"]
15
+ shell = <<~SHELL
16
+ git clone https://github.com/splaplapla/procon_bypass_man.git #{pathname.src_pbm_path}
17
+ SHELL
18
+ else
19
+ # TODO cache for testing
20
+ shell = <<~SHELL
21
+ curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz -C /tmp > /dev/null
22
+ SHELL
23
+ end
24
+
25
+ if Helper.system_and_puts(shell)
26
+ unless File.exists?(pathname.src_pbm_project_template_path)
27
+ raise NotSupportVersionError, "This version is not support by pbmenv"
28
+ end
29
+ else
30
+ raise DownloadError
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pbmenv
4
+ class UseVersionService
5
+ class VersionNotFoundError < StandardError; end
6
+
7
+ attr_accessor :version
8
+
9
+ def initialize(version: )
10
+ self.version = version
11
+ end
12
+
13
+ def execute!
14
+ throw_error_if_has_not_version
15
+ relink_current_path
16
+ end
17
+
18
+ private
19
+
20
+ def throw_error_if_has_not_version
21
+ version_pathname = VersionPathname.new(version)
22
+
23
+ if !File.exists?(version_pathname.version_path_without_v) && !File.exists?(version_pathname.version_path)
24
+ raise UseVersionService::VersionNotFoundError
25
+ end
26
+ end
27
+
28
+ def relink_current_path
29
+ version_pathname = VersionPathname.new(version)
30
+
31
+ if File.symlink?(VersionPathname.current)
32
+ Helper.system_and_puts "unlink #{VersionPathname.current}"
33
+ end
34
+
35
+ Helper.system_and_puts "ln -s #{version_pathname.version_path} #{VersionPathname.current}"
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pbmenv
4
- VERSION = "0.1.9"
4
+ VERSION = "0.1.11"
5
5
  end
@@ -0,0 +1,32 @@
1
+ module Pbmenv
2
+ class VersionObject
3
+ # @param [String] version_name
4
+ # @param [Boolean] is_latest
5
+ # @param [Boolean] is_current
6
+ def initialize(version_name: , is_latest: , is_current: )
7
+ @version_name = version_name
8
+ @is_latest = is_latest
9
+ @is_current = is_current
10
+ end
11
+
12
+ # @return [String]
13
+ def version_name
14
+ @version_name
15
+ end
16
+
17
+ # alias
18
+ def name
19
+ version_name
20
+ end
21
+
22
+ # @return [Boolean]
23
+ def current_version?
24
+ @is_current
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def latest_version?
29
+ @is_latest
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,75 @@
1
+ module Pbmenv
2
+ class VersionPathname
3
+ attr_accessor :version
4
+
5
+ def initialize(version)
6
+ self.version = version
7
+ end
8
+
9
+ def version_path
10
+ File.join(PBM_DIR, "v#{version}")
11
+ end
12
+
13
+ def version_path_without_v
14
+ File.join(PBM_DIR, "#{version}")
15
+ end
16
+
17
+ def app_rb_path
18
+ File.join(version_path, "app.rb")
19
+ end
20
+
21
+ def app_rb_erb_path
22
+ File.join(version_path, "app.rb.erb")
23
+ end
24
+
25
+ def device_id_path_in_version
26
+ File.join(version_path, "device_id")
27
+ end
28
+
29
+ def src_pbm_path
30
+ File.join("/tmp", "procon_bypass_man-#{version}")
31
+ end
32
+
33
+ def project_template_file_paths(include_app_erb: )
34
+ paths = ["README.md", "setting.yml"]
35
+ if include_app_erb
36
+ paths << "app.rb.erb"
37
+ else
38
+ paths << "app.rb"
39
+ end
40
+ return paths.map { |path| File.join(src_pbm_project_template_path, path) }
41
+ end
42
+
43
+ def device_id_path_in_shared
44
+ File.join(self.class.shared, "device_id")
45
+ end
46
+
47
+ def src_pbm_project_template_path
48
+ File.join(src_pbm_path, "project_template")
49
+ end
50
+
51
+ def src_pbm_project_template_app_rb_erb_path
52
+ File.join(src_pbm_project_template_path, "app.rb.erb")
53
+ end
54
+
55
+ def lib_app_generator
56
+ File.join(src_pbm_project_template_path, "lib", "app_generator")
57
+ end
58
+
59
+ def src_project_template_systemd_units
60
+ File.join(src_pbm_project_template_path, "systemd_units")
61
+ end
62
+
63
+ def self.device_id_path_in_shared
64
+ File.join(shared, "device_id")
65
+ end
66
+
67
+ def self.current
68
+ File.join(PBM_DIR, "current")
69
+ end
70
+
71
+ def self.shared
72
+ File.join(PBM_DIR, "shared")
73
+ end
74
+ end
75
+ end
data/lib/pbmenv.rb CHANGED
@@ -6,116 +6,103 @@ require "pathname"
6
6
  require_relative "pbmenv/version"
7
7
  require_relative "pbmenv/cli"
8
8
  require_relative "pbmenv/pbm"
9
+ require_relative "pbmenv/helper"
10
+ require_relative "pbmenv/version_pathname"
11
+ require_relative "pbmenv/version_object"
12
+ require_relative "pbmenv/directory_object"
13
+ require_relative "pbmenv/services/create_version_service"
14
+ require_relative "pbmenv/services/destroy_version_service"
15
+ require_relative "pbmenv/services/use_version_service"
16
+ require_relative "pbmenv/services/download_src_service"
9
17
 
10
18
  module Pbmenv
11
19
  PBM_DIR = "/usr/share/pbm"
12
20
 
21
+ # @return [Pbmenv::DirectoryObject]
22
+ def self.current_directory
23
+ Pbmenv::DirectoryObject.new(path: VersionPathname.current)
24
+ end
25
+
13
26
  def self.available_versions
14
27
  Pbmenv::PBM.new.available_versions.map { |x| x["name"] =~ /^v([\d.]+)/ && $1 }.compact
15
28
  end
16
29
 
30
+ # @return [Array<Pbmenv::VersionObject>]
31
+ def self.installed_versions
32
+ unsorted_dirs = Dir.glob("#{Pbmenv::PBM_DIR}/v*")
33
+ sorted_version_names = unsorted_dirs.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) }
34
+ sorted_version_names.map do |version_name|
35
+ VersionObject.new(
36
+ version_name: version_name,
37
+ is_latest: sorted_version_names.last == version_name,
38
+ is_current: Pbmenv.current_directory.readlink&.end_with?(version_name) || false,
39
+ )
40
+ end
41
+ end
42
+
43
+ # @deprecated
17
44
  def self.versions
18
- Pbmenv::PBM.new.versions.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) }.compact
45
+ unsorted_dirs = Dir.glob("#{Pbmenv::PBM_DIR}/v*")
46
+ unsorted_dirs.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) }.compact
19
47
  end
20
48
 
21
49
  def self.install(version, use_option: false, enable_pbm_cloud: false)
22
50
  raise "Need a version" if version.nil?
23
- if version == 'latest'
24
- version = available_versions.first
25
- end
26
-
27
- if File.exists?("/usr/share/pbm/v#{version}")
28
- return false
29
- end
30
-
31
- download_src(version)
32
- system_and_puts <<~SHELL
33
- mkdir -p #{PBM_DIR}/v#{version} && cp -r procon_bypass_man-#{version}/project_template/* #{PBM_DIR}/v#{version}/
34
- SHELL
35
-
36
- if enable_pbm_cloud
37
- text = File.read("#{PBM_DIR}/v#{version}/app.rb")
38
- if text =~ /config\.api_servers\s+=\s+\['(https:\/\/.+)'\]/ && (url = $1)
39
- text.gsub!(/#\s+config\.api_servers\s+=\s+.+$/, "config.api_servers = '#{url}'")
51
+ version =
52
+ if version == 'latest'
53
+ available_versions.first
54
+ else
55
+ Helper.normalize_version(version) or raise "mismatch version number!"
40
56
  end
41
- File.write("#{PBM_DIR}/v#{version}/app.rb", text)
42
- end
43
-
44
- unless File.exists?("#{PBM_DIR}/shared")
45
- system_and_puts <<~SHELL
46
- mkdir -p #{PBM_DIR}/shared
47
- SHELL
48
- end
49
57
 
50
- unless File.exists?("#{PBM_DIR}/shared/device_id")
51
- File.write("#{PBM_DIR}/shared/device_id", "d_#{SecureRandom.uuid}")
52
- end
53
-
54
- system_and_puts <<~SHELL
55
- ln -s #{PBM_DIR}/shared/device_id #{PBM_DIR}/v#{version}/device_id
56
- SHELL
57
-
58
- # 初回だけinstall時にcurrentを作成する
59
- if !File.exists?("#{PBM_DIR}/current") || use_option
60
- use(version)
61
- end
62
- rescue => e
63
- system_and_puts "rm -rf #{PBM_DIR}/v#{version}"
64
- raise
65
- ensure
66
- if Dir.exists?("./procon_bypass_man-#{version}")
67
- system_and_puts "rm -rf ./procon_bypass_man-#{version}"
58
+ begin
59
+ CreateVersionService.new(version: version, use_option: use_option, enable_pbm_cloud: enable_pbm_cloud).execute!
60
+ rescue CreateVersionService::AlreadyCreatedError
61
+ return false
62
+ rescue CreateVersionService::NotSupportVersionError
63
+ return false
68
64
  end
69
65
  end
70
66
 
71
- # TODO currentが挿しているバージョンはどうする?
67
+ # TODO: 引数がcurrentを指しているバージョンはどうする?
72
68
  def self.uninstall(version)
73
69
  raise "Need a version" if version.nil?
70
+ version = Helper.normalize_version(version) or raise "mismatch version number!"
74
71
 
75
- unless File.exists?("/usr/share/pbm/v#{version}")
72
+ begin
73
+ DestroyVersionService.new(version: version).execute!
74
+ rescue DestroyVersionService::VersionNotFoundError
76
75
  return false
77
76
  end
78
- system_and_puts "rm -rf #{PBM_DIR}/v#{version}"
79
77
  end
80
78
 
81
79
  def self.use(version)
82
80
  raise "Need a version" if version.nil?
83
- version = versions.last if version == "latest"
81
+ version =
82
+ if version == 'latest'
83
+ self.versions.last
84
+ else
85
+ Helper.normalize_version(version) or raise "mismatch version number!"
86
+ end
84
87
 
85
- if !File.exists?("/usr/share/pbm/#{version}") && !File.exists?("/usr/share/pbm/v#{version}")
88
+ begin
89
+ UseVersionService.new(version: version).execute!
90
+ rescue UseVersionService::VersionNotFoundError
86
91
  return false
87
92
  end
93
+ end
88
94
 
89
- if File.symlink?("#{PBM_DIR}/current")
90
- system_and_puts "unlink #{PBM_DIR}/current"
91
- end
95
+ # @param [Integer] keep_versions_size
96
+ # @return [void]
97
+ def self.clean(keep_versions_size)
98
+ raise ArgumentError if keep_versions_size.nil?
92
99
 
93
- if(version_number = version.match(/v?([\w.]+)/)[1])
94
- system_and_puts "ln -s #{PBM_DIR}/v#{version_number} #{PBM_DIR}/current"
95
- else
96
- raise "mismatch version number!"
97
- end
98
- end
100
+ clean_targets = self.installed_versions[(keep_versions_size + 1)..-1]
101
+ return if clean_targets.nil?
99
102
 
100
- def self.download_src(version)
101
- if ENV["DEBUG_INSTALL"]
102
- shell = <<~SHELL
103
- git clone https://github.com/splaplapla/procon_bypass_man.git procon_bypass_man-#{version}
104
- SHELL
105
- else
106
- # TODO cache for testing
107
- shell = <<~SHELL
108
- curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz
109
- SHELL
103
+ clean_targets.each do |version_object|
104
+ next if(version_object.latest_version? or version_object.current_version?)
105
+ self.uninstall(version_object.name)
110
106
  end
111
- system_and_puts(shell)
112
- unless File.exists?("procon_bypass_man-#{version}/project_template")
113
- raise "This version is not support by pbmenv"
114
- end
115
- end
116
-
117
- def self.system_and_puts(shell)
118
- puts "[SHELL] #{shell}"
119
- system(shell)
120
107
  end
121
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pbmenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - jiikko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-18 00:00:00.000000000 Z
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A package manager of PBM
14
14
  email:
@@ -18,11 +18,11 @@ executables:
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - ".circleci/config.yml"
22
21
  - ".github/workflows/release.yml"
23
22
  - ".github/workflows/ruby.yml"
24
23
  - ".gitignore"
25
24
  - ".rspec"
25
+ - ".ruby-version"
26
26
  - CHANGELOG.md
27
27
  - CODE_OF_CONDUCT.md
28
28
  - Dockerfile
@@ -39,8 +39,16 @@ files:
39
39
  - exe/pbmenv
40
40
  - lib/pbmenv.rb
41
41
  - lib/pbmenv/cli.rb
42
+ - lib/pbmenv/directory_object.rb
43
+ - lib/pbmenv/helper.rb
42
44
  - lib/pbmenv/pbm.rb
45
+ - lib/pbmenv/services/create_version_service.rb
46
+ - lib/pbmenv/services/destroy_version_service.rb
47
+ - lib/pbmenv/services/download_src_service.rb
48
+ - lib/pbmenv/services/use_version_service.rb
43
49
  - lib/pbmenv/version.rb
50
+ - lib/pbmenv/version_object.rb
51
+ - lib/pbmenv/version_pathname.rb
44
52
  - pbmenv.gemspec
45
53
  homepage: https://github.com/splaplapla/pbmenv
46
54
  licenses:
data/.circleci/config.yml DELETED
@@ -1,44 +0,0 @@
1
- version: 2.1
2
-
3
- executors:
4
- ruby:
5
- parameters:
6
- tag:
7
- type: string
8
- default: "latest"
9
- docker:
10
- - image: ruby:<< parameters.tag >>
11
- environment:
12
- BUNDLE_PATH: vendor/bundle
13
- BUNDLE_JOBS: 4
14
- working_directory: ~/app
15
-
16
- jobs:
17
- rspec:
18
- parameters:
19
- version:
20
- type: string
21
- executor:
22
- name: ruby
23
- tag: << parameters.version >>
24
- steps:
25
- - checkout
26
- - run: ruby --version
27
- - run: bundle --version
28
- - run: gem --version
29
- - run: gem install bundler
30
- - run: bundle install --jobs 4
31
- - run: bundle exec rspec
32
-
33
- build_jobs: &build_jobs
34
- - rspec:
35
- matrix:
36
- parameters:
37
- version:
38
- - "2.5"
39
- - "2.7"
40
- - "3.0"
41
- workflows:
42
- version: 2
43
- build:
44
- jobs: *build_jobs