diff --git a/lib/lyp.rb b/lib/lyp.rb index 3da76a2..5b3ea41 100644 --- a/lib/lyp.rb +++ b/lib/lyp.rb @@ -1,14 +1,23 @@ -def req(f); require File.expand_path("lyp/#{f}", File.dirname(__FILE__)) end +def req_int(f) + require File.expand_path("lyp/#{f}", File.dirname(__FILE__)) +end -req 'base' -req 'system' -req 'settings' +module Kernel + @@ext_requires = {} + def req_ext(l) + @@ext_requires[l] ||= require(l) + end +end -req 'template' -req 'resolver' -req 'wrapper' -req 'package' -req 'lilypond' -req 'transform' +req_int 'base' +req_int 'system' +req_int 'settings' -req 'windows' if Lyp::WINDOWS +req_int 'template' +req_int 'resolver' +req_int 'wrapper' +req_int 'package' +req_int 'lilypond' +req_int 'transform' + +req_int 'windows' if Lyp::WINDOWS diff --git a/lib/lyp/lilypond.rb b/lib/lyp/lilypond.rb index 1c71206..9e1d773 100644 --- a/lib/lyp/lilypond.rb +++ b/lib/lyp/lilypond.rb @@ -1,7 +1,3 @@ -require 'uri' -require 'open3' -require 'ruby-progressbar' - module Lyp::Lilypond class << self NO_ARGUMENT_OPTIONS_REGEXP = /^\-([REnFOcSA]+)(.+)/ @@ -208,10 +204,14 @@ def select_default_lilypond! end def get_session_settings - YAML.load(IO.read(session_settings_filename)) rescue {} + req_ext('yaml') + settings = YAML.load(IO.read(session_settings_filename)) rescue {} + settings = {} unless settings.is_a?(Hash) + settings end def set_session_settings(settings) + req_ext('yaml') File.open(session_settings_filename, 'w+') do |f| f << YAML.dump(settings) end @@ -314,7 +314,7 @@ def get_system_lilyponds_paths # Returns a list of versions of lilyponds available for download def search(version_specifier = nil) - require 'open-uri' + req_ext 'open-uri' platform = detect_lilypond_platform url = "#{BASE_URL}/#{platform}/" @@ -452,12 +452,14 @@ def lilypond_install_url(platform, version, opts) end def temp_install_filename(url) + req_ext('uri') u = URI(url) "#{Lyp::TMP_ROOT}/#{File.basename(u.path)}" end def download_lilypond(url, fn, opts) - require 'httpclient' + req_ext 'ruby-progressbar' + req_ext 'httpclient' STDERR.puts "Downloading #{url}" unless opts[:silent] @@ -700,6 +702,7 @@ def uninstall_lilypond_version(path) end def exec(cmd, raise_on_failure = true) + req_ext('open3') $_out = "" $_err = "" success = nil diff --git a/lib/lyp/package.rb b/lib/lyp/package.rb index a37fe69..d34d874 100644 --- a/lib/lyp/package.rb +++ b/lib/lyp/package.rb @@ -1,7 +1,3 @@ -require 'fileutils' -require 'open-uri' -require 'yaml' - module Lyp::Package class << self @@ -371,6 +367,8 @@ def list_lyp_index(pattern = nil) end def lyp_index + req_ext 'yaml' + req_ext 'open-uri' @lyp_index ||= YAML.load(open(LYP_INDEX_URL)) end diff --git a/lib/lyp/settings.rb b/lib/lyp/settings.rb index b83fd14..a4c4b0e 100644 --- a/lib/lyp/settings.rb +++ b/lib/lyp/settings.rb @@ -1,12 +1,12 @@ -require 'yaml' - module Lyp::Settings class << self def load + req_ext 'yaml' @settings = YAML.load(IO.read(Lyp.settings_file)) rescue {} end def save + req_ext 'yaml' File.open(Lyp.settings_file, 'w+') {|f| f << YAML.dump(@settings)} end @@ -33,11 +33,13 @@ def []=(path, value) end def get_value(path, default = nil) + req_ext 'yaml' v = self[path] v ? YAML.load(v) : default end def set_value(path, value) + req_ext 'yaml' self[path] = YAML.dump(value) end end diff --git a/lib/lyp/system.rb b/lib/lyp/system.rb index 64a3f74..248dfb8 100644 --- a/lib/lyp/system.rb +++ b/lib/lyp/system.rb @@ -1,5 +1,3 @@ -require 'fileutils' - module Lyp::System class << self RUGGED_REQ = Lyp.version_req('>=0.23.0') @@ -26,7 +24,7 @@ def find_rugged_gem def require_rugged_gem gem 'rugged', RUGGED_REQ.to_s - require 'rugged' + req_ext 'rugged' end def use_git_based_rugged_gem diff --git a/lib/lyp/templates/deps_wrapper.rb b/lib/lyp/templates/deps_wrapper.rb index a5900b7..0d5fe3f 100644 --- a/lib/lyp/templates/deps_wrapper.rb +++ b/lib/lyp/templates/deps_wrapper.rb @@ -13,8 +13,6 @@ ` end -require 'fileutils' - user_filename = File.expand_path(_[:user_file]) user_dirname = File.dirname(user_filename) diff --git a/lib/lyp/transform.rb b/lib/lyp/transform.rb index 594a3ce..32f4a22 100644 --- a/lib/lyp/transform.rb +++ b/lib/lyp/transform.rb @@ -1,5 +1,3 @@ -require 'fileutils' - module Lyp::Transform class << self R = Lyp::DependencyResolver diff --git a/lib/lyp/wrapper.rb b/lib/lyp/wrapper.rb index 43491e2..8da1da5 100644 --- a/lib/lyp/wrapper.rb +++ b/lib/lyp/wrapper.rb @@ -1,5 +1,3 @@ -require 'tempfile' - module Lyp WRAPPER_TEMPLATE = Lyp::Template.new(IO.read( File.expand_path('templates/deps_wrapper.rb', File.dirname(__FILE__))