Class: Coffeetags::Utils

Inherits:
Object show all
Defined in:
lib/CoffeeTags.rb

Class Method Summary collapse

Class Method Details

.file_path(file, output, tag_relative) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/CoffeeTags.rb', line 147

def self.file_path file, output, tag_relative
  return file if output.nil?
  return file unless tag_relative

  output_path = Pathname.new(output).expand_path
  file_path = Pathname.new(file).expand_path

  file_path.relative_path_from(output_path.dirname).to_s
end

.option_parser(args) ⇒ Object



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/CoffeeTags.rb', line 26

def self.option_parser args
  args << '-h' if args.empty?
  options = {}
  optparse = OptionParser.new do |opts|
    opts.banner  = (<<-BAN
      ---------------------------------------------------------------------
      #{NAME} #{Coffeetags::VERSION}
      ---------------------------------------------------------------------
      by #{AUTHOR} ( #{URL} )
      Usage:
      coffeetags [OPTIONS] <list of files>

      CoffeeTags + TagBar + Vim ---> https://gist.github.com/1935512
      ---------------------------------------------------------------------
      BAN
                   ).gsub(/^\s*/,'')


    opts.on('--include-vars', "Include variables in generated tags") do |o|
      options[:include_vars] = true
    end

    opts.on('-f', '--file FILE', 'Write tags to FILE (use - for std out)') do |o|
      options[:output] = o unless o == '-'
    end

    opts.on('-a', '--append', 'Append tags to existing tags file') do |o|
      options[:append] = true
    end

    opts.on('-R', '--recursive', 'Process current directory recursively') do |o|
      options[:recur] = true
    end

    opts.on('--tag-relative', 'Should paths be relative to location of tag file?') do |o|
      options[:tag_relative] = true
    end

    opts.on('-v', '--version', 'Current version') do
      puts Coffeetags::VERSION
      exit
    end

    opts.on('-h','--help','HALP') do
      puts opts
      exit
    end

  end

  optparse.parse! args

  options[:files]  = args.to_a
  options[:files] += Dir['./**/*.coffee', './**/Cakefile'] if options[:recur]

  options
end

.run(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/CoffeeTags.rb', line 85

def self.run options
  output = options[:output]
  include_vars = options[:include_vars]
  files = options[:files]

  files = [files] if files.is_a? String
  files = files.reject { |f| f =~ /^-/}

  lines = setup_tag_lines output, files, options[:append]

  __out = if output.nil?
            STDOUT
          else
            File.open output, 'w'
          end

  __out  << Coffeetags::Formatter.header

  files.each do |file|
    sc = File.read file
    parser = Coffeetags::Parser.new sc, include_vars
    parser.execute!

    tag_file_path = file_path file, output, options[:tag_relative]
    formatter = Coffeetags::Formatter.new tag_file_path, parser.tree

    formatter.parse_tree

    lines.concat(formatter.lines)
  end

  lines.sort!
  __out << lines.map { |l| "#{l}\n"}.join('')

  __out.close if __out.respond_to? :close

  __out.join("\n") if __out.is_a? Array
end

.setup_tag_lines(output, files, append) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/CoffeeTags.rb', line 124

def self.setup_tag_lines output, files, append
  return [] unless append
  return [] if output.nil?
  return [] unless File.exists? output

  files = [] if files.nil?
  absolute_files = files.map {|f| Pathname.new(f).expand_path }

  output_dir = Pathname.new(output).dirname

  File.readlines(output).
    map {|l| l.strip}.
    reject {|l| l =~ /^!_/ }.
    reject {|l|
      raw_file_path = l.split("\t")[1]
      tag_file = Pathname.new(raw_file_path)

      tag_file = output_dir + tag_file if raw_file_path =~ /^\./

      absolute_files.include? tag_file.expand_path
    }
end