20259: Add documentation for banner and tooltip features
[arvados.git] / apps / workbench / lib / app_version.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 # If you change this file, you'll probably also want to make the same
6 # changes in services/api/lib/app_version.rb.
7
8 class AppVersion
9   def self.git(*args, &block)
10     IO.popen(["git", "--git-dir", ".git"] + args, "r",
11              chdir: Rails.root.join('../..'),
12              err: "/dev/null",
13              &block)
14   end
15
16   def self.forget
17     @hash = nil
18     @package_version = nil
19   end
20
21   # Return abbrev commit hash for current code version: "abc1234", or
22   # "abc1234-modified" if there are uncommitted changes. If present,
23   # return contents of {root}/git-commit.version instead.
24   def self.hash
25     if (cached = Rails.configuration.source_version || @hash)
26       return cached
27     end
28
29     # Read the version from our package's git-commit.version file, if available.
30     begin
31       @hash = IO.read(Rails.root.join("git-commit.version")).strip
32     rescue Errno::ENOENT
33     end
34
35     if @hash.nil? or @hash.empty?
36       begin
37         local_modified = false
38         git("status", "--porcelain") do |git_pipe|
39           git_pipe.each_line do |_|
40             STDERR.puts _
41             local_modified = true
42             # Continue reading the pipe so git doesn't get SIGPIPE.
43           end
44         end
45         if $?.success?
46           git("log", "-n1", "--format=%H") do |git_pipe|
47             git_pipe.each_line do |line|
48               @hash = line.chomp[0...8] + (local_modified ? '-modified' : '')
49             end
50           end
51         end
52       rescue SystemCallError
53       end
54     end
55
56     @hash || "unknown"
57   end
58
59   def self.package_version
60     if (cached = Rails.configuration.package_version || @package_version)
61       return cached
62     end
63
64     begin
65       @package_version = IO.read(Rails.root.join("package-build.version")).strip
66     rescue Errno::ENOENT
67       @package_version = "unknown"
68     end
69
70     @package_version
71   end
72 end