lib/svelte_on_rails/installer/hello_world.rb



module SvelteOnRails
  module Installer
    module HelloWorld

      def self.install_hello_world
        utils_i = SvelteOnRails::Installer::Utils

        # write templates

        templates = %w[
          app/controllers/svelte_on_rails_hello_world_controller.rb
          app/views/svelte_on_rails_hello_world/index.haml
          app/frontend/javascript/components/SvelteOnRailsHelloWorld.svelte
        ]
        utils_i.write_templates(templates)

        # route

        route = 'svelte_on_rails_hello_world#index'
        rr = utils_i.which_root_route
        root_url = "/"
        url = root_url + route.sub('#', '/')

        if rr && rr == route
          puts "Root route «#{route}» already exists, skipping."
          root_url
        elsif rr && utils_i.route_exists?(route.sub('#', '/'))
          puts "route «#{route}» already exists, skipping."
          url
        elsif rr
          utils_i.add_route("  get \"#{route.sub('#', '/')}\"")
          url
        else
          utils_i.add_route('  root "svelte_on_rails_hello_world#index"')
          root_url
        end

      end

      def self.remove_hello_world

        utils = SvelteOnRails::Installer::Utils
        if utils.ask_yn('Remove the Hello World component?')
          files = %w[
            app/views/svelte_on_rails_hello_world/index.haml
            app/views/svelte_on_rails_hello_world
            app/controllers/svelte_on_rails_hello_world_controller.rb
            app/frontend/javascript/components/SvelteOnRailsHelloWorld.svelte
          ]
          utils.remove_files(files)
          utils.remove_line_from_file('config/routes.rb', 'svelte_on_rails_hello_world')
        end
      end

    end
  end
end