Module: Lyp::System

Defined in:
lib/lyp/system.rb

Constant Summary collapse

INSTALL_MSG =
<<EOF

Warning! Lyp is not yet properly installed in your home directory. 

To install lyp run 'lyp install self'. lyp will then:
  1. Setup ~/.lyp as its base directory.
  2. Add the lyp and lilypond scripts to ~/.lyp/bin.
  3. Add ~/.lyp/bin to front of $PATH.
  
You can uninstall lyp at any time by running 'lyp uninstall self'.

EOF
PROFILE_FILES =
%w{
  .profile .bash_profile .bash_login .bashrc .zshenv .zshrc .mkshrc
}.map {|fn| File.join(Dir.home, fn)}
LYP_LOAD_CODE =
<<EOF

[[ ":${PATH}:" == *":${HOME}/.lyp/bin:"* ]] || PATH="$HOME/.lyp/bin:$PATH"
EOF
RELEASE_BIN_PATH =
"lib/app/bin"

Class Method Summary collapse

Class Method Details

.install!Object

Adds ~/.lyp/bin to $PATH to the first profile file that exists, then returns the profile filename



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lyp/system.rb', line 33

def install!
  puts "\nInstalling lyp...\n\nAdding ~/.lyp/bin to $PATH..."
  profile_fn = setup_bin_path
  puts "Setting up binary scripts..."
  setup_files
  
  if installed?(no_path_check: true)
    puts "\nTo finish installation, open a new shell or run 'source ~/#{File.basename(profile_fn)}'.\n\n"
  else
    raise "Failed to install lyp"
  end
end

.installed?(opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/lyp/system.rb', line 24

def installed?(opts = {})
  path_is_there = ":#{::ENV['PATH']}:" =~ /#{Lyp::LYP_BIN_DIRECTORY}/
  file_is_there = File.file?("#{Lyp::LYP_BIN_DIRECTORY}/lyp")
  
  (opts[:no_path_check] || path_is_there) && file_is_there
end

.is_gem?(bin_dir) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/lyp/system.rb', line 79

def is_gem?(bin_dir)
  bin_dir !~ /#{RELEASE_BIN_PATH}$/
end

.setup_bin_pathObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lyp/system.rb', line 55

def setup_bin_path
  fn = PROFILE_FILES.find {|f| File.file?(f)}
  unless fn
    raise "Could not find a shell profile file"
  end
  
  unless (IO.read(fn) =~ /\.lyp\/bin/)
    File.open(fn, 'a') {|f| f << LYP_LOAD_CODE}
  end
  fn
end

.setup_filesObject



67
68
69
70
71
72
73
74
75
# File 'lib/lyp/system.rb', line 67

def setup_files
  bin_dir = File.expand_path(File.dirname($0))
  
  if is_gem?(bin_dir)
    setup_gem_files(bin_dir)
  else
    setup_release_files(bin_dir)
  end
end

.setup_gem_files(bin_dir) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/lyp/system.rb', line 83

def setup_gem_files(bin_dir)

  FileUtils.rm_rf(Lyp::LYP_BIN_DIRECTORY)
  FileUtils.mkdir_p(Lyp::LYP_BIN_DIRECTORY)

  %w{lyp lilypond}.each do |fn|
    FileUtils.ln_sf("#{bin_dir}/#{fn}", "#{Lyp::LYP_BIN_DIRECTORY}/#{fn}")
  end
end

.setup_release_files(bin_dir) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lyp/system.rb', line 93

def setup_release_files(bin_dir)
  FileUtils.rm_rf(Lyp::LYP_BIN_DIRECTORY)
  FileUtils.mkdir_p(Lyp::LYP_BIN_DIRECTORY)
  
  release_dir = File.expand_path(File.join(bin_dir, '../../../'))
  
  puts "Copying Ruby runtime & gems..."
  lib_dir = File.join(release_dir, 'lib')
  FileUtils.rm_rf(Lyp::LYP_LIB_DIRECTORY)
  FileUtils.cp_r(lib_dir, Lyp::LYP_LIB_DIRECTORY)
  
  puts "Copying binary scripts..."
  wrapper_bin_dir = File.join(release_dir, 'bin')
  %w{lyp lilypond}.each do |f|
    FileUtils.cp("#{wrapper_bin_dir}/#{f}", "#{Lyp::LYP_BIN_DIRECTORY}/#{f}")
  end
end

.test_installed_status!Object



18
19
20
21
22
# File 'lib/lyp/system.rb', line 18

def test_installed_status!
  unless installed?
    puts INSTALL_MSG
  end
end

.uninstall!Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lyp/system.rb', line 111

def uninstall!
  puts "\nUninstalling lyp...\n\nRemoving ~/.lyp/bin from $PATH..."
  
  # Remove ~/.lyp/bin from $PATH
  PROFILE_FILES.each do |fn|
    next unless File.file?(fn)
    
    content = IO.read(fn)
    if (content =~ /\.lyp\/bin/)
      content.gsub!(/\n?.*\.lyp\/bin.*\n/, '')
      File.open(fn, 'w+') {|f| f << content}
    end
  end
  
  puts "Removing binary scripts..."
  # Delete bin scripts
  Dir["#{Lyp::LYP_BIN_DIRECTORY}/*"].each do |fn|
    FileUtils.rm_f(fn) rescue nil
  end
  
  puts "\nTo completely remove installed packages and lilyponds run 'rm -rf ~/.lyp'.\n\n"
end