pbmenv 0.1.9 → 0.1.10

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: b0ac717a799d7bd7e906bfbf74fc721311ddc684f65c9c3b21e0e57219bce68c
4
+ data.tar.gz: 0631ccea4cd5ea8141f903b75373b669c6456fe87e9868e1b74b7f76c67ab4bd
5
5
  SHA512:
6
- metadata.gz: 5f4c9d3ba339adb0c6be98c276eb78e41648735035cf817b02986d8aa1c8c2b5829c3bbbc0bdb716f2cbfae7e25a311282a2af1c52af5990b5876baf91077c7b
7
- data.tar.gz: 0352bcc42d5561ca37d6a20554138d1cfffe744adb06700c5443f6068efa5b2a3d77db00c319b147b78f7939e829f51501e86d80b4790ce89e30628a6808a225
6
+ metadata.gz: bafd95cda10189f3ccd5403e979d4405017134a828c78aeed8ff95f712f4b71476a054eb7fd04d447c7964dcf231b092ff0054e0cd9047476ee44028e316a2dc
7
+ data.tar.gz: e928f94b5cfaa01892084b057d0dc5aebbb0c2f3782822cd72b7fc83a0441cac45c500cd4e922ecbabd4cd81818ca09611566ffba8ee32e7253a5f6c6f349d25
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [0.1.10] - 2022-06-05
2
+ - project_template/app.rb.erbを評価してapp.rbを生成するようになりました
3
+
1
4
  ## [0.1.9] - 2022-02-18
2
5
  - Pbmenv.installにenable_pbm_cloudオプションを追加しました
3
6
  - Pbmenv.installにuse_optionオプションを追加しました
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.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
@@ -47,3 +47,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
47
47
  * docker-compose build --no-cache
48
48
  * docker-compose run app bash
49
49
  * bin/rspec
50
+ * also `DISABLE_DEBUG_LOG=1 bin/rspec`
@@ -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.9"
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"
@@ -20,102 +26,47 @@ module Pbmenv
20
26
 
21
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}")
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}'")
29
+ version =
30
+ if version == 'latest'
31
+ available_versions.first
32
+ else
33
+ Helper.normalize_version(version) or raise "mismatch version number!"
40
34
  end
41
- File.write("#{PBM_DIR}/v#{version}/app.rb", text)
42
- end
43
35
 
44
- unless File.exists?("#{PBM_DIR}/shared")
45
- system_and_puts <<~SHELL
46
- mkdir -p #{PBM_DIR}/shared
47
- SHELL
48
- end
49
-
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}"
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
41
+ return false
68
42
  end
69
43
  end
70
44
 
71
45
  # TODO currentが挿しているバージョンはどうする?
72
46
  def self.uninstall(version)
73
47
  raise "Need a version" if version.nil?
48
+ version = Helper.normalize_version(version) or raise "mismatch version number!"
74
49
 
75
- unless File.exists?("/usr/share/pbm/v#{version}")
50
+ begin
51
+ DestroyVersionService.new(version: version).execute!
52
+ rescue DestroyVersionService::VersionNotFoundError
76
53
  return false
77
54
  end
78
- system_and_puts "rm -rf #{PBM_DIR}/v#{version}"
79
55
  end
80
56
 
81
57
  def self.use(version)
82
58
  raise "Need a version" if version.nil?
83
- version = versions.last if version == "latest"
59
+ version =
60
+ if version == 'latest'
61
+ versions.last
62
+ else
63
+ Helper.normalize_version(version) or raise "mismatch version number!"
64
+ end
84
65
 
85
- if !File.exists?("/usr/share/pbm/#{version}") && !File.exists?("/usr/share/pbm/v#{version}")
66
+ begin
67
+ UseVersionService.new(version: version).execute!
68
+ rescue UseVersionService::VersionNotFoundError
86
69
  return false
87
70
  end
88
-
89
- if File.symlink?("#{PBM_DIR}/current")
90
- system_and_puts "unlink #{PBM_DIR}/current"
91
- end
92
-
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
99
-
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
110
- 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
71
  end
121
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.9
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-02-18 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: