Class: SourceCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/source_cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basepath) ⇒ SourceCleaner

Returns a new instance of SourceCleaner.



4
5
6
# File 'lib/source_cleaner.rb', line 4

def initialize(basepath)
  self.basepath = basepath
end

Instance Attribute Details

#basepathObject

Returns the value of attribute basepath.



2
3
4
# File 'lib/source_cleaner.rb', line 2

def basepath
  @basepath
end

Instance Method Details

#clean(safe_mode = YARD::Config.options[:safe_mode]) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/source_cleaner.rb', line 8

def clean(safe_mode = YARD::Config.options[:safe_mode])
  if safe_mode
    puts "#{Time.now}: Cleaning #{basepath} (safemode=true)..."
  else
    puts "#{Time.now}: Not cleaning #{basepath} (safemode=false)"
    return
  end

  yardopts = File.join(basepath, '.yardopts')
  exclude = ['.yardoc', '.yardopts', '.git', '.document']
  exclude += Dir.glob(File.join(basepath, 'README*')).map {|f| remove_basepath(f) }
  if File.file?(yardopts)
    yardoc = YARD::CLI::Yardoc.new
    class << yardoc
      def basepath=(bp) @basepath = bp end
      def basepath; @basepath end
      def add_extra_files(*files)
        files.map! {|f| f.include?("*") ? Dir.glob(File.join(basepath, f)) : f }.flatten!
        files.each do |f|
          filename = f.sub(/^(#{File.realpath(basepath)}|#{basepath})\//, '')
          options[:files] << YARD::CodeObjects::ExtraFileObject.new(filename, '')
        end
      end
      def support_rdoc_document_file!(file = '.document')
        return [] unless use_document_file
        File.read(File.join(basepath, file)).
          gsub(/^[ \t]*#.+/m, '').split(/\s+/)
      rescue Errno::ENOENT
        []
      end
    end
    yardoc.basepath = basepath
    yardoc.options_file = yardopts if File.file?(yardopts)
    yardoc.parse_arguments

    exclude += yardoc.options[:files].map {|f| f.filename }
    exclude += yardoc.assets.keys
  end

  # make sure to keep relevant symlink targets
  link_exclude = exclude.inject(Array.new) do |lx, filespec|
    filespec = filespec.filename if filespec.respond_to?(:filename)
    Dir.glob(File.join(basepath, filespec)) do |file|
      if File.symlink?(file)
        ep = remove_basepath(File.realpath(file, basepath))
        log.debug "Not deleting #{ep} (linked by #{file})"
        lx << ep
      end
    end

    lx
  end

  exclude += link_exclude

  # delete all source files minus excluded ones
  files = Dir.glob(basepath + '/**/**') +
          Dir.glob(basepath + '/.*')
  files = files.map {|f| remove_basepath(f) }
  files -= ['.', '..']
  files = files.sort_by {|f| f.length }.reverse
  files.each do |file|
    begin
      fullfile = File.join(basepath, file)
      if exclude.any? {|ex| true if file == ex || file =~ /^#{ex}\// }
        log.debug "Skipping #{fullfile}"
        next
      end
      del = File.directory?(fullfile) ? Dir : File
      log.debug "Deleting #{fullfile}"
      del.delete(fullfile)
    rescue Errno::ENOTEMPTY, Errno::ENOENT, Errno::ENOTDIR
    end
  end
end