Class: Zammad::SZPM

Inherits:
Object
  • Object
show all
Defined in:
lib/zammad/szpm.rb,
lib/zammad/szpm/version.rb

Overview

Handles all SZPM and ZPM related stuff

Constant Summary collapse

VERSION =
'0.2.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(szpm_file) ⇒ SZPM, Hash

Creates an instance based on a given SZPM file path.

Parameters:

  • szpm_file (String)

    the path to the SZPM file.



17
18
19
20
21
22
# File 'lib/zammad/szpm.rb', line 17

def initialize(szpm_file)

  @szpm_file = szpm_file

  parse
end

Instance Attribute Details

#structureObject (readonly)

Returns the value of attribute structure.



11
12
13
# File 'lib/zammad/szpm.rb', line 11

def structure
  @structure
end

#szpmObject (readonly)

Returns the value of attribute szpm.



10
11
12
# File 'lib/zammad/szpm.rb', line 10

def szpm
  @szpm
end

Instance Method Details

#add_build_information(build_host) ⇒ Hash

Adds the buildhost and builddate to the SZPM file.

Parameters:

  • build_host (String)

    build host on which the ZPM file was created.

Returns:

  • (Hash)

    the parsed SZPM structure.



58
59
60
61
62
63
64
65
66
67
# File 'lib/zammad/szpm.rb', line 58

def add_build_information(build_host)
  parse

  # add buildhost
  @structure['buildhost'] = build_host
  # add builddate
  @structure['builddate'] = Time.zone.now

  store
end

#add_file(location, permission = 644) ⇒ Hash

Adds a new file to the filelist of the SZPM file.

Parameters:

  • location (String)

    the file location.

  • permission (Integer) (defaults to: 644)

    the permissions with which the files should get created.

Returns:

  • (Hash)

    the parsed SZPM structure.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/zammad/szpm.rb', line 74

def add_file( location, permission = 644 )
  parse

  update = true
  @structure['files'].each { |file|

    next if file['location'] != location
    next if file['permission'] != permission

    update = false

    break
  }
  return if !update

  @structure['files'].push({
                             'location'   => location,
                             'permission' => permission,
                           })

  store
end

#parseHash

Parses the given SZPM file.

Returns:

  • (Hash)

    the parsed SZPM structure.



112
113
114
115
116
117
118
119
# File 'lib/zammad/szpm.rb', line 112

def parse

  szpm_read_handle = File.open(@szpm_file)
  @szpm            = szpm_read_handle.read
  szpm_read_handle.close

  @structure = JSON.parse( @szpm )
end

#storeHash

Stores the changes to the SZPM file.

Returns:

  • (Hash)

    the parsed SZPM structure.



100
101
102
103
104
105
106
107
# File 'lib/zammad/szpm.rb', line 100

def store

  File.open(@szpm_file, 'w') { |file|
    file.write( JSON.pretty_generate(@structure) )
  }

  parse
end

#version(version, change_log) ⇒ Hash

Adds a new version and the change_log to the SZPM file.

Parameters:

  • version (String)

    the version number.

  • change_log (String)

    the change_log.

Returns:

  • (Hash)

    the parsed SZPM structure.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zammad/szpm.rb', line 29

def version(version, change_log)
  parse

  # change version
  @structure['version'] = version

  # append change_log
  @structure['change_log'] ||= []

  # remove tabs from change_log
  change_log.gsub!(/\t/, '  ')

  # strip whitespaces
  change_log.strip!

  # append entry
  @structure['change_log'].push({
                                 'version' => version,
                                 'date'    => Time.zone.now,
                                 'content' => change_log
                               })

  store
end

#zpmString

Creates an ZPM string out of the SZPM file.

Returns:

  • (String)

    ZPM JSON content with Base64 encoded files.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/zammad/szpm.rb', line 124

def zpm
  zpm = parse

  folder = File.dirname(@szpm_file)

  zpm['files'].collect! { |file|

    file_location = file['location']
    file_handle   = File.open("#{folder}/#{file_location}", 'r')
    file_content  = file_handle.read
    file_handle.close

    file['encode']  = 'base64'
    file['content'] = Base64.strict_encode64( file_content )

    file
  }

  JSON.pretty_generate(zpm)
end