irbrc 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/irbrc.rb +174 -57
  3. metadata +9 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 92cb0c8b3423ff656c642973dced68bdf05804aa
4
- data.tar.gz: d402efbebb2ef805c6698d86b62d8c7aea7e3c38
2
+ SHA256:
3
+ metadata.gz: d3172a820fd9f812f38c6dceb3ed2a21ceed0f9581e3820e3b786d4f0a951ea9
4
+ data.tar.gz: 3676e2b725a0126f0802f34c6b276420f71dbc0fae91208635589f6fc770c6ac
5
5
  SHA512:
6
- metadata.gz: dd9f944c5793e48bd7d0b8d74defbcc3c5fb15deaf5efa4e203debe61c91ea48cae12317b6934deab7754be9ec509ac11e241766994b679437423b50fece4002
7
- data.tar.gz: 67194d89e47f37d91451db9ef1aac27a2b879aa00de374e04c72b5ba87545ffbe4a2a87efd2a74bbf4f93af63e0f10b5c6f85f3185d67ce4c01690ad5e3c794f
6
+ metadata.gz: c0fefc7683c8e1793c09736087665ed9ca797accaa81e013ecd43adfd824e6dcce3db2d2e8c5bbb23ccd36fac2115616dde51895fa928a000fa1f8c9217090ef
7
+ data.tar.gz: 6b3e13f9538a5a57922d7cf34c69d9de7a24acbc0bdb15001c7ef5cde3711ba82a351725e7cd691000de3489693fa72fd97a3409216b14978bac455f3f02f94b
@@ -1,122 +1,201 @@
1
1
  require 'fileutils'
2
- require 'highline'
3
2
 
4
3
 
5
4
  module Irbrc
6
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
7
6
  BASE_DIR = [
8
7
  Dir.home,
9
8
  '.irb/rc',
10
9
  ].join File::SEPARATOR
11
- LOCAL_FILE = '.irbrc'
12
10
 
13
11
 
14
12
  class << self
15
13
 
14
+
16
15
  def load_rc
17
- unless File.exists? rc_path
18
- if File.exists? LOCAL_FILE
19
- if agree("Move existing rc: #{LOCAL_FILE}")
20
- File.rename LOCAL_FILE, rc_path
21
- link_rc
22
- else
23
- link_rc reverse: true
24
- end
25
- elsif agree('Create irbrc')
26
- create_rc
27
- link_rc
16
+ if File.exists? local_rc
17
+ # avoid circular reload
18
+ if local_rc != global_rc
19
+ load local_rc
28
20
  end
29
21
  end
30
-
31
- load rc_path if File.exists? rc_path
32
- # eval(File.read(rc_path))
33
22
  end
34
23
 
35
24
 
36
- def localize opts = {}
37
- if File.exists? LOCAL_FILE
38
- if opts[:force] or File.realpath(LOCAL_FILE) == rc_path
39
- unlink LOCAL_FILE
40
- else
41
- unlink LOCAL_FILE if agree "Remove local rc: #{LOCAL_FILE}"
25
+ # set up or fix this project's rc file and symlink
26
+ def init
27
+ if File.symlink? local_rc
28
+ if ! File.exists? local_rc
29
+ # clean up bad symlink
30
+ unlink local_rc
42
31
  end
32
+ elsif File.exists? local_rc
33
+ if remote_rc and agree("Move local rc: mv #{local_rc} #{remote_rc}")
34
+ FileUtils.mkpath File.dirname remote_rc
35
+ File.rename local_rc, remote_rc
36
+ end
37
+ elsif ! realpath remote_rc and agree('Create irbrc', default: true)
38
+ # create new rc file
39
+ create_rc
43
40
  end
44
41
 
45
- File.rename rc_path, LOCAL_FILE unless File.exists? LOCAL_FILE
46
- end
42
+ if ! File.exists? local_rc and realpath remote_rc
43
+ # symlink remote rc
44
+ File.symlink remote_rc, local_rc
45
+ end
47
46
 
47
+ init_global_rc
48
+ git_ignore if git_env?
48
49
 
49
- def remove_rc opts = {}
50
- unlink rc_path, LOCAL_FILE
50
+ nil
51
51
  end
52
52
 
53
53
 
54
- def create_rc opts = {}
55
- unlink rc_path if opts[:force]
54
+ def create_rc
55
+ path = remote_rc || local_rc
56
56
 
57
- if File.exists? rc_path
58
- raise Exception.new "rc file already exists: #{rc_path}"
57
+ if File.exists? path
58
+ raise Exception.new "rc file already exists: #{path}"
59
59
  end
60
60
 
61
- FileUtils.mkpath File.dirname rc_path
62
- File.open(rc_path, 'w') do |fh|
63
- fh.write "# IRBRC for #{parse_repo[:repo]}\n"
64
- fh.write "\n\n"
61
+ if remote_rc
62
+ FileUtils.mkpath File.dirname remote_rc
65
63
  end
66
64
 
67
- nil
65
+ msg = if repo = parse_repo
66
+ "# IRBRC for #{parse_repo[:source]}:#{repo[:repo]}\n"
67
+ else
68
+ "# IRBRC"
69
+ end
70
+
71
+ File.open(path, 'w') do |fh|
72
+ fh.write "#{msg}\n\n"
73
+ end
68
74
  end
69
75
 
70
76
 
71
- def link_rc opts = {}
72
- if opts[:reverse]
73
- unlink rc_path if opts[:force]
74
- File.symlink File.realpath(LOCAL_FILE), rc_path
77
+ # Ensure ~/.irbrc loads Irbrc upon irb start.
78
+ def init_global_rc
79
+ require_cmd = "require 'irbrc'"
80
+
81
+ add_required = if File.exists? global_rc
82
+ add_msg = "Add `#{require_cmd}` to #{global_rc}"
83
+ File.read(global_rc) !~ /\W#{require_cmd}\W/ and agree(add_msg)
75
84
  else
76
- unlink LOCAL_FILE if opts[:force]
77
- File.symlink rc_path, LOCAL_FILE
85
+ true
78
86
  end
79
87
 
80
- nil
88
+ if add_required
89
+ File.open(global_rc, 'a') do |fh|
90
+ fh.write "\n"
91
+ fh.write "# load per project .irbrc\n"
92
+ fh.write "#{require_cmd}\n"
93
+ fh.write "load_rc\n\n"
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+ # Add rc file to git ignore.
100
+ def git_ignore
101
+ ignore_path = [
102
+ project_root,
103
+ '.git',
104
+ 'info',
105
+ 'exclude'
106
+ ].join File::SEPARATOR
107
+
108
+ add_ignore = if File.exists? ignore_path
109
+ msg = "Add .irbrc to #{ignore_path}"
110
+ File.read(ignore_path) !~ /\W\.irbrc\W/ and agree(msg, default: true)
111
+ end
112
+
113
+ if add_ignore
114
+ File.open(ignore_path, 'a') do |fh|
115
+ fh.write "\n.irbrc\n"
116
+ end
117
+ end
81
118
  end
82
119
 
83
120
 
84
- def rc_path
121
+ def remote_rc
85
122
  repo = parse_repo
86
123
 
87
- [
88
- BASE_DIR,
89
- repo[:source],
90
- repo[:repo].sub(/#{File::SEPARATOR}/, '.') + '.rc',
91
- ].join File::SEPARATOR
124
+ if repo
125
+ [
126
+ BASE_DIR,
127
+ repo[:source],
128
+ repo[:repo].gsub(/#{File::SEPARATOR}/, '.') + '.rb',
129
+ ].join File::SEPARATOR
130
+ else
131
+ nil
132
+ end
92
133
  end
93
134
 
94
135
 
95
136
  def parse_repo str = nil
96
- str = `git remote -v` unless str
137
+ str = git_cmd "remote -v" unless str
138
+ return unless str
97
139
 
98
140
  repos = str.split("\n").map(&:split).map do |line|
141
+ next unless line.first == "origin"
142
+ next unless line.last == "(fetch)"
143
+
99
144
  source, repo = line[1].split ':'
100
- source.sub! /^.*@/, ''
101
- source.sub! /\.(com|org)$/, ''
145
+ source.sub!(/^.*@/, '')
146
+ source.sub!(/\.(com|org)$/, '')
102
147
 
103
148
  {
104
149
  source: source,
105
150
  repo: repo,
106
151
  }
107
- end.uniq
152
+ end.compact.uniq
108
153
 
109
154
  if repos.count != 1
110
- raise Error.new "parse error: #{str}"
155
+ raise Exception.new "parse error: #{str}"
111
156
  end
112
157
 
113
158
  repos.first
114
159
  end
115
160
 
116
161
 
117
- def agree msg
118
- HighLine.new.agree("#{msg}? [Y/n]") do |q|
119
- yield q if block_given?
162
+ def local_rc
163
+ [
164
+ project_root || Dir.pwd,
165
+ '.irbrc'
166
+ ].join File::SEPARATOR
167
+ end
168
+
169
+
170
+ def global_rc
171
+ [ Dir.home, '.irbrc' ].join File::SEPARATOR
172
+ end
173
+
174
+
175
+ def project_root
176
+ git_cmd("rev-parse --show-toplevel")
177
+ end
178
+
179
+
180
+ def agree msg, opts = {}
181
+ # ask yes or no question and return true/false
182
+ # optional 'default' arg
183
+
184
+ default = if opts.has_key? :default
185
+ opts[:default] ? 'y' : 'n'
186
+ else
187
+ ''
188
+ end
189
+
190
+ loop do
191
+ puts "#{msg.chomp '?'}? %s" % '[y/n]'.sub(default, default.upcase)
192
+ res = gets.strip.downcase
193
+ res = default if res.empty?
194
+ if ['y', 'yes', 'n', 'no'].include? res
195
+ return ['y', 'yes'].include? res
196
+ else
197
+ puts "\ninvalid response\n\n"
198
+ end
120
199
  end
121
200
  end
122
201
 
@@ -128,6 +207,44 @@ module Irbrc
128
207
  end
129
208
 
130
209
 
210
+ def realpath path
211
+ File.realpath path if path and File.exists? path
212
+ end
213
+
214
+
215
+ def git_env?
216
+ !! git_cmd('status --short')
217
+ end
218
+
219
+
220
+ def git_cmd cmd
221
+ res = `git #{cmd} 2>/dev/null`.chomp
222
+ res.empty? ? nil : res
223
+ end
224
+
225
+
226
+ # remove rc file
227
+ def remove
228
+ if agree "remove rc file"
229
+ unlink local_rc, remote_rc
230
+ end
231
+ end
232
+
233
+
234
+ # use local rc file instead of symlink
235
+ def localize
236
+ if File.symlink? local_rc
237
+ unlink local_rc
238
+ end
239
+
240
+ if File.exists? local_rc
241
+ unlink local_rc if agree "Overwrite local rc: #{local_rc}"
242
+ end
243
+
244
+ File.rename remote_rc, local_rc unless File.exists? local_rc
245
+ end
246
+
247
+
131
248
  end
132
249
  end
133
250
 
metadata CHANGED
@@ -1,57 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: irbrc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-30 00:00:00.000000000 Z
11
+ date: 2020-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: highline
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :runtime
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '10'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '10'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: minitest
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - "~>"
31
+ - - ">="
46
32
  - !ruby/object:Gem::Version
47
- version: '5'
33
+ version: '0'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - "~>"
38
+ - - ">="
53
39
  - !ruby/object:Gem::Version
54
- version: '5'
40
+ version: '0'
55
41
  description: irbrc loader
56
42
  email:
57
43
  executables: []
@@ -78,8 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
64
  - !ruby/object:Gem::Version
79
65
  version: '0'
80
66
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.6.11
67
+ rubygems_version: 3.0.1
83
68
  signing_key:
84
69
  specification_version: 4
85
70
  summary: Irbrc