pbmenv 0.1.10 → 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: b0ac717a799d7bd7e906bfbf74fc721311ddc684f65c9c3b21e0e57219bce68c
4
- data.tar.gz: 0631ccea4cd5ea8141f903b75373b669c6456fe87e9868e1b74b7f76c67ab4bd
3
+ metadata.gz: 71360d9bbca44d55f4ca11d3740f66de12ed58fc4b019c1aec14c9aecdd3967f
4
+ data.tar.gz: 14c5ffc901de8531836215cb17b82c8774d5610ace13c8ba735d3870bb2bfbc6
5
5
  SHA512:
6
- metadata.gz: bafd95cda10189f3ccd5403e979d4405017134a828c78aeed8ff95f712f4b71476a054eb7fd04d447c7964dcf231b092ff0054e0cd9047476ee44028e316a2dc
7
- data.tar.gz: e928f94b5cfaa01892084b057d0dc5aebbb0c2f3782822cd72b7fc83a0441cac45c500cd4e922ecbabd4cd81818ca09611566ffba8ee32e7253a5f6c6f349d25
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,7 @@
1
+ ## [0.1.11] - 2023-02-
2
+ - ダウンロードしたソースコードを/tmpに展開するようにしました
3
+ - 古いバージョンディレクトリを削除するcleanコマンドを実装しました
4
+
1
5
  ## [0.1.10] - 2022-06-05
2
6
  - project_template/app.rb.erbを評価してapp.rbを生成するようになりました
3
7
 
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.10)
4
+ pbmenv (0.1.11)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
 
@@ -48,3 +52,8 @@ The gem is available as open source under the terms of the [MIT License](https:/
48
52
  * docker-compose run app bash
49
53
  * bin/rspec
50
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
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
@@ -9,19 +9,21 @@ module Pbmenv
9
9
  end
10
10
 
11
11
  def execute!
12
+ pathname = VersionPathname.new(version)
13
+ pathname.src_pbm_path
12
14
  if ENV["DEBUG_INSTALL"]
13
15
  shell = <<~SHELL
14
- git clone https://github.com/splaplapla/procon_bypass_man.git procon_bypass_man-#{version}
16
+ git clone https://github.com/splaplapla/procon_bypass_man.git #{pathname.src_pbm_path}
15
17
  SHELL
16
18
  else
17
19
  # TODO cache for testing
18
20
  shell = <<~SHELL
19
- curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz > /dev/null
21
+ curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz -C /tmp > /dev/null
20
22
  SHELL
21
23
  end
22
24
 
23
25
  if Helper.system_and_puts(shell)
24
- unless File.exists?("procon_bypass_man-#{version}/project_template")
26
+ unless File.exists?(pathname.src_pbm_project_template_path)
25
27
  raise NotSupportVersionError, "This version is not support by pbmenv"
26
28
  end
27
29
  else
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pbmenv
4
- VERSION = "0.1.10"
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
@@ -1,17 +1,17 @@
1
1
  module Pbmenv
2
2
  class VersionPathname
3
- PBM_DIR = "/usr/share/pbm"
3
+ attr_accessor :version
4
4
 
5
5
  def initialize(version)
6
- @version = version
6
+ self.version = version
7
7
  end
8
8
 
9
9
  def version_path
10
- File.join(PBM_DIR, "/v#{@version}")
10
+ File.join(PBM_DIR, "v#{version}")
11
11
  end
12
12
 
13
13
  def version_path_without_v
14
- File.join(PBM_DIR, "/#{@version}")
14
+ File.join(PBM_DIR, "#{version}")
15
15
  end
16
16
 
17
17
  def app_rb_path
@@ -23,23 +23,53 @@ module Pbmenv
23
23
  end
24
24
 
25
25
  def device_id_path_in_version
26
- File.join(version_path, "/device_id")
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) }
27
41
  end
28
42
 
29
43
  def device_id_path_in_shared
30
- File.join(self.class.shared, "/device_id")
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")
31
61
  end
32
62
 
33
63
  def self.device_id_path_in_shared
34
- File.join(shared, "/device_id")
64
+ File.join(shared, "device_id")
35
65
  end
36
66
 
37
67
  def self.current
38
- File.join(PBM_DIR, "/current")
68
+ File.join(PBM_DIR, "current")
39
69
  end
40
70
 
41
71
  def self.shared
42
- File.join(PBM_DIR, "/shared")
72
+ File.join(PBM_DIR, "shared")
43
73
  end
44
74
  end
45
75
  end
data/lib/pbmenv.rb CHANGED
@@ -8,20 +8,42 @@ require_relative "pbmenv/cli"
8
8
  require_relative "pbmenv/pbm"
9
9
  require_relative "pbmenv/helper"
10
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"
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"
15
17
 
16
18
  module Pbmenv
17
19
  PBM_DIR = "/usr/share/pbm"
18
20
 
21
+ # @return [Pbmenv::DirectoryObject]
22
+ def self.current_directory
23
+ Pbmenv::DirectoryObject.new(path: VersionPathname.current)
24
+ end
25
+
19
26
  def self.available_versions
20
27
  Pbmenv::PBM.new.available_versions.map { |x| x["name"] =~ /^v([\d.]+)/ && $1 }.compact
21
28
  end
22
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
23
44
  def self.versions
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
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
25
47
  end
26
48
 
27
49
  def self.install(version, use_option: false, enable_pbm_cloud: false)
@@ -42,7 +64,7 @@ module Pbmenv
42
64
  end
43
65
  end
44
66
 
45
- # TODO currentが挿しているバージョンはどうする?
67
+ # TODO: 引数がcurrentを指しているバージョンはどうする?
46
68
  def self.uninstall(version)
47
69
  raise "Need a version" if version.nil?
48
70
  version = Helper.normalize_version(version) or raise "mismatch version number!"
@@ -58,7 +80,7 @@ module Pbmenv
58
80
  raise "Need a version" if version.nil?
59
81
  version =
60
82
  if version == 'latest'
61
- versions.last
83
+ self.versions.last
62
84
  else
63
85
  Helper.normalize_version(version) or raise "mismatch version number!"
64
86
  end
@@ -69,4 +91,18 @@ module Pbmenv
69
91
  return false
70
92
  end
71
93
  end
94
+
95
+ # @param [Integer] keep_versions_size
96
+ # @return [void]
97
+ def self.clean(keep_versions_size)
98
+ raise ArgumentError if keep_versions_size.nil?
99
+
100
+ clean_targets = self.installed_versions[(keep_versions_size + 1)..-1]
101
+ return if clean_targets.nil?
102
+
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)
106
+ end
107
+ end
72
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.10
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-06-05 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,13 +39,15 @@ 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
42
+ - lib/pbmenv/directory_object.rb
45
43
  - lib/pbmenv/helper.rb
46
44
  - lib/pbmenv/pbm.rb
47
- - lib/pbmenv/use_version_service.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
48
49
  - lib/pbmenv/version.rb
50
+ - lib/pbmenv/version_object.rb
49
51
  - lib/pbmenv/version_pathname.rb
50
52
  - pbmenv.gemspec
51
53
  homepage: https://github.com/splaplapla/pbmenv
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
@@ -1,117 +0,0 @@
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