rusky 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a00a714408281d031099446610b64c09660f508b
4
- data.tar.gz: fba6ebd6126cb2381e0663143261f1af85f2355f
3
+ metadata.gz: a2481de2ebd82496ba818a663ad9939ca9bf62bf
4
+ data.tar.gz: 96c97ed651c2317205e24851be6ca21044f8d828
5
5
  SHA512:
6
- metadata.gz: 07b16db70461073fbc0791f5476aa81ec92b89e23477d3165b49820216bdbe7f8f0858ee6246ce526b7f1fe8cb3b87e4d6b1866380805626fa7afd561bae2fde
7
- data.tar.gz: 84c6e65a3bc61ef46162fa58e12e20abbf5773674da0d2381ff210a0b415ef8fdecbd270cd44fa7c40fa1b590f4b1be6f5724dff4390e22c150007e486c753c1
6
+ metadata.gz: ad6a71c3a2c76724f35a796f04222c54e8b84d67d5078ddc34553c3ccc3e1b3ee47265e6c48b445035c7f1ef02627aac17ae6a9e314363f387fb7cc8cf1962f0
7
+ data.tar.gz: b6ac28dd6aaca658bb5c9d60d455c62b8d195f55c9912d40c667f2e98b8ca4ffa6d14d80a08d47bc6166acaee100b07ccc359d9cffc438c80553946919eeef15
@@ -1,7 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.4.1
5
- before_install: gem install bundler -v 1.15.4
4
+ - 2.4.4
5
+ - 2.5.1
6
6
  script:
7
7
  - bundle exec rspec
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rubygems"
2
4
  require "rusky"
3
5
 
@@ -1,134 +1,50 @@
1
- require 'rusky/version'
2
1
  require 'rusky/cli'
2
+ require 'rusky/hook'
3
+ require 'rusky/hooks'
4
+ require 'rusky/setting'
5
+ require 'rusky/version'
3
6
  require 'yaml'
4
7
  require 'fileutils'
5
8
 
6
9
  module Rusky
7
- HOOKS = %w[
8
- applypatch-msg
9
- pre-applypatch
10
- post-applypatch
11
- pre-commit
12
- prepare-commit-msg
13
- commit-msg
14
- post-commit
15
- pre-rebase
16
- post-checkout
17
- post-merge
18
- pre-push
19
- pre-receive
20
- update
21
- post-receive
22
- post-update
23
- push-to-checkout
24
- pre-auto-gc
25
- post-rewrite
26
- sendemail-validate
27
- ].freeze
28
-
29
10
  def self.install
30
- cwd = `lsof -p #{Process.ppid} | grep cwd`.split(" ").last
11
+ cwd = current_work_directory_name
31
12
 
32
- git_path = File.join(cwd, '.git')
33
- if !File.exists? git_path
34
- puts "can't find .git directory, skipping Git hooks installation"
13
+ unless processable?(cwd)
14
+ puts "rusky > can't find .git directory, so skipping rusky process"
35
15
  return
36
16
  end
37
17
 
38
- hook_path = File.join(git_path, 'hooks')
39
- if !File.exists? hook_path
40
- FileUtils.mkdir_p hook_path
41
- end
18
+ Rusky::Hooks.new(cwd).create
19
+ Rusky::Setting.new(cwd).create
42
20
 
43
- HOOKS.each do |hook_name|
44
- create_hook(hook_name, hook_path, cwd)
45
- end
46
-
47
- rusky_setting_file_path = File.join(cwd, '.rusky')
48
- if !File.exists? rusky_setting_file_path
49
- File.write(rusky_setting_file_path, '')
50
- end
21
+ puts "rusky > installation is done. enjoy!"
51
22
  rescue => e
52
- puts "unexpected error happened: #{e.inspect}"
53
- end
54
-
55
- def self.create_hook(hook_name, hook_path, cwd)
56
- script = get_hook_script(hook_name, cwd)
57
- filename = File.join(hook_path, hook_name)
58
-
59
- if File.exists? filename
60
- if File.read(filename).include? 'rusky'
61
- # Overwrite
62
- write(filename, script)
63
- else
64
- # Keep user original Git hook
65
- end
66
- else
67
- write(filename, script)
68
- end
69
- end
70
-
71
- def self.write(filename, script)
72
- File.write filename, script
73
- FileUtils.chmod(0755, filename)
23
+ puts "rusky > unexpected error happened: #{e.inspect}"
74
24
  end
75
25
 
76
- def self.get_hook_script(hook_name, cwd)
77
- no_verify_message = if hook_name == 'prepare-commit-msg'
78
- '(cannot be bypassed with --no-verify due to Git specs)'
79
- else
80
- '(add --no-verify to bypass)'
81
- end
82
-
83
- rake_task_name = "rusky:#{hook_name.gsub('-', '_')}"
84
-
85
- <<~EOS
86
- #!/bin/sh
87
- #rusky #{Rusky::VERSION}
88
- has_hook_script () {
89
- [ -f .rusky ] && cat .rusky | grep -q "$1:"
90
- }
91
- cd "#{cwd}"
92
- # Check if #{hook_name} script is defined, skip if not
93
- has_hook_script #{hook_name} || exit 0
94
-
95
- # Export Git hook params
96
- export GIT_PARAMS="$*"
97
- # Run command
98
- echo "rusky > #{hook_name} Git hook is running"
99
- echo "rusky > bundle exec rake #{rake_task_name}"
100
- echo
101
- bundle exec rake #{rake_task_name} || {
102
- echo
103
- echo "rusky > #{hook_name} Git hook failed #{no_verify_message}"
104
- exit 1
105
- }
106
- EOS
107
- end
108
-
109
-
110
26
  def self.uninstall
111
- cwd = `lsof -p #{Process.ppid} | grep cwd`.split(" ").last
27
+ cwd = current_work_directory_name
112
28
 
113
- HOOKS.each do |hook_name|
114
- remove_hook(cwd, hook_name)
29
+ unless processable?(cwd)
30
+ puts "rusky > can't find .git directory, so skipping rusky process"
31
+ return
115
32
  end
116
33
 
117
- rusky_setting_file_path = File.join(cwd, '.rusky')
118
- if File.exists? rusky_setting_file_path
119
- File.delete(rusky_setting_file_path)
120
- puts "rusky > removing .rusky file..."
121
- end
34
+ Rusky::Hooks.new(cwd).delete
35
+ Rusky::Setting.new(cwd).delete
122
36
 
123
- puts "rusky > uninstall is done. please remove rake tasks for rusky if you have them"
37
+ puts "rusky > uninstallation is done. please remove rake tasks for rusky if you have them"
124
38
  puts "rusky > Thank you for using rusky!"
39
+ rescue => e
40
+ puts "rusky > unexpected error happened: #{e.inspect}"
125
41
  end
126
42
 
127
- def self.remove_hook(cwd, hook_name)
128
- filename = File.join(cwd, '.git', 'hooks', hook_name)
129
- if File.exists?(filename) && File.read(filename).include?('rusky')
130
- puts "rusky > removing #{hook_name} hook script..."
131
- File.delete(filename)
132
- end
43
+ def self.current_work_directory_name
44
+ `lsof -p #{Process.ppid} | grep cwd`.split(" ").last
45
+ end
46
+
47
+ def self.processable?(cwd)
48
+ File.exists? File.join(cwd, '.git')
133
49
  end
134
50
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rusky'
2
4
  require 'thor'
3
5
 
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'yaml'
5
+ require 'fileutils'
6
+
7
+ module Rusky
8
+ class Hook
9
+ include Rake::DSL if defined? Rake::DSL
10
+
11
+ attr_reader :hook_name, :cwd, :filename, :setting
12
+
13
+ def initialize(hook_name, cwd, setting=nil)
14
+ @hook_name = hook_name
15
+ @cwd = cwd
16
+ @filename = File.join(@cwd, '.git', 'hooks', hook_name)
17
+ @setting = setting
18
+ end
19
+
20
+ def create
21
+ if !exists?
22
+ puts "rusky > creating #{hook_name} hook script..."
23
+ write
24
+ else
25
+ if is_rusky_hook?
26
+ puts "rusky > overwriting #{hook_name} hook script because also existing one is created by rusky."
27
+ write
28
+ else
29
+ puts "rusky > skip creating #{hook_name} hook script because existing one is created by you or other tool."
30
+ end
31
+ end
32
+ self
33
+ end
34
+
35
+ def delete
36
+ if !exists?
37
+ puts "rusky > skip deleting #{hook_name} hook script since there is no git hook file."
38
+ else
39
+ if is_rusky_hook?
40
+ puts "rusky > removing #{hook_name} hook script..."
41
+ File.delete(filename)
42
+ else
43
+ puts "rusky > skip deleting #{hook_name} hook script because existing one is created by you or other tool."
44
+ end
45
+ end
46
+ self
47
+ end
48
+
49
+ def define_task
50
+ # prioritize existing user hook
51
+ if Rake::Task.task_defined? rake_task_name
52
+ puts "rusky > skip creating a new rake task since the task for #{hook_name} is already defined."
53
+ return
54
+ end
55
+
56
+ commands = setting&.commands_for hook_name
57
+
58
+ task "#{rake_task_name}" do
59
+ if commands.empty?
60
+ puts "rusky > No command for #{hook_name} is defined in .rusky file"
61
+ next
62
+ end
63
+
64
+ commands.each do |command|
65
+ puts "rusky > #{command}"
66
+ system(command) || raise("rusky > #{command} failed")
67
+ end
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def rake_task_name
74
+ @rake_task_name ||= "rusky:#{hook_name.gsub('-', '_')}"
75
+ end
76
+
77
+ def exists?
78
+ @exists ||= File.exists?(filename)
79
+ end
80
+
81
+ def is_rusky_hook?
82
+ @is_rusky_hook ||= exists? && File.read(filename).include?('rusky')
83
+ end
84
+
85
+ def write
86
+ File.write filename, hook_script
87
+ FileUtils.chmod(0755, filename)
88
+ end
89
+
90
+ def no_verify_message
91
+ can_bypass? ? '(add --no-verify to bypass)' : '(cannot be bypassed with --no-verify due to Git specs)'
92
+ end
93
+
94
+ def can_bypass?
95
+ hook_name != 'prepare-commit-msg'
96
+ end
97
+
98
+ def hook_script
99
+ <<~EOS
100
+ #!/bin/sh
101
+ #rusky #{Rusky::VERSION}
102
+ has_hook_script () {
103
+ [ -f .rusky ] && cat .rusky | grep -q "$1:"
104
+ }
105
+ cd "#{cwd}"
106
+ # Check if #{hook_name} script is defined, skip if not
107
+ has_hook_script #{hook_name} || exit 0
108
+
109
+ # Export Git hook params
110
+ export GIT_PARAMS="$*"
111
+ # Run command
112
+ echo "rusky > #{hook_name} Git hook is running"
113
+ echo "rusky > bundle exec rake #{rake_task_name}"
114
+ echo
115
+ bundle exec rake #{rake_task_name} || {
116
+ echo
117
+ echo "rusky > #{hook_name} Git hook failed #{no_verify_message}"
118
+ exit 1
119
+ }
120
+ EOS
121
+ end
122
+ end
123
+ end
124
+
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rusky'
4
+ require 'rusky/hook'
5
+ require 'fileutils'
6
+
7
+ module Rusky
8
+ class Hooks
9
+ DIR_NAME = 'hooks'
10
+
11
+ HOOK_NAMES = %w[
12
+ applypatch-msg
13
+ pre-applypatch
14
+ post-applypatch
15
+ pre-commit
16
+ prepare-commit-msg
17
+ commit-msg
18
+ post-commit
19
+ pre-rebase
20
+ post-checkout
21
+ post-merge
22
+ pre-push
23
+ pre-receive
24
+ update
25
+ post-receive
26
+ post-update
27
+ push-to-checkout
28
+ pre-auto-gc
29
+ post-rewrite
30
+ sendemail-validate
31
+ ].freeze
32
+
33
+ attr_reader :cwd, :dir_name, :hooks
34
+
35
+ def initialize(cwd, setting=nil)
36
+ @cwd = cwd
37
+ @dir_name = File.join(cwd, '.git', 'hooks')
38
+ @hooks = HOOK_NAMES.map { |hook_name| Rusky::Hook.new(hook_name, cwd, setting) }
39
+ end
40
+
41
+ def create
42
+ mkdir unless exists?
43
+ hooks.each(&:create)
44
+ end
45
+
46
+ def delete
47
+ return unless exists?
48
+ hooks.each(&:delete)
49
+ end
50
+
51
+ def define_tasks
52
+ hooks.each(&:define_task)
53
+ end
54
+
55
+ private
56
+
57
+ def exists?
58
+ @exists ||= File.exists? dir_name
59
+ end
60
+
61
+ def mkdir
62
+ FileUtils.mkdir_p dir_name
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Rusky
6
+ class Setting
7
+ attr_reader :cwd, :filename
8
+
9
+ FILENAME = '.rusky'
10
+
11
+ def initialize(cwd)
12
+ @cwd = cwd
13
+ @filename = File.join(@cwd, FILENAME)
14
+ @yaml = exists? ? YAML.load_file(filename) : Hash.new([])
15
+ end
16
+
17
+ def create
18
+ if !exists?
19
+ puts "rusky > creating .rusky file..."
20
+ File.write(filename, '')
21
+ end
22
+ self
23
+ end
24
+
25
+ def delete
26
+ if exists?
27
+ puts "rusky > deleting .rusky file..."
28
+ File.delete(filename)
29
+ end
30
+ self
31
+ end
32
+
33
+ def commands_for(hook_name)
34
+ @yaml[hook_name]
35
+ end
36
+
37
+ private
38
+
39
+ def exists?
40
+ @exists ||= File.exists? filename
41
+ end
42
+ end
43
+ end
44
+
@@ -1,11 +1,10 @@
1
- require 'rake'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'yaml'
3
4
  require 'rusky'
4
5
 
5
6
  module Rusky
6
7
  class Task
7
- include Rake::DSL if defined? Rake::DSL
8
-
9
8
  class << self
10
9
  def install(base=nil)
11
10
  new(base).install
@@ -13,36 +12,12 @@ module Rusky
13
12
  end
14
13
 
15
14
  def initialize(base=nil)
16
- @base = base || `lsof -p #{Process.ppid} | grep cwd`.split(" ").last
17
- rusky_setting_file_path = File.join(@base, '.rusky')
18
- @yaml = File.exists?(rusky_setting_file_path) ? YAML.load_file(File.join(@base, '.rusky')) : Hash.new([])
15
+ @cwd = base || Rusky.current_work_directory_name
19
16
  end
20
17
 
21
18
  def install
22
- Rusky::HOOKS.each do |hook_name|
23
- rake_task_name = "rusky:#{hook_name.gsub('-', '_')}"
24
-
25
- # prioritize existing user hook
26
- next if Rake::Task.task_defined? rake_task_name
27
-
28
- define_task hook_name, rake_task_name
29
- end
30
- end
31
-
32
- def define_task(hook_name, rake_task_name)
33
- task "#{rake_task_name}" do
34
- commands = @yaml[hook_name]
35
-
36
- if commands.empty?
37
- puts "rusky > No command for #{hook_name} is defined in .rusky file"
38
- next
39
- end
40
-
41
- commands.each do |command|
42
- puts "rusky > #{command}"
43
- system(command) || raise("rusky > #{command} failed")
44
- end
45
- end
19
+ setting = Rusky::Setting.new(@cwd)
20
+ Rusky::Hooks.new(@cwd, setting).define_tasks
46
21
  end
47
22
  end
48
23
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rusky
2
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rusky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masato Ohba
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-09 00:00:00.000000000 Z
11
+ date: 2018-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -101,6 +101,9 @@ files:
101
101
  - lib/rubygems_plugin.rb
102
102
  - lib/rusky.rb
103
103
  - lib/rusky/cli.rb
104
+ - lib/rusky/hook.rb
105
+ - lib/rusky/hooks.rb
106
+ - lib/rusky/setting.rb
104
107
  - lib/rusky/task.rb
105
108
  - lib/rusky/version.rb
106
109
  - rusky.gemspec