Class: TMBundle

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/tm_bundle.rb,
lib/tm_bundle/item.rb,
lib/tm_bundle/menu.rb

Defined Under Namespace

Classes: Action, Command, Macro, Menu, Snippet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ TMBundle

Returns a new instance of TMBundle.



47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



45
46
47
# File 'lib/tm_bundle.rb', line 45

def actions
  @actions
end

#dataObject

Returns the value of attribute data.



45
46
47
# File 'lib/tm_bundle.rb', line 45

def data
  @data
end

Returns the value of attribute menu.



45
46
47
# File 'lib/tm_bundle.rb', line 45

def menu
  @menu
end

#pathObject

Returns the value of attribute path.



45
46
47
# File 'lib/tm_bundle.rb', line 45

def path
  @path
end

Instance Method Details

#fill_missing_menu_names!Object



74
75
76
77
78
79
80
81
82
# 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!Object



68
69
70
71
72
# 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_actionsObject



57
58
59
60
61
62
63
64
65
66
# 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


84
85
86
87
# File 'lib/tm_bundle.rb', line 84

def menu_export!(prefix)
  fill_missing_menu_names!
  menu.write_yaml_hierarchy!(prefix && "-#{prefix}")
end

#process_xml_output(xml) ⇒ Object



102
103
104
105
106
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# 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