Module: Exercise::Helpers

Includes:
RenderMethods
Defined in:
lib/commands/exercise/command.rb

Instance Method Summary collapse

Methods included from RenderMethods

#render_exercise, #render_file_path, #templates_directory

Methods included from DigestMethods

#digest, #excluded_files, #paths

Methods included from Instructions

#after_rendering_run, #capture, #cd, #command, #command_output, #env, #last_command_output, #path, #substitute, #wait_until, #write_to_file

Methods included from Commandline::Output

#error, #ok, #output, #prefix, #say

Methods included from Commandline

#capture_output, #execute, #run

Instance Method Details

#all_files_in(name) ⇒ Object



77
78
79
# File 'lib/commands/exercise/command.rb', line 77

def all_files_in(name)
  Dir.glob("#{name}/**/*", File::FNM_DOTMATCH).find_all { |file| !%w[. ..].include?(File.basename(file)) }
end

#all_updated(paths, project_root_dir) ⇒ Object



81
82
83
84
85
# File 'lib/commands/exercise/command.rb', line 81

def all_updated(paths, project_root_dir)
  paths.find_all { |path| template_updated?(path, project_root_dir) }.collect do |template|
    Template.new(template, project_root_dir)
  end
end

#exercise_directories(path) ⇒ Object



91
92
93
# File 'lib/commands/exercise/command.rb', line 91

def exercise_directories(path)
  Dir["#{path}/**/.templates"]
end

#exercise_structureObject



87
88
89
# File 'lib/commands/exercise/command.rb', line 87

def exercise_structure
  @exercise_structure ||= YAML.safe_load(File.read(ENV['SCAFFOLD_STRUCTURE']))
end


95
96
97
98
99
# File 'lib/commands/exercise/command.rb', line 95

def print_rendering_banner(template_path)
  template_message = "# Generating template: #{template_path} in path: #{ENV['exercise_path']} #"
  top_and_tail = ''.rjust(template_message.length, '#')
  say "#{top_and_tail}\n#{template_message}\n#{top_and_tail}"
end

#process_template(original_dir, template) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/commands/exercise/command.rb', line 101

def process_template(original_dir, template)
  template_dir = template.dir
  Dir.chdir template_dir
  ENV['exercise_path'] = template_dir.gsub(original_dir, '')
  ENV['CIC_PWD'] = "#{ENV['CIC_PWD']}/#{template.relative_path(template_dir, original_dir)}"
  print_rendering_banner(template.path)
  !render_exercise(template.path, digest_component: options[:digest_component])
ensure
  ENV['CIC_PWD'] = original_dir
  Dir.chdir original_dir
end

#quiet?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/commands/exercise/command.rb', line 113

def quiet?
  options[:verbose] == false
end

#register_environment(environment_variables_string) ⇒ Object



121
122
123
124
# File 'lib/commands/exercise/command.rb', line 121

def register_environment(environment_variables_string)
  environment_variables = environment_variables_string.to_s.scan(%r{([\w+.]+)\s*=\s*([\w+./-]+)?}).to_h
  environment_variables.each { |key, value| ENV[key] = value }
end

#scaffold_pathObject



117
118
119
# File 'lib/commands/exercise/command.rb', line 117

def scaffold_path
  @scaffold_path ||= ENV['SCAFFOLD_PATH']
end

#template_updated?(template, project_root_dir) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
# File 'lib/commands/exercise/command.rb', line 132

def template_updated?(template, project_root_dir)
  template = template.is_a?(String) ? Template.new(template, project_root_dir) : template

  return true unless File.exist?(template.rendered_file_path)

  digest = digest(path: template.dir,
                  digest_component: options[:digest_component],
                  excludes: excluded_files(template.full_path))

  !File.read(template.rendered_file_path).include?(digest)
end

#templates(path) ⇒ Object



126
127
128
129
130
# File 'lib/commands/exercise/command.rb', line 126

def templates(path)
  exercise_directories(path).collect do |templates_dir|
    Dir["#{File.expand_path(templates_dir)}/*.md.erb"]
  end.flatten
end