Class: Jets::Code::Copy::Base

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util::Git, Util::Logging, Util::Sh
Defined in:
lib/jets/code/copy/base.rb

Direct Known Subclasses

Full, GitCopy, GitInline, Rsync

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Sh

#capture, #quiet_sh, #sh

Methods included from Util::Logging

#log

Methods included from Util::Git

#git?, #git_installed?

Class Method Details

.runObject



11
12
13
# File 'lib/jets/code/copy/base.rb', line 11

def self.run
  new.run
end

Instance Method Details

#always_keepObject

Copy always_keep files and folders like .deploy for remote runner regardless of .gitignore. Allows user to use .deploy/.env files for remote runner And have a .gitignore for .env at the project root level.



68
69
70
71
72
73
74
75
76
# File 'lib/jets/code/copy/base.rb', line 68

def always_keep
  config.code.copy.always_keep.each do |path|
    next unless File.exist?(path)
    # Remove in case not gitignore. IE: avoid creating .deploy/.deploy
    FileUtils.rm_rf("#{build_root}/stage/code/#{path}")
    FileUtils.mkdir_p("#{build_root}/stage/code")
    FileUtils.cp_r(path, "#{build_root}/stage/code/#{path}")
  end
end

#always_removeObject



78
79
80
81
82
# File 'lib/jets/code/copy/base.rb', line 78

def always_remove
  config.code.copy.always_remove.each do |path|
    FileUtils.rm_rf("#{build_root}/stage/code/#{path}")
  end
end

#extract_codeObject

Extract code-temp.zip immediately



26
27
28
# File 'lib/jets/code/copy/base.rb', line 26

def extract_code
  quiet_sh "unzip -q #{build_root}/stage/code-temp.zip -d #{build_root}/stage/code"
end

#git_infoObject



47
48
49
# File 'lib/jets/code/copy/base.rb', line 47

def git_info
  Jets::Git::Info.new
end

#gitconfigObject

Ensure gitconfig to avoid the Author identity unknown git warning.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jets/code/copy/base.rb', line 53

def gitconfig
  home = ENV["HOME"] || "/root"
  git_user = capture "git config user.name || true"
  return unless git_user.blank?

  IO.write("#{home}/.gitconfig", <<~EOL)
    [user]
      name = Nobody
      email = "[email protected]"
  EOL
end

#remove_temp_filesObject

Remove code-temp files. Not needed anymore.



31
32
33
34
# File 'lib/jets/code/copy/base.rb', line 31

def remove_temp_files
  FileUtils.rm_f "#{build_root}/stage/code-temp.zip"
  FileUtils.rm_rf "#{build_root}/stage/code-temp"
end

#runObject



15
16
17
18
19
20
21
22
23
# File 'lib/jets/code/copy/base.rb', line 15

def run
  create_temp_zip # interface method
  extract_code # Now have a working area: stage/code
  remove_temp_files
  always_keep
  always_remove
  save_gitinfo
  "#{build_root}/stage/code"
end

#save_gitinfoObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/jets/code/copy/base.rb', line 36

def save_gitinfo
  dest = "#{build_root}/stage/code/.jets/gitinfo.yml"
  FileUtils.mkdir_p(File.dirname(dest))
  if File.exist?(".jets/gitinfo.yml")
    # already copied over
    FileUtils.cp(".jets/gitinfo.yml", dest)
  else
    IO.write(dest, git_info.params.deep_stringify_keys.to_yaml)
  end
end