summaryrefslogtreecommitdiffstats
path: root/tinyusb/test/vendor/ceedling/lib/ceedling/flaginator.rb
blob: 31d62c46a5f04c62eb44f43edfb1ca42550983b6 (plain)
1
2
3
4
5
6
7
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
require 'rubygems'
require 'rake' # for ext()
require 'fileutils'
require 'ceedling/constants'


# :flags:
#   :release:
#     :compile:
#       :'test_.+'
#         - -pedantic   # add '-pedantic' to every test file
#       :*:          # add '-foo' to compilation of all files not main.c
#         - -foo
#       :main:       # add '-Wall' to compilation of main.c
#         - -Wall
#   :test:
#     :link:
#       :test_main:  # add '--bar --baz' to linking of test_main.exe
#         - --bar
#         - --baz

def partition(hash, &predicate)
  hash.partition(&predicate).map(&:to_h)
end

class Flaginator

  constructor :configurator

  def get_flag(hash, file_name)
    file_key = file_name.to_sym
   
    # 1. try literals
    literals, magic = partition(hash) { |k, v| k.to_s =~ /^\w+$/ }  
    return literals[file_key] if literals.include?(file_key)
    
    any, regex = partition(magic) { |k, v| (k == :'*') || (k == :'.*')  } # glob or regex wild card
    
    # 2. try regexes
    find_res = regex.find { |k, v| file_name =~ /^#{k.to_s}$/ }
    return find_res[1] if find_res
    
    # 3. try anything
    find_res = any.find { |k, v| file_name =~ /.*/ }
    return find_res[1] if find_res
      
    # 4. well, we've tried
    return []
  end
  
  def flag_down( operation, context, file )
    # create configurator accessor method
    accessor = ('flags_' + context.to_s).to_sym

    # create simple filename key from whatever filename provided
    file_name = File.basename( file ).ext('')
    file_key = File.basename( file ).ext('').to_sym

    # if no entry in configuration for flags for this context, bail out
    return [] if not @configurator.respond_to?( accessor )

    # get flags sub hash associated with this context
    flags = @configurator.send( accessor )

    # if operation not represented in flags hash, bail out
    return [] if not flags.include?( operation )

    # redefine flags to sub hash associated with the operation
    flags = flags[operation]

    return get_flag(flags, file_name)
  end

end