Class: Kmc::Versioner

Inherits:
Object
  • Object
show all
Defined in:
lib/kmc/versioner.rb

Constant Summary collapse

INIT_COMMIT_MESSAGE =
"INIT: Initialize KMC"

Class Method Summary collapse

Class Method Details

.already_installed?(path, package) ⇒ Boolean

Has this package already been installed?

Returns:

  • (Boolean)


41
42
43
# File 'lib/kmc/versioner.rb', line 41

def already_installed?(path, package)
  installed_packages(path).include?(package)
end

.init_repo(path) ⇒ Object



6
7
8
9
# File 'lib/kmc/versioner.rb', line 6

def init_repo(path)
  GitAdapter.init_repo(path)
  GitAdapter.commit_everything(path, INIT_COMMIT_MESSAGE)
end

.installed_packages(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kmc/versioner.rb', line 19

def installed_packages(path)
  commits = GitAdapter.list_commits(path)

  postinstalls = commits.select(&:post?).map(&:subject)
  uninstalls = commits.select(&:uninstall?).map(&:subject)

  # The packages that have actually been installed are those that have a
  # 'post-install' without any corresponding 'uninstall'. We can find
  # these packages by getting post-installs, and removing the first
  # instance of each package also found in uninstall.
  #
  # Note that we can't use Array#delete because that method will delete
  # *all* instances of a package, which won't work because it's possible
  # for a user to install, uninstall, then re-install a package.
  uninstalls.each do |package|
    postinstalls.delete_at(postinstalls.index(package))
  end

  postinstalls.map { |package_title| Package.find(package_title) }
end

.mark_postinstall(path, package) ⇒ Object



15
16
17
# File 'lib/kmc/versioner.rb', line 15

def mark_postinstall(path, package)
  GitAdapter.commit_everything(path, post_install_message(package))
end

.mark_preinstall(path, package) ⇒ Object



11
12
13
# File 'lib/kmc/versioner.rb', line 11

def mark_preinstall(path, package)
  GitAdapter.commit_everything(path, pre_install_message(package))
end

.uninstall_package(path, package) ⇒ Object



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

def uninstall_package(path, package)
  to_revert = GitAdapter.list_commits(path).find do |commit|
    commit.post? && commit.subject == package.title
  end

  GitAdapter.revert_commit(path, to_revert)
  Util.run_post_processors!(path)
  GitAdapter.commit_everything(path, uninstall_message(package))
end