class TMBundle

Attributes

actions[RW]
data[RW]
menu[RW]
path[RW]

Public Class Methods

new(path = nil) click to toggle source
# File lib/tm_bundle.rb, line 47
def initialize(path = nil)
  self.path    = path ? Pathname.new(path) : Pathname.pwd
  self.data    = Plutil.load_json(@path/'info.plist')
  self.menu    = Menu.new(data['mainMenu'], @path)
  self.actions = []
  locate_actions
  fill_missing_menu_names!
  # menu.write_yaml_hierarchy!
end

Public Instance Methods

fill_missing_menu_names!() click to toggle source
# File lib/tm_bundle.rb, line 74
def fill_missing_menu_names!
  actions.each do |action|
    if item = menu.uuids[action.uuid]
      item.name ||= action.name
    else
      Menu.find_or_create_new_item(action.uuid, 0, name: action.name)
    end
  end
end
fix_names!() click to toggle source
# File lib/tm_bundle.rb, line 68
def fix_names!
  actions.select(&:unmatching_path?).each do |action|
    action.path.rename(action.path.dirname/action.expected_path)
  end
end
locate_actions() click to toggle source
# File lib/tm_bundle.rb, line 57
def locate_actions
  Pathname.glob("#{path}/{Commands,Snippets,Macros}/*.{plist,tm{Command,Snippet}}")
    .group_by {|p| p.dirname.basename.to_s }
    .each do |group, files|
      # p group.downcase.to_sym, actions
      files.each do |pathname|
        actions << TMBundle.const_get(group.singularize.to_sym).new(pathname)
      end
    end
end
menu_export!(prefix) click to toggle source
process_xml_output(xml) click to toggle source
# File lib/tm_bundle.rb, line 102
def process_xml_output(xml)
  xml.lines[3...-1]
    .join
    .gsub(/(?<=$\n|\t)\t/, "\n\t" => "\n", "\t" => "  ")
end
save_plist!(type = :default) click to toggle source
# File lib/tm_bundle.rb, line 89
def save_plist!(type = :default)
  fill_missing_menu_names!
  
  file = case type
         when :last  then Pathname.glob((path/"menu-*.yml").to_s).last
         when String then path/"menu-#{type}.yml"
                     else path/"menu.yml" end
  
  hash = menu.read_yaml_hieararchy! file
  xml  = process_xml_output Plutil::JSON.dump(hash)
  puts Plutil.replace(@path/'info.plist', 'mainMenu', xml, &:read)
end