Class: Metaverse::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/metaverse/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Base



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/metaverse/base.rb', line 11

def initialize path
  @logger = Logger.new STDOUT
  @base_path = path
  read_config "#{path}/.meta.yml"
  @repos_paths = Metaverse::Iterator.new path, @ignored_repos, @repos_paths
  if @repos_paths.empty?
    @repos_paths.build 
    save_config "#{path}/.meta.yml"
    puts "Repos cache built.".green
  end      
  @repos = @repos_paths.map {|repo| Metaverse::Repo.new repo}
end

Instance Method Details

#add_remote(name, base_url) ⇒ Object



176
177
178
179
180
181
# File 'lib/metaverse/base.rb', line 176

def add_remote name, base_url
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.add_remote name, "#{base_url}/#{repo.name}"
  }
end

#branchesObject



115
116
117
# File 'lib/metaverse/base.rb', line 115

def branches
  @repos.each { |repo| puts "#{repo.name}@#{repo.current_branch}" }
end

#check_dirtinessObject



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/metaverse/base.rb', line 128

def check_dirtiness
  dirty_repos = @repos.reject { |repo|
    !repo.dirty?
  }.map &:name

  if dirty_repos.length == 0
    puts 'The system is clean'.green
  else
    puts 'The system is dirty. Please check the following repos :'.red , dirty_repos
  end
  dirty_repos.length == 0 
end

#checkout(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/metaverse/base.rb', line 29

def checkout name
  action_options = -> (repo) {
    {
      up: -> {
        puts "\n # #{repo.name}".blue
        repo.checkout name
      },
      down: -> {
        puts "\n # Rolling back in #{repo.name}".blue
        repo.checkout repo.pop_previous_branch
      }
    }
  }

  if check_dirtiness
    actions = @repos.map {|repo| 
      puts "\n # #{repo.name}".blue
      Transacted::Action.new action_options.call(repo)
    }

    checkout_transaction = Transacted::Transaction.new actions
    case checkout_transaction.execute
    when :execution_success then puts "Checkout successful".green
    when :rollback_success then puts "An error prevented checking out the system. Rolled back successfully".yellow
    when :rollback_failure then puts "An error was encountered during the checkout. In addition, an error happened while trying to rollback the system. Please fix the state of your system manually.".red
    end
  end
    
end

#clear_reposObject



163
164
165
166
167
# File 'lib/metaverse/base.rb', line 163

def clear_repos
  @repos_paths = []
  save_config "#{@base_path}/.meta.yml"
  puts "Repos cache cleared.".green
end

#create_state(prefix, state) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/metaverse/base.rb', line 59

def create_state prefix, state
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.create_state prefix, state
    repo.checkout "#{prefix}/#{state}" if not prefix == "snapshot"
  } if check_dirtiness
end

#exec(env, command) ⇒ Object



183
184
185
186
187
188
# File 'lib/metaverse/base.rb', line 183

def exec env, command
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.exec env, command
  }
end

#load_state(prefix, state, remote = nil, should_create_branch = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/metaverse/base.rb', line 68

def load_state prefix, state, remote = nil, should_create_branch = false
  action_options = -> (repo) {
    {
      up: -> { 
        puts "\n # #{repo.name}".blue
        repo.load_state prefix, state, remote, should_create_branch
      },
      down: -> { 
        puts "\n # Rolling back in #{repo.name}".blue
        puts repo.checkout repo.pop_previous_branch
      }
    }
  }

  if check_dirtiness
    
    actions = @repos.map { |repo|
      Transacted::Action.new action_options.call repo
    }

    load_state_transaction = Transacted::Transaction.new actions
    case load_state_transaction.execute
    when :execution_success then puts "Loading state successful".green
    when :rollback_success then puts "An error prevented loading the state of the system. Rolled back successfully".yellow
    when :rollback_failure then puts "An error was encountered during the loading of the state. In addition, an error happened while trying to rollback the system. Please fix the state of your system manually.".red
    end
  end
end

#pullObject



169
170
171
172
173
174
# File 'lib/metaverse/base.rb', line 169

def pull
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.pull @origin_remote
  } if check_dirtiness
end

#read_config(path) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/metaverse/base.rb', line 141

def read_config path
  Errors::config_not_found! if not File.exist? path

  config = YAML.load File.open(path)
  @repos_paths = config['repos'] || []
  @origin_remote = config['remotes']['main']
  @own_remote = config['remotes']['own']
  @ignored_repos = config['ignore'] || []
end

#save_config(path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/metaverse/base.rb', line 151

def save_config path
  config = {
    'repos' => @repos_paths.map{ |repo| repo },
    'remotes'=> {
      'main' => @origin_remote,
      'own' => @own_remote
    },
    'ignore' => @ignored_repos
  }
  File.open(path, 'w') {|f| f.write config.to_yaml }
end

#send_state(prefix, state, remote, should_clean = false) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/metaverse/base.rb', line 98

def send_state prefix, state, remote, should_clean = false
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    is_branch = repo.current_branch.match /refs\/heads\/(.*)/
    repo.send_state prefix, state, remote, !!is_branch, should_clean
  } if check_dirtiness
end

#statusObject



24
25
26
# File 'lib/metaverse/base.rb', line 24

def status
  check_dirtiness
end

#system_stateObject



119
120
121
122
123
124
125
126
# File 'lib/metaverse/base.rb', line 119

def system_state
  branches = Hash.new 0
  @repos.each {|repo| branches[repo.current_branch] += 1}

  state = branches.max{|a,b| a[1] <=> b[1]}
  state[0] = state.first.strip
  state
end

#update(remote = nil) ⇒ Object



107
108
109
110
111
112
# File 'lib/metaverse/base.rb', line 107

def update remote = nil
  @repos.each { |repo|
    puts "\n # #{repo.name}".blue
    repo.update remote
  } if check_dirtiness
end