fdietz-ruby-config 0.3.0 → 0.4.0

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,61 +1,77 @@
1
- # Not used currently
2
1
  module RubyConfig
2
+
3
3
  class ProfileConfig
4
4
 
5
5
  def initialize(bash_profile_path)
6
6
  @bash_profile_path = bash_profile_path
7
7
  end
8
8
 
9
- def change
10
- RubyConfig::FileHelper.append_content_to_file(content_with_header, @bash_profile_path)
9
+ def apply_changes
10
+ File.open(@bash_profile_path, 'a') { |file| file.write content_for_existing_script }
11
11
  end
12
12
 
13
13
  def exists?
14
- RubyConfig::FileHelper.read_content_from_file(@bash_profile_path).include?(content)
14
+ File.open(@bash_profile_path, "r") { |file| file.read }.include?(export_variables_string)
15
15
  end
16
16
 
17
- def content_with_header
18
- str = header
19
- str << content
20
- str
17
+ def file_exists?
18
+ File.exists?(@bash_profile_path)
19
+ end
20
+
21
+ def create_new_file
22
+ File.open(@bash_profile_path, 'w') { |file| file.write content_for_new_script }
21
23
  end
22
24
 
23
- def content
25
+ def export_variables_string
24
26
  str = ""
25
27
  [ruby_home, gem_home, gem_path, path].each do |line|
26
28
  str << export_line(line)
27
29
  end
28
30
  str
29
31
  end
30
-
32
+
31
33
  private
32
34
 
35
+ def content_for_new_script
36
+ str = bash_header
37
+ str << "\n"
38
+ str << header
39
+ str << export_variables_string
40
+ end
41
+
42
+ def content_for_existing_script
43
+ str = "\n"
44
+ str = header
45
+ str << export_variables_string
46
+ str
47
+ end
48
+
49
+ def bash_header
50
+ "#!/bin/bash"
51
+ end
52
+
33
53
  def header
34
54
  "# ruby-config v#{RubyConfig.version}"
35
55
  end
36
56
 
37
57
  def ruby_home
38
- "RUBY_HOME=#{home}/#{ruby_config}/ruby"
58
+ "RUBY_HOME=#{ruby_config_path}/ruby"
39
59
  end
40
60
 
41
61
  def gem_home
42
- "GEM_HOME=#{home}/#{ruby_config}/gem"
62
+ "GEM_HOME=#{ruby_config_path}/gem"
43
63
  end
44
64
 
45
65
  def gem_path
46
- "GEM_PATH=#{home}/#{ruby_config}/gem"
66
+ "GEM_PATH=#{ruby_config_path}/gem"
47
67
  end
48
68
 
49
69
  def path
50
70
  "PATH=$RUBY_HOME/bin:$GEM_HOME/bin:$PATH"
51
71
  end
52
-
53
- def home
54
- "$HOME"
55
- end
56
-
57
- def ruby_config
58
- ".ruby-config"
72
+
73
+ def ruby_config_path
74
+ "$HOME/.ruby-config"
59
75
  end
60
76
 
61
77
  def export_line(str)
@@ -46,23 +46,29 @@ module RubyConfig
46
46
 
47
47
  def setup
48
48
  config = RubyConfig::ProfileConfig.new(bash_profile_path)
49
-
50
- if config.exists?
49
+ if config.file_exists? && config.exists?
51
50
  puts "Found existing profile configuration. You are all set!"
52
51
  else
53
- puts "Ruby-Config will add the following lines to your environment:"
54
- puts "#{bash_profile_path}:"
55
- puts config.content
56
- puts
57
-
58
- if agree("Append configuration to your profile? [y/n] ", true)
59
- config.change
60
- puts
61
- puts "Done! To apply changes execute the following command:"
62
- puts " source #{bash_profile_path}"
52
+ show_configuration_changes(config.export_variables_string)
53
+ if agree("Apply configuration changes? [y/n] ", true)
54
+ config.file_exists? ? config.apply_changes : config.create_new_file
55
+ educate_about_source_profile
63
56
  end
64
57
  end
65
58
  end
59
+
60
+ def educate_about_source_profile
61
+ puts
62
+ puts "Done! Either start a new shell or execute the following command to apply the changes to your current shell:"
63
+ puts "# source #{bash_profile_path}"
64
+ end
65
+
66
+ def show_configuration_changes(content)
67
+ puts "Ruby-Config will apply the following changes to your profile"
68
+ puts "#{bash_profile_path}:"
69
+ puts content
70
+ puts
71
+ end
66
72
 
67
73
  def bash_profile_path
68
74
  File.join(ENV['HOME'], '.bash_profile')
@@ -0,0 +1,79 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+ #require 'ruby_config/profile_config'
3
+
4
+ class ProfileConfigTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @file = File.join('/', 'tmp', 'bash_profile')
8
+ @config = RubyConfig::ProfileConfig.new(@file)
9
+ end
10
+
11
+ def teardown
12
+ FileUtils.rm(@file) if File.exist?(@file)
13
+ end
14
+
15
+ test "should return true if file exists" do
16
+ File.open(@file, 'w') { |file| file.write "blabla" }
17
+ assert @config.file_exists?
18
+ end
19
+
20
+ test "should return false if file does NOT exist" do
21
+ assert_equal false, @config.file_exists?
22
+ end
23
+
24
+ test "should return false if configuration does NOT exist" do
25
+ File.open(@file, 'w') { |file| file.write "blabla" }
26
+ assert_equal false, @config.exists?
27
+ end
28
+
29
+ test "should return true if configuration exists" do
30
+ @config.create_new_file
31
+ assert @config.exists?
32
+ end
33
+
34
+ test "should create new bash_profile file" do
35
+ @config.create_new_file
36
+
37
+ content = File.open(@file, "r") { |file| file.read }
38
+ assert content.include?("ruby-config")
39
+ end
40
+
41
+ test "should apply changes to existing bash_profile file" do
42
+ File.open(@file, 'w') { |file| file.write "#!bin/bash\n" }
43
+ @config.apply_changes
44
+
45
+ content = File.open(@file, "r") { |file| file.read }
46
+ assert content.include?("ruby-config")
47
+ end
48
+
49
+ test "should contain the RUBY_HOME variable" do
50
+ result = @config.send(:export_variables_string)
51
+ assert result.include?("RUBY_HOME=$HOME/.ruby-config/ruby")
52
+ end
53
+
54
+ test "should contain the GEM_HOME variable" do
55
+ result = @config.send(:export_variables_string)
56
+ assert result.include?("GEM_HOME=$HOME/.ruby-config/gem")
57
+ end
58
+
59
+ test "should contain the GEM_PATH variable" do
60
+ result = @config.send(:export_variables_string)
61
+ assert result.include?("GEM_PATH=$HOME/.ruby-config/gem")
62
+ end
63
+
64
+ test "should contain the PATH variable" do
65
+ result = @config.send(:export_variables_string)
66
+ assert result.include?("PATH=$RUBY_HOME/bin:$GEM_HOME/bin:$PATH")
67
+ end
68
+
69
+ test "should contain the ruby-config header" do
70
+ result = @config.send(:content_for_existing_script)
71
+ assert result.include?("# ruby-config v#{RubyConfig.version}")
72
+ end
73
+
74
+ test "should contain bash script header" do
75
+ result = @config.send(:content_for_new_script)
76
+ assert result.include?("#!/bin/bash")
77
+ end
78
+
79
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fdietz-ruby-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederik Dietz
@@ -41,6 +41,7 @@ files:
41
41
  - README.md
42
42
  has_rdoc: false
43
43
  homepage: http://github.com/fdietz/ruby-config
44
+ licenses:
44
45
  post_install_message:
45
46
  rdoc_options:
46
47
  - --charset=UTF-8
@@ -61,10 +62,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  requirements: []
62
63
 
63
64
  rubyforge_project:
64
- rubygems_version: 1.2.0
65
+ rubygems_version: 1.3.5
65
66
  signing_key:
66
67
  specification_version: 3
67
68
  summary: Install and Switch between various Ruby Runtimes easily
68
69
  test_files:
70
+ - test/unit/profile_config_test.rb
69
71
  - test/unit/registry_test.rb
70
72
  - test/unit/runtime_base_test.rb