FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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