<commit_before>require 'etc'
require 'pathname'
require 'fileutils'

require 'pdk'
require 'pdk/logger'
require 'pdk/module/metadata'
require 'pdk/module/templatedir'
require 'pdk/cli/exec'
require 'pdk/cli/input'
require 'pdk/util'

module PDK
  module Generate
    class Module
      DEFAULT_TEMPLATE = 'https://github.com/puppetlabs/pdk-module-template'

      def self.invoke(opts={})
        defaults = {
          'version'      => '0.1.0',
          'dependencies' => [
            { 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 1.0.0' }
          ]
        }

        defaults['license'] = opts[:license] if opts.has_key? :license
        target_dir = File.expand_path(opts[:target_dir])

        if File.exists?(target_dir)
          raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % {:dir => target_dir}
        end

        metadata = PDK::Module::Metadata.new(defaults)

        module_interview(metadata, opts) unless opts[:'skip-interview'] # @todo Build way to get info by answers file

        temp_target_dir = PDK::Util.make_tmpdir_name('pdk-module-target')

        prepare_module_directory(temp_target_dir)

        template_url = opts.fetch(:'template-url', DEFAULT_TEMPLATE)

        PDK::Module::TemplateDir.new(template_url) do |templates|
          templates.render do |file_path, file_content|
            file = Pathname.new(temp_target_dir) + file_path
            file.dirname.mkpath
            file.write(file_content)
          end

          # Add information about the template used to generate the module to the
          # metadata (for a future update command).
          metadata.update!(templates.metadata)

          File.open(File.join(temp_target_dir, 'metadata.json'), 'w') do |metadata_file|
            metadata_file.puts metadata.to_json
          end
        end

        FileUtils.mv(temp_target_dir, target_dir)
      end

      def self.prepare_module_directory(target_dir)
        [
          File.join(target_dir, 'manifests'),
          File.join(target_dir, 'templates'),
        ].each do |dir|
          begin
            FileUtils.mkdir_p(dir)
          rescue SystemCallError
            raise PDK::CLI::FatalError, _("Unable to create directory '%{dir}'") % {:dir => dir}
          end
        end
      end

      def self.module_interview(metadata, opts={})
        puts _(
          "We need to create a metadata.json file for this module. Please answer the " +
          "following questions; if the question is not applicable to this module, feel free " +
          "to leave it blank."
        )

        begin
          puts ""
          forge_user = PDK::CLI::Input.get(_("What is your Puppet Forge username?"), Etc.getlogin)
          metadata.update!('name' => "#{forge_user}-#{opts[:name]}")
        rescue StandardError => e
          PDK.logger.error(_("We're sorry, we could not parse your module name: %{message}") % {:message => e.message})
          retry
        end

        begin
          puts "\n" + _("Puppet uses Semantic Versioning (semver.org) to version modules.")
          module_version = PDK::CLI::Input.get(_("What version is this module?"), metadata.data['version'])
          metadata.update!('version' => module_version)
        rescue StandardError => e
          PDK.logger.error(_("We're sorry, we could not parse that as a Semantic Version: %{message}") % {message: e.message})<commit_msg>Populates name & author in default metadata value<commit_after>require 'etc'
require 'pathname'
require 'fileutils'

require 'pdk'
require 'pdk/logger'
require 'pdk/module/metadata'
require 'pdk/module/templatedir'
require 'pdk/cli/exec'
require 'pdk/cli/input'
require 'pdk/util'

module PDK
  module Generate
    class Module
      DEFAULT_TEMPLATE = 'https://github.com/puppetlabs/pdk-module-template'

      def self.invoke(opts={})
        defaults = {
          'name'         => "#{Etc.getlogin}-#{opts[:name]}",
          'version'      => '0.1.0',
          'dependencies' => [
            { 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 1.0.0' }
          ],
        }

        defaults['license'] = opts[:license] if opts.has_key? :license
        target_dir = File.expand_path(opts[:target_dir])

        if File.exists?(target_dir)
          raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % {:dir => target_dir}
        end

        metadata = PDK::Module::Metadata.new(defaults)

        module_interview(metadata, opts) unless opts[:'skip-interview'] # @todo Build way to get info by answers file

        temp_target_dir = PDK::Util.make_tmpdir_name('pdk-module-target')

        prepare_module_directory(temp_target_dir)

        template_url = opts.fetch(:'template-url', DEFAULT_TEMPLATE)

        PDK::Module::TemplateDir.new(template_url) do |templates|
          templates.render do |file_path, file_content|
            file = Pathname.new(temp_target_dir) + file_path
            file.dirname.mkpath
            file.write(file_content)
          end

          # Add information about the template used to generate the module to the
          # metadata (for a future update command).
          metadata.update!(templates.metadata)

          File.open(File.join(temp_target_dir, 'metadata.json'), 'w') do |metadata_file|
            metadata_file.puts metadata.to_json
          end
        end

        FileUtils.mv(temp_target_dir, target_dir)
      end

      def self.prepare_module_directory(target_dir)
        [
          File.join(target_dir, 'manifests'),
          File.join(target_dir, 'templates'),
        ].each do |dir|
          begin
            FileUtils.mkdir_p(dir)
          rescue SystemCallError
            raise PDK::CLI::FatalError, _("Unable to create directory '%{dir}'") % {:dir => dir}
          end
        end
      end

      def self.module_interview(metadata, opts={})
        puts _(
          "We need to create a metadata.json file for this module. Please answer the " +
          "following questions; if the question is not applicable to this module, feel free " +
          "to leave it blank."
        )

        begin
          puts ""
          forge_user = PDK::CLI::Input.get(_("What is your Puppet Forge username?"), metadata.data['author'])
          metadata.update!('name' => "#{forge_user}-#{opts[:name]}")
        rescue StandardError => e
          PDK.logger.error(_("We're sorry, we could not parse your module name: %{message}") % {:message => e.message})
          retry
        end

        begin
          puts "\n" + _("Puppet uses Semantic Versioning (semver.org) to version modules.")
          module_version = PDK::CLI::Input.get(_("What version is this module?"), metadata.data['version'])
          metadata.update!('version' => module_version)
        rescue StandardError => e
          PDK.logger.error(_("We're sorry, we could not parse that as a Semantic Version: %{message}") % {message: e.message})
{'fraction_non_alphanumeric': 0.1211340206185567, 'fraction_numerical': 0.001718213058419244, 'mean_word_length': 2.4872690963554667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 2, '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': '2617840', 'n_tokens_mistral': 2068, 'n_tokens_neox': 2028, 'n_words': 503}