<commit_before># nuweb build tasks
namespace :nuweb do

  desc "Generate Ruby code from nuweb source"
  task :tangle => Dir['source/*.w'].collect{|fn| fn.gsub /\.w/,'.ws'}+
                  Dir['source/lib/**/*.rb'].collect{|fn| fn.gsub('source/lib/','lib/')}+
                  Dir['source/test/**/*'].collect{|fn| fn.gsub('source/test/','test/')}+
                  [:test]

  # directory 'lib'
  # directory 'lib/nio'
  # directory 'source/pdf'

  rule '.ws' => ['.w'] do |t|
    puts "build dir: #{Dir.pwd}"
    puts "nuweb -t #{t.source}"
    puts `nuweb -t #{t.source}`
     File.open(t.name,'w'){|f| f.puts "sentinel"}
  end

  clean_exts = ['*.tex','*.dvi','*.log','*.aux','*.out','*.ws']
  clobber_dirs = ['lib', 'source/pdf', 'test']
  clobber_exceptions = ['test/data.yaml', 'test/test_helper.rb']

  desc "Remove all nuweb generated files"
  task :clobber=>[:clean] do |t|
    clobber_dirs.map{|dir| Dir["#{dir}/**/*"]}.flatten.each do |fn|
      rm fn unless File.directory?(fn)
    end
  end

  desc "Clean up nuweb temporary files"
  task :clean do |t|
    rm_r clean_exts.collect{|x| Dir.glob('*'+x)+Dir.glob('source/*'+x)+Dir.glob('source/pdf/*'+x)}.flatten
  end

  desc "Generate nuweb source code documentation"
  task :weave => ['source/pdf'] + Dir['source/*.w'].collect{|fn| fn.gsub(/\.w/,'.pdf').gsub('source/','source/pdf/')}

  def rem_ext(fn, ext)
    ext = File.extname(fn) unless fn
    File.join(File.dirname(fn),File.basename(fn,ext))
  end

  def sub_dir(dir, fn)
    d,n = File.split(fn)
    File.join(d,File.join(dir,n))
  end

  def rep_dir(dir, fn)
    File.join(dir, File.basename(fn))
  end

  #note that if latex is run from the base directory and the file is in a subdirectory (source)
  # .aux/.out/.log files are created in the subdirectory and won't be found by the second
  # pass of latex;
  def w_to_pdf(s)
    fn = rem_ext(s,'.w')
    puts "dir: #{File.dirname(fn)}"
    doc_dir = File.dirname(fn)!='.' ? './pdf' : '../source/pdf'
    cd(File.dirname(fn)) do
      fn = File.basename(fn)
      2.times do
        puts "nuweb -o -l #{fn}.w"
        puts `nuweb -o -l #{fn}.w`
        puts "latex -halt-on-error #{fn}.tex"
        puts `latex -halt-on-error #{fn}.tex`
        puts "dvipdfm -o #{rep_dir(doc_dir,fn)}.pdf #{fn}.dvi"
       puts `dvipdfm -o #{rep_dir(doc_dir,fn)}.pdf #{fn}.dvi`
      end
    end
  end

  rule '.pdf' => [proc{|tn| File.join('source',File.basename(tn,'.pdf')+'.w')}] do |t|
    w_to_pdf t.source
  end

  rule /\Alib\/.*\.rb/ =>[proc{|tn| tn.sub(/\Alib\//, 'source/lib/') }]  do |t|
    cp t.source, t.name  if t.source
  end

  rule /\Atest\/.*/ =>[proc{|tn| tn.sub(/\Atest\//, 'source/test/') }]  do |t|
    cp t.source, t.name  if t.source
  end

  namespace :docs do

    task :package=>['nuweb:weave']
    Rake::PackageTask.new('nio-source-pdf', Nio::VERSION::STRING) do |p|
      # generate same formats as for the gem contents
      p.need_tar = PROJ.gem.need_tar
      p.need_zip = PROJ.gem.need_zip
      p.package_files.include "source/pdf/**/*.pdf"
    end

  end

    Rake::PackageTask.new('nio-source', Nio::VERSION::STRING) do |p|
      # generate same formats as for the gem contents
      p.need_tar = PROJ.gem.need_tar
      p.need_zip = PROJ.gem.need_zip
      # to generate the strict source we could require the clobber task and then
      # pack everything left... but we will just define what to pack
      p.package_files.include "source/**/*.txt"
      p.package_files.include "source/helpers/**/*"
      p.package_files.include "source/lib/**/*"
      p.package_files.include "source/test/**/*"
      p.package_files.include "source/**/*.w"
      p.package_files.exclude "source/pdf/**/*"
      p.package_files.include 'History.txt', 'License.txt', 'Manifest.txt', 'Rakefile', 'README.txt', 'setup.rb'
      p.package_files.include "tasks/**/*"
    end

end

task :clobber=>'nuweb:clobber'
task :clean=>'nuweb:clean'
# namespace :gem do
#   task :package=>'nuweb:tangle'
# end

desc 'Generate code and documentation from nuweb sources'
task :nuweb => ['nuweb:tangle', 'nuweb:weave']<commit_msg>fixes for rake tasks<commit_after># nuweb build tasks
namespace :nuweb do

  desc "Generate Ruby code from nuweb source"
  task :tangle => Dir['source/*.w'].collect{|fn| fn.gsub /\.w/,'.ws'}+
                  Dir['source/lib/**/*.rb'].collect{|fn| fn.gsub('source/lib/','lib/')}+
                  Dir['source/test/**/*'].collect{|fn| fn.gsub('source/test/','test/')}+
                  [:test]

  # directory 'lib'
  # directory 'lib/nio'
  # directory 'source/pdf'

  rule '.ws' => ['.w'] do |t|
    puts "build dir: #{Dir.pwd}"
    puts "nuweb -t #{t.source}"
    puts `nuweb -t #{t.source}`
     File.open(t.name,'w'){|f| f.puts "sentinel"}
  end

  clean_exts = ['*.tex','*.dvi','*.log','*.aux','*.out','*.ws']
  clobber_dirs = ['lib', 'source/pdf', 'test']
  clobber_exceptions = ['test/data.yaml', 'test/test_helper.rb']

  desc "Remove all nuweb generated files"
  task :clobber=>[:clean] do |t|
    clobber_dirs.map{|dir| Dir["#{dir}/**/*"]}.flatten.each do |fn|
      rm fn unless File.directory?(fn)
    end
  end

  desc "Clean up nuweb temporary files"
  task :clean do |t|
    rm_r clean_exts.collect{|x| Dir.glob('*'+x)+Dir.glob('source/*'+x)+Dir.glob('source/pdf/*'+x)}.flatten
  end

  desc "Generate nuweb source code documentation"
  task :weave => ['source/pdf'] + Dir['source/*.w'].collect{|fn| fn.gsub(/\.w/,'.pdf').gsub('source/','source/pdf/')}

  def rem_ext(fn, ext)
    ext = File.extname(fn) unless fn
    File.join(File.dirname(fn),File.basename(fn,ext))
  end

  def sub_dir(dir, fn)
    d,n = File.split(fn)
    File.join(d,File.join(dir,n))
  end

  def rep_dir(dir, fn)
    File.join(dir, File.basename(fn))
  end

  #note that if latex is run from the base directory and the file is in a subdirectory (source)
  # .aux/.out/.log files are created in the subdirectory and won't be found by the second
  # pass of latex;
  def w_to_pdf(s)
    fn = rem_ext(s,'.w')
    puts "dir: #{File.dirname(fn)}"
    doc_dir = File.dirname(fn)!='.' ? './pdf' : '../source/pdf'
    cd(File.dirname(fn)) do
      fn = File.basename(fn)
      2.times do
        puts "nuweb -o -l #{fn}.w"
        puts `nuweb -o -l #{fn}.w`
        puts "latex -halt-on-error #{fn}.tex"
        puts `latex -halt-on-error #{fn}.tex`
        puts "dvipdfm -o #{rep_dir(doc_dir,fn)}.pdf #{fn}.dvi"
       puts `dvipdfm -o #{rep_dir(doc_dir,fn)}.pdf #{fn}.dvi`
      end
    end
  end

  rule '.pdf' => [proc{|tn| File.join('source',File.basename(tn,'.pdf')+'.w')}] do |t|
    w_to_pdf t.source
  end

  rule /\Alib\/.*\.rb/ =>[proc{|tn| tn.sub(/\Alib\//, 'source/lib/') }]  do |t|
    cp t.source, t.name  if t.source
  end

  rule /\Atest\/.*/ =>[proc{|tn| tn.sub(/\Atest\//, 'source/test/') }]  do |t|
    cp t.source, t.name  if t.source
  end

  namespace :docs do

    task :package=>['nuweb:weave']
    Rake::PackageTask.new('nio-source-pdf', Nio::VERSION::STRING) do |p|
      # generate same formats as for the gem contents
      p.need_tar = PROJ.gem.need_tar
      p.need_zip = PROJ.gem.need_zip
      p.package_files.include "source/pdf/**/*.pdf"
    end

  end

    Rake::PackageTask.new('nio-source', Nio::VERSION::STRING) do |p|
      # generate same formats as for the gem contents
      p.need_tar = PROJ.gem.need_tar
      p.need_zip = PROJ.gem.need_zip
      # to generate the strict source we could require the clobber task and then
      # pack everything left... but we will just define what to pack
      p.package_files.include "source/**/*.txt"
      p.package_files.include "source/helpers/**/*"
      p.package_files.include "source/lib/**/*"
      p.package_files.include "source/test/**/*"
      p.package_files.include "source/**/*.w"
      p.package_files.exclude "source/pdf/**/*"
      p.package_files.include 'History.txt', 'License.txt', 'Manifest.txt', 'Rakefile', 'README.txt', 'setup.rb'
      p.package_files.include "tasks/**/*"
    end

end

task :clobber=>'nuweb:clobber'
task :clean=>'nuweb:clean'

Rake::Task['gem:package'].enhance ['nuweb:tangle'] # this is ineffective: the prerequisite should come first
Rake::Task['gem:release'].clear_prerequisites.enhance ['gem'] # remove clobber prerequisite

desc 'Generate code and documentation from nuweb sources'
task :nuweb => ['nuweb:tangle', 'nuweb:weave']

STDERR.puts "TTT #{Rake::Task['gem:package'].prerequisites.inspect}"
{'fraction_non_alphanumeric': 0.19236713827131138, 'fraction_numerical': 0.00023778385447628106, 'mean_word_length': 2.966053748231966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '183219', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3128, 'n_words': 724}