<commit_before>provides :cpan_module

property :module_name, String, name_property: true
property :force, [TrueClass, FalseClass], default: false
property :test, [TrueClass, FalseClass], default: false
property :version, String
property :cwd, String

action :install do
  execute "CPAN :install #{new_resource.name}" do
    cwd current_working_dir
    command cpanm_install_cmd
    environment 'HOME' => current_working_dir, 'PATH' => '/usr/local/bin:/usr/bin:/bin'
    not_if { module_exists_new_enough }
  end
end

action :uninstall do
  execute "CPAN :uninstall #{new_resource.name}" do
    cwd current_working_dir
    command cpanm_uninstall_cmd
    only_if module_exists
  end
end

action_class.class_eval do
  def module_exists_new_enough
    existing_version = parse_cpan_version
    return false if existing_version.empty? # mod doesn't exist
    return true if new_resource.version.nil? # mod exists and version is unimportant
    @comparator, @pending_version = new_resource.version.split(' ', 2)
    @current_vers = Gem::Version.new(existing_version)
    @pending_vers = Gem::Version.new(@pending_version)
    @current_vers.method(@comparator).call(@pending_vers)
  end

  def parse_cpan_version
    mod_ver_cmd = Mixlib::ShellOut.new("perl -M#{new_resource.name} -e 'print $#{new_resource.name}::VERSION;' 2> /dev/null")
    mod_ver_cmd.run_command
    mod_ver = mod_ver_cmd.stdout
    return mod_ver if mod_ver.empty?
    # remove leading v and convert underscores to dots since gems parses them wrong
    mod_ver.gsub!(/v_/, 'v' => 3, '_' => '.')
    # in the event that this command outputs whatever it feels like, only keep the first vers number!
    version_match = /(^[0-9.]*)/.match(mod_ver)
    version_match[0]
  end

  def module_exists
    "perl -m#{new_resource.name} -e ';' 2> /dev/null"
  end

  def cpanm_install_cmd
    @cmd = "#{node['perl']['cpanm']['path']} --quiet "
    @cmd += '--force ' if new_resource.force
    @cmd += '--notest ' unless new_resource.test
    @cmd += new_resource.name
    @cmd += parsed_version
    @cmd
  end

  def cpanm_uninstall_cmd
    @cmd = "#{node['perl']['cpanm']['path']} "
    @cmd += '--force ' if new_resource.force
    @cmd += '--uninstall '
    @cmd += new_resource.name
    @cmd
  end

  # a bit of a stub, could use a version parser for really consistent expeirence
  def parsed_version
    return "~\"#{new_resource.version}\"" if new_resource.version
    ''
  end

  def current_working_dir
    return new_resource.cwd if new_resource.cwd
    return '/var/root' if node['platform'] == 'mac_os_x'
    '/root'
  end
end<commit_msg>Fix module_name property to properly allow overriding the name<commit_after>provides :cpan_module

property :module_name, String, name_property: true
property :force, [TrueClass, FalseClass], default: false
property :test, [TrueClass, FalseClass], default: false
property :version, String
property :cwd, String

action :install do
  execute "CPAN :install #{new_resource.module_name}" do
    cwd current_working_dir
    command cpanm_install_cmd
    environment 'HOME' => current_working_dir, 'PATH' => '/usr/local/bin:/usr/bin:/bin'
    not_if { module_exists_new_enough }
  end
end

action :uninstall do
  execute "CPAN :uninstall #{new_resource.module_name}" do
    cwd current_working_dir
    command cpanm_uninstall_cmd
    only_if module_exists
  end
end

action_class.class_eval do
  def module_exists_new_enough
    existing_version = parse_cpan_version
    return false if existing_version.empty? # mod doesn't exist
    return true if new_resource.version.nil? # mod exists and version is unimportant
    @comparator, @pending_version = new_resource.version.split(' ', 2)
    @current_vers = Gem::Version.new(existing_version)
    @pending_vers = Gem::Version.new(@pending_version)
    @current_vers.method(@comparator).call(@pending_vers)
  end

  def parse_cpan_version
    mod_ver_cmd = Mixlib::ShellOut.new("perl -M#{new_resource.module_name} -e 'print $#{new_resource.module_name}::VERSION;' 2> /dev/null")
    mod_ver_cmd.run_command
    mod_ver = mod_ver_cmd.stdout
    return mod_ver if mod_ver.empty?
    # remove leading v and convert underscores to dots since gems parses them wrong
    mod_ver.gsub!(/v_/, 'v' => 3, '_' => '.')
    # in the event that this command outputs whatever it feels like, only keep the first vers number!
    version_match = /(^[0-9.]*)/.match(mod_ver)
    version_match[0]
  end

  def module_exists
    "perl -m#{new_resource.module_name} -e ';' 2> /dev/null"
  end

  def cpanm_install_cmd
    @cmd = "#{node['perl']['cpanm']['path']} --quiet "
    @cmd += '--force ' if new_resource.force
    @cmd += '--notest ' unless new_resource.test
    @cmd += new_resource.module_name
    @cmd += parsed_version
    @cmd
  end

  def cpanm_uninstall_cmd
    @cmd = "#{node['perl']['cpanm']['path']} "
    @cmd += '--force ' if new_resource.force
    @cmd += '--uninstall '
    @cmd += new_resource.module_name
    @cmd
  end

  # a bit of a stub, could use a version parser for really consistent expeirence
  def parsed_version
    return "~\"#{new_resource.version}\"" if new_resource.version
    ''
  end

  def current_working_dir
    return new_resource.cwd if new_resource.cwd
    return '/var/root' if node['platform'] == 'mac_os_x'
    '/root'
  end
end
{'fraction_non_alphanumeric': 0.11689291101055807, 'fraction_numerical': 0.0026395173453996985, 'mean_word_length': 3.8984302862419207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3592439', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1752, 'n_words': 457}