Class: LearnGenerate::TemplateMaker

Inherits:
Object
  • Object
show all
Includes:
Helpers::DotLearnHelper, Helpers::GemfileHelper, Helpers::TemplateHelper
Defined in:
lib/learn_generate/template_maker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::DotLearnHelper

#build_dot_learn

Methods included from Helpers::GemfileHelper

#edit_classic_gemfile, #edit_gemfile, #edit_mvc_gemfile

Methods included from Helpers::TemplateHelper

#command_line_helper, #fe_helper, #fundamental_helper, #js_helper, #kids_helper, #rake_helper, #sinatra_classic_helper, #sinatra_mvc_helper, #sql_helper

Constructor Details

#initialize(template_type, lab_name) ⇒ TemplateMaker

Returns a new instance of TemplateMaker.



9
10
11
12
13
14
15
16
# File 'lib/learn_generate/template_maker.rb', line 9

def initialize(template_type, lab_name)
  @template_type = template_type
  @lab_name = lab_name
  templates_path     = File.expand_path('~/.learn-generate/templates')
  templates_git_path = File.expand_path('~/.learn-generate/templates/.git')
  @has_templates_dir = File.exists?(templates_path) && File.directory?(templates_path) && File.exists?(templates_git_path) && File.directory?(templates_git_path)
  @full_templates_path = templates_path + '/templates'
end

Instance Attribute Details

#full_templates_pathObject (readonly)

Returns the value of attribute full_templates_path.



7
8
9
# File 'lib/learn_generate/template_maker.rb', line 7

def full_templates_path
  @full_templates_path
end

#has_templates_dirObject (readonly)

Returns the value of attribute has_templates_dir.



7
8
9
# File 'lib/learn_generate/template_maker.rb', line 7

def has_templates_dir
  @has_templates_dir
end

#lab_nameObject (readonly)

Returns the value of attribute lab_name.



7
8
9
# File 'lib/learn_generate/template_maker.rb', line 7

def lab_name
  @lab_name
end

#template_typeObject (readonly)

Returns the value of attribute template_type.



7
8
9
# File 'lib/learn_generate/template_maker.rb', line 7

def template_type
  @template_type
end

Class Method Details

.run(template_type, lab_name) ⇒ Object



18
19
20
# File 'lib/learn_generate/template_maker.rb', line 18

def self.run(template_type, lab_name)
  new(template_type, lab_name).create
end

Instance Method Details



128
129
130
131
132
# File 'lib/learn_generate/template_maker.rb', line 128

def add_link_to_learn
  File.open('README.md', 'a') do |f|
    f << "\n<a href='https://learn.co/lessons/#{lab_name}' data-visibility='hidden'>View this lesson on Learn.co</a>\n"
  end
end

#bundle_initObject



88
89
90
# File 'lib/learn_generate/template_maker.rb', line 88

def bundle_init
  `bundle init`
end

#change_filename(path, filename, extension) ⇒ Object



102
103
104
105
106
# File 'lib/learn_generate/template_maker.rb', line 102

def change_filename(path, filename, extension)
  FileUtils.cd(path) do
    File.rename(filename, "#{formatted_lab_name}.#{extension}")
  end
end

#copyObject



55
56
57
58
59
60
61
# File 'lib/learn_generate/template_maker.rb', line 55

def copy
  if has_templates_dir
    FileUtils.cp_r("#{full_templates_path}/#{template_type}", FileUtils.pwd)
  else
    FileUtils.cp_r(LearnGenerate::FileFinder.location_to_dir("../templates/#{template_type}"), FileUtils.pwd)
  end
end

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/learn_generate/template_maker.rb', line 22

def create
  if template_type != 'ios'
    copy
    name_lab
    FileUtils.cd("#{lab_name}") do
      touch_spec unless ['js', 'sinatra-mvc', 'front-end', 'python', 'readme'].include?(template_type)
      touch_dot_learn
      build_dot_learn
      git_init
      bundle_init unless ['js', 'front-end', 'python', 'readme'].include?(template_type)
      edit_readme
      fundamental_helper if template_type == "fundamental-ruby"
      command_line_helper if template_type == "command-line"
      sql_helper if template_type == "SQL"
      rake_helper if template_type == "rake"
      sinatra_mvc_helper if template_type == "sinatra-mvc"
      sinatra_classic_helper if template_type == "sinatra-classic"
      js_helper if template_type == "js"
      fe_helper if template_type == "front-end"
      kids_helper if template_type == "kids"
      add_link_to_learn
    end
  else
    create_ios_lab
  end

  success_message
end

#create_ios_labObject



51
52
53
# File 'lib/learn_generate/template_maker.rb', line 51

def create_ios_lab
  LearnGenerate::IosLab.new(lab_name).execute
end

#edit_file(file, text) ⇒ Object



108
109
110
111
# File 'lib/learn_generate/template_maker.rb', line 108

def edit_file(file, text)
  new_rr = IO.read(file) % { file_name: text }
  File.open(file, 'w') { |f| f.write(new_rr) }
end

#edit_readmeObject



71
72
73
74
75
76
77
78
# File 'lib/learn_generate/template_maker.rb', line 71

def edit_readme
  readme_contents = File.read('README.md')
  readme_contents.sub!('## Objectives', "# #{formatted_name}\n\n## Objectives")

  File.open('README.md', 'w+') do |f|
    f.write(readme_contents)
  end
end

#edit_spec(file) ⇒ Object



113
114
115
# File 'lib/learn_generate/template_maker.rb', line 113

def edit_spec(file)
  File.open(file, 'w') { |f| f.write("require_relative './spec_helper'") }
end

#formatted_lab_nameObject



84
85
86
# File 'lib/learn_generate/template_maker.rb', line 84

def formatted_lab_name
  lab_name.gsub('-', '_')
end

#formatted_nameObject



80
81
82
# File 'lib/learn_generate/template_maker.rb', line 80

def formatted_name
  lab_name.gsub('-', ' ').split.map(&:capitalize).join(' ')
end

#git_initObject



67
68
69
# File 'lib/learn_generate/template_maker.rb', line 67

def git_init
  `git init`
end

#name_labObject



63
64
65
# File 'lib/learn_generate/template_maker.rb', line 63

def name_lab
  FileUtils.mv(template_type, lab_name)
end

#success_messageObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/learn_generate/template_maker.rb', line 117

def success_message
  puts "\n#{formatted_name} Lab successfully created in #{FileUtils.pwd}\n"
  FileUtils.cd("#{lab_name}") do
    tree_output = `which tree 2>/dev/null`

    if !tree_output.empty?
      puts "#{`tree`}"
    end
  end
end

#touch_dot_learnObject



98
99
100
# File 'lib/learn_generate/template_maker.rb', line 98

def touch_dot_learn
  `touch .learn`
end

#touch_specObject



92
93
94
95
96
# File 'lib/learn_generate/template_maker.rb', line 92

def touch_spec
  FileUtils.cd("spec/") do
    `touch #{formatted_lab_name}_spec.rb`
  end
end