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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IO_helper

#cli_exist?, #command_exist?, #copy, #copy_dir, #have_subcommands?, #in_file?, #mkdir, #rm_block, #rm_dir, #rm_file, #rm_string, #run_cmd, #subcommand_exist?, #template, #write_after, #write_end, #write_start

Constructor Details

#initialize(path = DEFAULT_PATH) ⇒ CLI

Returns a new instance of CLI.



30
31
32
33
# File 'lib/simple_commander/cli.rb', line 30

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.



13
14
15
# File 'lib/simple_commander/cli.rb', line 13

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



82
83
84
# File 'lib/simple_commander/cli.rb', line 82

def exec_path
	YAML.load_file(@config_file)[:exec_path]
end

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



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

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



35
36
37
38
# File 'lib/simple_commander/cli.rb', line 35

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

#new(*args) ⇒ Object

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/simple_commander/cli.rb', line 56

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" 
	mkdir s_path 
	mkdir "#{s_path}/bin"
	mkdir "#{s_path}/spec"
	mkdir "#{s_path}/lib"
	mkdir "#{s_path}/lib/#{@program_name}"
	template "#{TEMPLATE_PATH}/lib.erb",
		"#{s_path}/lib/#{@program_name}.rb"

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

	template "#{TEMPLATE_PATH}/bin.erb",
		"#{s_path}/bin/#{@program_name}"
	FileUtils.chmod "+x", "#{s_path}/bin/#{@program_name}"
	copy "#{s_path}/bin/#{@program_name}", exec_path if exec_path
end

#set_exec_path(path) ⇒ Object

set exec path



88
89
90
91
92
# File 'lib/simple_commander/cli.rb', line 88

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:



51
52
53
54
# File 'lib/simple_commander/cli.rb', line 51

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