lolcommits 0.5.2 → 0.5.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.
@@ -1,33 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Lolcommits
2
3
  class Configuration
3
4
  LOLBASEDIR = ENV['LOLCOMMITS_DIR'] || File.join(ENV['HOME'], '.lolcommits')
4
5
  LOLCOMMITS_ROOT = File.join(File.dirname(__FILE__), '../..')
5
6
  attr_writer :loldir
6
7
 
7
- def initialize(attributes={})
8
+ def initialize(attributes = {})
8
9
  attributes.each do |attr, val|
9
10
  self.send("#{attr}=", val)
10
11
  end
11
12
  end
12
13
 
13
- def self.platform
14
- if is_fakeplatform?
15
- ENV['LOLCOMMITS_FAKEPLATFORM']
16
- elsif is_fakecapture?
17
- 'Fake'
18
- elsif is_mac?
19
- 'Mac'
20
- elsif is_linux?
21
- 'Linux'
22
- elsif is_windows?
23
- 'Windows'
24
- elsif is_cygwin?
25
- 'Cygwin'
26
- else
27
- raise "Unknown / Unsupported Platform."
28
- end
29
- end
30
-
31
14
  def read_configuration
32
15
  if File.exists?(configuration_file)
33
16
  YAML.load(File.open(configuration_file))
@@ -57,21 +40,12 @@ module Lolcommits
57
40
  dir
58
41
  end
59
42
 
60
- def self.loldir_for(basename)
61
- loldir = File.join(LOLBASEDIR, basename)
62
-
63
- if not File.directory? loldir
64
- FileUtils.mkdir_p loldir
65
- end
66
- loldir
67
- end
68
-
69
43
  def most_recent
70
- Dir.glob(File.join self.loldir, "*.jpg").max_by {|f| File.mtime(f)}
44
+ Dir.glob(File.join self.loldir, '*.jpg').max_by { |f| File.mtime(f) }
71
45
  end
72
46
 
73
47
  def images
74
- Dir.glob(File.join self.loldir, "*.jpg").sort_by {|f| File.mtime(f)}
48
+ Dir.glob(File.join self.loldir, '*.jpg').sort_by { |f| File.mtime(f) }
75
49
  end
76
50
 
77
51
  def images_today
@@ -94,13 +68,22 @@ module Lolcommits
94
68
  File.join(self.loldir, 'tmp_frames')
95
69
  end
96
70
 
71
+ def puts_devices
72
+ # TODO: handle other platforms here (linux/windows)
73
+ if self.class.platform_mac?
74
+ capturer = Lolcommits::CaptureMacAnimated.new
75
+ puts `#{capturer.executable_path} -l`
76
+ puts "Specify a device with --device=\"{device name}\" or set the LOLCOMMITS_DEVICE env variable"
77
+ end
78
+ end
79
+
97
80
  def puts_plugins
98
81
  puts "Available plugins: #{Lolcommits::PLUGINS.map(&:name).join(', ')}"
99
82
  end
100
83
 
101
84
  def ask_for_plugin_name
102
85
  puts_plugins
103
- print "Name of plugin to configure: "
86
+ print 'Name of plugin to configure: '
104
87
  STDIN.gets.strip
105
88
  end
106
89
 
@@ -120,8 +103,9 @@ module Lolcommits
120
103
  plugin_name = ask_for_plugin_name
121
104
  end
122
105
 
123
- if plugin = find_plugin(plugin_name)
124
- config = self.read_configuration || Hash.new
106
+ plugin = find_plugin(plugin_name)
107
+ if plugin
108
+ config = self.read_configuration || {}
125
109
  plugin_config = plugin.configure_options!
126
110
  # having a plugin_config, means configuring went OK
127
111
  if plugin_config
@@ -147,27 +131,56 @@ module Lolcommits
147
131
  read_configuration.to_yaml.to_s
148
132
  end
149
133
 
150
- def self.is_mac?
151
- RUBY_PLATFORM.to_s.downcase.include?("darwin")
134
+ # class methods
135
+
136
+ def self.platform
137
+ if platform_fakeplatform?
138
+ ENV['LOLCOMMITS_FAKEPLATFORM']
139
+ elsif platform_fakecapture?
140
+ 'Fake'
141
+ elsif platform_mac?
142
+ 'Mac'
143
+ elsif platform_linux?
144
+ 'Linux'
145
+ elsif platform_windows?
146
+ 'Windows'
147
+ elsif platform_cygwin?
148
+ 'Cygwin'
149
+ else
150
+ fail 'Unknown / Unsupported Platform.'
151
+ end
152
152
  end
153
153
 
154
- def self.is_linux?
155
- RUBY_PLATFORM.to_s.downcase.include?("linux")
154
+ def self.loldir_for(basename)
155
+ loldir = File.join(LOLBASEDIR, basename)
156
+
157
+ if not File.directory? loldir
158
+ FileUtils.mkdir_p loldir
159
+ end
160
+ loldir
156
161
  end
157
162
 
158
- def self.is_windows?
159
- !! RUBY_PLATFORM.match(/(win|w)32/)
163
+ def self.platform_mac?
164
+ RUBY_PLATFORM.to_s.downcase.include?('darwin')
160
165
  end
161
166
 
162
- def self.is_cygwin?
163
- RUBY_PLATFORM.to_s.downcase.include?("cygwin")
167
+ def self.platform_linux?
168
+ RUBY_PLATFORM.to_s.downcase.include?('linux')
164
169
  end
165
170
 
166
- def self.is_fakecapture?
171
+ def self.platform_windows?
172
+ !!RUBY_PLATFORM.match(/(win|w)32/)
173
+ end
174
+
175
+ def self.platform_cygwin?
176
+ RUBY_PLATFORM.to_s.downcase.include?('cygwin')
177
+ end
178
+
179
+ def self.platform_fakecapture?
167
180
  (ENV['LOLCOMMITS_FAKECAPTURE'] == '1' || false)
168
181
  end
169
182
 
170
- def self.is_fakeplatform?
183
+ def self.platform_fakeplatform?
171
184
  ENV['LOLCOMMITS_FAKEPLATFORM']
172
185
  end
173
186
 
@@ -176,7 +189,7 @@ module Lolcommits
176
189
  return false unless self.command_which('mogrify')
177
190
  # you'd expect the below to work on its own, but it only handles old versions
178
191
  # and will throw an exception if IM is not installed in PATH
179
- MiniMagick::valid_version_installed?
192
+ MiniMagick.valid_version_installed?
180
193
  end
181
194
 
182
195
  def self.valid_ffmpeg_installed?
@@ -198,13 +211,12 @@ module Lolcommits
198
211
  def self.command_which(cmd)
199
212
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
200
213
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
201
- exts.each { |ext|
214
+ exts.each do |ext|
202
215
  exe = "#{path}/#{cmd}#{ext}"
203
216
  return exe if File.executable? exe
204
- }
217
+ end
205
218
  end
206
- return nil
219
+ nil
207
220
  end
208
-
209
221
  end
210
222
  end
@@ -1,19 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Lolcommits
2
3
  class GitInfo
3
4
  include Methadone::CLILogging
4
- attr_accessor :sha, :message, :repo_internal_path, :repo
5
+ attr_accessor :sha, :message, :repo_internal_path, :repo, :url
6
+
7
+ def remote_https_url(url)
8
+ url.gsub(':', '/').gsub(/^git@/, 'https://').gsub(/\.git$/, '') + '/commit/'
9
+ end
5
10
 
6
11
  def initialize
7
- debug "GitInfo: attempting to read local repository"
12
+ debug 'GitInfo: attempting to read local repository'
8
13
  g = Git.open('.')
9
- debug "GitInfo: reading commits logs"
14
+ debug 'GitInfo: reading commits logs'
10
15
  commit = g.log.first
11
16
  debug "GitInfo: most recent commit is '#{commit}'"
12
17
 
13
18
  self.message = commit.message.split("\n").first
14
19
  self.sha = commit.sha[0..10]
15
20
  self.repo_internal_path = g.repo.path
16
- regex = /.*[:\/]([\w\-]*).git/
21
+ self.url = remote_https_url(g.remote.url) if g.remote.url
22
+
23
+ regex = /.*[:]([\/\w\-]*).git/
17
24
  match = g.remote.url.match regex if g.remote.url
18
25
  if match
19
26
  self.repo = match[1]
@@ -21,10 +28,10 @@ module Lolcommits
21
28
  self.repo = g.repo.path.split(File::SEPARATOR)[-2]
22
29
  end
23
30
 
24
- debug "GitInfo: parsed the following values from commit:"
31
+ debug 'GitInfo: parsed the following values from commit:'
25
32
  debug "GitInfo: \t#{self.message}"
26
33
  debug "GitInfo: \t#{self.sha}"
27
- debug "GitInfo: \t#{self.repo_internal_path}"
34
+ debug "GitInfo: \t#{self.repo_internal_path}"
28
35
  debug "GitInfo: \t#{self.repo}"
29
36
  end
30
37
  end
@@ -0,0 +1,117 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Lolcommits
3
+ #
4
+ # Methods to handle enabling and disabling of lolcommits
5
+ #
6
+ class Installation
7
+ HOOK_PATH = File.join '.git', 'hooks', 'post-commit'
8
+ HOOK_DIR = File.join '.git', 'hooks'
9
+
10
+ #
11
+ # IF --ENABLE, DO ENABLE
12
+ #
13
+ def self.do_enable
14
+ if not File.directory?('.git')
15
+ fatal "You don't appear to be in the base directory of a git project."
16
+ exit 1
17
+ end
18
+
19
+ # its possible a hooks dir doesnt exist, so create it if so
20
+ if not File.directory?(HOOK_DIR)
21
+ Dir.mkdir(HOOK_DIR)
22
+ end
23
+
24
+ # clear away any existing lolcommits hook
25
+ if hook_file_exists?
26
+ remove_existing_hook! if lolcommits_hook_exists?
27
+
28
+ # check for a good shebang line in the existing hook
29
+ unless good_shebang?
30
+ warn "the existing hook (at #{HOOK_PATH}) doesn't start with with a good shebang; like #!/bin/sh"
31
+ exit 1
32
+ end
33
+ end
34
+
35
+ File.open(HOOK_PATH, hook_file_exists? ? 'a' : 'w') do |f|
36
+ f.write(hook_script(!hook_file_exists?))
37
+ end
38
+
39
+ FileUtils.chmod 0755, HOOK_PATH
40
+
41
+ info 'installed lolcommit hook to:'
42
+ info " -> #{File.expand_path(HOOK_PATH)}"
43
+ info '(to remove later, you can use: lolcommits --disable)'
44
+ # we dont symlink, but rather install a small stub that calls the one from path
45
+ # that way, as gem version changes, script updates even if new file thus breaking symlink
46
+ end
47
+
48
+ #
49
+ # IF --DISABLE, DO DISABLE
50
+ #
51
+ def self.do_disable
52
+ if lolcommits_hook_exists?
53
+ remove_existing_hook!
54
+ info "uninstalled lolcommits hook (from #{HOOK_PATH})"
55
+ elsif File.exists?(HOOK_PATH)
56
+ info "couldn't find an lolcommits hook (at #{HOOK_PATH})"
57
+ if File.read(HOOK_PATH) =~ /lolcommit/
58
+ info "warning: an older-style lolcommit hook may still exist, edit #{HOOK_PATH} to remove it manually"
59
+ end
60
+ else
61
+ info "no post commit hook found (at #{HOOK_PATH}), so there is nothing to uninstall"
62
+ end
63
+ end
64
+
65
+ protected
66
+
67
+ def self.hook_script(add_shebang = true)
68
+ shebang = add_shebang ? "#!/bin/sh\n\n" : ''
69
+ ruby_path = Lolcommits::Configuration.command_which('ruby')
70
+ hook_export = "export PATH=\"#{ruby_path}:$PATH\"\n" if ruby_path
71
+ capture_cmd = 'lolcommits --capture'
72
+ capture_args = " #{ARGV[1..-1].join(' ')}" if ARGV.length > 1
73
+
74
+ <<-EOS
75
+ #{shebang}### lolcommits hook (begin) ###
76
+ #{hook_export}#{capture_cmd}#{capture_args}
77
+ ### lolcommits hook (end) ###
78
+ EOS
79
+ end
80
+
81
+ # does a git hook exist at all?
82
+ def self.hook_file_exists?
83
+ File.exists?(HOOK_PATH)
84
+ end
85
+
86
+ # does a git hook exist with lolcommits commands?
87
+ def self.lolcommits_hook_exists?
88
+ hook_file_exists? &&
89
+ File.read(HOOK_PATH).to_s =~ /lolcommits.*\(begin\)(.*\n)*.*lolcommits.*\(end\)/
90
+ end
91
+
92
+ # does the git hook file have a good shebang?
93
+ def self.good_shebang?
94
+ File.read(HOOK_PATH).lines.first =~ /^\#\!\/bin\/.*sh/
95
+ end
96
+
97
+ def self.remove_existing_hook!
98
+ hook = File.read(HOOK_PATH)
99
+ out = File.open(HOOK_PATH, 'w')
100
+ skip = false
101
+
102
+ hook.lines.each do |line|
103
+ if !skip && (line =~ /lolcommits.*\(begin\)/)
104
+ skip = true
105
+ end
106
+
107
+ out << line unless skip
108
+
109
+ if skip && (line =~ /lolcommits.*\(end\)/)
110
+ skip = false
111
+ end
112
+ end
113
+
114
+ out.close
115
+ end
116
+ end
117
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Lolcommits
2
3
  class Plugin
3
4
  include Methadone::CLILogging
@@ -5,34 +6,34 @@ module Lolcommits
5
6
  attr_accessor :runner, :options
6
7
 
7
8
  def initialize(runner)
8
- debug "Initializing"
9
+ debug 'Initializing'
9
10
  self.runner = runner
10
11
  self.options = ['enabled']
11
12
  end
12
13
 
13
14
  def execute
14
- if is_enabled?
15
- debug "I am enabled, about to run"
15
+ if enabled?
16
+ debug 'I am enabled, about to run'
16
17
  run
17
18
  else
18
- debug "Disabled, doing nothing for execution"
19
+ debug 'Disabled, doing nothing for execution'
19
20
  end
20
21
  end
21
22
 
22
23
  def run
23
- debug "base plugin, does nothing to anything"
24
+ debug 'base plugin, does nothing to anything'
24
25
  end
25
26
 
26
27
  def configuration
27
28
  config = runner.config.read_configuration if runner
28
- return Hash.new unless config
29
- config[self.class.name] || Hash.new
29
+ return {} unless config
30
+ config[self.class.name] || {}
30
31
  end
31
32
 
32
33
  # ask for plugin options
33
34
  def configure_options!
34
35
  puts "Configuring plugin: #{self.class.name}\n"
35
- options.inject(Hash.new) do |acc, option|
36
+ options.reduce(Hash.new) do |acc, option|
36
37
  print "#{option}: "
37
38
  val = STDIN.gets.strip.downcase
38
39
  if %w(true yes).include?(val)
@@ -44,13 +45,13 @@ module Lolcommits
44
45
  end
45
46
  end
46
47
 
47
- def is_enabled?
48
+ def enabled?
48
49
  configuration['enabled'] == true
49
50
  end
50
51
 
51
52
  # check config is valid
52
53
  def valid_configuration?
53
- if is_configured?
54
+ if configured?
54
55
  true
55
56
  else
56
57
  puts "Missing #{self.class.name} config - configure with: lolcommits --config -p #{self.class.name}"
@@ -59,7 +60,7 @@ module Lolcommits
59
60
  end
60
61
 
61
62
  # empty plugin configuration
62
- def is_configured?
63
+ def configured?
63
64
  !configuration.empty?
64
65
  end
65
66
 
@@ -1,8 +1,8 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'httmultiparty'
2
3
 
3
4
  module Lolcommits
4
5
  class DotCom < Plugin
5
-
6
6
  def initialize(runner)
7
7
  super
8
8
  self.options.concat(['api_key', 'api_secret', 'repo_id'])
@@ -13,22 +13,22 @@ module Lolcommits
13
13
 
14
14
  t = Time.now.to_i.to_s
15
15
  resp = HTTMultiParty.post('http://www.lolcommits.com/git_commits.json',
16
- :body => {
17
- :git_commit => {
18
- :sha => self.runner.sha,
19
- :repo_external_id => configuration['repo_id'],
20
- :image => File.open(self.runner.main_image),
21
- :raw => File.open(self.runner.snapshot_loc)
22
- },
16
+ :body => {
17
+ :git_commit => {
18
+ :sha => self.runner.sha,
19
+ :repo_external_id => configuration['repo_id'],
20
+ :image => File.open(self.runner.main_image),
21
+ :raw => File.open(self.runner.snapshot_loc)
22
+ },
23
23
 
24
- :key => configuration['api_key'],
25
- :t => t,
26
- :token => Digest::SHA1.hexdigest(configuration['api_secret'] + t)
27
- }
24
+ :key => configuration['api_key'],
25
+ :t => t,
26
+ :token => Digest::SHA1.hexdigest(configuration['api_secret'] + t)
27
+ }
28
28
  )
29
29
  end
30
30
 
31
- def is_configured?
31
+ def configured?
32
32
  !configuration['enabled'].nil? &&
33
33
  configuration['api_key'] &&
34
34
  configuration['api_secret'] &&