Class: Hippo::WorkingDirectory
- Inherits:
-
Object
- Object
- Hippo::WorkingDirectory
- Defined in:
- lib/hippo/working_directory.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#can_update? ⇒ Boolean
Can this working directory be updated?.
-
#config_path ⇒ String
Return the path to the config file in this working directory.
-
#initialize(root = FileUtils.pwd) ⇒ WorkingDirectory
constructor
A new instance of WorkingDirectory.
-
#last_updated_at ⇒ Time?
Return the time this manifest was last updated.
-
#local_config_path ⇒ String
Return the path to the local config file.
-
#manifest(update: true) ⇒ Hippo::Manifest
Return the manifest objet for this working directory.
-
#manifest_path ⇒ String
Return the path to the manifest directory.
-
#options ⇒ Hash
Return all the options configured in this working directory.
-
#remote_branch ⇒ String
Return the branch to use from the remote repository.
-
#remote_path ⇒ String
Return the path within the remote repository that we wish to work with.
-
#remote_repository ⇒ String
Return the URL to the remote repository.
-
#remote_root_path ⇒ String
Return the path on the local filesystem that the remote repository should be stored in.
-
#source_type ⇒ String
Return the type of manifest.
-
#stages ⇒ Hash<Symbol, Hippo::Stage>
Load all stages that are available in this working directory.
-
#update_from_remote(verbose: false) ⇒ Boolean
Update the local cached copy of the manifest from the remote.
-
#update_timestamp_path ⇒ String
Return the path to the file where the last updated timestamp is stored.
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
#root ⇒ Object (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?
163 164 165 |
# File 'lib/hippo/working_directory.rb', line 163 def can_update? source_type == 'remote' end |
#config_path ⇒ String
Return the path to the config file in this working directory
18 19 20 |
# File 'lib/hippo/working_directory.rb', line 18 def config_path File.join(@root, 'manifest.yaml') end |
#last_updated_at ⇒ Time?
Return the time this manifest was last updated
145 146 147 148 149 150 |
# File 'lib/hippo/working_directory.rb', line 145 def last_updated_at if File.file?() = File.read() Time.at(.strip.to_i) end end |
#local_config_path ⇒ String
Return the path to the local config file
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
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_path ⇒ String
Return the path to the manifest directory
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' .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 |
#options ⇒ Hash
Return all the options configured in this working directory
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hippo/working_directory.rb', line 32 def 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_branch ⇒ String
Return the branch to use from the remote repository
94 95 96 |
# File 'lib/hippo/working_directory.rb', line 94 def remote_branch .dig('source', 'remoteOptions', 'branch') || 'master' end |
#remote_path ⇒ String
Return the path within the remote repository that we wish to work with.
109 110 111 |
# File 'lib/hippo/working_directory.rb', line 109 def remote_path .dig('source', 'remoteOptions', 'path') end |
#remote_repository ⇒ String
Return the URL to the remote repository
101 102 103 |
# File 'lib/hippo/working_directory.rb', line 101 def remote_repository .dig('source', 'remoteOptions', 'repository') end |
#remote_root_path ⇒ String
Return the path on the local filesystem that the remote repository should be stored in.
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_type ⇒ String
Return the type of manifest
78 79 80 |
# File 'lib/hippo/working_directory.rb', line 78 def source_type .dig('source', 'type') end |
#stages ⇒ Hash<Symbol, Hippo::Stage>
Load all stages that are available in this working directory
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
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(, '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_path ⇒ String
Return the path to the file where the last updated timestamp is stored
156 157 158 |
# File 'lib/hippo/working_directory.rb', line 156 def File.join(remote_root_path + '.uptime-timestamp') end |