Sha256: e96c56d9d1f022bd7627a3eaab2c7297455d8ee2aaf92db0b1a966a1903ba15c

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "dry/inflector"
require "securerandom"
require "shellwords"
require "snowpack/version"
require_relative "../../generator"

module Snowpack
  module Generators
    module Application
      class Generator < Snowpack::Generator
        # FIXME: need to make templates_dir handling nicer, also support multiple dirs
        def initialize(templates_dir: nil)
          templates_dir ||= File.join(__dir__, "templates")
          super(templates_dir: templates_dir)
        end

        def call(output_dir, application_name)
          super(output_dir, env(application_name))
          generate_tool_versions output_dir
        end

        private

        def env(application_name)
          {
            application_path: inflector.underscore(application_name),
            application_module: inflector.camelize(application_name),
            random: -> name, *args { SecureRandom.public_send(name, *args) },
            snowpack_version: Snowpack::VERSION,
          }
        end

        def generate_tool_versions(output_dir)
          if `which asdf` && $?.success?
            versions = %w[nodejs postgres ruby]
              .map { |tool|
                version = asdf_current_version(tool)
                "#{tool} #{version}" if version
              }
              .compact

            if versions.any?
              files.write(
                File.join(output_dir, ".tool-versions"),
                versions.join("\n") + "\n",
              )
            end
          end
        end

        def asdf_current_version(tool)
          output = `asdf current #{Shellwords.shellescape(tool)}`

          # Handle this output:
          # 2.6.2    (set by /path/to/.tool-versions)
          output.split(" ").first if $?.success?
        end

        def inflector
          @inflector ||= Dry::Inflector.new
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/generators/application/generator.rb
snowpack-1.0.0.alpha6 lib/snowpack/generators/application/generator.rb
snowpack-1.0.0.alpha5 lib/snowpack/generators/application/generator.rb
snowpack-1.0.0.alpha4 lib/snowpack/generators/application/generator.rb
snowpack-1.0.0.alpha3 lib/snowpack/generators/application/generator.rb
snowpack-1.0.0.alpha2 lib/snowpack/generators/application/generator.rb