Module: Cielli::Utils
Instance Method Summary collapse
- #async_reader_thread_for(io, accum) ⇒ Object
- #debug(arg) ⇒ Object
- #debug!(arg) ⇒ Object
- #debug? ⇒ Boolean
- #deepcopy(object) ⇒ Object
- #esc(*args) ⇒ Object
- #extract_options(args) ⇒ Object
- #extract_options!(args) ⇒ Object
- #filelist(*args, &block) ⇒ Object
- #indent(arg, *args) ⇒ Object
- #noop ⇒ Object (also: #noop?)
- #realpath(path) ⇒ Object
- #slug_for(*args, &block) ⇒ Object
- #symbolize_keys(hash) ⇒ Object
- #symbolize_keys!(hash) ⇒ Object
- #sys(*args, &block) ⇒ Object
- #sys!(*args, &block) ⇒ Object
- #tmpfile(*args, &block) ⇒ Object
- #tmpname(*args) ⇒ Object
- #unindent(arg) ⇒ Object
- #uuid ⇒ Object
Instance Method Details
#async_reader_thread_for(io, accum) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/cielli/utils.rb', line 218 def async_reader_thread_for(io, accum) Thread.new(io, accum) do |i, a| Thread.current.abort_on_exception = true while true buf = i.read(8192) if buf a << buf else break end end end end |
#debug(arg) ⇒ Object
124 125 126 |
# File 'lib/cielli/utils.rb', line 124 def debug(arg) debug!(arg) if debug? end |
#debug!(arg) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/cielli/utils.rb', line 116 def debug!(arg) if arg.is_a?(String) warn "[DEBUG] #{ arg }" else warn "[DEBUG] >\n#{ arg.to_yaml rescue arg.pretty_inspect }" end end |
#debug? ⇒ Boolean
128 129 130 |
# File 'lib/cielli/utils.rb', line 128 def debug? ENV['CIELLI_DEBUG'] || ENV['DEBUG'] end |
#deepcopy(object) ⇒ Object
112 113 114 |
# File 'lib/cielli/utils.rb', line 112 def deepcopy(object) Marshal.load(Marshal.dump(object)) end |
#esc(*args) ⇒ Object
25 26 27 |
# File 'lib/cielli/utils.rb', line 25 def esc(*args) args.flatten.compact.map{|arg| Shellwords.escape(arg)}.join(' ') end |
#extract_options(args) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/cielli/utils.rb', line 84 def (args) opts = (args) args.push(opts) opts end |
#extract_options!(args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cielli/utils.rb', line 72 def (args) unless args.is_a?(Array) args = [args] end opts = args.last.is_a?(Hash) ? args.pop : {} symbolize_keys!(opts) return opts end |
#filelist(*args, &block) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/cielli/utils.rb', line 238 def filelist(*args, &block) accum = (block || proc{ Set.new }).call raise ArgumentError.new('accum.class != Set') unless accum.is_a?(Set) _ = args.last.is_a?(Hash) ? args.pop : {} entries = args.flatten.compact.map{|arg| realpath("#{ arg }")}.uniq.sort entries.each do |entry| case when test(?f, entry) file = realpath(entry) accum << file when test(?d, entry) glob = File.join(entry, '**/**') Dir.glob(glob) do |_entry| case when test(?f, _entry) filelist(_entry){ accum } when test(?d, entry) filelist(_entry){ accum } end end end end accum.to_a end |
#indent(arg, *args) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cielli/utils.rb', line 14 def indent(arg, *args) opts = (args) n = (args.shift || opts[:n] || 2).to_i string = unindent(arg) indentation = ' ' * n string.gsub(/^/, indentation) end |
#noop ⇒ Object Also known as: noop?
132 133 134 |
# File 'lib/cielli/utils.rb', line 132 def noop ENV['CIELLI_NOOP'] || ENV['NOOP'] end |
#realpath(path) ⇒ Object
234 235 236 |
# File 'lib/cielli/utils.rb', line 234 def realpath(path) Pathname.new(path.to_s)..realpath.to_s end |
#slug_for(*args, &block) ⇒ Object
271 272 273 |
# File 'lib/cielli/utils.rb', line 271 def slug_for(*args, &block) Slug.for(*args, &block) end |
#symbolize_keys(hash) ⇒ Object
108 109 110 |
# File 'lib/cielli/utils.rb', line 108 def symbolize_keys(hash) symbolize_keys!(deepcopy(hash)) end |
#symbolize_keys!(hash) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cielli/utils.rb', line 92 def symbolize_keys!(hash) hash.keys.each do |key| if key.is_a?(String) val = hash.delete(key) if val.is_a?(Hash) symbolize_keys!(val) end hash[key.to_s.gsub('-', '_').to_sym] = val end end return hash end |
#sys(*args, &block) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cielli/utils.rb', line 205 def sys(*args, &block) opts = (args) opts[:quiet] = true args.push(opts) begin sys!(*args, &block) rescue Object false end end |
#sys!(*args, &block) ⇒ Object
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/cielli/utils.rb', line 137 def sys!(*args, &block) opts = (args) cmd = args debug(:cmd => cmd) open3 = ( block || opts[:stdin] || opts[:quiet] || opts[:capture] ) die = proc do |command, *args| status = args.shift || $? warn("#{ [command].join(' ') } #=> status=#{ status.exitstatus }") unless opts[:quiet] exit(1) end if(open3) stdin = opts[:stdin] stdout = '' stderr = '' status = nil begin Open3.popen3(*cmd) do |i, o, e, t| ot = async_reader_thread_for(o, stdout) et = async_reader_thread_for(e, stderr) i.write(stdin) if stdin i.close ot.join et.join status = t.value end rescue die[cmd] end if status.exitstatus == 0 result = nil if opts[:capture] result = stdout.to_s.strip else if block result = block.call(status, stdout, stderr) else result = [status, stdout, stderr] end end return(result) else die[cmd, status] end else env = opts[:env] || {} argv = [env, *cmd] system(*argv) || die[cmd] return true end end |
#tmpfile(*args, &block) ⇒ Object
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 69 70 |
# File 'lib/cielli/utils.rb', line 43 def tmpfile(*args, &block) opts = (args) path = tmpname(opts) tmp = open(path, 'w+') tmp.binmode tmp.sync = true unless args.empty? src = args.join tmp.write(src) tmp.flush tmp.rewind end if block begin block.call(tmp) ensure FileUtilss.rm_rf(path) end else at_exit{ Kernel.system("rm -rf #{ esc(path) }") } return tmp end end |
#tmpname(*args) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/cielli/utils.rb', line 33 def tmpname(*args) opts = (*args) base = opts.fetch(:base){ uuid }.to_s.strip ext = opts.fetch(:ext){ 'tmp' }.to_s.strip.sub(/^[.]+/, '') basename = opts.fetch(:basename){ "#{ base }.#{ ext }" } File.join(Dir.tmpdir, basename) end |
#unindent(arg) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/cielli/utils.rb', line 3 def unindent(arg) string = arg.to_s.dup margin = nil string.each_line do |line| next if line =~ %r/^\s*$/ margin = line[%r/^\s*/] and break end string.gsub!(%r/^#{ margin }/, "") if margin margin ? string : nil end |
#uuid ⇒ Object
29 30 31 |
# File 'lib/cielli/utils.rb', line 29 def uuid SecureRandom.uuid end |