.patch
以下是示例:
文件目录(示例)
|__ .gitkeep
|__ pmet-add-install-module-timings.patch
|__ pmet-change-sample-data-load-order.patch
|__ pmet-stop-catching-sample-data-errrors-during-install.patch
|__ pmet-fix-admin-label-word-breaking.patch
|__ pmet-declare-shipping-factory-for-compilation.patch.disabled
...
文件名:
pmet-add-install-module-timings.patch
diff --git a/setup/src/Magento/Setup/Model/Installer.php b/setup/src/Magento/Setup/Model/Installer.php
文件名:
pmet-change-sample-data-load-order.patch
diff --git a/vendor/magento/module-sample-data/etc/module.xml b/vendor/magento/module-sample-data/etc/module.xml
文件名:
pmet-stop-catching-sample-data-errrors-during-install.patch
第一行:
diff --git a/vendor/magento/framework/Setup/SampleData/Executor.php b/vendor/magento/framework/Setup/SampleData/Executor.php
文件名:
pmet-fix-admin-label-word-breaking.patch
第一行:
diff --git a/vendor/magento/theme-adminhtml-backend/web/css/styles-old.less b/vendor/magento/theme-adminhtml-backend/web/css/styles-old.less
JSON文件示例
{
"patches": {
"magento/magento2-base": {
"Patch 1": "m2-hotfixes/pmet-add-install-module-timings.patch"
},
"magento/module-sample-data": {
"Patch 2": "m2-hotfixes/pmet-change-sample-data-load-order.patch"
},
"magento/theme-adminhtml-backend": {
"Patch 3": "m2-hotfixes/pmet-fix-admin-label-word-breaking.patch"
},
"magento/framework": {
"Patch 4": "m2-hotfixes/pmet-stop-catching-sample-data-errrors-during-install.patch"
}
}
}
目前为止的解决方案
scrape.rb
下面是完整的代码。我将进一步细分如下:
files_hash = Hash.new
modules = Array.new
data_hash = {
patches: {}
}
files = Dir["*.patch"]
files.each do |file|
files_hash.store(files.index(file), file)
end
files_hash.each do |key, file|
value = File.open(file, &:readline).split('/')[3]
if value.match(/module-/) || value.match(/theme-/)
result = "magento/#{value}"
else
result = "magento2-base"
end
modules << result
modules.each do |val|
data_hash[:patches][val][key] = "m2-hotfixes/#{file}"
end
end
print data_hash
在强调问题之前,我需要首先详细说明我为实现预期的最终结果所做的工作:
首先,我设置了一个空文件哈希、模块数组和数据哈希:
files_hash = Hash.new
modules = Array.new
data_hash = {
patches: {}
}
接下来,我扫描补丁文件目录
.补丁
files = Dir["*.patch"]
files.each do |file|
files_hash.store(files.index(file), file)
end
magento/module-name
,
magento/theme-name
或者别的什么。两个
module-name
和
theme-name
magento/#{value}
语法。“其他东西”的情况下将使用
magento/magento2-base
:
files_hash.each do |key, file|
value = File.open(file, &:readline).split('/')[3]
if value.match(/module-/) || value.match(/theme-/)
result = "magento/#{value}"
else
result = "magento2-base"
end
modules << result
...
这不是我想要的
解决方案(如果差异结构发生变化怎么办?)但它现在可以工作了,而且我还不能很好地找到合适的正则表达式来搜索字符串并返回相同的结果。上面的代码给出了我想要的,它是以下数组:
#=>["magento2-base", "magento/module-sample-data", "magento/theme-adminhtml-backend", "magento2-base"]
接下来,虽然仍然能够从文件散列访问文件名和键,但我需要循环遍历这个数组并创建散列,其中数组值作为键,文件名作为值(附加到文件路径)。就像这样,(或者我是这么想的):
modules.each do |val|
data_hash[:patches][val][key] = "m2-hotfixes/#{file}"
end
end
我对这部分代码有意见。运行此命令会出现以下错误:
Traceback (most recent call last):
4: from scrape.rb:10:in `<main>'
3: from scrape.rb:10:in `each'
2: from scrape.rb:18:in `block in <main>'
1: from scrape.rb:18:in `each'
scrape.rb:19:in `block (2 levels) in <main>': undefined method `[]=' for nil:NilClass (NoMethodError)
我注意到如果我忽略
key
data_hash
到
data_hash[:patches][val]
,我得到一个散列值。
那么,我明显的问题是: