Module: Gempilot::CLI::Generator

Includes:
CommandKit::Colors, CommandKit::FileUtils
Included in:
Commands::Create, Commands::New
Defined in:
lib/gempilot/cli/generator.rb

Overview

File generation utilities for scaffolding gems and components.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#template_dirObject (readonly)

Returns the value of attribute template_dir.



29
30
31
# File 'lib/gempilot/cli/generator.rb', line 29

def template_dir
  @template_dir
end

Class Method Details

.included(command) ⇒ Object



14
15
16
# File 'lib/gempilot/cli/generator.rb', line 14

def self.included(command)
  command.extend ClassMethods
end

Instance Method Details

#chmod(mode, path) ⇒ Object



63
64
65
66
# File 'lib/gempilot/cli/generator.rb', line 63

def chmod(mode, path)
  print_action "chmod", path
  ::FileUtils.chmod(mode, path)
end

#cp(source, dest) ⇒ Object



68
69
70
71
# File 'lib/gempilot/cli/generator.rb', line 68

def cp(source, dest)
  print_action "cp", dest, source: source
  ::FileUtils.cp(File.join(@template_dir, source), dest)
end

#create_file(path, content) ⇒ Object



58
59
60
61
# File 'lib/gempilot/cli/generator.rb', line 58

def create_file(path, content)
  print_action "create", path
  File.write(path, content)
end

#erb(source, dest = nil) ⇒ Object



73
74
75
76
77
78
# File 'lib/gempilot/cli/generator.rb', line 73

def erb(source, dest = nil)
  print_action("erb", dest, source: source) if dest

  source_path = File.join(@template_dir, source)
  super(source_path, dest)
end

#initialize(**kwargs) ⇒ Object

Raises:

  • (NotImplementedError)


31
32
33
34
35
36
37
# File 'lib/gempilot/cli/generator.rb', line 31

def initialize(**kwargs)
  super

  return if (@template_dir = self.class.template_dir)

  raise NotImplementedError, "#{self.class} did not define template_dir"
end

#mkdir(path) ⇒ Object



48
49
50
51
# File 'lib/gempilot/cli/generator.rb', line 48

def mkdir(path)
  print_action "mkdir", path
  ::FileUtils.mkdir_p(path)
end


39
40
41
42
43
44
45
46
# File 'lib/gempilot/cli/generator.rb', line 39

def print_action(command, dest, source: nil)
  line = +""
  line << "\t" << colors.bold(colors.green(command))
  line << "\t" << colors.green(source) if source
  line << "\t" << colors.green(dest) if dest

  puts(line)
end

#sh(command, *arguments) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/gempilot/cli/generator.rb', line 80

def sh(command, *arguments)
  print_action "run", [command, *arguments].join(" ")
  success = Bundler.with_unbundled_env do
    system(command, *arguments)
  end
  puts colors.yellow("Warning: '#{[command, *arguments].join(" ")}' exited with non-zero status") unless success
  success
end

#touch(path) ⇒ Object



53
54
55
56
# File 'lib/gempilot/cli/generator.rb', line 53

def touch(path)
  print_action "touch", path
  ::FileUtils.touch(path)
end