Module: StickyFlag::Tags::SourceCode

Defined in:
lib/stickyflag/tags/source_code.rb

Class Method Summary collapse

Class Method Details

.clear(file_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/stickyflag/tags/source_code.rb', line 86

def clear(file_name)
  outpath = File.tmpnam
  File.open(outpath, 'w:UTF-8') do |outfile|
    File.open(file_name, 'r:UTF-8').each_line do |line|
      next if line =~ tag_line_regex
      outfile.puts line
    end
  end

  FileUtils.mv(outpath, file_name)
end

.get(file_name) ⇒ Object

Specify:

comment_line_regex: matches any line of comments at the start of the source code (when this doesn't match, we'll stop looking)

tag_line_regex: matches the specific line that contains the tags, the one group in this regex should be the comma-separated list of tags

tag_line_for(tags): convert the tag string (already a string!) into the output line we should print


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stickyflag/tags/source_code.rb', line 17

def get(file_name)
  File.open(file_name, 'r:UTF-8').each_line do |line|
    return [] unless line =~ comment_line_regex

    m = line.match(tag_line_regex)
    if m
      tag_string = m[1]
      return [] if tag_string.nil? || tag_string.empty?
      return tag_string.split(',').map { |t| t.empty? ? nil : t.strip }.compact
    end
  end

  []
end

.set(file_name, tag) ⇒ Object



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
# File 'lib/stickyflag/tags/source_code.rb', line 32

def set(file_name, tag)
  tags = get(file_name)
  return if tags.include? tag

  tags << tag      
  set_tags = false

  outpath = File.tmpnam
  File.open(outpath, 'w:UTF-8') do |outfile|
    File.open(file_name, 'r:UTF-8').each_line do |line|
      if line !~ comment_line_regex
        if set_tags == false
          # We haven't set the tags yet, but the current line does *not*
          # match the comment line regex.  Add the tags line as a new line
          # at the end of the comment block.
          outfile.puts tag_line_for(tags.join(', '))
          set_tags = true
        else
          outfile.puts line
        end
      else
        if line =~ tag_line_regex
          # Replace the old tag line with the new tag line
          outfile.puts tag_line_for(tags.join(', '))
        else
          outfile.puts line
        end
      end
    end
  end

  FileUtils.mv(outpath, file_name)
end

.unset(file_name, tag) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/stickyflag/tags/source_code.rb', line 66

def unset(file_name, tag)
  tags = get(file_name)
  return unless tags.include? tag

  tags.delete(tag)

  outpath = File.tmpnam
  File.open(outpath, 'w:UTF-8') do |outfile|
    File.open(file_name, 'r:UTF-8').each_line do |line|
      if line =~ tag_line_regex
        outfile.puts tag_line_for(tags.join(', '))
      else
        outfile.puts line
      end
    end
  end

  FileUtils.mv(outpath, file_name)
end