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