This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
forked from ThrowTheSwitch/Ceedling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceedling
executable file
·351 lines (303 loc) · 13.4 KB
/
ceedling
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env ruby
#these are always used
require 'rubygems'
require 'fileutils'
# Check for the main project file (either the one defined in the ENV or the default)
main_filepath = ENV['CEEDLING_MAIN_PROJECT_FILE']
project_found = (!main_filepath.nil? && File.exists?(main_filepath))
if (!project_found)
main_filepath = "project.yml"
project_found = File.exists?(main_filepath)
end
def is_windows?
return ((RbConfig::CONFIG['host_os'] =~ /mswin|mingw/) ? true : false) if defined?(RbConfig)
return ((Config::CONFIG['host_os'] =~ /mswin|mingw/) ? true : false)
end
unless (project_found)
#===================================== We Do Not Have A Project ================================================
puts "Welcome to Ceedling!"
require 'thor'
def here
File.dirname(__FILE__) + "/.."
end
class CeedlingTasks < Thor
include Thor::Actions
desc "new PROJECT_NAME", "create a new ceedling project"
method_option :docs, :type => :boolean, :default => false, :desc => "Add docs in project vendor directory"
method_option :local, :type => :boolean, :default => false, :desc => "Create a copy of Ceedling in the project vendor directory"
method_option :gitignore, :type => :boolean, :default => false, :desc => "Create a gitignore file for ignoring ceedling generated files"
method_option :no_configs, :type => :boolean, :default => false, :desc => "Don't install starter configuration files"
method_option :noconfigs, :type => :boolean, :default => false
#deprecated:
method_option :no_docs, :type => :boolean, :default => false
method_option :nodocs, :type => :boolean, :default => false
method_option :as_gem, :type => :boolean, :default => false
method_option :asgem, :type => :boolean, :default => false
method_option :with_ignore, :type => :boolean, :default => false
method_option :withignore, :type => :boolean, :default => false
def new(name, silent = false)
copy_assets_and_create_structure(name, silent, false, options)
end
desc "upgrade PROJECT_NAME", "upgrade ceedling for a project (not req'd if gem used)"
def upgrade(name, silent = false)
as_local = true
begin
require "yaml"
as_local = (YAML.load_file(File.join(name, "project.yml"))[:project][:which_ceedling] != 'gem')
rescue
raise "ERROR: Could not find valid project file '#{yaml_path}'"
end
found_docs = File.exists?( File.join(name, "docs", "CeedlingPacket.md") )
copy_assets_and_create_structure(name, silent, true, {:upgrade => true, :no_configs => true, :local => as_local, :docs => found_docs})
end
no_commands do
def copy_assets_and_create_structure(name, silent=false, force=false, options = {})
puts "WARNING: --no_docs deprecated. It is now the default. Specify -docs if you want docs installed." if (options[:no_docs] || options[:nodocs])
puts "WARNING: --as_gem deprecated. It is now the default. Specify -local if you want ceedling installed to this project." if (options[:as_gem] || options[:asgem])
puts "WARNING: --with_ignore deprecated. It is now called -gitignore" if (options[:with_ignore] || options[:with_ignore])
use_docs = options[:docs] || false
use_configs = !(options[:no_configs] || options[:noconfigs] || false)
use_gem = !(options[:local])
use_ignore = options[:gitignore] || false
is_upgrade = options[:upgrade] || false
ceedling_path = File.join(name, 'vendor', 'ceedling')
source_path = File.join(name, 'src')
test_path = File.join(name, 'test')
test_support_path = File.join(name, 'test/support')
# If it's not an upgrade, make sure we have the paths we expect
if (!is_upgrade)
[source_path, test_path, test_support_path].each do |d|
FileUtils.mkdir_p d
end
end
# Genarate gitkeep in test support path
FileUtils.touch(File.join(test_support_path, '.gitkeep'))
# If documentation requested, create a place to dump them and do so
doc_path = ""
if use_docs
doc_path = use_gem ? File.join(name, 'docs') : File.join(ceedling_path, 'docs')
FileUtils.mkdir_p doc_path
in_doc_path = lambda {|f| File.join(doc_path, f)}
# Add documentation from main projects to list
doc_files = {}
['docs','vendor/unity/docs','vendor/cmock/docs','vendor/cexception/docs'].each do |p|
Dir[ File.expand_path(File.join(here, p, '*.md')) ].each do |f|
doc_files[ File.basename(f) ] = f unless(doc_files.include? f)
end
end
# Add documentation from plugins to list
Dir[ File.join(here, 'plugins/**/README.md') ].each do |plugin_path|
k = "plugin_" + plugin_path.split(/\\|\//)[-2] + ".md"
doc_files[ k ] = File.expand_path(plugin_path)
end
# Copy all documentation
doc_files.each_pair do |k, v|
copy_file(v, in_doc_path.call(k), :force => force)
end
end
# If installed locally to project, copy ceedling, unity, cmock, & supports to vendor
unless use_gem
FileUtils.mkdir_p ceedling_path
#copy full folders from ceedling gem into project
%w{plugins lib bin}.map do |f|
{:src => f, :dst => File.join(ceedling_path, f)}
end.each do |f|
directory(f[:src], f[:dst], :force => force)
end
# mark ceedling as an executable
File.chmod(0755, File.join(ceedling_path, 'bin', 'ceedling')) unless is_windows?
#copy necessary subcomponents from ceedling gem into project
sub_components = [
{:src => 'vendor/c_exception/lib/', :dst => 'vendor/c_exception/lib'},
{:src => 'vendor/cmock/config/', :dst => 'vendor/cmock/config'},
{:src => 'vendor/cmock/lib/', :dst => 'vendor/cmock/lib'},
{:src => 'vendor/cmock/src/', :dst => 'vendor/cmock/src'},
{:src => 'vendor/deep_merge/lib/', :dst => 'vendor/deep_merge/lib'},
{:src => 'vendor/diy/lib', :dst => 'vendor/diy/lib'},
{:src => 'vendor/unity/auto/', :dst => 'vendor/unity/auto'},
{:src => 'vendor/unity/src/', :dst => 'vendor/unity/src'},
]
sub_components.each do |c|
directory(c[:src], File.join(ceedling_path, c[:dst]), :force => force)
end
end
# We're copying in a configuration file if we haven't said not to
if (use_configs)
dst_yaml = File.join(name, 'project.yml')
src_yaml = if use_gem
File.join(here, 'assets', 'project_as_gem.yml')
else
if is_windows?
copy_file(File.join('assets', 'ceedling.cmd'), File.join(name, 'ceedling.cmd'), :force => force)
else
copy_file(File.join('assets', 'ceedling'), File.join(name, 'ceedling'), :force => force)
File.chmod(0755, File.join(name, 'ceedling'))
end
File.join(here, 'assets', 'project_with_guts.yml')
end
# Perform the actual clone of the config file, while updating the version
File.open(dst_yaml,'w') do |dst|
require 'ceedling/version.rb'
dst << File.read(src_yaml).gsub(":ceedling_version: '?'",":ceedling_version: #{Ceedling::Version::CEEDLING}")
puts " create #{dst_yaml}"
end
end
# Copy the gitignore file if requested
if (use_ignore)
copy_file(File.join('assets', 'default_gitignore'), File.join(name, '.gitignore'), :force => force)
end
unless silent
puts "\n"
puts "Project '#{name}' #{force ? "upgraded" : "created"}!"
puts " - Tool documentation is located in #{doc_path}" if use_docs
puts " - Execute 'ceedling help' from #{name} to view available test & build tasks"
puts ''
end
end
end
desc "examples", "list available example projects"
def examples()
puts "Available sample projects:"
FileUtils.cd(File.join(here, "examples")) do
Dir["*"].each {|proj| puts " #{proj}"}
end
end
desc "example PROJ_NAME [DEST]", "new specified example project (in DEST, if specified)"
def example(proj_name, dest=nil)
if dest.nil? then dest = proj_name end
copy_assets_and_create_structure(dest, true, false, {:local=>true, :docs=>true})
dest_src = File.join(dest,'src')
dest_test = File.join(dest,'test')
dest_project = File.join(dest,'project.yml')
directory "examples/#{proj_name}/src", dest_src
directory "examples/#{proj_name}/test", dest_test
remove_file dest_project
copy_file "examples/#{proj_name}/project.yml", dest_project
puts "\n"
puts "Example project '#{proj_name}' created!"
puts " - Tool documentation is located in vendor/ceedling/docs"
puts " - Execute 'ceedling help' to view available test & build tasks"
puts ''
end
desc "version", "return the version of the tools installed"
def version()
require 'ceedling/version.rb'
puts " Ceedling:: #{Ceedling::Version::CEEDLING}"
puts " CMock:: #{Ceedling::Version::CMOCK}"
puts " Unity:: #{Ceedling::Version::UNITY}"
puts " CException:: #{Ceedling::Version::CEXCEPTION}"
end
end
if (ARGV[0] =~ /^\-T$/)
puts "\n(No Project Detected, Therefore Showing Options to Create Projects)"
CeedlingTasks.tasks.each_pair do |k,v|
puts v.usage.ljust(25,' ') + v.description
end
puts "\n"
else
CeedlingTasks.source_root here
CeedlingTasks.start
end
#===================================== We Have A Project Already ================================================
else
require 'yaml'
require 'rbconfig'
#determine platform
platform = begin
case(RbConfig::CONFIG['host_os'])
when /mswin|mingw|cygwin/i
:mswin
when /darwin/
:osx
else
:linux
end
rescue
:linux
end
#create our default meta-runner option set
options = {
:pretest => nil,
:args => [],
:add_path => [],
:path_connector => (platform == :mswin) ? ";" : ":",
:graceful_fail => false,
:which_ceedling => (Dir.exists?("vendor/ceedling") ? "vendor/ceedling" : 'gem'),
:default_tasks => [ 'test:all' ],
:list_tasks => false
}
#guess that we need a special script file first if it exists
if (platform == :mswin)
options[:pretest] = File.exists?("#{ platform.to_s }_setup.bat") ? "#{ platform.to_s }_setup.bat" : nil
else
options[:pretest] = File.exists?("#{ platform.to_s }_setup.sh") ? "source #{ platform.to_s }_setup.sh" : nil
end
#merge in project settings if they can be found here
yaml_options = YAML.load_file(main_filepath)
if (yaml_options[:paths])
options[:add_path] = yaml_options[:paths][:tools] || []
else
options[:add_path] = []
end
options[:graceful_fail] = yaml_options[:graceful_fail] if yaml_options[:graceful_fail]
options[:which_ceedling] = yaml_options[:project][:which_ceedling] if (yaml_options[:project] && yaml_options[:project][:which_ceedling])
options[:default_tasks] = yaml_options[:default_tasks] if yaml_options[:default_tasks]
#sort through command line options
ARGV.each do |v|
case(v)
when /^(?:new|examples?|templates?)$/
puts "\nOops. You called ceedling with argument '#{v}'.\n" +
" This is an operation that will create a new project... \n" +
" but it looks like you're already in a project. If you really \n" +
" want to do this, try moving to an empty folder.\n\n"
abort
when /^help$/
options[:list_tasks] = true
when /^-T$/
options[:list_tasks] = true
when /^--tasks$/
options[:list_tasks] = true
when /^project:(\w+)/
ENV['CEEDLING_USER_PROJECT_FILE'] = "#{$1}.yml"
else
options[:args].push(v)
end
end
#add to the path
if (options[:add_path] && !options[:add_path].empty?)
path = ENV["PATH"]
options[:add_path].each do |p|
f = File.expand_path(File.dirname(__FILE__),p)
path = (f + options[:path_connector] + path) unless path.include? f
end
ENV["PATH"] = path
end
# Load Ceedling (either through the rakefile OR directly)
if (File.exists?("rakefile.rb"))
load 'rakefile.rb'
else
if (options[:which_ceedling] == 'gem')
require 'ceedling'
else
load "#{options[:which_ceedling]}/lib/ceedling.rb"
end
Ceedling.load_project
end
Rake.application.standard_exception_handling do
if options[:list_tasks]
# Display helpful task list when requested. This required us to dig into Rake internals a bit
Rake.application.define_singleton_method(:name=) {|n| @name = n}
Rake.application.name = 'ceedling'
Rake.application.options.show_tasks = :tasks
Rake.application.options.show_task_pattern = /^(?!.*build).*$/
Rake.application.display_tasks_and_comments()
else
task :default => options[:default_tasks]
# Run our Tasks!
Rake.application.collect_command_line_tasks(options[:args])
Rake.application.top_level
end
end
true
#===================================================================================================================
end