Class: Cult::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/cult/project.rb

Constant Summary collapse

CULT_RC =
'.cultrc'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Project

Returns a new instance of Project.



13
14
15
16
# File 'lib/cult/project.rb', line 13

def initialize(path)
  @default_ip_protocol = :ipv4
  @path = path
end

Instance Attribute Details

#cult_versionObject

Returns the value of attribute cult_version.



10
11
12
# File 'lib/cult/project.rb', line 10

def cult_version
  @cult_version
end

#default_ip_protocolObject

Returns the value of attribute default_ip_protocol.



11
12
13
# File 'lib/cult/project.rb', line 11

def default_ip_protocol
  @default_ip_protocol
end

#default_providerObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cult/project.rb', line 88

def default_provider
  @default_provider_instance ||= begin
    case @default_provider
      when Cult::Provider
        @default_provider
      when nil;
        providers.first
      else
        providers[@default_provider]
    end
  end
end

#git_integrationObject Also known as: git?

Returns the value of attribute git_integration.



129
130
131
# File 'lib/cult/project.rb', line 129

def git_integration
  @git_integration
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/cult/project.rb', line 9

def path
  @path
end

Class Method Details

.from_cwdObject



125
126
127
# File 'lib/cult/project.rb', line 125

def self.from_cwd
  locate Dir.getwd
end

.locate(path) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cult/project.rb', line 109

def self.locate(path)
  path = File.expand_path(path)
  loop do
    return nil if path == '/'

    unless File.directory?(path)
      path = File.dirname(path)
    end

    candidate = File.join(path, CULT_RC)
    return new(path) if File.exist?(candidate)
    path = File.dirname(path)
  end
end

Instance Method Details

#constructed?Boolean Also known as: exist?

Returns:

  • (Boolean)


61
62
63
# File 'lib/cult/project.rb', line 61

def constructed?
  File.exist?(cultrc)
end

#cultrcObject



24
25
26
# File 'lib/cult/project.rb', line 24

def cultrc
  location_of(CULT_RC)
end

#development?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/cult/project.rb', line 158

def development?
  env == 'development'
end

#driversObject



102
103
104
105
106
# File 'lib/cult/project.rb', line 102

def drivers
  @drivers ||= begin
    Cult::Drivers.all
  end
end

#envObject



147
148
149
150
151
152
153
154
155
# File 'lib/cult/project.rb', line 147

def env
  ENV['CULT_ENV'] || begin
    if git_branch&.match(/\bdev(el(opment)?)?\b/)
      'development'
    else
      'production'
    end
  end
end

#execute_cultrcObject



29
30
31
# File 'lib/cult/project.rb', line 29

def execute_cultrc
  load(cultrc)
end

#git_branchObject



132
133
134
135
136
137
# File 'lib/cult/project.rb', line 132

def git_branch
  res = %x(git -C #{Shellwords.escape(path)} branch --no-color)
  if res && (m = res.match(/^\* (.*)/))
    return m[1].chomp
  end
end

#git_commit_id(short: false) ⇒ Object



139
140
141
142
143
144
# File 'lib/cult/project.rb', line 139

def git_commit_id(short: false)
  short = short ? "--short" : ''
  cmd = "git -C #{Shellwords.escape(path)} rev-parse #{short} " +
        "--verify HEAD"
  %x(#{cmd}).chomp
end

#inspectObject Also known as: to_s



34
35
36
# File 'lib/cult/project.rb', line 34

def inspect
  "\#<#{self.class.name} name=#{name.inspect} path=#{path.inspect}>"
end

#location_of(file) ⇒ Object



40
41
42
# File 'lib/cult/project.rb', line 40

def location_of(file)
  File.join(path, file)
end

#nameObject



19
20
21
# File 'lib/cult/project.rb', line 19

def name
  File.basename(path)
end

#nodesObject



66
67
68
69
70
# File 'lib/cult/project.rb', line 66

def nodes
  @nodes ||= begin
    Node.all(self)
  end
end

#providersObject



80
81
82
83
84
# File 'lib/cult/project.rb', line 80

def providers
  @providers ||= begin
    Cult::Provider.all(self)
  end
end

#relative_path(obj_path) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/cult/project.rb', line 45

def relative_path(obj_path)
  prefix = "#{path}/"

  if obj_path.start_with?(prefix)
    return obj_path[prefix.length .. -1]
  end

  fail ArgumentError, "#{path} isn't in the project"
end

#remote_pathObject



56
57
58
# File 'lib/cult/project.rb', line 56

def remote_path
  "cult"
end

#rolesObject



73
74
75
76
77
# File 'lib/cult/project.rb', line 73

def roles
  @roles ||= begin
    Role.all(self)
  end
end