我在一個專案中,我必須從 Ubuntu for Windows 創建一個 msi 包。
我設法創建了一個從 Ubuntu rgace 到“msitools”的 msi 檔案,它使用 wxs 檔案(它就像一個 xml 檔案)來配置包。
wxs 檔案的示例,它采用一個檔案 (FoobarAppl10.exe)。
<?xml version='1.0' encoding='windows-1252'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
<Product Name='Foobar 1.0' Id='ABCDDCBA-86C7-4D14-AEC0-86416A69ABDE' UpgradeCode='ABCDDCBA-7349-453F-94F6-BCB5110BA4FD'
Language='1033' Codepage='1252' Version='1.0.0' Manufacturer='Acme Ltd.'>
<Package Id='*' Keywords='Installer' Description="Acme's Foobar 1.0 Installer"
Comments='Foobar is a registered trademark of Acme Ltd.' Manufacturer='Acme Ltd.'
InstallerVersion='100' Languages='1033' Compressed='yes' SummaryCodepage='1252' />
<Media Id='1' Cabinet='Sample.cab' EmbedCab='yes' DiskPrompt="CD-ROM #1" />
<Property Id='DiskPrompt' Value="Acme's Foobar 1.0 Installation [1]" />
<Directory Id='TARGETDIR' Name='SourceDir'>
<Directory Id='ProgramFilesFolder' Name='PFiles'>
<Directory Id='Acme' Name='Acme'>
<Directory Id='INSTALLDIR' Name='Foobar 1.0'>
<Component Id='MainExecutable' Guid='ABCDDCBA-83F1-4F22-985B-FDB3C8ABD471'>
<File Id='FoobarEXE' Name='FoobarAppl10.exe' DiskId='1' Source='FoobarAppl10.exe' KeyPath='yes'/>
</Component>
</Directory>
</Directory>
</Directory>
</Directory>
<Feature Id='Complete' Level='1'>
<ComponentRef Id='MainExecutable' />
</Feature>
</Product>
</Wix>
現在我的問題是用ruby創建一個樹結構生成器,這樣我的wxs檔案就可以打包我專案的所有檔案,而無需手動撰寫。
我撰寫了一個代碼,可以創建一個 xml 檔案并在此列印我的蠻力代碼:

$ ls
build.wxs foobarAppl10.exe generator_xml.rb
require 'nokogiri'
out_file = File.new("generated.wxs", "w")
builder = Nokogiri::XML::Builder.new(:encoding => 'windows-1252') { |xml|
xml.Wix('xmlns' => 'http://schemas.microsoft.com/wix/2006/wi') do
xml.Product('Name' => 'Foobar 1.0', 'Id' => 'ABCDDCBA-86C7-4D14-AEC0-86416A69ABDE',
'UpgradeCode' =>'ABCDDCBA-7349-453F-94F6-BCB5110BA4FD', 'Language' => '1033', 'Codepage' => '1252',
'Version' => '1.0.0', 'Manufacturer' => 'Acme Ltd.') do
xml.Package('Id' => '*', 'Keywords' => 'Installer', 'Description' => "Acme's Foobar 1.0 Installer",
'Comments' => 'Foobar is a registered trademark of Acme Ltd.', 'Manufacturer' => 'Acme Ltd.',
'InstallerVersion' => '100', 'Languages' => '1033', 'Compressed' => 'yes', 'SummaryCodepage' => '1252')
xml.Media('Id' => '1', 'Cabinet' => 'Sample.cab', 'EmbedCab' => 'yes', 'DiskPrompt' => "CD-ROM #1")
xml.Property('Id' => 'DiskPromt', 'Value' => "Acme's Foobar 1.0 Installation [1]")
xml.Directory('Id' => 'TARGETDIR', 'Name' =>'SourceDir') do
xml.Directory('Id' => 'ProgramFilesFolder', 'Name' =>'PFiles') do
xml.Directory('Id' => 'Acme', 'Name' =>'Acme') do
xml.Directory('Id' => 'INSTALLDIR', 'Name' =>'Foobar 1.0') do
xml.Component('Id' => 'MainExecutable', 'Guid' => 'ABCDDCBA-83F1-4F22-985B-FDB3C8ABD471') do
xml.File('Id' => 'FoobarEXE', 'Name' => 'FoobarAppl10.exe', 'DiskId' => '1', 'Source' => 'FoobarAppl10.exe', 'KeyPath' =>'yes')
end
end
end
end
end
xml.Feature('Id' => 'Complete', 'Level' => '1') do
xml.Component('Id' => 'MainExecutable')
end
end
end
}
puts builder.to_xml
out_file.puts(builder.to_xml)
out_file.close
如果有人知道如何從樹結構生成代碼,那將對我有很大幫助!
uj5u.com熱心網友回復:
聽起來您想創建一個模仿目錄結構的 XML 結構?這是一個遞回函式,它應該可以完成您想要的大部分操作,或者至少可以作為一個很好的起點。當您找到目錄或檔案時,這些puts陳述句可以被洗掉或替換為您需要執行的任何其他操作。
require 'nokogiri'
def process_dir(current_path, xml)
return if !File.directory?(current_path) || Dir.empty?(current_path)
directory_name = current_path.split("/").last
# insert code here to compute other attributes for Directory node
xml.Directory(Name: directory_name) do
Dir.children(current_path).each do |entry|
file = "#{current_path}/#{entry}"
if !File.directory?(file)
# insert code here to compute other attributes for File node
xml.File(Name: entry)
puts "found file named #{entry} at #{current_path}"
else
puts "found directory named #{current_path}/#{entry}"
process_dir("#{current_path}/#{entry}", xml)
end
end
end
end
builder = Nokogiri::XML::Builder.new do |xml|
xml.root { process_dir(".", xml) }
end
pp builder.to_xml
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436294.html
上一篇:Newtonsoft.Json將xml檔案轉換為帶有陣列檢測的json
下一篇:如何撤消我的最后一次提交?gitreset--softHEAD^或gitreset--softHEAD~1都在作業嗎?
