Module: Creact::FileOperations

Extended by:
FileOperations
Included in:
FileOperations
Defined in:
lib/creact/file_handlers.rb

Instance Method Summary collapse

Instance Method Details

#create_dirsObject



9
10
11
12
13
# File 'lib/creact/file_handlers.rb', line 9

def create_dirs
  @dirs = ['views', 'js']
  puts 'Creating the following dirs: ' + @dirs.join(' ')
  FileUtils.makedirs @dirs
end

#create_js(template = :mote) ⇒ Object



23
24
25
26
27
# File 'lib/creact/file_handlers.rb', line 23

def create_js(template=:mote)
  puts 'Creating js files...'
  @js_files = ['jquery.js', 'react.rb'].map { |file| get_absolute_path(file) }
  FileUtils.cp(@js_files, 'js')
end

#create_views(template = :mote) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/creact/file_handlers.rb', line 15

def create_views(template=:mote)
  # TODO: Add support for other template languages
  puts 'Creating view files...'
  files = ['layout.mote', 'home.mote']
  @view_files = files.map { |file| get_absolute_path(file) }
  FileUtils.cp(@view_files, 'views')
end

#delete_dirsObject



29
30
31
32
# File 'lib/creact/file_handlers.rb', line 29

def delete_dirs
  FileUtils.rm_rf 'views'
  FileUtils.rm_rf 'js'
end

#get_absolute_path(file) ⇒ Object



4
5
6
7
# File 'lib/creact/file_handlers.rb', line 4

def get_absolute_path(file)
  location = File.dirname(__FILE__)
  "#{location}/files/#{file}"
end

#run_generatorObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/creact/file_handlers.rb', line 34

def run_generator
  existing_files = []
  files_to_create = [
    'views/layout.mote',
    'views/home.mote',
    'js/react.rb',
    'js/jquery.js'
  ]
  files_to_create.each do |file|
    if File.exists? file
      existing_files << file
    end
  end
  if not existing_files.empty?
    raise IOError, "Move or rename the following files to use this generator: #{existing_files}"
  end

  puts 'Running generator'
  create_dirs
  create_views
  @view_files.map { |file| puts file }
  create_js
  @js_files.map { |file| puts file }
  puts 'Done!'
end