Class: SimpleCommander::CLI

Inherits:
Object show all
Includes:
IO_helper
Defined in:
lib/simple_commander/cli.rb

Defined Under Namespace

Classes: InvalidProgram, UndefinedSCPath

Constant Summary collapse

DEFAULT_PATH =
"#{File.dirname(__FILE__)}/config.yml"
TEMPLATE_PATH =
File.expand_path "#{File.dirname(__FILE__)}" + 
'/../../templates'
HELPERS_PATH =
File.expand_path "#{File.dirname(__FILE__)}" + 
'/../../helpers'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = DEFAULT_PATH) ⇒ CLI

Returns a new instance of CLI.



32
33
34
35
# File 'lib/simple_commander/cli.rb', line 32

def initialize(path=DEFAULT_PATH)
	@config_file = path
	init_yaml_config if !File.file?(@config_file)
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



15
16
17
# File 'lib/simple_commander/cli.rb', line 15

def config_file
  @config_file
end

Instance Method Details

#exec_pathObject

if set the bin/executable* file of any program created will be put in this folder



89
90
91
92
93
94
95
96
# File 'lib/simple_commander/cli.rb', line 89

def exec_path
	path = YAML.load_file(@config_file)[:exec_path]
	if(path.empty?)
		return false
	else
		return path
	end
end

#init(path = './') ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/simple_commander/cli.rb', line 42

def init(path='./')
	if path
		local_path = File.expand_path(path)
	else
		local_path = File.expand_path('./')
	end
	yml = YAML.load_file(@config_file)
	yml[:path] = local_path
	File.open(@config_file, 'w+'){|f| f.write(yml.to_yaml)}
end

#init_yaml_configObject



37
38
39
40
# File 'lib/simple_commander/cli.rb', line 37

def init_yaml_config
	yml = { path: "", exec_path: "" }.to_yaml
	File.open(@config_file, 'w+'){|f| f.write(yml)}
end

#new(*args) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/simple_commander/cli.rb', line 58

def new(*args)
	sc_path = YAML.load_file(@config_file)[:path]
	raise UndefinedSCPath if !sc_path
	s_path = "#{sc_path}/#{args[0]}"
	fail InvalidProgram, "program #{args[0]} already exists!", caller if File.directory?(s_path)
	@program_name = args[0]
	@lib_path    = "#{s_path}/lib" 
	@helper_path = File.expand_path "#{s_path}" + "/../helpers" 

	mkdir s_path 
	mkdir "#{s_path}/bin"
	mkdir "#{s_path}/spec"
	mkdir "#{s_path}/lib"
	mkdir @helper_path
	mkdir "#{s_path}/lib/#{@program_name}"
	template "#{TEMPLATE_PATH}/lib.erb",
		"#{s_path}/lib/#{@program_name}.rb", binding

	template "#{TEMPLATE_PATH}/version.erb",
		"#{s_path}/lib/#{@program_name}/version.rb", binding

	template "#{TEMPLATE_PATH}/bin.erb",
		"#{s_path}/bin/#{@program_name}", binding
	FileUtils.chmod "+x", "#{s_path}/bin/#{@program_name}"
	copy "#{s_path}/bin/#{@program_name}", exec_path if exec_path
	copy "#{HELPERS_PATH}/io_helper.rb", "#{@helper_path}/io_helper.rb"
	copy "#{HELPERS_PATH}/http_helper.rb", "#{@helper_path}/http_helper.rb"
end

#set_exec_path(path) ⇒ Object

set exec path



100
101
102
103
104
# File 'lib/simple_commander/cli.rb', line 100

def set_exec_path(path)
	yml = YAML.load_file(@config_file)
	yml[:exec_path] = File.expand_path(path)
	File.open(@config_file, 'w+'){|f| f.write(yml.to_yaml)}
end

#show_configObject

Raises:



53
54
55
56
# File 'lib/simple_commander/cli.rb', line 53

def show_config
	raise UndefinedSCPath if !File.file?(@config_file)
	say YAML.load_file(@config_file)
end