Class: Workshop::Project::Setup
- Inherits:
-
Object
- Object
- Workshop::Project::Setup
- Defined in:
- lib/workshop/project/setup.rb
Instance Attribute Summary collapse
-
#base_directory ⇒ Object
Returns the value of attribute base_directory.
-
#project_name ⇒ Object
Returns the value of attribute project_name.
Instance Method Summary collapse
- #copy_blink_app ⇒ Object
- #create_base_directory ⇒ Object
- #create_rakefile ⇒ Object
- #create_sub_directories ⇒ Object
-
#initialize(base_directory, project_name) ⇒ Setup
constructor
A new instance of Setup.
- #log_create(name) ⇒ Object
- #read_file(file) ⇒ Object
- #relative(path) ⇒ Object
- #resolve_file(file) ⇒ Object
- #run ⇒ Object
- #write_file(file, contents) ⇒ Object
Constructor Details
#initialize(base_directory, project_name) ⇒ Setup
Returns a new instance of Setup.
6 7 8 9 |
# File 'lib/workshop/project/setup.rb', line 6 def initialize(base_directory, project_name) self.base_directory = base_directory self.project_name = project_name end |
Instance Attribute Details
#base_directory ⇒ Object
Returns the value of attribute base_directory.
4 5 6 |
# File 'lib/workshop/project/setup.rb', line 4 def base_directory @base_directory end |
#project_name ⇒ Object
Returns the value of attribute project_name.
4 5 6 |
# File 'lib/workshop/project/setup.rb', line 4 def project_name @project_name end |
Instance Method Details
#copy_blink_app ⇒ Object
68 69 70 71 |
# File 'lib/workshop/project/setup.rb', line 68 def copy_blink_app file = read_file('../templates/blink.cpp') write_file('src/' + project_name + '.cpp', file) end |
#create_base_directory ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/workshop/project/setup.rb', line 29 def create_base_directory if Dir.exist?(base_directory) puts "folder already exists" else log_create(relative(base_directory)) Dir.mkdir(base_directory) end end |
#create_rakefile ⇒ Object
62 63 64 65 66 |
# File 'lib/workshop/project/setup.rb', line 62 def create_rakefile template = ERB.new(read_file('../templates/Rakefile.erb')) rakefile = template.result(binding) write_file('Rakefile', rakefile) end |
#create_sub_directories ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/workshop/project/setup.rb', line 38 def create_sub_directories ['src', 'config'].each do |dirname| dir = base_directory + '/' + dirname log_create(relative(dir)) Dir.mkdir(dir) end end |
#log_create(name) ⇒ Object
18 19 20 |
# File 'lib/workshop/project/setup.rb', line 18 def log_create(name) puts "create #{name}".colorize(:green) end |
#read_file(file) ⇒ Object
51 52 53 |
# File 'lib/workshop/project/setup.rb', line 51 def read_file(file) File.read(resolve_file(file)) end |
#relative(path) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/workshop/project/setup.rb', line 22 def relative(path) to = Pathname.new(path) from = Pathname.new(base_directory) rel = to.relative_path_from(from) rel.to_s end |
#resolve_file(file) ⇒ Object
46 47 48 49 |
# File 'lib/workshop/project/setup.rb', line 46 def resolve_file(file) dir = File.dirname(__FILE__) File.join(dir, file) end |
#run ⇒ Object
11 12 13 14 15 16 |
# File 'lib/workshop/project/setup.rb', line 11 def run create_base_directory create_sub_directories create_rakefile copy_blink_app end |
#write_file(file, contents) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/workshop/project/setup.rb', line 55 def write_file(file, contents) log_create(file) File.open(base_directory + '/' + file, 'w') do |file| file.puts contents end end |