Class: Package

Inherits:
Object
  • Object
show all
Includes:
GitCommands
Defined in:
lib/lace/package/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GitCommands

#clone_args, #clone_repo, #git_dir, #repo_modified?, #repo_valid?, #reset, #submodules?, #update_repo, #update_submodules

Constructor Details

#initialize(name, flavor = nil) ⇒ Package

Returns a new instance of Package.



10
11
12
13
14
15
16
17
# File 'lib/lace/package/package.rb', line 10

def initialize(name, flavor = nil)
  @name = name
  @path = Lace.pkgs_folder / name
  raise PackageNotInstalled, name unless @path.exist?

  @flavor = flavor
  read_facts!
end

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts.



8
9
10
# File 'lib/lace/package/package.rb', line 8

def facts
  @facts
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/lace/package/package.rb', line 8

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/lace/package/package.rb', line 8

def path
  @path
end

Instance Method Details

#activate!Object



77
78
79
80
81
82
# File 'lib/lace/package/package.rb', line 77

def activate!
  files = @facts.config_files
  files.each do |file|
    link_file file
  end
end

#after_updateObject



30
31
32
33
34
35
36
37
38
# File 'lib/lace/package/package.rb', line 30

def after_update
  return if ARGV.nohooks?

  @path.cd do
    facts.post(:update).each do |cmd|
      system cmd
    end
  end
end

#deactivate!Object



70
71
72
73
74
75
# File 'lib/lace/package/package.rb', line 70

def deactivate!
  files = @facts.config_files
  files.each do |file|
    unlink_file file
  end
end

#is_active?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lace/package/package.rb', line 52

def is_active?
  home_dir = Dir.home
  if @facts.has_flavors? && @flavor == false
    @facts.flavors.any? { |f| Package.new(@name, f).is_active? }
  else
    config_files = Set.new @facts.config_files
    config_files.all? do |p|
      dotfile = p.as_dotfile(home_dir, @path)
      dotfile.exist? and dotfile.symlink? and dotfile.readlink.dirname.to_s.include?(@path)
    end
  end
end

#is_git_repo?Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/lace/package/package.rb', line 40

def is_git_repo?
  @target_folder = @path
  repo_valid?
end

#is_modified?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/lace/package/package.rb', line 45

def is_modified?
  return false unless is_git_repo?

  @target_folder = @path
  repo_modified?
end


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lace/package/package.rb', line 93

def link_file(file)
  home_dir = Dir.home
  # if ends in erb -> generate it
  src = file
  dest = src.as_dotfile(home_dir, @path)
  if dest.exist? && dest.directory?
    raise WouldOverwriteError.new(dest, src) unless ARGV.force?

    FileUtils.mv dest, dest.as_backup
  end
  link_file_or_directory src, dest, force: ARGV.force?
end


106
107
108
109
# File 'lib/lace/package/package.rb', line 106

def link_file_or_directory(src, dest, force: ARGV.force?)
  dest.dirname.mkpath unless dest.dirname.exist?
  FileUtils.ln_s src, dest, force: force
end

#read_facts!Object



65
66
67
68
# File 'lib/lace/package/package.rb', line 65

def read_facts!
  @facts = PackageFacts.new @path
  @facts.flavor_with @flavor
end

#setupObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/lace/package/package.rb', line 19

def setup
  return if ARGV.nohooks?

  ENV['LACEPKG_PATH'] = @path
  @path.cd do
    facts.setup_files.each do |cmd|
      safe_system cmd
    end
  end
end


84
85
86
87
88
89
90
91
# File 'lib/lace/package/package.rb', line 84

def unlink_file(file, force: ARGV.force?)
  dotfile = file.as_dotfile(Dir.home, @path)
  if (dotfile.exist? || force) && dotfile.symlink? && dotfile.readlink == file
    FileUtils.rm_f(dotfile)
    return true
  end
  false
end