[英]Ruby Appending comment block to YAML file
I have a yml file that I am using to store a list of stories I have added between releases. 我有一个yml文件,我用它来存储我在版本之间添加的故事列表。
I am using a rake task to dynamically update the version number based off what stories I have added to this file. 我正在使用rake任务根据我添加到此文件的故事动态更新版本号。
It is introducing a new process, so I created the following comment block this will help anyone commenting here to add stories in the right format: 它引入了一个新流程,因此我创建了以下注释块,这将有助于任何评论此处的人以正确的格式添加故事:
# Version control file.
# Versions should be incremented as follows
#
# [X - major change] . [V - new feature] . [I - Bug fix / Small change]
#
# Update the undefined block with a one line readable description of what your story was about. example:
#
# undefined:
# stories:
# - "I - fixed spelling mistake"
# - "V - added import functionality"
# - "X - rebuilt the main dashboard"
#
The issue is after my rake task is done the job the file loses the comment block. 问题是在我的rake任务完成后,文件丢失了注释块。
I pretty much load the YAML versions = YAML.load_file( 'doc/release.yml' )
and then once the logic is finished I File.open("doc/release.yml", 'w') { |f| YAML.dump(versions, f) }
我几乎加载YAML versions = YAML.load_file( 'doc/release.yml' )
然后一旦逻辑完成我File.open("doc/release.yml", 'w') { |f| YAML.dump(versions, f) }
File.open("doc/release.yml", 'w') { |f| YAML.dump(versions, f) }
Where versions
is the new updated hash. versions
是新更新的哈希值。 However, this removes the comment block to the file. 但是,这会删除文件的注释块。
Other solutions I have found just modify existing lines. 我发现的其他解决方案只是修改现有的行。
Is there a way to open the file and adding the above without messing up the YAML beneath. 有没有办法打开文件并添加上面的内容而不会弄乱下面的YAML。 Any help will be much appreciated. 任何帮助都感激不尽。
That the comments are lost with a dump is unfortunatly normal. 转储丢失的评论是不幸的正常。 You have two options: 您有两种选择:
{ :a => 'b'}.to_yaml
, add the comments and with File.write
do the dump yourself, you could overwrite the normal .dump method in YAML this way 将您的版本哈希转换为yaml { :a => 'b'}.to_yaml
,添加注释并使用File.write
执行转储,您可以用YAML覆盖正常的.dump方法 Here's a possible solution. 这是一个可能的解决方案。
require 'yaml'
versions_yaml = File.read('release.yml')
versions = YAML.load(versions_yaml)
comments = versions_yaml.scan(/^#.*?$/)
File.open("release2.yml", 'w') { |f|
f.puts comments
YAML.dump(versions, f)
}
puts File.read("release2.yml")
With release.yml : 使用release.yml:
# I'm a comment on first line
---
- 1
- 2
- 3
# I'm a comment somewhere in the middle
- - 4
- 5
it outputs : 它输出:
# I'm a comment on first line
# I'm a comment somewhere in the middle
---
- 1
- 2
- 3
- - 4
- 5
I came up with this method of adding comments to my Hash before dumping out to yaml. 在推出yaml之前,我提出了这种向Hash添加注释的方法。 I use this to initialize configuration files with embedded comments to document the configuration options. 我使用它来初始化带有嵌入式注释的配置文件以记录配置选项。 It's a limited solution. 这是一个有限的解决方案。 No in-Array comments, for example. 例如,没有in-Array注释。 But it'll work for simple cases. 但它适用于简单的情况。
cfg = {
c: 'This is a comment',
'foo' => 'bar',
'level2' => {
'level3' => {
'foo' => 'bar',
c1: 'This is a comment (line 1)',
c2: 'This is a comment (line 2)',
'foo2' => 'bar2',
},
},
}
YAML.dump(cfg).each_line do |l|
if l.match(/:c(\d+)?:/)
l.sub!(/:c(\d+)?:/, '#')
l.sub!(/(^\s*# )["']/, '\1')
l.sub!(/["']\s*$/, '')
end
puts l
end
Produces: 生产:
---
# This is a comment
foo: bar
level2:
level3:
foo: bar
# This is a comment (line 1)
# This is a comment (line 2)
foo2: bar2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.