Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-cloud-client/mbed-client-pal/Test/Unity/auto/generate_module.rb@0:276e7a263c35, 2018-07-02 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Jul 02 06:30:39 2018 +0000
- Revision:
- 0:276e7a263c35
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:276e7a263c35 | 1 | # ========================================== |
MACRUM | 0:276e7a263c35 | 2 | # Unity Project - A Test Framework for C |
MACRUM | 0:276e7a263c35 | 3 | # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams |
MACRUM | 0:276e7a263c35 | 4 | # [Released under MIT License. Please refer to license.txt for details] |
MACRUM | 0:276e7a263c35 | 5 | # ========================================== |
MACRUM | 0:276e7a263c35 | 6 | |
MACRUM | 0:276e7a263c35 | 7 | # This script creates all the files with start code necessary for a new module. |
MACRUM | 0:276e7a263c35 | 8 | # A simple module only requires a source file, header file, and test file. |
MACRUM | 0:276e7a263c35 | 9 | # Triad modules require a source, header, and test file for each triad type (like model, conductor, and hardware). |
MACRUM | 0:276e7a263c35 | 10 | |
MACRUM | 0:276e7a263c35 | 11 | require 'rubygems' |
MACRUM | 0:276e7a263c35 | 12 | require 'fileutils' |
MACRUM | 0:276e7a263c35 | 13 | |
MACRUM | 0:276e7a263c35 | 14 | HERE = File.expand_path(File.dirname(__FILE__)) + '/' |
MACRUM | 0:276e7a263c35 | 15 | |
MACRUM | 0:276e7a263c35 | 16 | #help text when requested |
MACRUM | 0:276e7a263c35 | 17 | HELP_TEXT = [ "\nGENERATE MODULE\n-------- ------", |
MACRUM | 0:276e7a263c35 | 18 | "\nUsage: ruby generate_module [options] module_name", |
MACRUM | 0:276e7a263c35 | 19 | " -i\"include\" sets the path to output headers to 'include' (DEFAULT ../src)", |
MACRUM | 0:276e7a263c35 | 20 | " -s\"../src\" sets the path to output source to '../src' (DEFAULT ../src)", |
MACRUM | 0:276e7a263c35 | 21 | " -t\"C:/test\" sets the path to output source to 'C:/test' (DEFAULT ../test)", |
MACRUM | 0:276e7a263c35 | 22 | " -p\"MCH\" sets the output pattern to MCH.", |
MACRUM | 0:276e7a263c35 | 23 | " dh - driver hardware.", |
MACRUM | 0:276e7a263c35 | 24 | " dih - driver interrupt hardware.", |
MACRUM | 0:276e7a263c35 | 25 | " mch - model conductor hardware.", |
MACRUM | 0:276e7a263c35 | 26 | " mvp - model view presenter.", |
MACRUM | 0:276e7a263c35 | 27 | " src - just a single source module. (DEFAULT)", |
MACRUM | 0:276e7a263c35 | 28 | " -d destroy module instead of creating it.", |
MACRUM | 0:276e7a263c35 | 29 | " -u update subversion too (requires subversion command line)", |
MACRUM | 0:276e7a263c35 | 30 | " -y\"my.yml\" selects a different yaml config file for module generation", |
MACRUM | 0:276e7a263c35 | 31 | "" ].join("\n") |
MACRUM | 0:276e7a263c35 | 32 | |
MACRUM | 0:276e7a263c35 | 33 | #Built in patterns |
MACRUM | 0:276e7a263c35 | 34 | PATTERNS = { 'src' => {'' => { :inc => [] } }, |
MACRUM | 0:276e7a263c35 | 35 | 'dh' => {'Driver' => { :inc => ['%1$sHardware.h'] }, |
MACRUM | 0:276e7a263c35 | 36 | 'Hardware' => { :inc => [] } |
MACRUM | 0:276e7a263c35 | 37 | }, |
MACRUM | 0:276e7a263c35 | 38 | 'dih' => {'Driver' => { :inc => ['%1$sHardware.h', '%1$sInterrupt.h'] }, |
MACRUM | 0:276e7a263c35 | 39 | 'Interrupt'=> { :inc => ['%1$sHardware.h'] }, |
MACRUM | 0:276e7a263c35 | 40 | 'Hardware' => { :inc => [] } |
MACRUM | 0:276e7a263c35 | 41 | }, |
MACRUM | 0:276e7a263c35 | 42 | 'mch' => {'Model' => { :inc => [] }, |
MACRUM | 0:276e7a263c35 | 43 | 'Conductor'=> { :inc => ['%1$sModel.h', '%1$sHardware.h'] }, |
MACRUM | 0:276e7a263c35 | 44 | 'Hardware' => { :inc => [] } |
MACRUM | 0:276e7a263c35 | 45 | }, |
MACRUM | 0:276e7a263c35 | 46 | 'mvp' => {'Model' => { :inc => [] }, |
MACRUM | 0:276e7a263c35 | 47 | 'Presenter'=> { :inc => ['%1$sModel.h', '%1$sView.h'] }, |
MACRUM | 0:276e7a263c35 | 48 | 'View' => { :inc => [] } |
MACRUM | 0:276e7a263c35 | 49 | } |
MACRUM | 0:276e7a263c35 | 50 | } |
MACRUM | 0:276e7a263c35 | 51 | |
MACRUM | 0:276e7a263c35 | 52 | #TEMPLATE_TST |
MACRUM | 0:276e7a263c35 | 53 | TEMPLATE_TST = %q[#include "unity.h" |
MACRUM | 0:276e7a263c35 | 54 | %2$s#include "%1$s.h" |
MACRUM | 0:276e7a263c35 | 55 | |
MACRUM | 0:276e7a263c35 | 56 | void setUp(void) |
MACRUM | 0:276e7a263c35 | 57 | { |
MACRUM | 0:276e7a263c35 | 58 | } |
MACRUM | 0:276e7a263c35 | 59 | |
MACRUM | 0:276e7a263c35 | 60 | void tearDown(void) |
MACRUM | 0:276e7a263c35 | 61 | { |
MACRUM | 0:276e7a263c35 | 62 | } |
MACRUM | 0:276e7a263c35 | 63 | |
MACRUM | 0:276e7a263c35 | 64 | void test_%1$s_NeedToImplement(void) |
MACRUM | 0:276e7a263c35 | 65 | { |
MACRUM | 0:276e7a263c35 | 66 | TEST_IGNORE_MESSAGE("Need to Implement %1$s"); |
MACRUM | 0:276e7a263c35 | 67 | } |
MACRUM | 0:276e7a263c35 | 68 | ] |
MACRUM | 0:276e7a263c35 | 69 | |
MACRUM | 0:276e7a263c35 | 70 | #TEMPLATE_SRC |
MACRUM | 0:276e7a263c35 | 71 | TEMPLATE_SRC = %q[%2$s#include "%1$s.h" |
MACRUM | 0:276e7a263c35 | 72 | ] |
MACRUM | 0:276e7a263c35 | 73 | |
MACRUM | 0:276e7a263c35 | 74 | #TEMPLATE_INC |
MACRUM | 0:276e7a263c35 | 75 | TEMPLATE_INC = %q[#ifndef _%3$s_H |
MACRUM | 0:276e7a263c35 | 76 | #define _%3$s_H%2$s |
MACRUM | 0:276e7a263c35 | 77 | |
MACRUM | 0:276e7a263c35 | 78 | #endif // _%3$s_H |
MACRUM | 0:276e7a263c35 | 79 | ] |
MACRUM | 0:276e7a263c35 | 80 | |
MACRUM | 0:276e7a263c35 | 81 | # Parse the command line parameters. |
MACRUM | 0:276e7a263c35 | 82 | ARGV.each do |arg| |
MACRUM | 0:276e7a263c35 | 83 | case(arg) |
MACRUM | 0:276e7a263c35 | 84 | when /^-d/ then @destroy = true |
MACRUM | 0:276e7a263c35 | 85 | when /^-u/ then @update_svn = true |
MACRUM | 0:276e7a263c35 | 86 | when /^-p(\w+)/ then @pattern = $1 |
MACRUM | 0:276e7a263c35 | 87 | when /^-s(.+)/ then @path_src = $1 |
MACRUM | 0:276e7a263c35 | 88 | when /^-i(.+)/ then @path_inc = $1 |
MACRUM | 0:276e7a263c35 | 89 | when /^-t(.+)/ then @path_tst = $1 |
MACRUM | 0:276e7a263c35 | 90 | when /^-y(.+)/ then @yaml_config = $1 |
MACRUM | 0:276e7a263c35 | 91 | when /^(\w+)/ |
MACRUM | 0:276e7a263c35 | 92 | raise "ERROR: You can't have more than one Module name specified!" unless @module_name.nil? |
MACRUM | 0:276e7a263c35 | 93 | @module_name = arg |
MACRUM | 0:276e7a263c35 | 94 | when /^-(h|-help)/ |
MACRUM | 0:276e7a263c35 | 95 | puts HELP_TEXT |
MACRUM | 0:276e7a263c35 | 96 | exit |
MACRUM | 0:276e7a263c35 | 97 | else |
MACRUM | 0:276e7a263c35 | 98 | raise "ERROR: Unknown option specified '#{arg}'" |
MACRUM | 0:276e7a263c35 | 99 | end |
MACRUM | 0:276e7a263c35 | 100 | end |
MACRUM | 0:276e7a263c35 | 101 | raise "ERROR: You must have a Module name specified! (use option -h for help)" if @module_name.nil? |
MACRUM | 0:276e7a263c35 | 102 | |
MACRUM | 0:276e7a263c35 | 103 | #load yaml file if one was requested |
MACRUM | 0:276e7a263c35 | 104 | if @yaml_config |
MACRUM | 0:276e7a263c35 | 105 | require 'yaml' |
MACRUM | 0:276e7a263c35 | 106 | cfg = YAML.load_file(HERE + @yaml_config)[:generate_module] |
MACRUM | 0:276e7a263c35 | 107 | @path_src = cfg[:defaults][:path_src] if @path_src.nil? |
MACRUM | 0:276e7a263c35 | 108 | @path_inc = cfg[:defaults][:path_inc] if @path_inc.nil? |
MACRUM | 0:276e7a263c35 | 109 | @path_tst = cfg[:defaults][:path_tst] if @path_tst.nil? |
MACRUM | 0:276e7a263c35 | 110 | @update_svn = cfg[:defaults][:update_svn] if @update_svn.nil? |
MACRUM | 0:276e7a263c35 | 111 | @extra_inc = cfg[:includes] |
MACRUM | 0:276e7a263c35 | 112 | @boilerplates = cfg[:boilerplates] |
MACRUM | 0:276e7a263c35 | 113 | else |
MACRUM | 0:276e7a263c35 | 114 | @boilerplates = {} |
MACRUM | 0:276e7a263c35 | 115 | end |
MACRUM | 0:276e7a263c35 | 116 | |
MACRUM | 0:276e7a263c35 | 117 | # Create default file paths if none were provided |
MACRUM | 0:276e7a263c35 | 118 | @path_src = HERE + "../src/" if @path_src.nil? |
MACRUM | 0:276e7a263c35 | 119 | @path_inc = @path_src if @path_inc.nil? |
MACRUM | 0:276e7a263c35 | 120 | @path_tst = HERE + "../test/" if @path_tst.nil? |
MACRUM | 0:276e7a263c35 | 121 | @path_src += '/' unless (@path_src[-1] == 47) |
MACRUM | 0:276e7a263c35 | 122 | @path_inc += '/' unless (@path_inc[-1] == 47) |
MACRUM | 0:276e7a263c35 | 123 | @path_tst += '/' unless (@path_tst[-1] == 47) |
MACRUM | 0:276e7a263c35 | 124 | @pattern = 'src' if @pattern.nil? |
MACRUM | 0:276e7a263c35 | 125 | @includes = { :src => [], :inc => [], :tst => [] } |
MACRUM | 0:276e7a263c35 | 126 | @includes.merge!(@extra_inc) unless @extra_inc.nil? |
MACRUM | 0:276e7a263c35 | 127 | |
MACRUM | 0:276e7a263c35 | 128 | #create triad definition |
MACRUM | 0:276e7a263c35 | 129 | TRIAD = [ { :ext => '.c', :path => @path_src, :template => TEMPLATE_SRC, :inc => :src, :boilerplate => @boilerplates[:src] }, |
MACRUM | 0:276e7a263c35 | 130 | { :ext => '.h', :path => @path_inc, :template => TEMPLATE_INC, :inc => :inc, :boilerplate => @boilerplates[:inc] }, |
MACRUM | 0:276e7a263c35 | 131 | { :ext => '.c', :path => @path_tst+'Test', :template => TEMPLATE_TST, :inc => :tst, :boilerplate => @boilerplates[:tst] }, |
MACRUM | 0:276e7a263c35 | 132 | ] |
MACRUM | 0:276e7a263c35 | 133 | |
MACRUM | 0:276e7a263c35 | 134 | #prepare the pattern for use |
MACRUM | 0:276e7a263c35 | 135 | @patterns = PATTERNS[@pattern.downcase] |
MACRUM | 0:276e7a263c35 | 136 | raise "ERROR: The design pattern specified isn't one that I recognize!" if @patterns.nil? |
MACRUM | 0:276e7a263c35 | 137 | |
MACRUM | 0:276e7a263c35 | 138 | # Assemble the path/names of the files we need to work with. |
MACRUM | 0:276e7a263c35 | 139 | files = [] |
MACRUM | 0:276e7a263c35 | 140 | TRIAD.each do |triad| |
MACRUM | 0:276e7a263c35 | 141 | @patterns.each_pair do |pattern_file, pattern_traits| |
MACRUM | 0:276e7a263c35 | 142 | files << { |
MACRUM | 0:276e7a263c35 | 143 | :path => "#{triad[:path]}#{@module_name}#{pattern_file}#{triad[:ext]}", |
MACRUM | 0:276e7a263c35 | 144 | :name => "#{@module_name}#{pattern_file}", |
MACRUM | 0:276e7a263c35 | 145 | :template => triad[:template], |
MACRUM | 0:276e7a263c35 | 146 | :boilerplate => triad[:boilerplate], |
MACRUM | 0:276e7a263c35 | 147 | :includes => case(triad[:inc]) |
MACRUM | 0:276e7a263c35 | 148 | when :src then @includes[:src] | pattern_traits[:inc].map{|f| f % [@module_name]} |
MACRUM | 0:276e7a263c35 | 149 | when :inc then @includes[:inc] |
MACRUM | 0:276e7a263c35 | 150 | when :tst then @includes[:tst] | pattern_traits[:inc].map{|f| "Mock#{f}"% [@module_name]} |
MACRUM | 0:276e7a263c35 | 151 | end |
MACRUM | 0:276e7a263c35 | 152 | } |
MACRUM | 0:276e7a263c35 | 153 | end |
MACRUM | 0:276e7a263c35 | 154 | end |
MACRUM | 0:276e7a263c35 | 155 | |
MACRUM | 0:276e7a263c35 | 156 | # destroy files if that was what was requested |
MACRUM | 0:276e7a263c35 | 157 | if @destroy |
MACRUM | 0:276e7a263c35 | 158 | files.each do |filespec| |
MACRUM | 0:276e7a263c35 | 159 | file = filespec[:path] |
MACRUM | 0:276e7a263c35 | 160 | if File.exist?(file) |
MACRUM | 0:276e7a263c35 | 161 | if @update_svn |
MACRUM | 0:276e7a263c35 | 162 | `svn delete \"#{file}\" --force` |
MACRUM | 0:276e7a263c35 | 163 | puts "File #{file} deleted and removed from source control" |
MACRUM | 0:276e7a263c35 | 164 | else |
MACRUM | 0:276e7a263c35 | 165 | FileUtils.remove(file) |
MACRUM | 0:276e7a263c35 | 166 | puts "File #{file} deleted" |
MACRUM | 0:276e7a263c35 | 167 | end |
MACRUM | 0:276e7a263c35 | 168 | else |
MACRUM | 0:276e7a263c35 | 169 | puts "File #{file} does not exist so cannot be removed." |
MACRUM | 0:276e7a263c35 | 170 | end |
MACRUM | 0:276e7a263c35 | 171 | end |
MACRUM | 0:276e7a263c35 | 172 | puts "Destroy Complete" |
MACRUM | 0:276e7a263c35 | 173 | exit |
MACRUM | 0:276e7a263c35 | 174 | end |
MACRUM | 0:276e7a263c35 | 175 | |
MACRUM | 0:276e7a263c35 | 176 | #Abort if any module already exists |
MACRUM | 0:276e7a263c35 | 177 | files.each do |file| |
MACRUM | 0:276e7a263c35 | 178 | raise "ERROR: File #{file[:name]} already exists. Exiting." if File.exist?(file[:path]) |
MACRUM | 0:276e7a263c35 | 179 | end |
MACRUM | 0:276e7a263c35 | 180 | |
MACRUM | 0:276e7a263c35 | 181 | # Create Source Modules |
MACRUM | 0:276e7a263c35 | 182 | files.each_with_index do |file, i| |
MACRUM | 0:276e7a263c35 | 183 | File.open(file[:path], 'w') do |f| |
MACRUM | 0:276e7a263c35 | 184 | f.write(file[:boilerplate] % [file[:name]]) unless file[:boilerplate].nil? |
MACRUM | 0:276e7a263c35 | 185 | f.write(file[:template] % [ file[:name], |
MACRUM | 0:276e7a263c35 | 186 | file[:includes].map{|f| "#include \"#{f}\"\n"}.join, |
MACRUM | 0:276e7a263c35 | 187 | file[:name].upcase ] |
MACRUM | 0:276e7a263c35 | 188 | ) |
MACRUM | 0:276e7a263c35 | 189 | end |
MACRUM | 0:276e7a263c35 | 190 | if (@update_svn) |
MACRUM | 0:276e7a263c35 | 191 | `svn add \"#{file[:path]}\"` |
MACRUM | 0:276e7a263c35 | 192 | if $?.exitstatus == 0 |
MACRUM | 0:276e7a263c35 | 193 | puts "File #{file[:path]} created and added to source control" |
MACRUM | 0:276e7a263c35 | 194 | else |
MACRUM | 0:276e7a263c35 | 195 | puts "File #{file[:path]} created but FAILED adding to source control!" |
MACRUM | 0:276e7a263c35 | 196 | end |
MACRUM | 0:276e7a263c35 | 197 | else |
MACRUM | 0:276e7a263c35 | 198 | puts "File #{file[:path]} created" |
MACRUM | 0:276e7a263c35 | 199 | end |
MACRUM | 0:276e7a263c35 | 200 | end |
MACRUM | 0:276e7a263c35 | 201 | |
MACRUM | 0:276e7a263c35 | 202 | puts 'Generate Complete' |