summaryrefslogtreecommitdiffstats
path: root/tinyusb/test/vendor/ceedling/lib/ceedling/tool_executor_helper.rb
blob: de4cafe469c12ddb185c901a56df122b15e23d59 (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
75
76
77
78
79
80
81
82
83
84
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'ceedling/constants' # for Verbosity enumeration & $stderr redirect enumeration

##
# Helper functions for the tool executor
class ToolExecutorHelper

  constructor :streaminator, :system_utils, :system_wrapper

  ##
  # Returns the stderr redirection based on the config and logging.
  # ==== Attributes
  #
  # * _tool_config_:  A hash containing config information.
  # * _logging_:  A boolean representing if logging is enabled or not.
  #
  def stderr_redirection(tool_config, logging)
    # if there's no logging enabled, return :stderr_redirect unmodified
    return tool_config[:stderr_redirect] if (not logging)

    # if there is logging enabled but the redirect is a custom value (not enum), return the custom string
    return tool_config[:stderr_redirect] if (tool_config[:stderr_redirect].class == String)

    # if logging is enabled but there's no custom string, return the AUTO enumeration so $stderr goes into the log
    return StdErrRedirect::AUTO
  end


  ##
  # Returns the background execution prepend based on the config.
  # ==== Attributes
  #
  # * _tool_config_:  A hash containing config information.
  #
  def background_exec_cmdline_prepend(tool_config)
    return nil if (tool_config.nil? || tool_config[:background_exec].nil?)

    config_exec = tool_config[:background_exec]

    if ((config_exec == BackgroundExec::AUTO) and (@system_wrapper.windows?))
      return 'start'
    end

    if (config_exec == BackgroundExec::WIN)
      return 'start'
    end

    return nil
  end


  ##
  # Modifies an executables path based on platform.
  # ==== Attributes
  #
  # * _executable_:  The executable's path.
  #
  def osify_path_separators(executable)
    return executable.gsub(/\//, '\\') if (@system_wrapper.windows?)
    return executable
  end

  ##
  # Returns the stderr redirect append based on the config.
  # ==== Attributes
  #
  # * _tool_config_:  A hash containing config information.
  #
  def stderr_redirect_cmdline_append(tool_config)
    return nil if (tool_config.nil? || tool_config[:stderr_redirect].nil?)

    config_redirect = tool_config[:stderr_redirect]
    redirect        = StdErrRedirect::NONE

    if (config_redirect == StdErrRedirect::AUTO)
       if (@system_wrapper.windows?)
         redirect = StdErrRedirect::WIN
       elsif (@system_utils.tcsh_shell?)
         redirect = StdErrRedirect::TCSH
       else
         redirect = StdErrRedirect::UNIX
       end
    end

    case redirect
      # we may need more complicated processing after some learning with various environments
      when StdErrRedirect::NONE then nil
      when StdErrRedirect::WIN  then '2>&1'
      when StdErrRedirect::UNIX then '2>&1'
      when StdErrRedirect::TCSH then '|&'
      else redirect.to_s
    end
  end

  ##
  # Returns the background execution append based on the config.
  # ==== Attributes
  #
  # * _tool_config_:  A hash containing config information.
  #
  def background_exec_cmdline_append(tool_config)
    return nil if (tool_config.nil? || tool_config[:background_exec].nil?)

    config_exec = tool_config[:background_exec]

    # if :auto & windows, then we already prepended 'start' and should append nothing
    return nil if ((config_exec == BackgroundExec::AUTO) and (@system_wrapper.windows?))

    # if :auto & not windows, then we append standard '&'
    return '&' if ((config_exec == BackgroundExec::AUTO) and (not @system_wrapper.windows?))

    # if explicitly Unix, then append '&'
    return '&' if (config_exec == BackgroundExec::UNIX)

  # * _command_str_:  A hash containing config information.
    # all other cases, including :none, :win, & anything unrecognized, append nothing
    return nil
  end

  ##
  # Outputs success results if command succeeded and we have verbosity cranked up.
  # ==== Attributes
  #
  # * _command_str_:  The command ran.
  # * _shell_results_:  The outputs of the command including exit code and
  # output.
  # * _boom_:  A boolean representing if a non zero result is erroneous.
  #
  def print_happy_results(command_str, shell_result, boom=true)
    if ((shell_result[:exit_code] == 0) or ((shell_result[:exit_code] != 0) and not boom))
      output  = "> Shell executed command:\n"
      output += "'#{command_str}'\n"
      output += "> Produced output:\n"             if (not shell_result[:output].empty?)
      output += "#{shell_result[:output].strip}\n" if (not shell_result[:output].empty?)
      output += "> And exited with status: [#{shell_result[:exit_code]}].\n" if (shell_result[:exit_code] != 0)
      output += "\n"

      @streaminator.stdout_puts(output, Verbosity::OBNOXIOUS)
    end
  end

  ##
  # Outputs failures results if command failed and we have verbosity set to minimum error level.
  # ==== Attributes
  #
  # * _command_str_:  The command ran.
  # * _shell_results_:  The outputs of the command including exit code and
  # output.
  # * _boom_:  A boolean representing if a non zero result is erroneous.
  #
  def print_error_results(command_str, shell_result, boom=true)
    if ((shell_result[:exit_code] != 0) and boom)
      output  = "ERROR: Shell command failed.\n"
      output += "> Shell executed command:\n"
      output += "'#{command_str}'\n"
      output += "> Produced output:\n"             if (not shell_result[:output].empty?)
      output += "#{shell_result[:output].strip}\n" if (not shell_result[:output].empty?)
      output += "> And exited with status: [#{shell_result[:exit_code]}].\n" if (shell_result[:exit_code] != nil)
      output += "> And then likely crashed.\n"                               if (shell_result[:exit_code] == nil)
      output += "\n"

      @streaminator.stderr_puts(output, Verbosity::ERRORS)
    end
  end
end