r866 - in /trunk/utils/addons/src: vim-addons vim/addon-manager.rb vim/common.rb vim/constants.rb vim/registry.rb

zack at users.alioth.debian.org zack at users.alioth.debian.org
Wed Jan 24 21:27:59 UTC 2007


Author: zack
Date: Wed Jan 24 22:27:58 2007
New Revision: 866

URL: http://svn.debian.org/wsvn/?sc=1&rev=866
Log:
snapshot:
- probably the first fully implemented version committed (i.e. system-wide
  addon support in place, with features for disabling/amending addons)
- better code organization in multiple files (embrace the Ruby way!)

Added:
    trunk/utils/addons/src/vim/common.rb   (with props)
    trunk/utils/addons/src/vim/constants.rb   (with props)
Modified:
    trunk/utils/addons/src/vim-addons
    trunk/utils/addons/src/vim/addon-manager.rb
    trunk/utils/addons/src/vim/registry.rb

Modified: trunk/utils/addons/src/vim-addons
URL: http://svn.debian.org/wsvn/trunk/utils/addons/src/vim-addons?rev=866&op=diff
==============================================================================
--- trunk/utils/addons/src/vim-addons (original)
+++ trunk/utils/addons/src/vim-addons Wed Jan 24 22:27:58 2007
@@ -12,9 +12,9 @@
 # Created:    Tue, 16 Jan 2007 10:04:20 +0100 zack
 # Last-Modified:  $Id$
 #
-# TODO: support usage of system-wide plugins
 # TODO: create a vim-addons-manager package shipping vim-addons
 # TODO: rework the README.Debian of vim-scripts pointing to vim-addons
+# TODO: work on vim-{scripts,latex-suite} to add support for 'disabledby'
 #
 
 =begin
@@ -136,6 +136,7 @@
 require 'getoptlong'
 
 require 'vim/addon-manager'
+require 'vim/common'
 require 'vim/registry'
 
 def die_usage
@@ -159,9 +160,10 @@
 
 def parse_cmdline
   options = { # defaults
+    :registry_dir => '/usr/share/vim/registry',
     :source_dir => '/usr/share/vim/addons',
+    :system_dir => '/var/lib/vim/addons',
     :target_dir => File.join(ENV['HOME'], '.vim'),
-    :registry_dir => '/usr/share/vim/registry',
   }
   cmds = %w{install remove disable amend list status files}
   req_arg_cmds = # commands requiring >= 1 arg
@@ -196,21 +198,25 @@
 end
 
 cmd, args, options = parse_cmdline
-registry = Vim::AddonRegistry.new(options[:registry_dir])
+registry = Vim::AddonRegistry.new(options[:registry_dir], options[:source_dir])
 selected_addons =
   args.empty? ? registry.to_a : registry.select {|a| args.member? a.name}
+unknown = args.select {|name| not (registry.any? {|a| a.name == name})}
+Vim.warn "Ignoring unknown addons: #{unknown.join ', '}" unless unknown.empty?
 
 case cmd
 when 'list'
   puts registry.sort
 when 'status'
+  printf("# %-25s%-13s%-14s\n", 'Name', 'User Status', 'System Status')
   selected_addons.sort.each do |a|
-    printf "%-28s%s\n", a, a.status(options[:target_dir], options[:system_dir])
+    printf("%-28s%-14s%-14s\n", a, a.status(options[:target_dir]),
+           a.status(options[:system_dir]))
   end
 when 'files'
   selected_addons.each {|a| puts a.files.to_a}
 else
-  Vim::AddonManager.send cmd, selected_addons,
-    options[:source_dir], options[:target_dir], options[:system_dir]
+  mgr = Vim::AddonManager.new options[:source_dir], options[:target_dir]
+  mgr.send cmd, selected_addons
 end
 

Modified: trunk/utils/addons/src/vim/addon-manager.rb
URL: http://svn.debian.org/wsvn/trunk/utils/addons/src/vim/addon-manager.rb?rev=866&op=diff
==============================================================================
--- trunk/utils/addons/src/vim/addon-manager.rb (original)
+++ trunk/utils/addons/src/vim/addon-manager.rb Wed Jan 24 22:27:58 2007
@@ -1,3 +1,5 @@
+# vim-addons: command line manager of Vim addons
+#
 # Copyright (C) 2007 Stefano Zacchiroli
 #
 # This program is free software, you can redistribute it and/or modify it under
@@ -9,20 +11,30 @@
 
 require 'fileutils'
 
+require 'vim/common'
+
 module Vim
 
   class AddonManager
 
-    def AddonManager.install(addons, src_dir, target_dir, system_dir)
+    def initialize(source_dir, target_dir)
+      @source_dir = source_dir
+      @target_dir = target_dir
+    end
+
+    attr_accessor :source_dir
+    attr_accessor :target_dir
+
+    def install(addons)
       addons.each do |a|
-	src_dir = (a.basedir or src_dir)
+	base_dir = (a.basedir or @source_dir)
 	symlink = lambda do |f|
-	  dest = File.join(target_dir, f)
+	  dest = File.join(@target_dir, f)
 	  dest_dir = File.dirname dest
 	  FileUtils.mkdir_p dest_dir unless File.directory? dest_dir
-	  FileUtils.ln_sf(File.join(src_dir, f), dest)
+	  FileUtils.ln_sf(File.join(base_dir, f), dest)
 	end
-	status = a.status(target_dir, system_dir)
+	status = a.status(@target_dir)
 	case status.status
 	when :broken
 	  status.missing_files.each(&symlink)
@@ -32,12 +44,12 @@
       end
     end
 
-    def AddonManager.remove(addons, src_dir, target_dir, system_dir)
+    def remove(addons)
       # TODO remove empty directories (recursively toward the top of ~/.vim/,
       # a la rmdir -p)
-      rmlink = lambda {|f| File.delete(File.join(target_dir, f)) }
+      rmlink = lambda {|f| File.delete(File.join(@target_dir, f)) }
       addons.each do |a|
-	status = a.status(target_dir, system_dir)
+	status = a.status(@target_dir)
 	case status.status
 	when :installed
 	  a.files.each(&rmlink)
@@ -47,6 +59,57 @@
       end
     end
 
+    def disable(addons)
+      map_override_lines do |lines|
+        addons.each do |addon|  # disable each not yet disabled addon
+          if not addon.disabled_by_line
+            Vim.warn \
+              "#{addon} can't be disabled (since it has no disabledby field)"
+            next
+          end
+          unless lines.any? {|line| addon.is_disabled_by? line}
+            lines << addon.disabled_by_line + "\n"
+          end
+        end
+      end
+    end
+
+    def amend(addons)
+      map_override_lines do |lines|
+        addons.each do |addon|
+          if not addon.disabled_by_line
+            Vim.warn \
+              "#{addon} can't be amended (since it has no disabledby field)"
+            next
+          end
+          lines.reject! {|line| addon.is_disabled_by? line}
+        end
+      end
+    end
+
+    private
+    
+    def map_override_lines
+      override_lines = []
+      override_file = Vim.override_file @target_dir
+      if File.exist? override_file
+        File.open(override_file) do |file|
+          override_lines += file.to_a
+        end
+      end
+      checksum = override_lines.hash
+
+      yield override_lines
+
+      if override_lines.empty?
+        FileUtils.rm override_file if File.exist? override_file
+      elsif override_lines.hash != checksum
+        File.open(override_file, 'w') do |file|
+          file.write override_lines
+        end
+      end
+    end
+
   end
 
 end

Added: trunk/utils/addons/src/vim/common.rb
URL: http://svn.debian.org/wsvn/trunk/utils/addons/src/vim/common.rb?rev=866&op=file
==============================================================================
--- trunk/utils/addons/src/vim/common.rb (added)
+++ trunk/utils/addons/src/vim/common.rb Wed Jan 24 22:27:58 2007
@@ -1,0 +1,39 @@
+# vim-addons: command line manager of Vim addons
+#
+# Copyright (C) 2007 Stefano Zacchiroli
+#
+# This program is free software, you can redistribute it and/or modify it under
+# the terms of the GNU General Public License version 2 as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# $Id$
+
+require 'vim/constants'
+
+module Vim
+
+  def Vim.override_file(dir)
+    File.join dir, OVERRIDE_FILE
+  end
+
+
+  @verbosity = 0
+
+  class << self
+
+    def increase_verbosity
+      @verbosity += 1
+    end
+
+    def verbose?
+      @verbosity >= 1
+    end
+
+    def warn(s)
+      puts "Warning: #{s}"
+    end
+  end
+
+
+end

Propchange: trunk/utils/addons/src/vim/common.rb
------------------------------------------------------------------------------
    svn:keywords = Id

Added: trunk/utils/addons/src/vim/constants.rb
URL: http://svn.debian.org/wsvn/trunk/utils/addons/src/vim/constants.rb?rev=866&op=file
==============================================================================
--- trunk/utils/addons/src/vim/constants.rb (added)
+++ trunk/utils/addons/src/vim/constants.rb Wed Jan 24 22:27:58 2007
@@ -1,0 +1,16 @@
+# vim-addons: command line manager of Vim addons
+#
+# Copyright (C) 2007 Stefano Zacchiroli
+#
+# This program is free software, you can redistribute it and/or modify it under
+# the terms of the GNU General Public License version 2 as published by the
+# Free Software Foundation; either version 2 of the License, or (at your
+# option) any later version.
+#
+# $Id$
+
+module Vim
+
+  OVERRIDE_FILE = 'plugin/000-vim-addons.vim'
+
+end

Propchange: trunk/utils/addons/src/vim/constants.rb
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: trunk/utils/addons/src/vim/registry.rb
URL: http://svn.debian.org/wsvn/trunk/utils/addons/src/vim/registry.rb?rev=866&op=diff
==============================================================================
--- trunk/utils/addons/src/vim/registry.rb (original)
+++ trunk/utils/addons/src/vim/registry.rb Wed Jan 24 22:27:58 2007
@@ -1,3 +1,5 @@
+# vim-addons: command line manager of Vim addons
+#
 # Copyright (C) 2007 Stefano Zacchiroli
 #
 # This program is free software, you can redistribute it and/or modify it under
@@ -11,20 +13,9 @@
 require 'set'
 require 'yaml'
 
+require 'vim/common'
+
 module Vim
-
-  @verbosity = 0
-
-  class << self
-    def increase_verbosity
-      @verbosity += 1
-    end
-
-    def verbose?
-      @verbosity >= 1
-    end
-  end
-
 
   # an addon status is one of the following
   # - :not_installed
@@ -32,41 +23,54 @@
   # - :broken (missing_files attribute is then used to list not installed
   # files)
   #
-  AddonStatus = Struct.new(:status, :missing_files)
-  class AddonStatus
+  AddonStatusStruct = Struct.new(:status, :missing_files)
+
+  class AddonStatus < AddonStatusStruct
+
+    def initialize(*args)
+      super(*args)
+      @disabled = false
+    end
+
+    attr_accessor :disabled
+
     def to_s
-      case status
-      when :installed
-	  'installed'
-      when :not_installed
-	  'removed'
-      when :broken
-	  s = 'broken'
-	  s << " (missing: #{missing_files.join ', '})" if Vim.verbose?
-	  s
+      if @disabled
+        'disabled'
+      else
+        case status
+        when :installed
+            'installed'
+        when :not_installed
+            'removed'
+        when :broken
+            s = 'broken'
+            s << " (missing: #{missing_files.join ', '})" if Vim.verbose?
+            s
+        end
       end
     end
 
-    def ===(other)
-      case other
-      when AddonStatus
-	self.status == other.status
-      else
-	false
-      end
-    end
   end
 
 
   class Addon
-    def initialize(yaml)
-      @basedir = yaml['basedir']
+
+    def initialize(yaml, basedir)
+      @basedir = (yaml['basedir'] or basedir)
       @description = yaml['description']
+      @name = yaml['addon']
+
       @files = Set.new yaml['files']
-      @name = yaml['addon']
-      @status = nil
+      raise ArgumentError.new('empty addon') if @files.size == 0
 
-      raise ArgumentError.new('empty addon') if @files.size == 0
+      @disabled_by_line = yaml['disabledby']
+      if @disabled_by_line then
+        @disabled_by_RE = /^\s*#{Regexp.escape @disabled_by_line}\s*$/
+      else
+        @disabled_by_RE = nil
+      end
+
     end
 
     # return the status of the self add-on wrt a target installation
@@ -75,20 +79,36 @@
     # at all), :installed (the addon is completely installed), :broken (the
     # addon is only partially installed)
     #
-    def status(target_dir, system_dir)
+    def status(target_dir)
       expected = @files.collect {|f| File.join(target_dir, f)}
-      installed = expected.select {|f| File.symlink? f}
+      installed = expected.select do |f|
+        case
+        when (File.exist? f)
+          true
+        #when (File.symlink? f)
+          #(File.readlink f) ==
+          #(File.join @basedir, f.gsub(/^#{Regexp.escape target_dir}\/*/, ''))
+        #when (File.file? f)
+          #true
+        else
+          false
+        end
+      end
 
-      if installed.size == expected.size
-	AddonStatus.new :installed
-      elsif installed.size == 0
-	AddonStatus.new :not_installed
-      else
-	missing = expected - installed
-	prefix = /^#{Regexp.escape target_dir}\/+/o
-	missing.collect! {|f| f.gsub(prefix, '')}
-	AddonStatus.new(:broken, missing)
-      end
+      status =
+        if installed.size == expected.size
+          AddonStatus.new :installed
+        elsif installed.size == 0
+          AddonStatus.new :not_installed
+        else
+          missing = expected - installed
+          prefix = /^#{Regexp.escape target_dir}\/+/o
+          missing.collect! {|f| f.gsub(prefix, '')}
+          AddonStatus.new(:broken, missing)
+        end
+
+      status.disabled = is_disabled_in? target_dir
+      status
     end
 
     def to_s
@@ -99,21 +119,44 @@
       self.name <=> other.name
     end
 
+    # checks if a given line (when present in a Vim configuration file) is
+    # suitable for disabling the addon
+    #
+    def is_disabled_by?(line)
+      return false unless @disabled_by_RE # the addon can't be disabled if no
+                                          # disabledby field has been provided
+      line =~ @disabled_by_RE ? true : false
+    end
+
     attr_reader :basedir
     attr_reader :description
     attr_reader :files
     attr_reader :name
+    attr_reader :disabled_by_line
     alias_method :addon, :name
+
+    private
+
+    # checks whether the addon is disabled wrt a given target installation dir
+    #
+    def is_disabled_in?(target_dir)
+      return false unless File.exist?(Vim.override_file(target_dir))
+      File.open(Vim.override_file(target_dir)) do |file|
+        file.any? {|line| is_disabled_by? line}
+      end
+    end
+
   end
 
 
   class AddonRegistry
+
     include Enumerable
 
-    def initialize(registry_dir)
-      @dir = registry_dir
+    def initialize(registry_dir, source_dir)
+      @basedir = source_dir # default basedir, can be overridden by addons
       @addons = {}
-      AddonRegistry.each_addon(@dir) {|a| @addons[a.name] = a}
+      AddonRegistry.each_addon(registry_dir, @basedir) {|a| @addons[a.name] = a}
     end
 
     def [](name)
@@ -124,7 +167,7 @@
       @addons.each_value {|a| yield a}
     end
 
-    def AddonRegistry.each_addon(dir)
+    def AddonRegistry.each_addon(dir, basedir)
       Find.find(dir) do |path|
 	# selects .yaml files (non-recursively) contained in dir
 	next if path == dir
@@ -133,7 +176,7 @@
 	  Find.prune if path !~ /\.yaml$/
 	  File.open path do |f|
 	    YAML.load_documents f do |ydoc|
-	      yield(Addon.new(ydoc)) if ydoc
+	      yield(Addon.new(ydoc, basedir)) if ydoc
 	    end
 	  end
 	end
@@ -142,6 +185,5 @@
 
   end
 
-
 end
 




More information about the pkg-vim-maintainers mailing list