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(config_file: CONFIG_FILE, old_config_files: [OLD_CONFIG_FILE], mode: MODE_MAY_WRITE) ⇒ 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(config_file: CONFIG_FILE, old_config_files: [OLD_CONFIG_FILE], mode: MODE_MAY_WRITE) ⇒ Config
Returns a new instance of Config.
88 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 165 166 167 168 169 170 171 172 173 |
# File 'lib/braid/config.rb', line 88 def initialize(config_file: CONFIG_FILE, old_config_files: [OLD_CONFIG_FILE], mode: MODE_MAY_WRITE) @config_file = config_file old_config_files = old_config_files @mode = mode data = load_config(@config_file, old_config_files) @config_existed = T.let(!data.nil?, T::Boolean) if !@config_existed @config_version = T.let(CURRENT_CONFIG_VERSION, Integer) @db = T.let({}, T::Hash[String, Mirror::MirrorAttributes]) elsif data['config_version'].is_a?(Numeric) @config_version = data['config_version'] # WARNING (typing): In general, slapping a type annotation on the loaded # data without validating its structure could be misleading as we work # on the rest of the code. In the worst case, it might lead us to # delete useful sanity checks because they trigger Sorbet unreachable # code errors. As of this writing (2024-08-04), we have nothing to lose # because we don't attempt to defend against malformed data anyway, and # there's a benefit to having the type information available. If we add # any validation, consider whether the annotations should be changed to # verify that we did the validation correctly, e.g., by starting with a # type like `T.anything` instead of `T.untyped`. @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, <<-MSG This version of Braid (#{VERSION}) is too old to understand your project's Braid configuration file (version #{@config_version}). See the instructions at https://cristibalan.github.io/braid/config_versions.html to install and use a compatible newer version of Braid. MSG end # In all modes, instantiate all mirrors to scan for breaking changes. @breaking_change_descs = T.let([], T::Array[String]) 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, <<-MSG This version of Braid (#{VERSION}) no longer supports a feature used by your Braid configuration file (version #{@config_version}). Run 'braid upgrade-config --dry-run' for information about upgrading your configuration file, or see the instructions at https://cristibalan.github.io/braid/config_versions.html to install and run a compatible older version of Braid. MSG end if @mode == MODE_MAY_WRITE && @config_version < CURRENT_CONFIG_VERSION raise BraidError, <<-MSG This command may need to write to your Braid configuration file, but this version of Braid (#{VERSION}) cannot write to your configuration file (currently version #{config_version}) without upgrading it to configuration version #{CURRENT_CONFIG_VERSION}, which would force other developers on your project to upgrade Braid. Run 'braid upgrade-config' to proceed with the upgrade, or see the instructions at https://cristibalan.github.io/braid/config_versions.html to install and run a compatible older version of Braid. MSG 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
203 204 205 206 207 |
# File 'lib/braid/config.rb', line 203 def add(mirror) raise PathAlreadyInUse, mirror.path if get(mirror.path) write_mirror(mirror) write_db end |
#add_from_options(url, options) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/braid/config.rb', line 176 def (url, ) mirror = Mirror.(url, ) add(mirror) mirror end |
#get(path) ⇒ Object
189 190 191 192 193 |
# File 'lib/braid/config.rb', line 189 def get(path) key = path.to_s.sub(/\/$/, '') attributes = @db[key] attributes ? Mirror.new(path, attributes) : nil end |
#get!(path) ⇒ Object
196 197 198 199 200 |
# File 'lib/braid/config.rb', line 196 def get!(path) mirror = get(path) raise MirrorDoesNotExist, path unless mirror mirror end |
#mirrors ⇒ Object
184 185 186 |
# File 'lib/braid/config.rb', line 184 def mirrors @db.keys end |
#remove(mirror) ⇒ Object
210 211 212 213 |
# File 'lib/braid/config.rb', line 210 def remove(mirror) @db.delete(mirror.path) write_db end |
#update(mirror) ⇒ Object
216 217 218 219 220 |
# File 'lib/braid/config.rb', line 216 def update(mirror) raise MirrorDoesNotExist, mirror.path unless get(mirror.path) write_mirror(mirror) write_db end |
#write_db ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/braid/config.rb', line 224 def write_db new_db = T.let({}, T::Hash[String, Mirror::MirrorAttributes]) @db.keys.sort.each do |key| attrs = T.must(@db[key]) new_db[key] = new_attrs = {} Braid::Mirror::ATTRIBUTES.each do |k| new_attrs[k] = attrs[k] if attrs.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 |