Skip to content

Commit

Permalink
add configuration for Consular and better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
achiurizo committed Oct 9, 2011
1 parent ea95408 commit 2f1d8f1
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 14 deletions.
34 changes: 33 additions & 1 deletion lib/consular.rb
@@ -1,6 +1,6 @@
lib_dir = File.expand_path("..", __FILE__)
$:.unshift( lib_dir ) unless $:.include?( lib_dir )

require 'consular/version'
require 'consular/core'
require 'consular/dsl'
Expand All @@ -9,6 +9,7 @@
module Consular

class << self
attr_accessor :global_path, :default_editor

# Returns all avaialble cores.
#
Expand All @@ -32,5 +33,36 @@ def add_core(klass)
cores << klass
end

# Returns the global script path. If not set,
# defaults to ~/.config/consular
#
# @param [String] path
# File name in path
#
# @return [String] global script path
#
# @api public
def global_path(path = nil)
root = @global_path || File.join(ENV['HOME'],'.config','consular')
File.join root, (path || '')
end

# Configure Consular options.
#
# @param [Proc] block
# Configuration block
#
# @example
#
# Consular.configure do |c|
# c.global_path = '~/.consular'
# c.default_editor = 'vim'
# end
#
# @api public
def configure(&block)
yield self
end

end
end
104 changes: 91 additions & 13 deletions lib/consular/cli.rb
Expand Up @@ -4,37 +4,101 @@ module Consular
class CLI < Thor
include Thor::Actions

TERM_PATH = File.join(ENV['HOME'], '.config', 'consular')

# Source root for Thor to find templates
def self.source_root; File.expand_path('../../', __FILE__); end

# Run the Termfile or project script.
#
# @param [String] project
# Name of the project script. Otherwise leave blank for Termfile.
#
# @example
#
# # Executes global script foobar.term
# Consular::CLI.start ['start', 'foobar']
#
# # Executes global script foobar.yml
# Consular::CLI.start ['start', 'foobar.yml']
#
# # Executes the Termfile
# Consular::CLI.start ['start'] # ./Termfile
# Consular::CLI.start ['start', '-r=/tmp'] # /tmp/Termfile
#
# @api public
desc 'start PROJECT', 'runs the consular script'
method_option :root, :type => :string, :default => '.', :aliases => '-r'
def start(project = '')
def start(project = nil)
valid_core.new(termfile_path(project)).process!
end

# Run the Termfile or project script setup.
#
# @param [String] project
# Name of the project script. Otherwise leave blank for Termfile.
#
# @example
#
# # Executes global script setup for foobar.term
# Consular::CLI.start ['setup', 'foobar']
#
# # Executes global script setup for foobar.yml
# Consular::CLI.start ['setup', 'foobar.yml']
#
# # Executes the Termfile setup
# Consular::CLI.start ['setup'] # ./Termfile
# Consular::CLI.start ['setup', '-r=/tmp'] # /tmp/Termfile
#
# @api public
desc 'setup PROJECT', 'run the consular script setup'
method_option :root, :type => :string, :default => '.', :aliases => '-r'
def setup(project = '')
def setup(project = nil)
valid_core.new(termfile_path(project)).setup!
end

# Lists all avaiable global scripts
#
# @example
#
# Consular::CLI.start ['list']
#
# @api public
desc 'list', 'lists all consular scripts'
def list
say "Global scripts available: \n"
Dir.glob("#{TERM_PATH}/*[^~]").each do |file|
Dir.glob("#{Consular.global_path}/*[^~]").each do |file|
name = File.basename(file, '.term')
title = file_comment(file)
say " * #{name} - #{title}"
end
end

# Create the global script directory for Consular.
#
# @example
#
# Consular::CLI.start ['init']
#
# @api public
desc 'init', 'create consular directory'
def init
empty_directory TERM_PATH
empty_directory Consular.global_path
end

# Edit the specified global script or Termfile.
#
# @param [String] project
# Name of project script.
#
# @example
#
# # opens foobar for editing
# Consular::CLI.start ['edit', 'foobar']
# # opens foobar with specified editor
# Consular::CLI.start ['edit', 'foobar', '-e=vim']
# # opens /tmp/Termfile
# Consular::CLI.start ['edit', '-r=/tmp']
#
# @api public
desc 'edit PROJECT', 'opens the Termfile to edit'
method_option :root, :type => :string, :default => '.', :aliases => '-r'
method_option :editor, :type => :string, :default => nil, :aliases => '-e'
Expand All @@ -46,6 +110,21 @@ def edit(project = nil)
open_in_editor path, options[:editor]
end

# Delete the global script or Termfile
#
# @param [String] project
# Name of the project script.
#
# @example
#
# # deletes global script foobar.term
# Consular::CLI.start ['delete', 'foobar']
# # deletes global script foobar.yml
# Consular::CLI.start ['delete', 'foobaryml']
# # deletes /tmp/Termfile
# Consular::CLI.start ['delete', '-r=/tmp']
#
# @api public
desc 'delete PROJECT', 'delete the Termfile script'
method_option :root, :type => :string, :default => '.', :aliases => '-r'
def delete(project = nil)
Expand Down Expand Up @@ -84,16 +163,16 @@ def file_comment(file)
#
# @example
# termfile_path #=> ROOT/Termfile
# termfile_path 'foo' #=> TERM_PATH/foo.term
# termfile_path 'bar.yml' #=> TERM_PATH/bar.yml
# termfile_path 'foo' #=> GLOBAL_PATH/foo.term
# termfile_path 'bar.yml' #=> GLOBAL_PATH/bar.yml
#
# @api semipublic
# @api private
def termfile_path(project = nil)
if !project || project.empty?
File.join(options[:root], 'Termfile')
else
path = project =~ /\..*/ ? project : project + '.term'
File.join(TERM_PATH, path)
Consular.global_path path
end
end

Expand All @@ -107,10 +186,9 @@ def termfile_path(project = nil)
# @example
# open_in_editor '/path/to/Termfile', 'vim'
#
# @api public
# @api private
def open_in_editor(path, editor = nil)
editor = editor || ENV['EDITOR']
say "please set $EDITOR in your or specify an editor." unless editor
editor = editor || Consular.default_editor || ENV['EDITOR']
system "#{editor || 'open'} #{path}"
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/consular_spec.rb
@@ -0,0 +1,30 @@
require File.expand_path('../spec_helper', __FILE__)

describe Consular do

after do
Consular.instance_variable_set(:@cores,[])
Consular.instance_variable_set(:@global_path,nil)
end

it "can add cores" do
Consular.add_core Consular::Core
assert_equal 1, Consular.cores.size
end

it "has default configurations" do
assert_equal File.join(ENV['HOME'],'.config','consular',''), Consular.global_path
end

it "can be configured" do
Consular.configure do |c|
c.global_path = '/tmp/'
c.default_editor = 'vim'
end

assert_equal '/tmp/', Consular.global_path
assert_equal 'vim', Consular.default_editor
assert_equal '/tmp/Termfile', Consular.global_path('Termfile')
end

end

0 comments on commit 2f1d8f1

Please sign in to comment.