On Wed, May 11, 2011 at 3:45 PM, Greg Hacke <greghacke / gmail.com> wrote:
> 7stud -- wrote in post #997885:
>>
>> Yeah, what is so hard about posting a sample of the original file, as
>> well as the modified file that you want to end up with, and leaving out
>> all your confusing descriptions?
>
> OK, here's the inbound:
> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
> "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
> <plist version=3D"1.0">
> <dict>
> =A0<key>PayloadContent</key>
> =A0<array>
> =A0 =A0<dict>
> =A0 =A0 =A0<key>EmailAddress</key>
> =A0 =A0 =A0<string>{username}</string>
> =A0 =A0 =A0<key>Host</key>
> =A0 =A0 =A0<string>exchange.domain.com</string>
> =A0 =A0 =A0<key>MailNumberOfPastDaysToSync</key>
> =A0 =A0 =A0<integer>3</integer>
> =A0 =A0 =A0<key>Password</key>
> =A0 =A0 =A0<string>{password}</string>
> =A0 =A0 =A0<key>PayloadDescription</key>
> =A0 =A0 =A0<string>Configures device for use with Microsoft Exchange
> ActiveSync services.</string>
> =A0 =A0 =A0<key>PayloadDisplayName</key>
> =A0 =A0 =A0<string>Exchange</string>
> =A0 =A0 =A0<key>PayloadIdentifier</key>
> =A0 =A0 =A0<string>com.domain.enterprise.eas</string>
> =A0 =A0 =A0<key>PayloadOrganization</key>
> =A0 =A0 =A0<string>TipsHouse</string>
> =A0 =A0 =A0<key>PayloadType</key>
> =A0 =A0 =A0<string>com.apple.eas.account</string>
> =A0 =A0 =A0<key>PayloadUUID</key>
> =A0 =A0 =A0<string>FE477C13-BF4F-4EA0-BE09-3968EC40C952</string>
> =A0 =A0 =A0<key>PayloadVersion</key>
> =A0 =A0 =A0<integer>1</integer>
> =A0 =A0 =A0<key>SSL</key>
> =A0 =A0 =A0<true/>
> =A0 =A0 =A0<key>UserName</key>
> =A0 =A0 =A0<string>domain\{userroot}</string>
> =A0 =A0</dict>
> =A0</array>
> =A0<key>PayloadDescription</key>
> =A0<string>Profile Test</string>
> =A0<key>PayloadDisplayName</key>
> =A0<string>TipsHouse</string>
> =A0<key>PayloadIdentifier</key>
> =A0<string>com.domain.enterprise</string>
> =A0<key>PayloadOrganization</key>
> =A0<string>Domain</string>
> =A0<key>PayloadRemovalDisallowed</key>
> =A0<false/>
> =A0<key>PayloadType</key>
> =A0<string>Configuration</string>
> =A0<key>PayloadUUID</key>
> =A0<string>A6B7D66D-1179-4E85-A005-4DAACD4EDF0F</string>
> =A0<key>PayloadVersion</key>
> =A0<integer>1</integer>
> </dict>
> </plist>
>
> And the outbound replaces elements in only the array area (in this case,
> just the EAS but other options may be included - LDAP, IMAP, etc.)
> <dict>
> =A0 =A0 =A0<key>EmailAddress</key>
> =A0 =A0 =A0<string>greghacke / domain.com</string>
> =A0 =A0 =A0<key>Host</key>
> =A0 =A0 =A0<string>exchange.domain.com</string>
> =A0 =A0 =A0<key>MailNumberOfPastDaysToSync</key>
> =A0 =A0 =A0<integer>3</integer>
> =A0 =A0 =A0<key>Password</key>
> =A0 =A0 =A0<string>MyPassword</string>
> =A0 =A0 =A0<key>PayloadDescription</key>
> =A0 =A0 =A0<string>Configures device for use with Microsoft Exchange
> ActiveSync services.</string>
> =A0 =A0 =A0<key>PayloadDisplayName</key>
> =A0 =A0 =A0<string>Exchange</string>
> =A0 =A0 =A0<key>PayloadIdentifier</key>
> =A0 =A0 =A0<string>com.domain.enterprise.eas</string>
> =A0 =A0 =A0<key>PayloadOrganization</key>
> =A0 =A0 =A0<string>TipsHouse</string>
> =A0 =A0 =A0<key>PayloadType</key>
> =A0 =A0 =A0<string>com.apple.eas.account</string>
> =A0 =A0 =A0<key>PayloadUUID</key>
> =A0 =A0 =A0<string>FE477C13-BF4F-4EA0-BE09-3968EC40C952</string>
> =A0 =A0 =A0<key>PayloadVersion</key>
> =A0 =A0 =A0<integer>1</integer>
> =A0 =A0 =A0<key>SSL</key>
> =A0 =A0 =A0<true/>
> =A0 =A0 =A0<key>UserName</key>
> =A0 =A0 =A0<string>domain\greghacke</string>
> =A0 =A0</dict>
>
> Now, the initial file is provided to me may be replaced as needed. I
> would like to avoid converting it to ERb as I cannot gaurentee anyone
> else will do the work to ensure it stays updated and correct.
>
> I've dug around - really - and found two examples for single element
> replacement.
>
> My belief is that there are a plethora of options execute this. I would
> like to do something like:
> File.copy(master,user_file)
> File.open('/tmp/temp_file.txt', 'w+') do | new_file |
> =A0 =A0 new_file.puts(File.open(user_file, 'r') do | original_file |
> =A0 =A0 original_file.read.gsub(/\{username\}/, username)
> =A0 =A0 end)
> end
> FileUtils.mv("/tmp/replaceable2.txt", user_file)
>
> =A0text=3D File.read user_file
> =A0File.open(user_file, 'w+'){|f| f << text.gsub(/\{username\}/,
> username)}

You can use the block form of gsub to do only one pass:

variables =3D {"username" =3D> "Bob", "password" =3D> "pwd"}

file_contents =3D File.read("master_file").read
file_contents.gsub!(/\{(.*?)\}/) {|m| variables[$1]}

#and then write the file contents to another file, delete and rename

Jesus.