Class: IDL::GenFile

Inherits:
Object
  • Object
show all
Defined in:
lib/ridl/genfile.rb

Defined Under Namespace

Classes: Content

Constant Summary collapse

REGEN_MARKER_DEFAULT =
'@@{__RIDL_REGEN_MARKER__}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ GenFile

Returns a new instance of GenFile.



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
# File 'lib/ridl/genfile.rb', line 95

def initialize(path, opts = {})
  if path
    @path = path
    @fullpath = File.expand_path(path)
    @name = File.basename(path)
    @ext = File.extname(path).sub(/^\./, '')
  else
    @path = @fullpath = @name = @ext = ''
  end
  @options = {
    regenerate: false,
    regen_marker_prefix: '//',
    regen_marker_postfix: nil,
    regen_marker: REGEN_MARKER_DEFAULT,
    regen_keep_header: true,
    output_file: nil,
    create_missing_dir: false
  }.merge(opts)
  if @options[:regenerate] && File.exist?(@fullpath)
    parse_regeneration_content
  else
    @content = Content.new
  end
  @fout = @options[:output_file] || Tempfile.new(@name)
  self.class.__send__(:_push, self)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



93
94
95
# File 'lib/ridl/genfile.rb', line 93

def content
  @content
end

#extObject (readonly)

Returns the value of attribute ext.



93
94
95
# File 'lib/ridl/genfile.rb', line 93

def ext
  @ext
end

#fullpathObject (readonly)

Returns the value of attribute fullpath.



93
94
95
# File 'lib/ridl/genfile.rb', line 93

def fullpath
  @fullpath
end

#nameObject (readonly)

Returns the value of attribute name.



93
94
95
# File 'lib/ridl/genfile.rb', line 93

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



93
94
95
# File 'lib/ridl/genfile.rb', line 93

def path
  @path
end

Class Method Details

.rollbackObject



63
64
65
# File 'lib/ridl/genfile.rb', line 63

def self.rollback
  _rollback
end

.transaction(&block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/ridl/genfile.rb', line 52

def self.transaction(&block)
  _start_transaction
  begin
    block.call if block_given?
    _commit
  ensure
    _rollback # after successful transaction should be nothing left
    _close_transaction
  end
end

Instance Method Details

#<<(txt) ⇒ Object



122
123
124
125
# File 'lib/ridl/genfile.rb', line 122

def <<(txt)
  @fout << txt if @fout
  self
end

#regen_end_marker(sectionid) ⇒ Object



131
132
133
# File 'lib/ridl/genfile.rb', line 131

def regen_end_marker(sectionid)
  "#{@options[:regen_marker_prefix]}#{@options[:regen_marker]} - END : #{sectionid}#{@options[:regen_marker_postfix]}"
end

#regen_header_end_marker(sectionid) ⇒ Object



135
136
137
# File 'lib/ridl/genfile.rb', line 135

def regen_header_end_marker(sectionid)
  "#{@options[:regen_marker_prefix]}#{@options[:regen_marker]} - HEADER_END : #{sectionid}#{@options[:regen_marker_postfix]}"
end

#regen_start_marker(sectionid) ⇒ Object



127
128
129
# File 'lib/ridl/genfile.rb', line 127

def regen_start_marker(sectionid)
  "#{@options[:regen_marker_prefix]}#{@options[:regen_marker]} - BEGIN : #{sectionid}#{@options[:regen_marker_postfix]}"
end

#removeObject



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ridl/genfile.rb', line 201

def remove
  return if @options[:output_file]

  if @fout
    begin
      @fout.close(true)
    rescue
      IDL.log(0, %Q{ERROR: FAILED to clean up temp file #{@fout.path}: #{$!}})
    end
    @fout = nil
  end
end

#saveObject



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
# File 'lib/ridl/genfile.rb', line 158

def save
  return if @options[:output_file]

  if @fout
    fgen = @fout
    @fout = nil
    fgen.close(false) # close but do NOT unlink
    if File.exist?(@fullpath)
      # create temporary backup
      ftmp = Tempfile.new(@name)
      ftmp_name = ftmp.path.dup
      ftmp.close(true) # close AND unlink
      FileUtils::mv(@fullpath, ftmp_name) # backup existing file
      # replace original
      begin
        # rename newly generated file
        FileUtils.mv(fgen.path, @fullpath)
        # preserve file mode
        FileUtils.chmod(File.lstat(ftmp_name).mode, @fullpath)
      rescue
        IDL.log(0, %Q{ERROR: FAILED updating #{@path}: #{$!}})
        # restore backup
        FileUtils.mv(ftmp_name, @fullpath)
        raise
      end
      # remove backup
      File.unlink(ftmp_name)
    else
      unless File.directory?(File.dirname(@fullpath))
        unless @options[:create_missing_dir]
          IDL.log(0, %Q{ERROR: Cannot access output folder #{File.dirname(@fullpath)}})
          exit(1)
        end
        FileUtils.mkdir_p(File.dirname(@fullpath))
      end
      # just rename newly generated file
      FileUtils.mv(fgen.path, @fullpath)
      # set default mode for new files
      FileUtils.chmod(0666 - File.umask, @fullpath)
    end
  end
end

#write_regen_section(sectionid, options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ridl/genfile.rb', line 139

def write_regen_section(sectionid, options = {})
  indent = options[:indent] || ''
  self << indent << regen_start_marker(sectionid) << "\n" unless options[:header]
  if content.has_section?(sectionid)
    self << content[sectionid].join unless content[sectionid].empty?
  elsif block_given?
    yield # block should yield default content
  elsif default_content = options[:default_content]
    default_content = (Array === default_content) ? default_content : default_content.to_s.split("\n")
    self << (default_content.collect { |l| (s = indent.dup) << l << "\n"
 s }.join) unless default_content.empty?
  end
  if options[:header]
    self << indent << regen_header_end_marker(sectionid) << "\n"
  else
    self << indent << regen_end_marker(sectionid) << "\n" unless options[:footer]
  end
end