Class: Embulk::Migrator
- Inherits:
-
Object
- Object
- Embulk::Migrator
- Defined in:
- lib/embulk/command/embulk_migrate_plugin.rb
Instance Attribute Summary collapse
-
#new_data ⇒ Object
readonly
Returns the value of attribute new_data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #data(file) ⇒ Object
-
#initialize(path) ⇒ Migrator
constructor
A new instance of Migrator.
- #insert_line(glob, pattern, text: nil) ⇒ Object
- #match(glob, pattern) ⇒ Object
- #modified_files ⇒ Object
- #replace(glob, pattern, text = nil) ⇒ Object
- #write(file, data) ⇒ Object
Constructor Details
#initialize(path) ⇒ Migrator
Returns a new instance of Migrator.
147 148 149 150 151 152 153 154 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 147 def initialize(path) @path = path @modified_files = {} require 'fileutils' require 'embulk/data/package_data' @new_data = PackageData.new("new", path) end |
Instance Attribute Details
#new_data ⇒ Object (readonly)
Returns the value of attribute new_data.
157 158 159 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 157 def new_data @new_data end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
156 157 158 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 156 def path @path end |
Instance Method Details
#data(file) ⇒ Object
210 211 212 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 210 def data(file) read(File.join(@path, file)) end |
#insert_line(glob, pattern, text: nil) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 193 def insert_line(glob, pattern, text: nil) ms = Dir[File.join(@path, glob)].map do |file| data = read(file) if m = data.match(pattern) ln = m.pre_match.split("\n").count replace = text || yield(m) lines = data.split("\n", -1) # preserve the last empty line lines.insert(ln + 1, replace) data = lines.join("\n") modify(file, data) m end end.compact return nil if ms.empty? ms end |
#match(glob, pattern) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 163 def match(glob, pattern) ms = Dir[File.join(@path, glob)].map do |file| read(file).match(pattern) end.compact return nil if ms.empty? ms end |
#modified_files ⇒ Object
159 160 161 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 159 def modified_files @modified_files.keys end |
#replace(glob, pattern, text = nil) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 171 def replace(glob, pattern, text=nil) ms = Dir[File.join(@path, glob)].map do |file| data = read(file) first = nil pos = 0 while pos < data.length m = data.match(pattern, pos) break unless m first ||= m replace = text || yield(m) data = m.pre_match + data[m.begin(0)..(m.begin(1)-1)] + replace + data[m.end(1)..(m.end(0)-1)] + m.post_match pos = m.begin(1) + replace.length + (m.end(0) - m.end(1)) end if first modify(file, data) end first end.compact return nil if ms.empty? ms end |
#write(file, data) ⇒ Object
214 215 216 217 218 |
# File 'lib/embulk/command/embulk_migrate_plugin.rb', line 214 def write(file, data) dst = File.join(@path, file) FileUtils.mkdir_p File.dirname(dst) modify(dst, data) end |