Skip to content

Instantly share code, notes, and snippets.

@mattsawyer77
Created March 23, 2012 02:37
Show Gist options
  • Save mattsawyer77/2166317 to your computer and use it in GitHub Desktop.
Save mattsawyer77/2166317 to your computer and use it in GitHub Desktop.
shell script to update a MySQL Workbench file from XML
#!/usr/bin/env ruby
begin
def usage(message = nil)
if !message.nil? then
print message + "\n"
end
print "\nusage: update_workbench_file <MWB file> <XML file>\n\n"
end
if ARGV.length == 2
xml_file, mwb_file = ARGV
xml_filetype = `file "#{xml_file}"`
if !xml_filetype.match(/xml/i) then
raise "#{xml_file} is not a valid XML file!"
end
mwb_filetype = `file "#{mwb_file}"`
if !mwb_filetype.match(/zip/i) then
raise "#{mwb_file} is not a MySQL Workbench file!"
end
mwb_file_basename = mwb_file[/[^\/]+$/]
xml_file_basename = mwb_file[/[^\/]+$/]
print "extracting XML from #{mwb_file}...\n"
`mkdir -p /tmp/workbench`
if $?.to_i != 0 then raise "could not create temporary directory!" end
`unzip "#{mwb_file}" -d /tmp/workbench`
if $?.to_i != 0 then raise "could not unzip #{mwb_file}!" end
print "overwriting XML data with #{xml_file}...\n"
`cp "#{xml_file}" /tmp/workbench/document.mwb.xml`
print "repackaging MWB file...\n"
`cd /tmp/workbench && zip "#{mwb_file_basename}" *`
if $?.to_i != 0 then raise "could not create zip file!" end
print "updating original MWB file...\n"
`cd /tmp/workbench && mv "#{mwb_file_basename}" "#{mwb_file}"`
if $?.to_i != 0 then raise "could not overwrite original file: #{mwb_file}!" end
print "#{mwb_file} updated successfully.\n"
`rm -rf /tmp/workbench`
else
usage
end
rescue StandardError => err
print err.to_s + "\n"
end
@Yamilquery
Copy link

You have an error in line 38 & 42 the correct code is:

#!/usr/bin/env ruby

begin
    def usage(message = nil)
        if !message.nil? then
            print message + "\n"
        end
        print "\nusage: update_workbench_file <MWB file> <XML file>\n\n"
    end

    if ARGV.length == 2
        xml_file, mwb_file = ARGV

        xml_filetype = `file "#{xml_file}"`
        if !xml_filetype.match(/xml/i) then
            raise "#{xml_file} is not a valid XML file!"
        end

        mwb_filetype = `file "#{mwb_file}"`
        if !mwb_filetype.match(/zip/i) then
            raise "#{mwb_file} is not a MySQL Workbench file!"
        end

        mwb_file_basename = mwb_file[/[^\/]+$/]
        xml_file_basename = mwb_file[/[^\/]+$/]

        print "extracting XML from #{mwb_file}...\n"
        `mkdir -p /tmp/workbench`
        if $?.to_i != 0 then raise "could not create temporary directory!" end

        `unzip "#{mwb_file}" -d /tmp/workbench`
        if $?.to_i != 0 then raise "could not unzip #{mwb_file}!" end

        print "overwriting XML data with #{xml_file}...\n"
        `cp "#{xml_file}" /tmp/workbench/document.mwb.xml`

        print "repackaging MWB file...\n"
        `(cd /tmp/workbench && zip "#{mwb_file_basename}" *)`
        if $?.to_i != 0 then raise "could not create zip file!" end

        print "updating original MWB file...\n"
        `mv /tmp/workbench/"#{mwb_file_basename}" "#{mwb_file}"`
        if $?.to_i != 0 then raise "could not overwrite original file: #{mwb_file}!" end

        print "#{mwb_file} updated successfully.\n"

        `rm -rf /tmp/workbench`
    else
        usage
    end
rescue StandardError => err
  print err.to_s + "\n"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment