<commit_before>require "rails/generators"
require "fileutils"

module Serviceworker
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      desc "Make your Rails app a progressive web app"
      source_root File.join(File.dirname(__FILE__), "templates")

      def create_assets
        template "manifest.json", javascripts_dir("manifest.json.erb")
        template "serviceworker.js", javascripts_dir("serviceworker.js.erb")
        template "serviceworker-companion.js", javascripts_dir("serviceworker-companion.js")
      end

      def create_initializer
        template "serviceworker.rb", initializers_dir("serviceworker.rb")
      end

      def update_application_js
        ext, directive = detect_js_format
        snippet = "#{directive} require serviceworker-companion\n"
        append_to_file application_js_path(ext), snippet
      end

      def update_precompiled_assets
        snippet = "Rails.configuration.assets.precompile += %w[serviceworker.js manifest.json]\n"
        file_path = initializers_dir("assets.rb")
        FileUtils.touch file_path
        append_to_file file_path, snippet
      end

      def update_application_layout
        layout = detect_layout or return
        snippet = %(<link rel="manifest" href="/manifest.json" />)
        snippet << %(\n<meta name="apple-mobile-web-app-capable" content="yes">)
        insert_into_file layout, snippet, before: "</head>\n"
      end

      def add_offline_html
        template "offline.html", public_dir("offline.html")
      end

      private

      def application_js_path(ext)
        javascripts_dir("application#{ext}")
      end

      def detect_js_format
        %w[.js .js.erb .coffee .coffee.erb .js.coffee .js.coffee.erb].each do |ext|
          next unless File.exist?(javascripts_dir("application#{ext}"))
          return [ext, "#="] if ext.include?(".coffee")
          return [ext, "//="]
        end
      end
<commit_msg>Add warning if application layout is not detected<commit_after>require "rails/generators"
require "fileutils"

module Serviceworker
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      desc "Make your Rails app a progressive web app"
      source_root File.join(File.dirname(__FILE__), "templates")

      def create_assets
        template "manifest.json", javascripts_dir("manifest.json.erb")
        template "serviceworker.js", javascripts_dir("serviceworker.js.erb")
        template "serviceworker-companion.js", javascripts_dir("serviceworker-companion.js")
      end

      def create_initializer
        template "serviceworker.rb", initializers_dir("serviceworker.rb")
      end

      def update_application_js
        ext, directive = detect_js_format
        snippet = "#{directive} require serviceworker-companion\n"
        append_to_file application_js_path(ext), snippet
      end

      def update_precompiled_assets
        snippet = "Rails.configuration.assets.precompile += %w[serviceworker.js manifest.json]\n"
        file_path = initializers_dir("assets.rb")
        FileUtils.touch file_path
        append_to_file file_path, snippet
      end

      def update_application_layout
        layout = detect_layout
        snippet = %(<link rel="manifest" href="/manifest.json" />)
        snippet << %(\n<meta name="apple-mobile-web-app-capable" content="yes">)
        unless layout
          warn "Could not locate application layout. To insert manifest tags manually, use:\n\n#{snippet}\n"
          return
        end
        insert_into_file layout, snippet, before: "</head>\n"
      end

      def add_offline_html
        template "offline.html", public_dir("offline.html")
      end

      private

      def application_js_path(ext)
        javascripts_dir("application#{ext}")
      end

      def detect_js_format
        %w[.js .js.erb .coffee .coffee.erb .js.coffee .js.coffee.erb].each do |ext|
          next unless File.exist?(javascripts_dir("application#{ext}"))
          return [ext, "#="] if ext.include?(".coffee")
          return [ext, "//="]
        end
      end

{'fraction_non_alphanumeric': 0.106201171875, 'fraction_numerical': 0.0, 'mean_word_length': 2.872400756143667, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '819411', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1173, 'n_words': 261}