Class: FaaStRuby::Template
- Inherits:
-
Object
- Object
- FaaStRuby::Template
- Defined in:
- lib/faastruby/cli/template.rb,
lib/faastruby/server/template.rb
Constant Summary collapse
- TYPES =
['local', 'git', 'github']
Instance Attribute Summary collapse
-
#source ⇒ Object
Returns the value of attribute source.
-
#target ⇒ Object
Returns the value of attribute target.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #copy_file(source:, destination:) ⇒ Object
- #create_dir(dir) ⇒ Object
- #git? ⇒ Boolean
- #git_pull(repo, local_dir) ⇒ Object
- #github? ⇒ Boolean
-
#initialize(variables: {}) ⇒ Template
constructor
A new instance of Template.
- #install(to:, force: false, print_base_dir: false) ⇒ Object
- #install_from_folder(folder) ⇒ Object
- #install_from_folder_run(folder) ⇒ Object
- #install_from_git(repo) ⇒ Object
- #install_from_github ⇒ Object
- #local? ⇒ Boolean
- #render(file) ⇒ Object
Constructor Details
#initialize(variables: {}) ⇒ Template
Returns a new instance of Template.
9 10 11 12 13 |
# File 'lib/faastruby/cli/template.rb', line 9 def initialize(type:, source:) FaaStRuby::CLI.error(["Unknown template type: '#{type}'. Valid types are:", TYPES], color: nil) unless TYPES.include?(type) @type = type @source = source end |
Instance Attribute Details
#source ⇒ Object
Returns the value of attribute source.
8 9 10 |
# File 'lib/faastruby/cli/template.rb', line 8 def source @source end |
#target ⇒ Object
Returns the value of attribute target.
8 9 10 |
# File 'lib/faastruby/cli/template.rb', line 8 def target @target end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/faastruby/cli/template.rb', line 8 def type @type end |
Class Method Details
.gem_template_path_for(name, runtime:) ⇒ Object
4 5 6 |
# File 'lib/faastruby/cli/template.rb', line 4 def self.gem_template_path_for(name, runtime:) "#{Gem::Specification.find_by_name("faastruby").gem_dir}/templates/#{runtime}/#{name}" end |
Instance Method Details
#copy_file(source:, destination:) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/faastruby/cli/template.rb', line 73 def copy_file(source:, destination:) if File.file?(destination) && !@force print "File '#{destination}' already exists. Overwrite? [y/N] " answer = STDIN.gets.chomp return(puts "[skipped] #{destination}") unless ['y', 'Y'].include?(answer) end FileUtils.cp(source, destination) puts "+ f #{@output_prefix}#{destination}".green end |
#create_dir(dir) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/faastruby/cli/template.rb', line 64 def create_dir(dir) if File.directory?(dir) puts "! d #{@output_prefix}#{dir}".yellow else FileUtils.mkdir_p(dir) puts "+ d #{@output_prefix}#{dir}".green end end |
#git? ⇒ Boolean
87 88 89 |
# File 'lib/faastruby/cli/template.rb', line 87 def git? type == 'git' end |
#git_pull(repo, local_dir) ⇒ Object
38 39 40 41 |
# File 'lib/faastruby/cli/template.rb', line 38 def git_pull(repo, local_dir) FaaStRuby::CLI.error("Could not clone repository #{repo} into #{local_dir}") unless system("git clone #{repo} #{local_dir}") end |
#github? ⇒ Boolean
91 92 93 |
# File 'lib/faastruby/cli/template.rb', line 91 def github? type == 'github' end |
#install(to:, force: false, print_base_dir: false) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/faastruby/cli/template.rb', line 15 def install(to:, force: false, print_base_dir: false) @output_prefix = print_base_dir ? "#{print_base_dir}/" : "" @target = to @force = force FaaStRuby::CLI.error("Could not determine the target path for template '#{type}:#{source}'. Please report this bug at https://github.com/FaaStRuby/faastruby-cli/issues", color: nil) unless target return install_from_folder(source) if local? return install_from_git(source) if git? return install_from_github if github? end |
#install_from_folder(folder) ⇒ Object
43 44 45 46 47 |
# File 'lib/faastruby/cli/template.rb', line 43 def install_from_folder(folder) folder = File.(folder) folder_exists = File.directory?(folder) folder_exists ? install_from_folder_run(folder) : FaaStRuby::CLI.error("Could not find folder #{folder}") end |
#install_from_folder_run(folder) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/faastruby/cli/template.rb', line 49 def install_from_folder_run(folder) if File.directory?(target) puts "! d #{@output_prefix}#{target}".yellow else FileUtils.mkdir_p(target) puts "+ d #{@output_prefix}#{target}".green end Dir.glob("**/*", base: folder).each do |entry| full_source_path = "#{folder}/#{entry}" full_target_path = "#{target}/#{entry}" create_dir(full_target_path) if File.directory?(full_source_path) copy_file(source: full_source_path, destination: full_target_path) if File.file?(full_source_path) end end |
#install_from_git(repo) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/faastruby/cli/template.rb', line 30 def install_from_git(repo) local_dir = Dir.mktmpdir git_pull(repo, local_dir) install_from_folder(local_dir) ensure FileUtils.remove_entry local_dir end |
#install_from_github ⇒ Object
25 26 27 28 |
# File 'lib/faastruby/cli/template.rb', line 25 def install_from_github repo = "[email protected]:/#{source}.git" install_from_git(repo) end |
#local? ⇒ Boolean
83 84 85 |
# File 'lib/faastruby/cli/template.rb', line 83 def local? type == 'local' end |
#render(file) ⇒ Object
9 10 11 |
# File 'lib/faastruby/server/template.rb', line 9 def render(file) ERB.new(File.read(file)).result(binding) end |