Module: Cleon::BrandedFile

Defined in:
lib/cleon/branded.rb

Overview

Branded file is a file with Dogen banner at the beginning of the fiele

Constant Summary collapse

<<~EOF
  # This source file was generated by Cleon
  # see: https://github.com/nvoynov/cleon
  # MD5: %s
  #

EOF

Instance Method Summary collapse

Instance Method Details

#file_branded?(name) ⇒ Boolean

tests if file is branded

Returns:

  • (Boolean)

15
16
17
18
19
20
# File 'lib/cleon/branded.rb', line 15

def file_branded?(name)
  bann, _ = read_branded(name)
  chk0, chk1 = BANNER.lines.then{|l| [l[0], l[1]]}
  ban0, ban1 = bann.lines.then{|l| [l[0], l[1]]}
  (ban0 == chk0) && (ban1 == chk1)
end

#file_changed?(name) ⇒ Boolean

test if branded content changed

Returns:

  • (Boolean)

23
24
25
26
27
# File 'lib/cleon/branded.rb', line 23

def file_changed?(name)
  bann, body = read_branded(name)
  md5 = bann.lines[2].match(%r{MD5: (.*)\Z})[1]
  md5 != md5(body)
end

#md5(body) ⇒ Object


37
38
39
# File 'lib/cleon/branded.rb', line 37

def md5(body)
  Digest::MD5.hexdigest(body)
end

#read_branded(name) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/cleon/branded.rb', line 29

def read_branded(name)
  lines = File.read(name).lines
  banner_size = BANNER.lines.size - 1
  bann = lines[0..banner_size].join
  body = lines[(banner_size + 1)..lines.size].join
  [bann, body]
end

#write_branded(name, body) ⇒ Object

writes file with streamer


8
9
10
11
12
# File 'lib/cleon/branded.rb', line 8

def write_branded(name, body)
  hash = md5(body)
  brnd = [BANNER % hash, body].join
  File.write(name, brnd)
end