Class: Hippo::WorkingDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/working_directory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = FileUtils.pwd) ⇒ WorkingDirectory

Returns a new instance of WorkingDirectory.



11
12
13
# File 'lib/hippo/working_directory.rb', line 11

def initialize(root = FileUtils.pwd)
  @root = root
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/hippo/working_directory.rb', line 9

def root
  @root
end

Instance Method Details

#can_update?Boolean

Can this working directory be updated?

Returns:

  • (Boolean)


163
164
165
# File 'lib/hippo/working_directory.rb', line 163

def can_update?
  source_type == 'remote'
end

#config_pathString

Return the path to the config file in this working directory

Returns:

  • (String)


18
19
20
# File 'lib/hippo/working_directory.rb', line 18

def config_path
  File.join(@root, 'manifest.yaml')
end

#last_updated_atTime?

Return the time this manifest was last updated

Returns:

  • (Time, nil)


145
146
147
148
149
150
# File 'lib/hippo/working_directory.rb', line 145

def last_updated_at
  if File.file?(update_timestamp_path)
    timestamp = File.read(update_timestamp_path)
    Time.at(timestamp.strip.to_i)
  end
end

#local_config_pathString

Return the path to the local config file

Returns:

  • (String)


25
26
27
# File 'lib/hippo/working_directory.rb', line 25

def local_config_path
  File.join(@root, 'manifest.local.yaml')
end

#manifest(update: true) ⇒ Hippo::Manifest

Return the manifest objet for this working directory

Returns:

Raises:



49
50
51
52
53
54
55
56
57
58
# File 'lib/hippo/working_directory.rb', line 49

def manifest(update: true)
  if update && !@updated_manifest
    update_from_remote if can_update?
    @updated_manifest = true
  end

  raise Error, 'No manifest path could be determined' if manifest_path.nil?

  @manifest ||= Manifest.load_from_file(File.join(manifest_path, 'Hippofile'))
end

#manifest_pathString

Return the path to the manifest directory

Returns:

  • (String)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hippo/working_directory.rb', line 63

def manifest_path
  case source_type
  when 'local'
    options.dig('source', 'localOptions', 'path')
  when 'remote'
    path = [remote_path] if remote_path
    File.join(remote_root_path, *path)
  else
    raise Error, "Invalid source.type ('#{source_type}')"
  end
end

#optionsHash

Return all the options configured in this working directory

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hippo/working_directory.rb', line 32

def options
  return @options if @options

  if File.file?(config_path)
    @options = YAML.load_file(config_path)
    if File.file?(local_config_path)
      @options = @options.deep_merge(YAML.load_file(local_config_path))
    end
    @options
  else
    raise Error, "No manifest config file found at #{config_path}"
  end
end

#remote_branchString

Return the branch to use from the remote repository

Returns:

  • (String)


94
95
96
# File 'lib/hippo/working_directory.rb', line 94

def remote_branch
  options.dig('source', 'remoteOptions', 'branch') || 'master'
end

#remote_pathString

Return the path within the remote repository that we wish to work with.

Returns:

  • (String)


109
110
111
# File 'lib/hippo/working_directory.rb', line 109

def remote_path
  options.dig('source', 'remoteOptions', 'path')
end

#remote_repositoryString

Return the URL to the remote repository

Returns:

  • (String)


101
102
103
# File 'lib/hippo/working_directory.rb', line 101

def remote_repository
  options.dig('source', 'remoteOptions', 'repository')
end

#remote_root_pathString

Return the path on the local filesystem that the remote repository should be stored in.

Returns:

  • (String)


86
87
88
89
# File 'lib/hippo/working_directory.rb', line 86

def remote_root_path
  repo_ref = Digest::SHA1.hexdigest([remote_repository, remote_branch].join('---'))
  File.join(Hippo.tmp_root, 'manifests', repo_ref)
end

#source_typeString

Return the type of manifest

Returns:

  • (String)


78
79
80
# File 'lib/hippo/working_directory.rb', line 78

def source_type
  options.dig('source', 'type')
end

#stagesHash<Symbol, Hippo::Stage>

Load all stages that are available in this working directory

Returns:



170
171
172
173
174
175
176
177
178
# File 'lib/hippo/working_directory.rb', line 170

def stages
  objects = Util.load_objects_from_path(File.join(@root, '*', 'config.{yml,yaml}'))
  objects.each_with_object({}) do |(path, objects), hash|
    objects.each do |obj|
      stage = Stage.new(self, File.dirname(path), obj)
      hash[stage.name] = stage
    end
  end
end

#update_from_remote(verbose: false) ⇒ Boolean

Update the local cached copy of the manifest from the remote

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/hippo/working_directory.rb', line 116

def update_from_remote(verbose: false)
  return false unless source_type == 'remote'

  Util.action "Updating manifest from #{remote_repository}..." do
    if File.directory?(remote_root_path)
      Util.system("git -C #{remote_root_path} fetch")
    else
      FileUtils.mkdir_p(File.dirname(remote_root_path))
      Util.system("git clone #{remote_repository} #{remote_root_path}")
    end

    Util.system("git -C #{remote_root_path} checkout origin/#{remote_branch}")
    File.open(update_timestamp_path, 'w') { |f| f.write(Time.now.to_i.to_s + "\n") }
  end

  if verbose
    puts
    puts "  Repository....: \e[33m#{wd.remote_repository}\e[0m"
    puts "  Branch........: \e[33m#{wd.remote_branch}\e[0m"
    puts "  Path..........: \e[33m#{wd.remote_path}\e[0m"
    puts
  end

  true
end

#update_timestamp_pathString

Return the path to the file where the last updated timestamp is stored

Returns:

  • (String)


156
157
158
# File 'lib/hippo/working_directory.rb', line 156

def update_timestamp_path
  File.join(remote_root_path + '.uptime-timestamp')
end