简体   繁体   English

PowerShell 的等效 Groovy ConfigSlurper 格式

[英]Equivalent Groovy ConfigSlurper format for PowerShell

We have a large number of groovy/gradle configuration files that are created by the ConfigSlurper class.我们有大量由 ConfigSlurper 类创建的 groovy/gradle 配置文件。

We are investigating to move our scripts over to PowerShell and quickly realized that the format is not JSON format.我们正在研究将我们的脚本移至 PowerShell,并很快意识到该格式不是 JSON 格式。

I'm not finding a lot out there on the 'industry standard' configuration format.我没有找到很多关于“行业标准”配置格式的信息。 Yes there is JSON and INI... but that doesn't nearly seem as powerful as the groovy Config capabilities.是的,有 JSON 和 INI……但它们似乎没有 groovy Config 功能强大。

What do people use out there?人们在那里用什么? Has anyone found an equivalent to the goovy/gradle config files?有没有人找到与 goovy/gradle 配置文件等效的文件?

thanks!谢谢!

Syntax-wise, the closest equivalent is to use a .psd1 file containing a PowerShell hashtable literal, which you can read into an in-memory hashtable using Import-PowerShellDataFile .在语法方面,最接近的等效项是使用包含 PowerShell 哈希表文字的.psd1文件,您可以使用Import-PowerShellDataFile将其读入内存中的哈希表。

However, note that - as of PowerShell 7.3.1 - there is no way to create such a file programmatically - GitHub issue #11300 discusses future enhancements to allow this.但是,请注意 - 从 PowerShell 7.3.1 开始 - 无法以编程方式创建此类文件 - GitHub 问题 #11300讨论了未来的增强功能以允许这样做。

This answer provides custom code for writing .psd1 files and also discusses alternative data formats, including JSON, which is also worth considering. This answer提供了用于编写.psd1文件的自定义代码,还讨论了替代数据格式,包括同样值得考虑的 JSON。

Example :示例

The PowerShell equivalent of the following Groovy config file (taken from the docs ): PowerShell 等效于以下 Groovy 配置文件(取自文档):

grails.webflow.stateless = true
smtp {
     mail.host = 'smtp.myisp.com'
     mail.auth.user = 'server'
}
resources.URL = 'http://localhost:80/resources'

is:是:

@{
  grails    = @{ webflow = @{ stateless = $true } }
  smtp      = @{
    mail    = @{ 
      host = 'smtp.myisp.com' 
      auth = @{ user = 'server' }
    }
  }
  resources = @{ URL = 'http://localhost:80/resources' }
}

As you can see, there are similarities in syntax, but PowerShell's is invariably more verbose, primarily because it doesn't support implicit nesting of the hasthables (maps, in Groovy terms).如您所见,语法有相似之处,但 PowerShell 的语法总是更冗长,主要是因为它不支持隐式嵌套 hasthables(映射,在 Groovy 术语中)。

Parsing the above with Import-PowerShellDataFile results in a single, nested hashtable whose top-level keys as grails , smtp , and resources - but not the the order of the entries is not guaranteed to match the order in the input file.使用Import-PowerShellDataFile解析以上内容会生成一个嵌套的哈希表,其顶级键为grailssmtpresources - 但条目的顺序不能保证与输入文件中的顺序匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM