Sha256: e2c78424b6a97cedefe3d855f518fd0e1c7e82c201c59f96253a240ebe53b98c

Contents?: true

Size: 1.76 KB

Versions: 9

Compression:

Stored size: 1.76 KB

Contents

require 'pathname'
require 'digest/sha2'
require 'plist'

module Mate
  class Tmproj
    TMPROJ_DIR = Pathname('~').expand_path + '.tmprojs'

    attr_reader :dirs, :file
    def initialize(dirs)
      @dirs = dirs.map{ |dir| Pathname(dir).expand_path }
      @file = TMPROJ_DIR + [
        Digest::SHA256.hexdigest(@dirs.join('-').downcase),
        "#{common_dir(@dirs).basename}.tmproj"
      ].join('/')
    end

    def open
      data = Plist::parse_xml(file) rescue {}

      data['documents'] = dirs.map do |dir|
        ignores = Ignores.new(dir)
        {
          :expanded => true,
          :name => dir.basename.to_s,
          :regexFileFilter => "!#{ignores.file_r}",
          :regexFolderFilter => "!#{ignores.folder_r}",
          :sourceDirectory => dir.to_s
        }
      end
      data['fileHierarchyDrawerWidth'] ||= 269
      data['metaData'] ||= {}
      data['showFileHierarchyDrawer'] = true unless data.has_key?('showFileHierarchyDrawer')
      dimensions = IO.popen(%q{osascript -e 'tell application "Finder" to get bounds of window of desktop'}, &:read).scan(/\d+/).map(&:to_i)
      data['windowFrame'] ||= "{{#{dimensions[0] + 100}, #{dimensions[1] + 50}}, {#{dimensions[2] - 200}, #{dimensions[3] - 100}}}"

      file.dirname.mkpath
      file.open('w'){ |f| f.write(data.to_plist) }

      system 'open', file
    end

  private

    def common_dir(paths)
      common = nil
      paths.each do |path|
        if path.file?
          path = path.dirname
        end
        ascendants = []
        path.ascend do |ascendant|
          ascendants << ascendant
        end
        unless common
          common = ascendants
        else
          common &= ascendants
        end
      end
      common && common.first
    end
  end
end

require 'mate/tmproj/ignores'

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mate-1.1.2 lib/mate/tmproj.rb
mate-1.1.1.2 lib/mate/tmproj.rb
mate-1.1.1.1 lib/mate/tmproj.rb
mate-1.1.1-darwin lib/mate/tmproj.rb
mate-1.1.0 lib/mate/tmproj.rb
mate-1.0.2.1 lib/mate/tmproj.rb
mate-1.0.2 lib/mate/tmproj.rb
mate-1.0.1 lib/mate/tmproj.rb
mate-1.0.0 lib/mate/tmproj.rb