Sha256: f6dce1add03013598205554cd9a11abbaf16cc5cc26a9099d8bd70ccbb8b40ef

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

module Aka
  module Upgrader
    module FromV0ToV1
      def self.run(aka_db)
        v0 = YAML::load_file(aka_db)

        current = {
          :version => '1',
          :shortcuts => v0
        }

        FileUtils.cp(aka_db, "#{aka_db}.backup")
        puts "Backed up to #{aka_db}.backup."

        File.open(aka_db, 'w+') do |f|
          f.write current.to_yaml
        end
        puts "Upgraded #{aka_db}."
      end
    end

    module FromV1ToV2
      def self.run(aka_db)
        v1 = YAML::load_file(aka_db)

        v2 = v1.merge(:version => '2')
        links = v2.delete(:links)
        v2[:links] = new_links = {}
        if links
          index = 0
          links.each do |element|
            index += 1
            new_links[index] = element
          end
        end

        FileUtils.cp(aka_db, "#{aka_db}.backup")
        puts "Backed up to #{aka_db}.backup."

        File.open(aka_db, 'w+') do |f|
          f.write v2.to_yaml
        end
        puts "Upgraded #{aka_db}."
      end
    end

    module FromV2ToV3
      def self.run(aka_db)
        v2 = YAML::load_file(aka_db)

        v3 = Configuration.new(:version => '3')

        v2[:shortcuts].each do |_, shortcut|
          v3.shortcuts << Configuration::Shortcut.new({
            :shortcut => shortcut[:shortcut],
            :command => shortcut[:command],
            :tag => shortcut[:tag],
            :description => shortcut[:description],
            :function => shortcut[:function]
          })
        end

        (v2[:links] || {}).each do |_, link|
          v3.links << Configuration::Link.new({
            :tag => link[:tag],
            :output => link[:output]
          })
        end

        FileUtils.cp(aka_db, "#{aka_db}.backup")
        puts "Backed up to #{aka_db}.backup."

        File.open(aka_db, 'w+') do |f|
          f.write v3.encode
        end
        puts "Upgraded #{aka_db}."
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hsume2-aka-0.4.3 lib/aka/upgrader.rb
hsume2-aka-0.4.2 lib/aka/upgrader.rb
hsume2-aka-0.4.1 lib/aka/upgrader.rb