Class: Keyman::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/keyman/manifest.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Manifest

Initialize a new manifest with the current directory



26
27
28
# File 'lib/keyman/manifest.rb', line 26

def initialize(directory)
  @directory = directory
end

Class Method Details

.load(directory = Keyman.manifest_dir) ⇒ Object

Loads a manifest directory as live



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/keyman/manifest.rb', line 5

def self.load(directory = Keyman.manifest_dir)
  Keyman.users          = []
  Keyman.groups         = []
  Keyman.servers        = []
  Keyman.server_groups  = []
  manifest       = self.new(directory)
  if File.directory?(directory)
    [File.join(directory, 'users.km'), Dir[File.join(directory, '*.km')]].flatten.uniq.compact.each do |file|
      if File.exist?(file)
        manifest.instance_eval(File.read(file))
      else
        raise Error, "No '#{file}' was found in your manifest directory. Abandoning..."
      end
    end
    Keyman.manifest = manifest
  else
    raise Error, "No folder found at '#{directory}'"
  end
end

Instance Method Details

#clean?Boolean

Is the current repo clean?

Returns:

  • (Boolean)


56
57
58
# File 'lib/keyman/manifest.rb', line 56

def clean?
  `cd #{@directory} && git status`.chomp.include?('nothing to commit')
end

#group(name, &users_block) ⇒ Object

Adds a new group



31
32
33
# File 'lib/keyman/manifest.rb', line 31

def group(name, &users_block)
  Group.add(name, &users_block)
end

#latest_commit?Boolean

Does the latest commit on the current branch match the remote brandh

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/keyman/manifest.rb', line 61

def latest_commit?
  local = `cd #{@directory} && git log --pretty=%H -n 1`.chomp
  if `cd #{@directory} && git status`.chomp.match(/On branch (.*)\n/)
    branch = $1
  else
    raise Error, "Unable to determine the local repository branch."
  end
  remote = `cd #{@directory} && git ls-remote 2> /dev/null | grep refs/heads/#{branch} `.chomp.split(/\s+/).first
  if local.length == 40 && remote.length == 40
    local == remote
  else
    raise Error, "Unable to determine local & remote commits"
  end
end

#server(&block) ⇒ Object

Adds a new server



36
37
38
# File 'lib/keyman/manifest.rb', line 36

def server(&block)
  Server.add(&block)
end

#server_group(name, &block) ⇒ Object

Adds a new server group



46
47
48
# File 'lib/keyman/manifest.rb', line 46

def server_group(name, &block)
  ServerGroup.add(name, &block)
end

#user(username, key) ⇒ Object

Adds a new user



41
42
43
# File 'lib/keyman/manifest.rb', line 41

def user(username, key)
  User.add(username, key)
end

#uses_git?Boolean

Does this manifest directory use git?

Returns:

  • (Boolean)


51
52
53
# File 'lib/keyman/manifest.rb', line 51

def uses_git?
  File.directory?(File.join(@directory, '.git'))
end