irbrc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/irbrc.rb +136 -0
  3. metadata +86 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92cb0c8b3423ff656c642973dced68bdf05804aa
4
+ data.tar.gz: d402efbebb2ef805c6698d86b62d8c7aea7e3c38
5
+ SHA512:
6
+ metadata.gz: dd9f944c5793e48bd7d0b8d74defbcc3c5fb15deaf5efa4e203debe61c91ea48cae12317b6934deab7754be9ec509ac11e241766994b679437423b50fece4002
7
+ data.tar.gz: 67194d89e47f37d91451db9ef1aac27a2b879aa00de374e04c72b5ba87545ffbe4a2a87efd2a74bbf4f93af63e0f10b5c6f85f3185d67ce4c01690ad5e3c794f
@@ -0,0 +1,136 @@
1
+ require 'fileutils'
2
+ require 'highline'
3
+
4
+
5
+ module Irbrc
6
+ VERSION = '0.0.1'
7
+ BASE_DIR = [
8
+ Dir.home,
9
+ '.irb/rc',
10
+ ].join File::SEPARATOR
11
+ LOCAL_FILE = '.irbrc'
12
+
13
+
14
+ class << self
15
+
16
+ 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
28
+ end
29
+ end
30
+
31
+ load rc_path if File.exists? rc_path
32
+ # eval(File.read(rc_path))
33
+ end
34
+
35
+
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}"
42
+ end
43
+ end
44
+
45
+ File.rename rc_path, LOCAL_FILE unless File.exists? LOCAL_FILE
46
+ end
47
+
48
+
49
+ def remove_rc opts = {}
50
+ unlink rc_path, LOCAL_FILE
51
+ end
52
+
53
+
54
+ def create_rc opts = {}
55
+ unlink rc_path if opts[:force]
56
+
57
+ if File.exists? rc_path
58
+ raise Exception.new "rc file already exists: #{rc_path}"
59
+ end
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"
65
+ end
66
+
67
+ nil
68
+ end
69
+
70
+
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
75
+ else
76
+ unlink LOCAL_FILE if opts[:force]
77
+ File.symlink rc_path, LOCAL_FILE
78
+ end
79
+
80
+ nil
81
+ end
82
+
83
+
84
+ def rc_path
85
+ repo = parse_repo
86
+
87
+ [
88
+ BASE_DIR,
89
+ repo[:source],
90
+ repo[:repo].sub(/#{File::SEPARATOR}/, '.') + '.rc',
91
+ ].join File::SEPARATOR
92
+ end
93
+
94
+
95
+ def parse_repo str = nil
96
+ str = `git remote -v` unless str
97
+
98
+ repos = str.split("\n").map(&:split).map do |line|
99
+ source, repo = line[1].split ':'
100
+ source.sub! /^.*@/, ''
101
+ source.sub! /\.(com|org)$/, ''
102
+
103
+ {
104
+ source: source,
105
+ repo: repo,
106
+ }
107
+ end.uniq
108
+
109
+ if repos.count != 1
110
+ raise Error.new "parse error: #{str}"
111
+ end
112
+
113
+ repos.first
114
+ end
115
+
116
+
117
+ def agree msg
118
+ HighLine.new.agree("#{msg}? [Y/n]") do |q|
119
+ yield q if block_given?
120
+ end
121
+ end
122
+
123
+
124
+ def unlink *paths
125
+ paths.select do |path|
126
+ 1 == File.unlink(path) if File.exists? path or File.symlink? path
127
+ end
128
+ end
129
+
130
+
131
+ end
132
+ end
133
+
134
+
135
+ # define global function for convenience
136
+ define_singleton_method(:load_rc) { Irbrc.load_rc }
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: irbrc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Pepper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5'
55
+ description: irbrc loader
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/irbrc.rb
62
+ homepage: https://github.com/dpep/rb_irbrc
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.6.11
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Irbrc
86
+ test_files: []