Sha256: 9bdb4ced0f3cc3aa8d4b369eaf047a4d83e7fc80b9164b17fa64063ecfaee294

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 KB

Contents

module Pantograph
  # Guides the new user through creating a new action
  module NewAction
    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

    def self.fetch_name
      puts("Must be lower case, and use a '_' between words. Do not use '.'".green)
      puts("examples: 'testflight', '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

    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 = File.join((PantographCore::PantographFolder.path || Dir.pwd), 'actions')
      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

    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

    def self.name_valid?(name)
      name =~ /^[a-z0-9_]+$/
    end
    private_class_method :name_valid?
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
pantograph-0.1.17 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.16 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.15 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.14 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.13 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.12 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.10 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.8 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.7 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.6 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.4 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.3 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.1 pantograph/lib/pantograph/new_action.rb
pantograph-0.1.0 pantograph/lib/pantograph/new_action.rb