Module: Kdep::State

Defined in:
lib/kdep/state.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

FILENAME =
"state.yml".freeze
INITIAL_TAG =
"0.0".freeze
HEADER =
<<~HEADER
  # Last tag deployed by kdep bump -- do not edit by hand.
  # This file is safe to commit. See: https://github.com/repleadfy/kdep
HEADER

Class Method Summary collapse

Class Method Details

.exists?(deploy_dir) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/kdep/state.rb', line 39

def self.exists?(deploy_dir)
  File.exist?(File.join(deploy_dir, FILENAME))
end

.load(deploy_dir) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/kdep/state.rb', line 15

def self.load(deploy_dir)
  path = File.join(deploy_dir, FILENAME)
  return nil unless File.exist?(path)
  data = YAML.safe_load(File.read(path)) || {}
  unless data.is_a?(Hash) && data["tag"]
    raise Error, "#{path}: missing required key 'tag'"
  end
  data
end

.parseable_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/kdep/state.rb', line 43

def self.parseable_tag?(tag)
  require "kdep/version_tagger"
  !!Kdep::VersionTagger.parse(tag)
end

.tag(deploy_dir) ⇒ Object



25
26
27
28
# File 'lib/kdep/state.rb', line 25

def self.tag(deploy_dir)
  loaded = load(deploy_dir)
  loaded && loaded["tag"]
end

.write(deploy_dir, tag:) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/kdep/state.rb', line 30

def self.write(deploy_dir, tag:)
  path = File.join(deploy_dir, FILENAME)
  tmp  = "#{path}.tmp"
  content = "#{HEADER}tag: \"#{tag}\"\n"
  File.write(tmp, content)
  File.rename(tmp, path)
  path
end