Module: Pantograph::NewAction

Defined in:
pantograph/lib/pantograph/new_action.rb

Overview

Guides the new user through creating a new action

Class Method Summary collapse

Class Method Details

.check_action_name_from_args(new_action_name) ⇒ Object



39
40
41
42
43
44
45
# File 'pantograph/lib/pantograph/new_action.rb', line 39

def self.check_action_name_from_args(new_action_name)
  if name_valid?(new_action_name)
    new_action_name
  else
    puts('Name is invalid. Please ensure the name is all lowercase, free of spaces and without special characters! Try again.')
  end
end

.fetch_nameObject



9
10
11
12
13
14
15
16
17
18
# File 'pantograph/lib/pantograph/new_action.rb', line 9

def self.fetch_name
  puts("Must be lower case, and use a '_' between words. Do not use '.'".green)
  puts("examples: 'gradle', 'upload_to_s3'".green)
  name = UI.input('Name of your action: ')
  until name_valid?(name)
    puts('Name is invalid. Please ensure the name is all lowercase, free of spaces and without special characters! Try again.')
    name = UI.input('Name of your action: ')
  end
  name
end

.generate_action(name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'pantograph/lib/pantograph/new_action.rb', line 20

def self.generate_action(name)
  template = File.read("#{Pantograph::ROOT}/lib/assets/custom_action_template.rb")
  template.gsub!('[[NAME]]', name)
  template.gsub!('[[NAME_UP]]', name.upcase)
  template.gsub!('[[NAME_CLASS]]', name.pantograph_class + 'Action')

  actions_path = if Dir.exist?('pantograph/lib/pantograph/actions')
                   File.join(Dir.pwd, 'pantograph/lib/pantograph/actions')
                 else
                   File.join((PantographCore::PantographFolder.path || Dir.pwd), 'actions')
                 end

  FileUtils.mkdir_p(actions_path) unless File.directory?(actions_path)

  path = File.join(actions_path, "#{name}.rb")
  File.write(path, template)
  UI.success("Created new action file '#{path}'. Edit it to implement your custom action.")
end

.run(new_action_name: nil) ⇒ Object



4
5
6
7
# File 'pantograph/lib/pantograph/new_action.rb', line 4

def self.run(new_action_name: nil)
  name = new_action_name && check_action_name_from_args(new_action_name) ? new_action_name : fetch_name
  generate_action(name)
end