From a2617722929bf03b72c5187c5c5128adf5afb537 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Fri, 15 Jul 2016 08:47:21 -0700 Subject: [PATCH] [git] Use PlistBuddy for plist manipulation --- bin/clean_alfred_workflow | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/clean_alfred_workflow b/bin/clean_alfred_workflow index 83914cd..8babd7a 100755 --- a/bin/clean_alfred_workflow +++ b/bin/clean_alfred_workflow @@ -1,13 +1,19 @@ #!/usr/bin/env ruby -require 'rexml/document' +require 'tempfile' -doc = REXML::Document.new(ARGF.read, ignore_whitespace_nodes: :all) -xpath = '//dict/key[text()="variablesdontexport"]/following-sibling::array' -vars = doc.elements[xpath].map(&:text) +plist = ARGF.read +Tempfile.create('info.plist') do |f| + f << plist + f.flush -vars.each do |var| - doc.elements["//key[text()='#{var}']/following-sibling::string"].text = '' -end + vars = `/usr/libexec/PlistBuddy -c "Print :variablesdontexport" #{f.path}` + vars = vars.split("\n")[1..-2].map(&:strip) + + vars.each do |var| + `/usr/libexec/PlistBuddy -c "Set :variables:#{var} ''" #{f.path}` + end -doc.write + f.rewind + puts f.read +end