Class: CreateTemplates

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/generators/likelion/create_templates.rb

Constant Summary collapse

@@path =
"#{File.dirname(__FILE__)}/templates/#{ARGV[0]}"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



9
10
11
# File 'lib/generators/likelion/create_templates.rb', line 9

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#add_attributes(line) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/likelion/create_templates.rb', line 48

def add_attributes(line)
  #TODO : Devise는...?
  # Thor::Actions::inject_into_class
  line = line.split
  if line[2].eql?"controller"
    run(copy_file("#{@@path}/#{line[3]}_controller.rb", \
      "app/controllers/#{line[3]}_controller.rb"))
  elsif line[2].eql?"model"
    if File.exist?("#{@@path}/#{line[3]}.rb")
      run(copy_file("#{@@path}/#{line[3]}.rb", \
        "app/models/#{line[3]}.rb"))
    end
    #데이터베이스 설정
    set_database(line[3].pluralize)
  end
end

#add_bootstrapsObject



80
81
82
83
84
# File 'lib/generators/likelion/create_templates.rb', line 80

def add_bootstraps
  #Bootstrap CDN 추가
  # Thor::Actions::insert_into_file
  insert_into_file "app/views/layouts/application.html.erb", bootstrap_cdn, :before => "</head>\n"
end

#bootstrap_cdnObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/generators/likelion/create_templates.rb', line 86

def bootstrap_cdn
  '<!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>'
end

#comment_application_controllerObject



74
75
76
77
78
# File 'lib/generators/likelion/create_templates.rb', line 74

def comment_application_controller
  # appication_controller.rb에 protect_from_forgery 주석 처리
  # Thor::Actions::comment_lines
  comment_lines 'app/controllers/application_controller.rb', /protect_from_forgery with: :exception/
end

#copy_file(src, dest) ⇒ Object



45
46
47
# File 'lib/generators/likelion/create_templates.rb', line 45

def copy_file(src, dest)
  "cp -rf #{src} #{dest}"
end

#create_viewsObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/generators/likelion/create_templates.rb', line 102

def create_views
  File.open("#{@@path}/views.txt","r") do |infile|
    while(filename = infile.gets)
      filename.rstrip!
      controller, view = filename.split('.',2)
      run(copy_file("#{@@path}/#{filename}", \
        "app/views/#{controller}/#{view}"))
    end
  end
end

#processObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/likelion/create_templates.rb', line 13

def process
  source_paths << @@path
  #모델, 컨트롤러 등 생성
  run_commands

  #view  생성
  create_views

  #application_controller.rb 주석 처리
  comment_application_controller
  #bootstrap 추가
  add_bootstraps
  #routes.rb 설정
  set_routes
end

#run_commandsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/likelion/create_templates.rb', line 29

def run_commands  
  File.open("#{@@path}/command.txt","r") do |infile|
    #command.txt 내에 있는 커맨드 읽기
    while( line = infile.gets )
      #주석은 통과
      if line[0] == '#'
        next
      end
      #Command 실행. Thor::Actions::run(cmd)
      run (line)
      #Model과 Controller인 경우 내용(method, attributes 등) 추가
      add_attributes(line)
    end
  end
end

#set_database(model_name) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/generators/likelion/create_templates.rb', line 65

def set_database(model_name)
  if not File.exist?("#{@@path}/create_#{model_name}.rb")
    return
  end
  db_file = Dir['db/migrate/*'].select { |f| f =~ /#{model_name}.rb$/ } 
  run(copy_file("#{@@path}/create_#{model_name}.rb",db_file[0]))
end

#set_routesObject



97
98
99
100
# File 'lib/generators/likelion/create_templates.rb', line 97

def set_routes
  #routes.rb 내용을 복사
  run(copy_file("#{@@path}/routes.rb", "config/routes.rb"))
end