Sha256: 43186b20cd2983f8894f3963c31dd3f20e9695bdc5e596b6f0f01f47b399cfb3

Contents?: true

Size: 1.46 KB

Versions: 13

Compression:

Stored size: 1.46 KB

Contents

require "rails/generators"
require "fileutils"

module Generators
  module Avo
    class EjectGenerator < ::Rails::Generators::Base
      argument :filename, type: :string, required: true

      source_root ::Avo::Engine.root

      namespace "avo:eject"

      TEMPLATES = {
        sidebar: "app/views/avo/sidebar/_sidebar.html.erb",
        logo: "app/views/avo/partials/logo.html.erb",
        header: "app/views/avo/partials/header.html.erb",
        footer: "app/views/avo/partials/footer.html.erb",
        scripts: "app/views/avo/partials/scripts.html.erb"
      }

      def handle
        if @filename.starts_with?(":")
          template_id = path_to_sym @filename
          template_path = TEMPLATES[template_id]

          if path_exists? template_path
            eject template_path
          else
            say("Failed to find the `#{template_id.to_sym}` template.", :yellow)
          end
        elsif path_exists? @filename
          eject @filename
        else
          say("Failed to find the `#{@filename}` template.", :yellow)
        end
      end

      no_tasks do
        def path_to_sym(filename)
          template_id = filename.dup
          template_id[0] = ""
          template_id.to_sym
        end

        def path_exists?(path)
          path.present? && File.file?(::Avo::Engine.root.join(path))
        end

        def eject(path)
          copy_file ::Avo::Engine.root.join(path), ::Rails.root.join(path)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
avo-1.2.10 lib/generators/avo/eject_generator.rb
avo-1.2.9 lib/generators/avo/eject_generator.rb
avo-1.2.8 lib/generators/avo/eject_generator.rb
avo-1.2.7 lib/generators/avo/eject_generator.rb
avo-1.2.6 lib/generators/avo/eject_generator.rb
avo-1.2.6.pre.1 lib/generators/avo/eject_generator.rb
avo-1.2.5 lib/generators/avo/eject_generator.rb
avo-1.2.4 lib/generators/avo/eject_generator.rb
avo-1.2.3 lib/generators/avo/eject_generator.rb
avo-1.3.0.pre.1 lib/generators/avo/eject_generator.rb
avo-1.2.2 lib/generators/avo/eject_generator.rb
avo-1.1.0 lib/generators/avo/eject_generator.rb
avo-1.1.0.pre.1 lib/generators/avo/eject_generator.rb