lib/scide.rb in scide-0.0.12 vs lib/scide.rb in scide-0.1.0

- old
+ new

@@ -1,71 +1,39 @@ require 'paint' -require 'upoj-rb' +require 'which_works' +require 'shellwords' -# Generator of GNU Screen configuration files. module Scide + VERSION = '0.1.0' - # Current version. - VERSION = File.open(File.join(File.dirname(__FILE__), '..', 'VERSION'), 'r').read - - # Exit status codes. - EXIT = { - :unexpected => 1, - :invalid_argument => 2, - :not_initialized => 3, - :screen_not_found => 4, - :config_not_found => 10, - :config_not_readable => 11, - :malformed_config => 12, - :invalid_config => 13, - :unknown_project => 14 - } - - # Prints a message on <tt>stderr</tt> and exits. - # If <tt>condition</tt> is a key from {EXIT}, the corresponding value - # will be used as the exit code. Otherwise, scide exits with - # status 1. - def self.fail condition, msg - if @@exit_on_fail - puts - warn Paint[msg, :yellow] - puts - EXIT.key?(condition) ? exit(EXIT[condition]) : exit(1) - else - raise Scide::Error.new condition, msg + # TODO: add detailed error description for non-trace mode + class Error < StandardError + attr_reader :code + + def initialize msg, code = 1 + super msg + @code = code end end - # By default, scide is meant to be used as a standalone script - # and exits if an error occurs. If <tt>exit_on_fail</tt> is - # false, a {Scide::Error} will be raised instead. Scide can then - # be used by another script. - def self.exit_on_fail= exit_on_fail - @@exit_on_fail = exit_on_fail - end + private - # Indicates whether scide is configured to exit on failure. - # See {Scide.exit_on_fail=}. - def self.exit_on_fail - @@exit_on_fail + def self.error msg, code = 1 + raise Error.new msg, code end - # Scide error. Can be raised if {exit_on_fail} is set to false. - class Error < StandardError - - # A symbol indicating the error type. See {EXIT}. - attr_reader :condition - - # Returns a new error. - def initialize condition, msg - super msg - @condition = condition - end + def self.projects_dir options = {} + options[:projects] || ENV['SCIDE_PROJECTS'] || File.expand_path('~/src') end - private - - @@exit_on_fail = true + def self.current_project_dir args, options = {} + project_name = args.shift + error "Only one project name must be given, got #{args.unshift project_name}" if args.any? + dir = project_name ? File.join(projects_dir(options), project_name) : Dir.pwd + error %/Cannot use home directory/ if File.expand_path(dir) == File.expand_path('~') + error %/No such directory "#{dir}"/ unless File.directory? dir + dir + end end -# load scide components -%w( command config global opts overmind project screen window ).each{ |dep| require File.join(File.dirname(__FILE__), 'scide', dep) } +Dir[File.join File.dirname(__FILE__), File.basename(__FILE__, '.*'), '*.rb'].each{ |lib| require lib } +