Class: IDL::GenFile
- Inherits:
-
Object
- Object
- IDL::GenFile
- Defined in:
- lib/ridl/genfile.rb
Defined Under Namespace
Classes: Content
Constant Summary collapse
- REGEN_MARKER_DEFAULT =
'@@{__RIDL_REGEN_MARKER__}'- @@stack =
[]
- @@cur_trans =
nil
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#fullpath ⇒ Object
readonly
Returns the value of attribute fullpath.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(txt) ⇒ Object
-
#initialize(path, opts = {}) ⇒ GenFile
constructor
A new instance of GenFile.
- #regen_end_marker(sectionid) ⇒ Object
- #regen_header_end_marker(sectionid) ⇒ Object
- #regen_start_marker(sectionid) ⇒ Object
- #remove ⇒ Object
- #save ⇒ Object
- #write_regen_section(sectionid, default_content, indent = '', options = {}) ⇒ Object
Constructor Details
#initialize(path, opts = {}) ⇒ GenFile
Returns a new instance of GenFile.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ridl/genfile.rb', line 71 def initialize(path, opts = {}) if path @path = path @fullpath = File.(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 => false, :output_file => nil }.merge(opts) if @options[:regenerate] && File.exists?(@fullpath) parse_regeneration_content else @content = Content.new end @fout = @options[:output_file] || Tempfile.new(@name) @@cur_trans << self end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
69 70 71 |
# File 'lib/ridl/genfile.rb', line 69 def content @content end |
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
69 70 71 |
# File 'lib/ridl/genfile.rb', line 69 def ext @ext end |
#fullpath ⇒ Object (readonly)
Returns the value of attribute fullpath.
69 70 71 |
# File 'lib/ridl/genfile.rb', line 69 def fullpath @fullpath end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
69 70 71 |
# File 'lib/ridl/genfile.rb', line 69 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
69 70 71 |
# File 'lib/ridl/genfile.rb', line 69 def path @path end |
Class Method Details
.rollback ⇒ Object
37 38 39 40 41 42 |
# File 'lib/ridl/genfile.rb', line 37 def self.rollback if @@cur_trans @@cur_trans.each {|fgen| fgen.remove } @@cur_trans.clear end end |
.transaction(&block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ridl/genfile.rb', line 23 def self.transaction(&block) @@cur_trans = [] @@stack << @@cur_trans begin block.call if block_given? @@cur_trans.each {|fgen| fgen.save } @@cur_trans.clear ensure self.rollback # after successful transaction should be nothing left @@stack.pop @@cur_trans = @@stack.last end end |
Instance Method Details
#<<(txt) ⇒ Object
97 98 99 100 |
# File 'lib/ridl/genfile.rb', line 97 def <<(txt) @fout << txt if @fout self end |
#regen_end_marker(sectionid) ⇒ Object
106 107 108 |
# File 'lib/ridl/genfile.rb', line 106 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
110 111 112 |
# File 'lib/ridl/genfile.rb', line 110 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
102 103 104 |
# File 'lib/ridl/genfile.rb', line 102 def regen_start_marker(sectionid) "#{@options[:regen_marker_prefix]}#{@options[:regen_marker]} - BEGIN : #{sectionid}#{@options[:regen_marker_postfix]}" end |
#remove ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ridl/genfile.rb', line 164 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 |
#save ⇒ Object
129 130 131 132 133 134 135 136 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 |
# File 'lib/ridl/genfile.rb', line 129 def save return if @options[:output_file] if @fout fgen = @fout @fout = nil fgen.close(false) # close but do NOT unlink if File.exists?(@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 # 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, default_content, indent = '', options = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ridl/genfile.rb', line 114 def write_regen_section(sectionid, default_content, indent = '', = {}) self << indent << regen_start_marker(sectionid) << "\n" unless [:header] if content.has_section?(sectionid) self << content[sectionid].join unless content[sectionid].empty? else 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 [:header] self << indent << regen_header_end_marker(sectionid) << "\n" else self << indent << regen_end_marker(sectionid) << "\n" unless [:footer] end end |