pbmenv 0.1.7 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f3ac7d44e0f095598e3a3a80728d61cf0fc8f8bd363c50a37beaa1bc08537bc
4
- data.tar.gz: 929ccd1944970ee956efe39855a17aa4c488462725ab7cf7c7f5b89dcca014bf
3
+ metadata.gz: b0ac717a799d7bd7e906bfbf74fc721311ddc684f65c9c3b21e0e57219bce68c
4
+ data.tar.gz: 0631ccea4cd5ea8141f903b75373b669c6456fe87e9868e1b74b7f76c67ab4bd
5
5
  SHA512:
6
- metadata.gz: af65fec992aeaaff02bf3ca0abc7f3f2c92f0deb5f065e4bce53b0169d1030f2f5f39cd0758bb630c016c0d66a53f6174ab1cd85b8017b7522d621c174493048
7
- data.tar.gz: 9716ea9f4f06624680d5169bf7f2f36a36957fbf960966dfbab279708006f40ac6855ccaa8df93abd27bfa67b077c4c4ffb766eefee9a3b11816ae38ff29746a
6
+ metadata.gz: bafd95cda10189f3ccd5403e979d4405017134a828c78aeed8ff95f712f4b71476a054eb7fd04d447c7964dcf231b092ff0054e0cd9047476ee44028e316a2dc
7
+ data.tar.gz: e928f94b5cfaa01892084b057d0dc5aebbb0c2f3782822cd72b7fc83a0441cac45c500cd4e922ecbabd4cd81818ca09611566ffba8ee32e7253a5f6c6f349d25
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ procon_bypass_man-*
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## [0.1.10] - 2022-06-05
2
+ - project_template/app.rb.erbを評価してapp.rbを生成するようになりました
3
+
4
+ ## [0.1.9] - 2022-02-18
5
+ - Pbmenv.installにenable_pbm_cloudオプションを追加しました
6
+ - Pbmenv.installにuse_optionオプションを追加しました
7
+ - また、 `pbmenv intall xxx --use` が使えるようになりました
8
+
9
+ ## [0.1.8] - 2022-02-08
10
+ - 不具合修正
11
+
1
12
  ## [0.1.7] - 2022-01-11
2
13
  - currentが存在しないときに、installを実行するとcurrentを貼るようにしました
3
14
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pbmenv (0.1.7)
4
+ pbmenv (0.1.10)
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
@@ -17,6 +17,8 @@ gem 'pbmenv'
17
17
  * pbmenv available_versions
18
18
  * pbmenv versions
19
19
  * pbmenv install $version
20
+ * --use
21
+ * そのまま/usr/share/pbm/currentディレクトリへのシンボリックリンクを貼ります
20
22
  * pbmenv use $version
21
23
  * pbmenv uninstall $version
22
24
  * API
@@ -45,3 +47,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
45
47
  * docker-compose build --no-cache
46
48
  * docker-compose run app bash
47
49
  * bin/rspec
50
+ * also `DISABLE_DEBUG_LOG=1 bin/rspec`
data/lib/pbmenv/cli.rb CHANGED
@@ -9,7 +9,18 @@ module Pbmenv
9
9
  Pbmenv.versions.each { |x| puts x }
10
10
  when 'install', 'i'
11
11
  sub_command_arg = argv[1]
12
- Pbmenv.install(sub_command_arg)
12
+ case argv[2]
13
+ when "--use"
14
+ use_option = true
15
+ when nil
16
+ use_option = false
17
+ else
18
+ puts <<~EOH
19
+ Unknown option:
20
+ available options: --use
21
+ EOH
22
+ end
23
+ Pbmenv.install(sub_command_arg, use_option: use_option)
13
24
  when 'use', 'u'
14
25
  sub_command_arg = argv[1]
15
26
  Pbmenv.use(sub_command_arg)
@@ -0,0 +1,117 @@
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
+ if File.exists?("/usr/share/pbm/v#{version}")
18
+ raise AlreadyCreatedError
19
+ end
20
+
21
+ begin
22
+ source_path = download_src(version)
23
+ build_app_file(source_path: source_path)
24
+ create_if_miss_shared_dir
25
+ create_if_miss_device_id_file
26
+ link_device_id_file(version: version)
27
+ create_if_miss_current_dir(version: version)
28
+ rescue DownloadSrcService::DownloadError
29
+ puts "Download failed. Check the version name."
30
+ raise NotSupportVersionError
31
+ rescue => e
32
+ Helper.system_and_puts "rm -rf #{VersionPathname.new(version).version_path}"
33
+ raise
34
+ ensure
35
+ if source_path && Dir.exists?(source_path)
36
+ Helper.system_and_puts "rm -rf #{source_path}"
37
+ end
38
+ end
39
+
40
+ return true
41
+ end
42
+
43
+ private
44
+
45
+ # @return [String]
46
+ def download_src(version)
47
+ Pbmenv::DownloadSrcService.new(version).execute!
48
+ return "procon_bypass_man-#{version}"
49
+ end
50
+
51
+ def build_app_file(source_path: )
52
+ pathname = VersionPathname.new(version)
53
+
54
+ if File.exists?(File.join(source_path, "project_template/app.rb.erb"))
55
+ Helper.system_and_puts <<~SHELL
56
+ mkdir -p #{pathname.version_path} &&
57
+ cp procon_bypass_man-#{version}/project_template/app.rb.erb #{pathname.version_path}/
58
+ cp procon_bypass_man-#{version}/project_template/README.md #{pathname.version_path}/
59
+ cp procon_bypass_man-#{version}/project_template/setting.yml #{pathname.version_path}/
60
+ cp -r procon_bypass_man-#{version}/project_template/systemd_units #{pathname.version_path}/
61
+ SHELL
62
+ require "./procon_bypass_man-#{version}/project_template/lib/app_generator"
63
+ AppGenerator.new(
64
+ prefix_path: pathname.version_path,
65
+ enable_integration_with_pbm_cloud: enable_pbm_cloud,
66
+ ).generate
67
+ Helper.system_and_puts "rm #{pathname.app_rb_erb_path}"
68
+ else
69
+ Helper.system_and_puts <<~SHELL
70
+ mkdir -p #{pathname.version_path} &&
71
+ cp procon_bypass_man-#{version}/project_template/app.rb #{pathname.version_path}/
72
+ cp procon_bypass_man-#{version}/project_template/README.md #{pathname.version_path}/
73
+ cp procon_bypass_man-#{version}/project_template/setting.yml #{pathname.version_path}/
74
+ cp -r procon_bypass_man-#{version}/project_template/systemd_units #{pathname.version_path}/
75
+ SHELL
76
+ end
77
+
78
+ # 旧実装バージョン
79
+ if enable_pbm_cloud
80
+ text = File.read(pathname.app_rb_path)
81
+ if text =~ /config\.api_servers\s+=\s+\['(https:\/\/.+)'\]/ && (url = $1)
82
+ text.gsub!(/#\s+config\.api_servers\s+=\s+.+$/, "config.api_servers = '#{url}'")
83
+ end
84
+ File.write(pathname.app_rb_path, text)
85
+ end
86
+ end
87
+
88
+ def create_if_miss_shared_dir
89
+ unless File.exists?(VersionPathname.shared)
90
+ Helper.system_and_puts <<~SHELL
91
+ mkdir -p #{VersionPathname.shared}
92
+ SHELL
93
+ end
94
+ end
95
+
96
+ def create_if_miss_device_id_file
97
+ device_id_path_in_shared = VersionPathname.device_id_path_in_shared
98
+ unless File.exists?(device_id_path_in_shared)
99
+ File.write(device_id_path_in_shared, "d_#{SecureRandom.uuid}")
100
+ end
101
+ end
102
+
103
+ def link_device_id_file(version: )
104
+ pathname = VersionPathname.new(version)
105
+ Helper.system_and_puts <<~SHELL
106
+ ln -s #{pathname.device_id_path_in_shared} #{pathname.device_id_path_in_version}
107
+ SHELL
108
+ end
109
+
110
+ def create_if_miss_current_dir(version: )
111
+ # 初回だけinstall時にcurrentを作成する
112
+ if !File.exists?(VersionPathname.current) || use_option
113
+ UseVersionService.new(version: version).execute!
114
+ end
115
+ end
116
+ end
117
+ 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,32 @@
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
+ if ENV["DEBUG_INSTALL"]
13
+ shell = <<~SHELL
14
+ git clone https://github.com/splaplapla/procon_bypass_man.git procon_bypass_man-#{version}
15
+ SHELL
16
+ else
17
+ # TODO cache for testing
18
+ shell = <<~SHELL
19
+ curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz > /dev/null
20
+ SHELL
21
+ end
22
+
23
+ if Helper.system_and_puts(shell)
24
+ unless File.exists?("procon_bypass_man-#{version}/project_template")
25
+ raise NotSupportVersionError, "This version is not support by pbmenv"
26
+ end
27
+ else
28
+ raise DownloadError
29
+ end
30
+ end
31
+ end
32
+ 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
@@ -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.7"
4
+ VERSION = "0.1.10"
5
5
  end
@@ -0,0 +1,45 @@
1
+ module Pbmenv
2
+ class VersionPathname
3
+ PBM_DIR = "/usr/share/pbm"
4
+
5
+ def initialize(version)
6
+ @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 device_id_path_in_shared
30
+ File.join(self.class.shared, "/device_id")
31
+ end
32
+
33
+ def self.device_id_path_in_shared
34
+ File.join(shared, "/device_id")
35
+ end
36
+
37
+ def self.current
38
+ File.join(PBM_DIR, "/current")
39
+ end
40
+
41
+ def self.shared
42
+ File.join(PBM_DIR, "/shared")
43
+ end
44
+ end
45
+ end
data/lib/pbmenv.rb CHANGED
@@ -6,6 +6,12 @@ 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/create_version_service"
12
+ require_relative "pbmenv/destroy_version_service"
13
+ require_relative "pbmenv/use_version_service"
14
+ require_relative "pbmenv/download_src_service"
9
15
 
10
16
  module Pbmenv
11
17
  PBM_DIR = "/usr/share/pbm"
@@ -18,91 +24,49 @@ module Pbmenv
18
24
  Pbmenv::PBM.new.versions.map { |name| Pathname.new(name).basename.to_s =~ /^v([\d.]+)/ && $1 }.compact.sort_by {|x| Gem::Version.new(x) }.compact
19
25
  end
20
26
 
21
- def self.install(version)
27
+ def self.install(version, use_option: false, enable_pbm_cloud: false)
22
28
  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}")
29
+ version =
30
+ if version == 'latest'
31
+ available_versions.first
32
+ else
33
+ Helper.normalize_version(version) or raise "mismatch version number!"
34
+ end
35
+
36
+ begin
37
+ CreateVersionService.new(version: version, use_option: use_option, enable_pbm_cloud: enable_pbm_cloud).execute!
38
+ rescue CreateVersionService::AlreadyCreatedError
39
+ return false
40
+ rescue CreateVersionService::NotSupportVersionError
28
41
  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
- unless File.exists?("#{PBM_DIR}/shared")
37
- system_and_puts <<~SHELL
38
- mkdir -p #{PBM_DIR}/shared
39
- SHELL
40
- end
41
-
42
- unless File.exists?("#{PBM_DIR}/shared/device_id")
43
- File.write("#{PBM_DIR}/shared/device_id", "d_#{SecureRandom.uuid}")
44
- end
45
-
46
- system_and_puts <<~SHELL
47
- ln -s #{PBM_DIR}/shared/device_id #{PBM_DIR}/v#{version}/device_id
48
- SHELL
49
-
50
- # 初回だけinstall時にcurrentを作成する
51
- if not File.exists?("#{PBM_DIR}/current")
52
- use(version)
53
- end
54
- rescue => e
55
- system_and_puts "rm -rf #{PBM_DIR}/v#{version}"
56
- raise
57
- ensure
58
- if Dir.exists?("./procon_bypass_man-#{version}")
59
- system_and_puts "rm -rf ./procon_bypass_man-#{version}"
60
42
  end
61
43
  end
62
44
 
63
45
  # TODO currentが挿しているバージョンはどうする?
64
46
  def self.uninstall(version)
65
47
  raise "Need a version" if version.nil?
48
+ version = Helper.normalize_version(version) or raise "mismatch version number!"
66
49
 
67
- unless File.exists?("/usr/share/pbm/v#{version}")
50
+ begin
51
+ DestroyVersionService.new(version: version).execute!
52
+ rescue DestroyVersionService::VersionNotFoundError
68
53
  return false
69
54
  end
70
- system_and_puts "rm -rf #{PBM_DIR}/v#{version}"
71
55
  end
72
56
 
73
57
  def self.use(version)
74
58
  raise "Need a version" if version.nil?
75
- version = versions.last if version == "latest"
76
-
77
- if !File.exists?("/usr/share/pbm/#{version}") && !File.exists?("/usr/share/pbm/v#{version}")
59
+ version =
60
+ if version == 'latest'
61
+ versions.last
62
+ else
63
+ Helper.normalize_version(version) or raise "mismatch version number!"
64
+ end
65
+
66
+ begin
67
+ UseVersionService.new(version: version).execute!
68
+ rescue UseVersionService::VersionNotFoundError
78
69
  return false
79
70
  end
80
-
81
- if File.exists?("#{PBM_DIR}/current")
82
- system_and_puts "unlink #{PBM_DIR}/current"
83
- end
84
- system_and_puts "ln -s #{PBM_DIR}/v#{version} #{PBM_DIR}/current"
85
- end
86
-
87
- def self.download_src(version)
88
- if ENV["DEBUG_INSTALL"]
89
- shell = <<~SHELL
90
- git clone https://github.com/splaplapla/procon_bypass_man.git procon_bypass_man-#{version}
91
- SHELL
92
- else
93
- # TODO cache for testing
94
- shell = <<~SHELL
95
- curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz
96
- SHELL
97
- end
98
- system_and_puts(shell)
99
- unless File.exists?("procon_bypass_man-#{version}/project_template")
100
- raise "This version is not support by pbmenv"
101
- end
102
- end
103
-
104
- def self.system_and_puts(shell)
105
- puts "[SHELL] #{shell}"
106
- system(shell)
107
71
  end
108
72
  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.7
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - jiikko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-11 00:00:00.000000000 Z
11
+ date: 2022-06-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A package manager of PBM
14
14
  email:
@@ -39,8 +39,14 @@ files:
39
39
  - exe/pbmenv
40
40
  - lib/pbmenv.rb
41
41
  - lib/pbmenv/cli.rb
42
+ - lib/pbmenv/create_version_service.rb
43
+ - lib/pbmenv/destroy_version_service.rb
44
+ - lib/pbmenv/download_src_service.rb
45
+ - lib/pbmenv/helper.rb
42
46
  - lib/pbmenv/pbm.rb
47
+ - lib/pbmenv/use_version_service.rb
43
48
  - lib/pbmenv/version.rb
49
+ - lib/pbmenv/version_pathname.rb
44
50
  - pbmenv.gemspec
45
51
  homepage: https://github.com/splaplapla/pbmenv
46
52
  licenses: