ballantine 0.1.5.pre.rc2 → 0.1.5

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
  SHA256:
3
- metadata.gz: 4c25803046495e2cb79cf8998bfaf50b45e1699c3d1abd751c4779e0dcd765bb
4
- data.tar.gz: c0a89a4903d9083d52a91e0ac64e35017d128d0ff6edaa67c563d452c640931c
3
+ metadata.gz: 77449354cec29e4273ad0ca09e096d7803d116dbf902b05c48b6107654dcd981
4
+ data.tar.gz: aaa7fc4269cf1bb7221bab4a5c7f6247314e6ab8517c87a99561275706e9198a
5
5
  SHA512:
6
- metadata.gz: 71b1f1a69c9085dea54e6494411d4101815986c153549cf0e7e2ce3d63f43f90475381a93ccfe5230e0b31beae44762e74f4c0fcaaed984cf3bb77dc95f28ccc
7
- data.tar.gz: ba01668f022733b3e0f622a4ad917e9ce572a5f0070e1060d8781593071ce430c2751679877cbe49820545c2f1dacfc9aa05d1b5dc590b7dc7afcc64f70d6861
6
+ metadata.gz: 9a91ea909252259c4916478d945b7fe9249fe14c36514a1536e978cbae67f5c719e52ca02e1d16052dd60389c691946ca4adcd8eb9cdbb235b07788dfd209948
7
+ data.tar.gz: b00877f1c352da8814c7c6ba927c3c4da96c03220ffc93ca4e9fe4ff1ff5afe384f48a46366c0bac4c2c5d7edaed093edd161ff76ef650f3bf9ad8e2a4f7d35a
@@ -36,8 +36,6 @@ module Ballantine
36
36
 
37
37
  # @return [Boolean]
38
38
  def print_commits
39
- conf.print_log(binding) if conf.verbose
40
-
41
39
  puts "\n" + "@#{name}".green
42
40
  commits_hash.each do |repo_name, commits|
43
41
  count, word = retrieve_count_and_word(commits)
@@ -54,8 +52,6 @@ module Ballantine
54
52
  # reference: https://api.slack.com/messaging/composing/layouts#building-attachments
55
53
  # @return [Hash]
56
54
  def slack_message
57
- conf.print_log(binding) if conf.verbose
58
-
59
55
  message = commits_hash.map do |repo_name, commits|
60
56
  count, word = retrieve_count_and_word(commits)
61
57
  "*#{repo_name}*: #{count} new #{word}\n" \
@@ -70,8 +66,6 @@ module Ballantine
70
66
 
71
67
  private
72
68
 
73
- def conf = Config.instance
74
-
75
69
  # @param [Array<Commit>] commits
76
70
  # @param [Array(Integer, String)] count, word
77
71
  def retrieve_count_and_word(commits)
@@ -14,7 +14,7 @@ module Ballantine
14
14
  option "force", type: :boolean, aliases: "-f", default: false, desc: "Initialize forcely if already initialized."
15
15
  desc "init", "Initialize ballantine"
16
16
  def init
17
- conf.init_file(force: options["force"])
17
+ Config.instance.init_file(force: options["force"])
18
18
 
19
19
  puts "🥃 Initialized ballantine."
20
20
 
@@ -25,8 +25,8 @@ module Ballantine
25
25
  option "verbose", type: :boolean, default: false, desc: "Print a progress."
26
26
  desc "config [--env] [KEY] [VALUE]", "Set ballantine's configuration"
27
27
  def config(key = nil, value = nil)
28
- conf.verbose = options["verbose"]
29
- puts "$ ballantine config #{key} #{value}" if conf.verbose
28
+ Config.instance.verbose = options["verbose"]
29
+ puts "$ ballantine config #{key} #{value}" if Config.instance.verbose
30
30
 
31
31
  # check environment value
32
32
  if Config::AVAILABLE_ENVIRONMENTS.map { |key| !options[key] }.reduce(:&)
@@ -38,28 +38,35 @@ module Ballantine
38
38
  env = Config::AVAILABLE_ENVIRONMENTS.find { |key| options[key] }
39
39
  raise AssertionFailed, "Environment value must exist: #{env}" if env.nil?
40
40
 
41
- conf.env = env
42
- value ? conf.set_data(key, value) : conf.print_data(key)
41
+ Config.instance.env = env
42
+ value ? Config.instance.set_data(key, value) : Config.instance.print_data(key)
43
+
44
+ true
43
45
  end
44
46
 
45
47
  option "verbose", type: :boolean, default: false, desc: "Print a progress."
46
48
  option Config::TYPE_SLACK, type: :boolean, aliases: "-s", default: false, desc: "Send to slack using slack webhook URL."
47
49
  desc "diff [TARGET] [SOURCE]", "Diff commits between TARGET and SOURCE"
48
50
  def diff(target, source = %x(git rev-parse --abbrev-ref HEAD).chomp)
49
- conf.verbose = options["verbose"]
50
- puts "$ ballantine diff #{target} #{source}" if conf.verbose
51
+ Config.instance.verbose = options["verbose"]
52
+ puts "$ ballantine diff #{target} #{source}" if Config.instance.verbose
53
+
54
+ # retrieve uncommitted files
55
+ uncommitted = retrieve_uncommitted
51
56
 
52
57
  # validate arguments
53
- validate(target, source, **options)
58
+ validate(target, source, uncommitted, **options)
54
59
 
55
- # init instance variables
56
- init_variables(target, source, **options)
60
+ with_stash(uncommitted) do
61
+ # init instance variables
62
+ init_variables(target, source, **options)
57
63
 
58
- # check commits
59
- check_commits(**options)
64
+ # check commits
65
+ check_commits
60
66
 
61
- # print commits
62
- print_commits(target, source, **options)
67
+ # print commits
68
+ print_commits(target, source)
69
+ end
63
70
  end
64
71
 
65
72
  desc "version", "Display version information about ballntine"
@@ -71,28 +78,25 @@ module Ballantine
71
78
 
72
79
  private
73
80
 
74
- def conf = Config.instance
75
-
76
81
  # @param [String] target
77
82
  # @param [String] source
83
+ # @param [Array<String>] uncommitted
78
84
  # @param [Hash] options
79
85
  # @return [NilClass] nil
80
- def validate(target, source, **options)
81
- conf.print_log(binding) if conf.verbose
82
-
86
+ def validate(target, source, uncommitted, **options)
83
87
  if Dir[".git"].empty?
84
88
  raise NotAllowed, "ERROR: There is no \".git\" in #{Dir.pwd}."
85
89
  end
86
90
 
87
- if (uncommitted = %x(git diff HEAD --name-only).split("\n")).any?
88
- raise NotAllowed, "ERROR: Uncommitted file exists. stash or commit uncommitted files.\n#{uncommitted.join("\n")}"
91
+ if uncommitted.any? && Config.instance.raise_uncommitted?
92
+ raise NotAllowed, "ERROR: Uncommitted file exists. stash or commit uncommitted files.\n\t#{uncommitted.join("\n\t")}"
89
93
  end
90
94
 
91
95
  if target == source
92
96
  raise NotAllowed, "ERROR: target(#{target}) and source(#{source}) can't be equal."
93
97
  end
94
98
 
95
- if options[Config::TYPE_SLACK] && !conf.get_data(Config::KEY_SLACK_WEBHOOK)
99
+ if options[Config::TYPE_SLACK] && !Config.instance.get_data(Config::KEY_SLACK_WEBHOOK)
96
100
  raise NotAllowed, "ERROR: Can't find any slack webhook. Set slack webhook using `ballantine config --#{Config::ENV_LOCAL} slack_webhook [YOUR_WEBHOOK]'."
97
101
  end
98
102
 
@@ -104,12 +108,10 @@ module Ballantine
104
108
  # @param [Hash] options
105
109
  # @return [Boolean]
106
110
  def init_variables(target, source, **options)
107
- conf.print_log(binding) if conf.verbose
108
-
109
111
  # check commits are newest
110
112
  system("git pull -f &> /dev/null")
111
113
 
112
- conf.print_type = options[Config::TYPE_SLACK] ? Config::TYPE_SLACK : Config::TYPE_TERMINAL
114
+ Config.instance.print_type = options[Config::TYPE_SLACK] ? Config::TYPE_SLACK : Config::TYPE_TERMINAL
113
115
  @repo = Repository.find_or_create_by(
114
116
  path: Dir.pwd,
115
117
  remote_url: %x(git config --get remote.origin.url).chomp,
@@ -120,23 +122,16 @@ module Ballantine
120
122
  true
121
123
  end
122
124
 
123
- # @param [Hash] options
124
125
  # @return [Boolean]
125
- def check_commits(**options)
126
- conf.print_log(binding) if conf.verbose
127
-
126
+ def check_commits
128
127
  repo.check_commits
129
-
130
128
  true
131
129
  end
132
130
 
133
131
  # @param [String] target
134
132
  # @param [String] source
135
- # @param [Hash] options
136
133
  # @return [Boolean]
137
- def print_commits(target, source, **options)
138
- conf.print_log(binding) if conf.verbose
139
-
134
+ def print_commits(target, source)
140
135
  authors = Author.all
141
136
  if authors.empty?
142
137
  raise ArgumentError, "ERROR: There is no commits between \"#{target}\" and \"#{source}\""
@@ -144,7 +139,7 @@ module Ballantine
144
139
 
145
140
  number = authors.size
146
141
 
147
- case conf.print_type
142
+ case Config.instance.print_type
148
143
  when Config::TYPE_TERMINAL
149
144
  puts_r "Check commits before #{repo.name.red} deployment. (#{target.cyan}...#{source.cyan})", "#{repo.url}/compare/#{repo.from.hash}...#{repo.to.hash}".gray
150
145
  puts "Author".yellow + ": #{number}"
@@ -158,7 +153,7 @@ module Ballantine
158
153
  # send message to slack
159
154
  require "net/http"
160
155
  require "uri"
161
- uri = URI.parse(conf.get_data(Config::KEY_SLACK_WEBHOOK))
156
+ uri = URI.parse(Config.instance.get_data(Config::KEY_SLACK_WEBHOOK))
162
157
  request = Net::HTTP::Post.new(uri)
163
158
  request.content_type = "application/json"
164
159
  request.body = JSON.dump({
@@ -171,9 +166,39 @@ module Ballantine
171
166
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) { |http| http.request(request) }
172
167
  puts response.message
173
168
  else
174
- raise AssertionFailed, "Unknown print type: #{conf.print_type}"
169
+ raise AssertionFailed, "Unknown print type: #{Config.instance.print_type}"
170
+ end
171
+
172
+ true
173
+ end
174
+
175
+ # @param [Array<String>] uncommitted
176
+ # @return [Boolean]
177
+ def with_stash(uncommitted)
178
+ if uncommitted.any? && !Config.instance.raise_uncommitted?
179
+ save_stash
180
+ yield
181
+ restore_stash
182
+ else
183
+ yield
175
184
  end
185
+ true
186
+ end
176
187
 
188
+ # @return [Array<String>]
189
+ def retrieve_uncommitted
190
+ %x(git diff HEAD --name-only).split("\n")
191
+ end
192
+
193
+ # @return [Boolean]
194
+ def save_stash
195
+ %x(git stash save &> /dev/null)
196
+ true
197
+ end
198
+
199
+ # @return [Boolean]
200
+ def restore_stash
201
+ %x(git stash apply &> /dev/null)
177
202
  true
178
203
  end
179
204
  end
@@ -12,10 +12,11 @@ module Ballantine
12
12
  ENV_GLOBAL,
13
13
  ].freeze
14
14
  KEY_SLACK_WEBHOOK = "slack_webhook"
15
+ KEY_RAISE_UNCOMMITTED = "raise_uncommitted"
15
16
  AVAILABLE_KEYS = [
16
17
  KEY_SLACK_WEBHOOK,
18
+ KEY_RAISE_UNCOMMITTED,
17
19
  ].freeze
18
- AVAILABLE_PRINT_INSTANCE_VARIABLES = [:@name, :@from, :@to].freeze
19
20
 
20
21
  attr_reader :data, :loaded
21
22
  attr_accessor :env, :print_type, :verbose
@@ -38,34 +39,31 @@ module Ballantine
38
39
  @verbose = false
39
40
  end
40
41
 
41
- # @param [Hash] options
42
+ # @param [Boolean] force
42
43
  # @return [Boolean] result
43
- def init_file(**options)
44
- raise NotAllowed, "#{FILE_BALLANTINE_CONFIG} already exists." if Dir[file_path].any? && !options[:force]
45
-
46
- File.write(file_path, {})
44
+ def init_file(force: false)
45
+ raise NotAllowed, "#{FILE_BALLANTINE_CONFIG} already exists." if Dir[file_path].any? && !force
46
+ File.write(file_path, JSON.pretty_generate(empty_data))
47
47
  @loaded = false
48
48
  end
49
49
 
50
- # @param [Hash] options
51
50
  # @return [Boolean] result
52
- def load_file(**options)
51
+ def load_file
53
52
  return false if @loaded
54
53
  raise NotAllowed, "Can't find #{FILE_BALLANTINE_CONFIG}" if Dir[file_path].empty?
55
54
 
56
55
  JSON.parse(File.read(file_path)).each do |key, value|
57
56
  next unless AVAILABLE_KEYS.include?(key)
58
57
 
59
- @data[key] = value
58
+ @data[key] = sanitize_value(key, value)
60
59
  end
61
60
 
62
61
  @loaded = true
63
62
  end
64
63
 
65
64
  # @param [String] key
66
- # @param [Hash] options
67
65
  # @return [Boolean] result
68
- def print_data(key, **options)
66
+ def print_data(key)
69
67
  load_file unless @loaded
70
68
 
71
69
  if key
@@ -83,38 +81,42 @@ module Ballantine
83
81
 
84
82
  # @param [String] key
85
83
  # @param [String] value
86
- # @param [Hash] options
87
84
  # @return [Stirng] value
88
- def set_data(key, value, **options)
85
+ def set_data(key, value)
89
86
  load_file unless @loaded
90
-
91
- @data[key] = value
92
- File.write(file_path, JSON.dump(@data))
87
+ raise InvalidParameter, "Key must be within #{AVAILABLE_KEYS}" unless AVAILABLE_KEYS.include?(key)
88
+ @data[key] = sanitize_value(key, value)
89
+ File.write(file_path, JSON.pretty_generate(@data))
93
90
  value
94
91
  end
95
92
 
96
93
  # @param [String] key
97
- # @param [Hash] options
98
94
  # @return [Stirng] value
99
- def get_data(key, **options)
95
+ def get_data(key)
100
96
  load_file unless @loaded
101
97
  @data[key]
102
98
  end
103
99
 
104
- # @param [Binding] binding
105
- # @return [NilClass]
106
- def print_log(binding)
107
- method = caller(1..1)[0][/`([^']*)'/, 1]
108
-
109
- puts [
110
- "#{binding.receiver.class.name}##{method}",
111
- (binding.receiver.instance_variables & AVAILABLE_PRINT_INSTANCE_VARIABLES).map { |var| "#{var}:#{binding.receiver.instance_variable_get(var).inspect}" }.join(" "),
112
- binding.receiver.method(method).parameters.map { |_, arg| arg }.map { |arg| "#{arg}:#{binding.local_variable_get(arg)}" }.join(" "),
113
- ].compact.join("\t")
114
- end
100
+ # @return [Boolean]
101
+ def raise_uncommitted? = get_data(KEY_RAISE_UNCOMMITTED)
115
102
 
116
103
  private
117
104
 
105
+ # @return [Hash{String => Object}]
106
+ def empty_data = AVAILABLE_KEYS.map { |key| [key, nil] }.to_h
107
+
108
+ # @param [String] key
109
+ # @param [Object] value
110
+ # @return [Object]
111
+ def sanitize_value(key, value)
112
+ case key
113
+ when KEY_RAISE_UNCOMMITTED then value == true || value == "true"
114
+ else value
115
+ end
116
+ end
117
+
118
+ # @param [String] env
119
+ # @return [String]
118
120
  def file_path(env = @env)
119
121
  case env
120
122
  when ENV_LOCAL then "./#{FILE_BALLANTINE_CONFIG}"
@@ -59,8 +59,6 @@ module Ballantine
59
59
  # @param [String] source
60
60
  # @return [Boolean]
61
61
  def init_variables(target, source)
62
- conf.print_log(binding) if conf.verbose
63
-
64
62
  current_revision = %x(git rev-parse --abbrev-ref HEAD).chomp
65
63
 
66
64
  foo = lambda do |hash, context|
@@ -100,8 +98,6 @@ module Ballantine
100
98
 
101
99
  # @return [Boolean]
102
100
  def check_commits
103
- conf.print_log(binding) if conf.verbose
104
-
105
101
  authors = retrieve_authors
106
102
  authors.each do |author|
107
103
  commits = retrieve_commits(author)
@@ -126,8 +122,6 @@ module Ballantine
126
122
 
127
123
  private
128
124
 
129
- def conf = Config.instance
130
-
131
125
  # @param [String] name
132
126
  # @return [String] hash
133
127
  def check_tag(name)
@@ -140,13 +134,13 @@ module Ballantine
140
134
 
141
135
  # @return [String]
142
136
  def check_format
143
- case conf.print_type
137
+ case Config.instance.print_type
144
138
  when Config::TYPE_TERMINAL
145
139
  " - " + "%h".yellow + " %<(#{ljust})%s " + "#{url}/commit/%H".gray
146
140
  when Config::TYPE_SLACK
147
141
  "\\\`<#{url}/commit/%H|%h>\\\` %s - %an"
148
142
  else
149
- raise AssertionFailed, "Unknown print type: #{conf.print_type}"
143
+ raise AssertionFailed, "Unknown print type: #{Config.instance.print_type}"
150
144
  end
151
145
  end
152
146
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ballantine
4
- VERSION = "0.1.5-rc2"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/ballantine.rb CHANGED
@@ -12,7 +12,11 @@ require_relative "ballantine/commit"
12
12
  require_relative "ballantine/cli"
13
13
 
14
14
  module Ballantine
15
- class Error < StandardError; end
15
+ class Error < StandardError
16
+ # @return [Array<String>, NilClass]
17
+ def backtrace = Config.instance.verbose ? super : nil
18
+ end
19
+
16
20
  class NotAllowed < Error; end
17
21
  class InvalidParameter < Error; end
18
22
  class AssertionFailed < Error; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ballantine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5.pre.rc2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - oohyun15