Class: ConfCtl::Swpins::Core

Inherits:
Object
  • Object
show all
Includes:
Utils::File
Defined in:
lib/confctl/swpins/core.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::File

#replace_symlink, #unlink_if_exists

Constructor Details

#initializeCore

Returns a new instance of Core.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/confctl/swpins/core.rb', line 29

def initialize
  @name = 'core'
  @path = File.join(ConfCtl::Swpins.core_dir, 'core.json')

  settings = ConfCtl::Settings.instance

  @channels = Swpins::ChannelList.get.select do |c|
    settings.core_swpin_channels.include?(c.name)
  end

  @nix_specs = settings.core_swpin_pins
end

Instance Attribute Details

#channelsArray<Swpins::Channel> (readonly)

Returns:



27
28
29
# File 'lib/confctl/swpins/core.rb', line 27

def channels
  @channels
end

#nameString (readonly)

Returns:

  • (String)


18
19
20
# File 'lib/confctl/swpins/core.rb', line 18

def name
  @name
end

#pathString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/confctl/swpins/core.rb', line 21

def path
  @path
end

#specsHash<String, Swpins::Specs::Base> (readonly)

Returns:



24
25
26
# File 'lib/confctl/swpins/core.rb', line 24

def specs
  @specs
end

Class Method Details

.getSwpins::Core

Returns:



7
8
9
10
11
12
13
# File 'lib/confctl/swpins/core.rb', line 7

def self.get
  return @instance if @instance

  @instance = new
  @instance.parse
  @instance
end

Instance Method Details

#parseObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/confctl/swpins/core.rb', line 42

def parse
  @specs = {}

  # Add specs from channels
  channels.each do |chan|
    chan.specs.each do |name, chan_spec|
      s = chan_spec.clone
      s.channel = chan.name
      specs[name] = s
    end
  end

  # Add core-specific specs
  @json_specs = if File.exist?(path)
                  JSON.parse(File.read(path))
                else
                  {}
                end

  nix_specs.each do |name, nix_opts|
    specs[name] = Swpins::Spec.for(nix_opts['type'].to_sym).new(
      name,
      nix_opts[nix_opts['type']],
      json_specs[name]
    )
  end
end

#pre_evaluateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/confctl/swpins/core.rb', line 100

def pre_evaluate
  nix = ConfCtl::Nix.new
  paths = nix.eval_core_swpins

  FileUtils.mkdir_p(cache_dir)

  paths.each do |pin, path|
    name = "core-swpin.#{pin}"
    link = File.join(cache_dir, name)
    replace_symlink(link, path)
    ConfCtl::GCRoot.add(name, link) unless ConfCtl::GCRoot.exist?(name)
  end
end

#pre_evaluated_store_pathsObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/confctl/swpins/core.rb', line 114

def pre_evaluated_store_paths
  swpins_path = File.join(cache_dir, 'core.swpins')
  return unless File.exist?(swpins_path)

  swpins = JSON.parse(File.read(swpins_path))

  missing_path = swpins.each_value.detect do |path|
    !Dir.exist?(path)
  end

  return if missing_path

  swpins
end

#saveObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/confctl/swpins/core.rb', line 74

def save
  custom = {}

  specs.each do |name, s|
    custom[name] = s unless s.from_channel?
  end

  if custom.empty?
    begin
      File.unlink(path)
    rescue Errno::ENOENT
      # ignore
    end
  else
    tmp = "#{path}.new"

    FileUtils.mkdir_p(ConfCtl::Swpins.core_dir)

    File.open(tmp, 'w') do |f|
      f.puts(JSON.pretty_generate(custom))
    end

    File.rename(tmp, path)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/confctl/swpins/core.rb', line 70

def valid?
  specs.values.all?(&:valid?)
end