Class: Workshop::Project::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/workshop/project/setup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_directoryObject

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_nameObject

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



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_directoryObject



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_rakefileObject



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_directoriesObject



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

#runObject



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