Class: FeatureMap::Private::AssignmentApplicator

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_map/private/assignment_applicator.rb

Class Method Summary collapse

Class Method Details

.apply_assignment(filepath, feature) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/feature_map/private/assignment_applicator.rb', line 17

def self.apply_assignment(filepath, feature)
  return apply_to_directory(filepath, feature) if File.directory?(filepath)

  # NOTE:  For simplicity we're reading the entire file into system memory
  #        and then writing it back out.  This breaks in theory for exceptionally
  #        large source files on very resource-constrained machines.  In practice it's
  #        probably fine.
  file = File.readlines(filepath)
  case File.extname(filepath)
  when '.cls'
    apply_to_apex(file, filepath, feature)
  when '.html'
    apply_to_html(file, filepath, feature)
  when '.js', '.jsx', '.ts', '.tsx'
    apply_to_javascript(file, filepath, feature)
  when '.rb'
    apply_to_ruby(file, filepath, feature)
  when '.xml'
    apply_to_xml(file, filepath, feature)
  else
    puts "Cannot auto assign #{filepath} to #{feature}"
  end
end

.apply_assignments!(assignments) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/feature_map/private/assignment_applicator.rb', line 7

def self.apply_assignments!(assignments)
  file_to_feature_map = map_files_to_feature
  assignments.each do |(filepath, feature)|
    next puts("Missing data: #{filepath}, #{feature}") unless filepath && feature
    next puts("Already assigned: #{filepath}, #{feature}") if file_to_feature_map[filepath]

    apply_assignment(filepath, feature)
  end
end

.apply_to_apex(file, filepath, feature) ⇒ Object



41
42
43
44
45
46
# File 'lib/feature_map/private/assignment_applicator.rb', line 41

def self.apply_to_apex(file, filepath, feature)
  File.open(filepath, 'w') do |f|
    f.write("// @feature #{feature}\n\n")
    file.each { |line| f.write(line) }
  end
end

.apply_to_directory(filepath, feature) ⇒ Object



48
49
50
51
52
# File 'lib/feature_map/private/assignment_applicator.rb', line 48

def self.apply_to_directory(filepath, feature)
  feature_path = File.join(filepath, '.feature')

  File.write(feature_path, "#{feature}\n")
end

.apply_to_html(file, filepath, feature) ⇒ Object



54
55
56
57
58
59
# File 'lib/feature_map/private/assignment_applicator.rb', line 54

def self.apply_to_html(file, filepath, feature)
  File.open(filepath, 'w') do |f|
    f.write("<!-- @feature #{feature} -->\n\n")
    file.each { |line| f.write(line) }
  end
end

.apply_to_javascript(file, filepath, feature) ⇒ Object



61
62
63
64
65
66
# File 'lib/feature_map/private/assignment_applicator.rb', line 61

def self.apply_to_javascript(file, filepath, feature)
  File.open(filepath, 'w') do |f|
    f.write("// @feature #{feature}\n\n")
    file.each { |line| f.write(line) }
  end
end

.apply_to_ruby(file, filepath, feature) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/feature_map/private/assignment_applicator.rb', line 68

def self.apply_to_ruby(file, filepath, feature)
  File.open(filepath, 'w') do |f|
    # NOTE:  No spacing newline; doing so would separate
    #        the feature declaration into the only "first" comment
    #        section, which breaks existing magic comments.
    #        https://docs.ruby-lang.org/en/3.1/syntax/comments_rdoc.html#label-Magic+Comments

    f.write("# @feature #{feature}\n")
    file.each { |line| f.write(line) }
  end
end

.apply_to_xml(file, filepath, feature) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/feature_map/private/assignment_applicator.rb', line 80

def self.apply_to_xml(file, filepath, feature)
  # NOTE:  Installation of top-level comments in some XML files (notably, in Salesforce)
  #        breaks parsing.  Instead, we'll insert them right after the opening XML declaration.
  xml_declaration = file.index { |line| line =~ /<\?xml/i }
  insert_index = xml_declaration.nil? ? 0 : xml_declaration + 1
  file.insert(insert_index, "<!-- @feature #{feature} -->\n\n")

  File.open(filepath, 'w') do |f|
    file.each { |line| f.write(line) }
  end
end

.map_files_to_featureObject



92
93
94
95
96
97
98
# File 'lib/feature_map/private/assignment_applicator.rb', line 92

def self.map_files_to_feature
  Private.feature_file_assignments.reduce({}) do |content, (feature_name, files)|
    mapped_files = files.to_h { |f| [f, feature_name] }

    content.merge(mapped_files)
  end
end