Commits

Shrikant Sharat Kandula committed b432286

Implemented Run command support and some code refactorings

Comments (0)

Files changed (2)

 	$ wget http://bitbucket.org/sharat87/vimpire/raw/428f69cf2d16/vimpire.py
 	$ python vimpire.py
 
+You may also include a `Run` directive below the `Bundle` directive to run a command
+after getting/updating the corresponding bundle. Note that this command will be run
+with the plugin's directory as the pwd. For example
+
+    " Bundle: git://github.com/tpope/vim-pathogen.git
+    " Run: ls -lh
+
 Deleting plugins is still experimental, but adding new ones and updating (pull) ing
 them works just fine.
 
     os.chdir(bundle_dir)
 
     plugin_dirs = []
+    bundles = []
 
     for line in bundles_file_lines:
-        m = re.match(r'^\s*"\s*(bundle)(!?):\s*(.*)$', line, re.I)
+        m = re.match(r'^\s*"\s*(\w+)(!?):(!?)\s*(.*)$', line, re.I)
         if not m: continue
-        bline = BLine(*m.groups())
-        bline.apply()
-        if bline.name == 'bundle':
-            plugin_dirs.append(bline.rman.dest_dir)
+        tline = TagLine(*m.groups())
+        if tline.name == 'bundle':
+            bundle = Bundle(tline)
+            bundles.append(bundle)
+            in_bundle_context = True
+        elif in_bundle_context:
+            bundle.add_tline(tline)
+        else:
+            in_bundle_context = False
 
-    all_dirs = os.listdir('.')
+    for bundle in bundles:
+        bundle.apply()
+        plugin_dirs.append(bundle.dest_dir)
+
     for dirname in (d for d in os.listdir('.') if d not in plugin_dirs):
         sh.rmtree(dirname)
 
-class BLine(object):
+class TagLine(object):
 
-    def __init__(self, name, is_bang, line):
+    def __init__(self, name, bang1, bang2, line):
         self.name = name.lower()
-        self.is_bang = is_bang
+        self.is_bang = bool(bang1 or bang2)
         self.line = line
 
+
+class Bundle(object):
+
+    def __init__(self, tline):
+        self.name = tline.name
+        self.is_bang = tline.is_bang
+        self.line = tline.line
+
+        self.cmds = []
+
+    def add_tline(self, tline):
+        if tline.name in 'run command cmd'.split():
+            self.cmds.append(tline.line)
+
     def apply(self):
-        getattr(self, '_' + self.name)()
-
-    def _bundle(self):
         url = self.line.split()[0]
         arg = self.line.lstrip(url)
 
+        # Detect the vcs to use
         vcs_match = re.search(r'with\s+(\w+)', arg)
-        vcs = vcs_match.group(1) if vcs_match else None
+        if vcs_match:
+            vcs = vcs_match.group(1)
+        elif 'bitbucket.org' in url or 'googlecode.com/hg' in url:
+            vcs = 'hg'
+        elif url.startswith('git:') or 'github.com' in url:
+            vcs = 'git'
+        elif url.startswith('svn:'):
+            vcs = 'svn'
+        else:
+            vcs = None
+            print 'Unable to detect vcs for', url
+        self.vcs = vcs
 
+        # Detect the plugin directory to clone/checkout into
         dest_dir_match = re.search(r'to\s+(\w+)', arg)
-        dest_dir = dest_dir_match.group(1) if dest_dir_match else None
-
-        self.rman = RepoManager(url, vcs, dest_dir)
-        if self.rman.vcs is not None:
-            print '+------------'
-            self.rman.reflect()
-
-class RepoManager(object):
-
-    def __init__(self, url, vcs=None, dest_dir=None):
-        self.url = url
-        self.vcs = vcs
+        if dest_dir_match:
+            dest_dir = dest_dir_match.group(1)
+        else:
+            dest_dir = url.split('/')[-1]
+        if dest_dir.endswith('.' + vcs):
+            dest_dir = dest_dir[:-(1 + len(vcs))]
         self.dest_dir = dest_dir
 
-        self.check_vcs()
-        self.check_dest_dir()
+        if vcs is not None:
+            print ('!' if self.is_bang else '-') * 20, self.dest_dir
+            getattr(self, '_vcs_' + vcs)()
 
-    def check_vcs(self):
-        if self.vcs is not None:
-            return
-        if 'bitbucket.org' in self.url or 'googlecode.com/hg' in self.url:
-            self.vcs = 'hg'
-        elif self.url.startswith('git:') or 'github.com' in self.url:
-            self.vcs = 'git'
-        elif self.url.startswith('svn:'):
-            self.vcs = 'svn'
-        else:
-            print 'Unable to detect vcs for', self.url
+    def _run_cmds(self):
+        previous_pwd = os.path.abspath('.')
+        os.chdir(self.dest_dir)
+        for cmd in self.cmds:
+            print cmd
+            sp.call(cmd, shell=True)
+        os.chdir(previous_pwd)
 
-    def check_dest_dir(self):
-        if self.dest_dir is not None:
-            return
-        self.dest_dir = self.url.split('/')[-1]
-        if self.dest_dir.endswith('.' + self.vcs):
-            self.dest_dir = self.dest_dir[:-(1 + len(self.vcs))]
+    def _vcs_hg(self):
+        self.cmds.insert(0, 'hg ' + ('pull' if os.path.exists(self.dest_dir) else 'clone ' + self.url))
+        self._run_cmds()
 
-    def reflect(self):
-        getattr(self, '_' + self.vcs)()
-
-    def _hg(self):
-        if os.path.exists(self.dest_dir):
-            cmd = 'cd ' + self.dest_dir + ' && hg pull'
-        else:
-            cmd = 'hg clone ' + self.url + ''
-        print cmd
-        sp.call(cmd, shell=True)
-
-    def _git(self):
-        if os.path.exists(self.dest_dir):
-            cmd = 'cd ' + self.dest_dir + ' && git pull'
-        else:
-            cmd = 'git clone ' + self.url
-        print cmd
-        sp.call(cmd, shell=True)
+    def _vcs_git(self):
+        self.cmds.insert(0, 'git ' + ('pull' if os.path.exists(self.dest_dir) else 'clone ' + self.url))
+        self._run_cmds()
 
 if __name__ == '__main__':
     main()