]> nos-oignons.net Git - gestion-adh.git/commitdiff
Move script logic into module methods
authorLunar <lunar@anargeek.net>
Sun, 2 Jun 2013 09:09:12 +0000 (11:09 +0200)
committerLunar <lunar@anargeek.net>
Sun, 2 Jun 2013 09:16:28 +0000 (11:16 +0200)
bin/list-emails
bin/pre-commit-hook
bin/pre-receive-hook
bin/update-ag-subscribers
lib/nos_oignons.rb [new file with mode: 0644]

index 6b13304b764262dd851848b068d1b879cff43c97..5315d8620549b4428395da6262306124189e30ae 100755 (executable)
@@ -5,10 +5,6 @@ require 'rubygems'
 require 'bundler'
 Bundler.setup
 
-require 'nos_oignons/subscriptions'
+require 'nos_oignons'
 
-NosOignons::Subscription.all.each do |subscription|
-  if subscription.up_to_date?
-    puts subscription.email
-  end
-end
+NosOignons.list_emails!
index 921c39c79f1c3f0c17eb410737c77482095c31fd..2bd3c77080d0438100ca804c34e4db631ec636d7 100755 (executable)
@@ -5,25 +5,6 @@ require 'rubygems'
 require 'bundler'
 Bundler.setup
 
-require 'nos_oignons/git'
-require 'nos_oignons/subscriptions'
+require 'nos_oignons'
 
-if system('git rev-parse --quiet --verify HEAD >/dev/null')
-  against = 'HEAD'
-else
-  # Initial commit: diff against an empty tree object
-  against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
-end
-
-IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
-  NosOignons::Git.handle_modified_files(io) do |file|
-    next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/")
-    begin
-      # Use empty ref to get the index
-      NosOignons::Subscription.read_from_git('', file)
-    rescue ArgumentError
-      $stderr.puts "Désolé : #{file} n'a pas le bon format !"
-      exit 1
-    end
-  end
-end
+NosOignons.pre_commit_hook!
index c86b3259ad66712b49154853748362d91ab3dfa6..8c204df4e9a47a94604936999bb10c83fb99b530 100755 (executable)
@@ -5,20 +5,6 @@ require 'rubygems'
 require 'bundler'
 Bundler.setup
 
-require 'nos_oignons/git'
-require 'nos_oignons/subscriptions'
+require 'nos_oignons'
 
-$stdin.readlines.each do |ref_line|
-  old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
-  IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
-    NosOignons::Git.handle_modified_files(io) do |file|
-      next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/")
-      begin
-        NosOignons::Subscription.read_from_git(new_value, file)
-      rescue ArgumentError
-        $stderr.puts "Désolé : #{file} n'a pas le bon format !"
-        exit 1
-      end
-    end
-  end
-end
+NosOignons.pre_receive_hook!($stdin)
index f10978ebc0fb26a5c657a4d6f722e48bf4a49281..59d5a91d659abad97decbdab7710a4470469527e 100755 (executable)
@@ -5,13 +5,6 @@ require 'rubygems'
 require 'bundler'
 Bundler.setup
 
-require 'nos_oignons/mailman'
-require 'nos_oignons/subscriptions'
+require 'nos_oignons'
 
-LIST = 'ag'
-
-current_emails = NosOignons::Mailman.list_members(LIST)
-uptodate_emails = NosOignons::Subscription.all.select(&:up_to_date?).collect(&:email)
-
-NosOignons::Mailman.add_members(LIST, uptodate_emails - current_emails)
-NosOignons::Mailman.remove_members(LIST, current_emails - uptodate_emails)
+NosOignons.update_ag_subscribers!
diff --git a/lib/nos_oignons.rb b/lib/nos_oignons.rb
new file mode 100644 (file)
index 0000000..d377775
--- /dev/null
@@ -0,0 +1,66 @@
+#-*- coding: utf-8 -*-
+
+require 'nos_oignons/git'
+require 'nos_oignons/mailman'
+require 'nos_oignons/subscriptions'
+
+module NosOignons
+  # The following class methods are all meant to be called as command-line scripts
+  class << self
+    def list_emails!
+      NosOignons::Subscription.all.each do |subscription|
+        if subscription.up_to_date?
+          puts subscription.email
+        end
+      end
+    end
+
+    MEMBER_MAILING_LIST = 'ag'
+    def update_ag_subscribers!
+      current_emails = NosOignons::Mailman.list_members(MEMBER_MAILING_LIST)
+      uptodate_emails = NosOignons::Subscription.all.select(&:up_to_date?).collect(&:email)
+
+      NosOignons::Mailman.add_members(MEMBER_MAILING_LIST, uptodate_emails - current_emails)
+      NosOignons::Mailman.remove_members(MEMBER_MAILING_LIST, current_emails - uptodate_emails)
+    end
+
+    def pre_commit_hook!
+      if system('git rev-parse --quiet --verify HEAD >/dev/null')
+        against = 'HEAD'
+      else
+        # Initial commit: diff against an empty tree object
+        against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
+      end
+
+      IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
+        NosOignons::Git.handle_modified_files(io) do |file|
+          next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/")
+          begin
+            # Use empty ref to get the index
+            NosOignons::Subscription.read_from_git('', file)
+          rescue ArgumentError
+            $stderr.puts "Désolé : #{file} n'a pas le bon format !"
+            exit 1
+          end
+        end
+      end
+    end
+
+    def pre_receive_hook!(stdin)
+      stdin.readlines.each do |ref_line|
+        old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
+        IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
+          NosOignons::Git.handle_modified_files(io) do |file|
+            next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/")
+            begin
+              NosOignons::Subscription.read_from_git(new_value, file)
+            rescue ArgumentError
+              $stderr.puts "Désolé : #{file} n'a pas le bon format !"
+              exit 1
+            end
+          end
+        end
+      end
+    end
+  end
+end