Upgrade to 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / lib / mruby / lockfile.rb
1 autoload :YAML, 'yaml'
2
3 module MRuby
4   autoload :Source, 'mruby/source'
5
6   class Lockfile
7     class << self
8       def enable
9         @enabled = true
10       end
11
12       def disable
13         @enabled = false
14       end
15
16       def enabled?
17         @enabled
18       end
19
20       def build(target_name)
21         instance.build(target_name)
22       end
23
24       def write
25         instance.write if enabled?
26       end
27
28       def instance
29         @instance ||= new("#{MRUBY_CONFIG}.lock")
30       end
31     end
32
33     def initialize(filename)
34       @filename = filename
35     end
36
37     def build(target_name)
38       read[target_name] ||= {}
39     end
40
41     def write
42       locks = {"mruby_version" => mruby}
43       locks["builds"] = @builds if @builds
44       File.write(@filename, YAML.dump(locks))
45     end
46
47     private
48
49     def read
50       @builds ||= if File.exist?(@filename)
51                     YAML.load_file(@filename)["builds"] || {}
52                   else
53                     {}
54                   end
55     end
56
57     def shellquote(s)
58       if ENV['OS'] == 'Windows_NT'
59         "\"#{s}\""
60       else
61         "'#{s}'"
62       end
63     end
64
65     def mruby
66       mruby = {
67         'version' => MRuby::Source::MRUBY_VERSION,
68         'release_no' => MRuby::Source::MRUBY_RELEASE_NO,
69       }
70
71       git_dir = "#{MRUBY_ROOT}/.git"
72       if File.directory?(git_dir)
73         mruby['git_commit'] = `git --git-dir #{shellquote(git_dir)} --work-tree #{shellquote(MRUBY_ROOT)} rev-parse --verify HEAD`.strip
74       end
75
76       mruby
77     end
78
79     enable
80   end
81 end