Class: Braid::Config
Defined Under Namespace
Classes: MirrorDoesNotExist, PathAlreadyInUse, RemoveMirrorDueToBreakingChange
Constant Summary collapse
- ConfigMode =
TODO (typing): Migrate to T::Enum?
T.type_alias { Integer }
- MODE_UPGRADE =
1- MODE_READ_ONLY =
2- MODE_MAY_WRITE =
3- CURRENT_CONFIG_VERSION =
1
Instance Attribute Summary collapse
-
#breaking_change_descs ⇒ Object
readonly
Returns the value of attribute breaking_change_descs.
-
#config_existed ⇒ Object
readonly
Returns the value of attribute config_existed.
-
#config_version ⇒ Object
readonly
Returns the value of attribute config_version.
Instance Method Summary collapse
- #add(mirror) ⇒ Object
- #add_from_options(url, options) ⇒ Object
- #get(path) ⇒ Object
- #get!(path) ⇒ Object
-
#initialize(options = {}) ⇒ Config
constructor
A new instance of Config.
- #mirrors ⇒ Object
- #remove(mirror) ⇒ Object
- #update(mirror) ⇒ Object
- #write_db ⇒ Object
Methods included from T::Sig
Constructor Details
#initialize(options = {}) ⇒ Config
Returns a new instance of Config.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/braid/config.rb', line 89 def initialize( = {}) @config_file = ['config_file'] || CONFIG_FILE old_config_files = ['old_config_files'] || [OLD_CONFIG_FILE] @mode = ['mode'] || MODE_MAY_WRITE data = load_config(@config_file, old_config_files) @config_existed = !data.nil? if !@config_existed @config_version = CURRENT_CONFIG_VERSION @db = {} elsif data['config_version'].is_a?(Numeric) @config_version = data['config_version'] @db = data['mirrors'] else # Before config versioning (Braid < 1.1.0) @config_version = 0 @db = data end if @config_version > CURRENT_CONFIG_VERSION raise BraidError, "This version of Braid (\#{VERSION}) is too old to understand your project's Braid\nconfiguration file (version \#{@config_version}). See the instructions at\nhttps://cristibalan.github.io/braid/config_versions.html to install and use a\ncompatible newer version of Braid.\n" end # In all modes, instantiate all mirrors to scan for breaking changes. @breaking_change_descs = [] paths_to_delete = [] @db.each do |path, attributes| begin mirror = Mirror.new(path, attributes, lambda {|desc| @breaking_change_descs.push(desc)}) # In MODE_UPGRADE, update @db now. In other modes, we won't write the # config if an upgrade is needed, so it doesn't matter that we don't # update @db. # # It's OK to change the values of existing keys during iteration: # https://groups.google.com/d/msg/comp.lang.ruby/r5OI6UaxAAg/SVpU0cktmZEJ write_mirror(mirror) if @mode == MODE_UPGRADE rescue RemoveMirrorDueToBreakingChange # I don't know if deleting during iteration is well-defined in all # Ruby versions we support, so defer the deletion. # ~ [email protected], 2017-12-31 paths_to_delete.push(path) if @mode == MODE_UPGRADE end end paths_to_delete.each do |path| @db.delete(path) end if @mode != MODE_UPGRADE && !@breaking_change_descs.empty? raise BraidError, "This version of Braid (\#{VERSION}) no longer supports a feature used by your\nBraid configuration file (version \#{@config_version}). Run 'braid upgrade-config --dry-run'\nfor information about upgrading your configuration file, or see the instructions\nat https://cristibalan.github.io/braid/config_versions.html to install and run a\ncompatible older version of Braid.\n" end if @mode == MODE_MAY_WRITE && @config_version < CURRENT_CONFIG_VERSION raise BraidError, "This command may need to write to your Braid configuration file,\nbut this version of Braid (\#{VERSION}) cannot write to your configuration file\n(currently version \#{config_version}) without upgrading it to configuration version \#{CURRENT_CONFIG_VERSION},\nwhich would force other developers on your project to upgrade Braid. Run\n'braid upgrade-config' to proceed with the upgrade, or see the instructions at\nhttps://cristibalan.github.io/braid/config_versions.html to install and run a\ncompatible older version of Braid.\n" end end |
Instance Attribute Details
#breaking_change_descs ⇒ Object (readonly)
Returns the value of attribute breaking_change_descs.
85 86 87 |
# File 'lib/braid/config.rb', line 85 def breaking_change_descs @breaking_change_descs end |
#config_existed ⇒ Object (readonly)
Returns the value of attribute config_existed.
83 84 85 |
# File 'lib/braid/config.rb', line 83 def config_existed @config_existed end |
#config_version ⇒ Object (readonly)
Returns the value of attribute config_version.
81 82 83 |
# File 'lib/braid/config.rb', line 81 def config_version @config_version end |
Instance Method Details
#add(mirror) ⇒ Object
194 195 196 197 198 |
# File 'lib/braid/config.rb', line 194 def add(mirror) raise PathAlreadyInUse, mirror.path if get(mirror.path) write_mirror(mirror) write_db end |
#add_from_options(url, options) ⇒ Object
167 168 169 170 171 172 |
# File 'lib/braid/config.rb', line 167 def (url, ) mirror = Mirror.(url, ) add(mirror) mirror end |
#get(path) ⇒ Object
180 181 182 183 184 |
# File 'lib/braid/config.rb', line 180 def get(path) key = path.to_s.sub(/\/$/, '') attributes = @db[key] attributes ? Mirror.new(path, attributes) : nil end |
#get!(path) ⇒ Object
187 188 189 190 191 |
# File 'lib/braid/config.rb', line 187 def get!(path) mirror = get(path) raise MirrorDoesNotExist, path unless mirror mirror end |
#mirrors ⇒ Object
175 176 177 |
# File 'lib/braid/config.rb', line 175 def mirrors @db.keys end |
#remove(mirror) ⇒ Object
201 202 203 204 |
# File 'lib/braid/config.rb', line 201 def remove(mirror) @db.delete(mirror.path) write_db end |
#update(mirror) ⇒ Object
207 208 209 210 211 |
# File 'lib/braid/config.rb', line 207 def update(mirror) raise MirrorDoesNotExist, mirror.path unless get(mirror.path) write_mirror(mirror) write_db end |
#write_db ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/braid/config.rb', line 215 def write_db new_db = {} @db.keys.sort.each do |key| new_db[key] = {} Braid::Mirror::ATTRIBUTES.each do |k| new_db[key][k] = @db[key][k] if @db[key].has_key?(k) end end new_data = { 'config_version' => CURRENT_CONFIG_VERSION, 'mirrors' => new_db } File.open(@config_file, 'wb') do |f| f.write JSON.pretty_generate(new_data) f.write "\n" end end |