Debian Codegraph @

lib/cmock_file_writer.rb

raw symbols SCIP
1 # ==========================================
2 # CMock Project - Automatic Mock Generation for C
3 # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 # [Released under MIT License. Please refer to license.txt for details]
5 # ==========================================
6
7 class CMockFileWriter
8 attr_reader :config
9
10 def initialize(config)
11 @config = config
12 end
13
14 def create_subdir(subdir)
15 require 'fileutils'
16 FileUtils.mkdir_p "#{@config.mock_path}/" unless Dir.exist?("#{@config.mock_path}/")
17 FileUtils.mkdir_p "#{@config.mock_path}/#{subdir + '/' if subdir}" if subdir && !Dir.exist?("#{@config.mock_path}/#{subdir + '/' if subdir}")
18 end
19
20 def create_file(filename, subdir)
21 raise "Where's the block of data to create?" unless block_given?
22
23 full_file_name_temp = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}.new"
24 full_file_name_done = "#{@config.mock_path}/#{subdir + '/' if subdir}#{filename}"
25 File.open(full_file_name_temp, 'w') do |file|
26 yield(file, filename)
27 end
28 update_file(full_file_name_done, full_file_name_temp)
29 end
30
31 def append_file(filename, subdir)
32 raise "Where's the block of data to create?" unless block_given?
33
34 full_file_name = "#{@config.skeleton_path}/#{subdir + '/' if subdir}#{filename}"
35 File.open(full_file_name, 'a') do |file|
36 yield(file, filename)
37 end
38 end
39
40 private ###################################
41
42 def update_file(dest, src)
43 require 'fileutils'
44 FileUtils.rm(dest, :force => true)
45 FileUtils.mv(src, dest)
46 end
47 end