verilog_rename 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ file_list = FileList['spec/*_spec.rb']
4
+
5
+ RSpec::Core::RakeTask.new('spec') do |t|
6
+ t.pattern = file_list
7
+ t.rspec_opts = ["--colour", "--format progress"]
8
+ end
9
+
10
+ desc 'Default: run specs.'
11
+ task :default => 'spec'
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'verilog_rename'
5
+
6
+ #operate on current directory
7
+ this_dir = Dir.pwd
8
+
9
+ ## TODO slop command line option parsing
10
+ #Parameter 1 old_name
11
+ old_name = ARGV[0]
12
+
13
+ #Parameter 2 new_name
14
+ new_name = ARGV[1]
15
+
16
+
17
+ path_files = Verilog::PathFiles.new( this_dir )
18
+ path_files.read_all
19
+
20
+ rename = Verilog::Rename.new(old_name, new_name, path_files)
21
+ rename.rename_module
@@ -0,0 +1,51 @@
1
+
2
+ module Verilog
3
+
4
+ class PathFiles
5
+ attr_reader :files
6
+
7
+ def initialize( path="" )
8
+ @files = []
9
+ load_path( path )
10
+ end
11
+
12
+ def load_path( paths )
13
+ #path is single string or array of strings
14
+ paths.each do |path|
15
+ files = Dir.glob( ::File.join( path, '*.*') )
16
+ files.each do |file|
17
+ #Skip if Directory got passed *.* Filter
18
+ next if ::File.directory?( file )
19
+
20
+ file_name = ::File.basename(file)
21
+ @files << Verilog::File.new(file_name, {:path => path})
22
+
23
+ end
24
+ end
25
+ end
26
+
27
+ def read_all
28
+ @files.each do |file|
29
+ file.read_from_disk
30
+ end
31
+ end
32
+
33
+ def find_by_module( name )
34
+ result = @files.select do |file|
35
+ file.module_name == name
36
+ end
37
+ return result[0]
38
+ end
39
+
40
+ def includes_file( name )
41
+ @files.select { |file| file.includes.include?( name ) }
42
+ end
43
+
44
+ def instantiates_module( name )
45
+ @files.select { |file| file.instantiations.include?( name ) }
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
@@ -0,0 +1,8 @@
1
+ module Verilog
2
+
3
+ class Prefix
4
+ #Verilog class to prefix module names. Iterates over cached files and renames all with a given prefix.
5
+ end
6
+
7
+ end
8
+
@@ -0,0 +1,130 @@
1
+ require 'FileUtils'
2
+
3
+ module Verilog
4
+
5
+ class Rename
6
+ attr_reader :path_files
7
+ attr_reader :old_name, :new_name
8
+ attr_reader :old_file, :new_file
9
+
10
+
11
+ def initialize(old_name, new_name, path_files)
12
+ @old_name = old_name
13
+ @new_name = new_name
14
+ @path_files = path_files
15
+ end
16
+
17
+ def rename
18
+ #@old_file, @new_file = find_rename_verilog_file
19
+
20
+ #Analyse file and rename module
21
+ rename_module
22
+
23
+ #update_refferences
24
+ end
25
+
26
+ def update_refferences
27
+ files = Dir.glob('*.*')
28
+ #puts "File list searching for insatances of #{@old_name}"
29
+ files.each do |file|
30
+ next if File.directory?( file )
31
+ contents = Verilog::FileReader.get_file_as_string( file )
32
+
33
+ #Update Instatiations
34
+ updated_instance_b = contents.gsub!(
35
+ /(^\s*)#{@old_name}(\s\w+\s*\()/i, "\\1#{@new_name}\\2")
36
+ if updated_instance_b
37
+ puts "Updating instantiation of #{@old_name} to #{@new_name} in #{file.to_s}"
38
+ end
39
+
40
+ #Update inculde
41
+ ## want filename of file being moved
42
+ updated_include_b = contents.gsub!(/(^\s*`include [\'\"])#{@old_file}([\'\"])/i, "\\1#{@new_file}\\2")
43
+ if updated_include_b
44
+ puts "Updating include of #{@old_file} to #{@new_file} in #{file.to_s}"
45
+ end
46
+
47
+
48
+ #Update lines in *.do runscripts
49
+ updated_run_b = contents.gsub!(/(\s+)#{@old_file}(\s+)/, "\\1#{@new_file}\\2")
50
+ if updated_instance_b
51
+ puts "Updating call to #{@old_file} to #{@new_file} in #{file.to_s}"
52
+ end
53
+
54
+ #Update File
55
+ if updated_instance_b or updated_include_b or updated_run_b
56
+ write_file_to_string( file, contents )
57
+ end
58
+
59
+
60
+ end
61
+
62
+ end
63
+
64
+ def rename_module
65
+
66
+ #find the correct file first
67
+ puts @old_name
68
+
69
+ file_to_rename = @path_files.find_by_module( @old_name )
70
+
71
+ file_to_rename.contents.gsub!(/(^\s*module )#{@old_name}(\s+|;|\()/i, "\\1#{@new_name}\\2")
72
+
73
+ extension = ::File.extname( file_to_rename.filename )
74
+
75
+ #Need to change file name but keep extension
76
+ file_to_rename.filename = @new_name + extension
77
+
78
+ pp file_to_rename
79
+
80
+ #Save file, will use new name.
81
+
82
+ #Delete Old file
83
+
84
+ #Update other files refferencing it.
85
+
86
+
87
+ #file_contents = Verilog::FileReader.get_file_as_string( new_file )
88
+
89
+ #file_contents
90
+
91
+ # if updated_b
92
+ # Verilog::FileReader.write_file_to_string( new_file, file_contents )
93
+ # end
94
+ end
95
+
96
+ def find_rename_verilog_file
97
+ old_file, ext = find_module_file( @old_name )
98
+
99
+ if old_file
100
+ new_file = @new_name + ext
101
+ puts "Moving #{old_file} to #{new_file}"
102
+ FileUtils.move( old_file, new_file )
103
+ else
104
+ #Old_filename does not exists
105
+ # This could be second tim running script.
106
+ # Look for newfile incase already moved.
107
+ new_file, ext = find_module_file( @new_name )
108
+ end
109
+
110
+ return [old_file, new_file]
111
+ end
112
+
113
+ end
114
+
115
+
116
+ end
117
+
118
+ if $0 == __FILE__
119
+ #puts "Running Verilog Rename"
120
+ #operate on current directory (future parse testbench.f)
121
+ this_dir = Dir.pwd
122
+
123
+ #Parameter 1 old_name
124
+ old_name = ARGV[0]
125
+ #Parameter 2 new_name
126
+ new_name = ARGV[1]
127
+
128
+ a = Verilog::Rename::File.new(this_dir, old_name, new_name)
129
+ a.rename
130
+ end
@@ -0,0 +1,195 @@
1
+ require 'FileUtils'
2
+ require 'pp'
3
+
4
+ module Verilog
5
+
6
+ EXTENSIONS = [".v", ".bh.v", ".sv"]
7
+
8
+ class File
9
+ attr_reader :path
10
+ attr_reader :file_name
11
+ attr_reader :ext
12
+
13
+ def initialise( path, file_name="", ext="" )
14
+ #TODO if path is a file then create file_name and ext
15
+ @path = path
16
+ @file_name = file_name
17
+ #TODO check ext starts with .
18
+ @ext = ext
19
+ end
20
+
21
+ def absolute_path
22
+ File.join( File.expand_path( path ), file_name ext )
23
+ end
24
+
25
+ def read
26
+ if File.exist?( absolute_path )
27
+ data = ''
28
+ f = File.open(absolute_path, "r")
29
+ f.each_line do |line|
30
+ data += line
31
+ end
32
+ f.close
33
+ return data
34
+ else
35
+ #TODO raise correct exception here
36
+ puts "ERROR File Not Found #{absolute_path}"
37
+ end
38
+
39
+ end
40
+
41
+ def save( contents )
42
+ f = File.open(absolute_path, "w")
43
+ f.write(contents)
44
+ f.close
45
+ end
46
+
47
+
48
+ end
49
+
50
+ class PathFiles
51
+ attr_reader :files
52
+
53
+ def initalize( path="" )
54
+ @files = []
55
+ load_path( path )
56
+ end
57
+
58
+ def load_path( paths )
59
+ #path is single string or array of strings
60
+ paths.each do |path|
61
+ files = Dir.glob('*.*')
62
+ files.each do |file|
63
+ #Skip if Directory got passed *.* Filter
64
+ next if File.directory?( file )
65
+
66
+ file_extension = File.extname(file)
67
+ name = File.basename(file,file_extension)
68
+ @files << Verilog::File.new(path, name, file_extension)
69
+
70
+ end
71
+ end
72
+ end
73
+
74
+ def <<( item )
75
+ @files
76
+ end
77
+
78
+ def find_module( name )
79
+ result = @files.select { |file| file.file_name == name }
80
+
81
+ if result.size == 1
82
+ return result
83
+ else
84
+ #TODO Fall back to scanning all files for module declarations
85
+ return false
86
+ end
87
+ end
88
+
89
+
90
+ end
91
+
92
+
93
+
94
+ module Rename
95
+ class Rename
96
+ attr_reader :dir, :old_name, :new_name
97
+ attr_reader :old_file, :new_file
98
+
99
+
100
+ def initialize(this_dir, old_name, new_name)
101
+ @dir = this_dir
102
+ @old_name = old_name
103
+ @new_name = new_name
104
+ end
105
+
106
+ def rename
107
+ @old_file, @new_file = find_rename_verilog_file
108
+
109
+ #Analyse file and rename module
110
+ rename_module( @new_file )
111
+
112
+ update_refferences
113
+ end
114
+
115
+ def update_refferences
116
+ files = Dir.glob('*.*')
117
+ #puts "File list searching for insatances of #{@old_name}"
118
+ files.each do |file|
119
+ next if File.directory?( file )
120
+ contents = Verilog::FileReader.get_file_as_string( file )
121
+
122
+ #Update Instatiations
123
+ updated_instance_b = contents.gsub!(/(^\s*)#{@old_name}(\s\w+\s*\()/i, "\\1#{@new_name}\\2")
124
+ if updated_instance_b
125
+ puts "Updating instantiation of #{@old_name} to #{@new_name} in #{file.to_s}"
126
+ end
127
+
128
+ #Update inculde
129
+ ## want filename of file being moved
130
+ updated_include_b = contents.gsub!(/(^\s*`include [\'\"])#{@old_file}([\'\"])/i, "\\1#{@new_file}\\2")
131
+ if updated_include_b
132
+ puts "Updating include of #{@old_file} to #{@new_file} in #{file.to_s}"
133
+ end
134
+
135
+
136
+ #Update lines in *.do runscripts
137
+ updated_run_b = contents.gsub!(/(\s+)#{@old_file}(\s+)/, "\\1#{@new_file}\\2")
138
+ if updated_instance_b
139
+ puts "Updating call to #{@old_file} to #{@new_file} in #{file.to_s}"
140
+ end
141
+
142
+ #Update File
143
+ if updated_instance_b or updated_include_b or updated_run_b
144
+ write_file_to_string( file, contents )
145
+ end
146
+
147
+
148
+ end
149
+
150
+ end
151
+
152
+ def rename_module( new_file )
153
+ file_contents = Verilog::FileReader.get_file_as_string( new_file )
154
+
155
+ updated_b = file_contents.gsub!(/(^\s*module )#{@old_name}(\s+|;|\()/i, "\\1#{@new_name}\\2")
156
+
157
+ if updated_b
158
+ Verilog::FileReader.write_file_to_string( new_file, file_contents )
159
+ end
160
+ end
161
+
162
+ def find_rename_verilog_file
163
+ old_file, ext = find_module_file( @old_name )
164
+
165
+ if old_file
166
+ new_file = @new_name + ext
167
+ puts "Moving #{old_file} to #{new_file}"
168
+ FileUtils.move( old_file, new_file )
169
+ else
170
+ #Old_filename does not exists
171
+ # This could be second tim running script.
172
+ # Look for newfile incase already moved.
173
+ new_file, ext = find_module_file( @new_name )
174
+ end
175
+
176
+ return [old_file, new_file]
177
+ end
178
+
179
+ end
180
+ end
181
+ end
182
+
183
+ if $0 == __FILE__
184
+ #puts "Running Verilog Rename"
185
+ #operate on current directory (future parse testbench.f)
186
+ this_dir = Dir.pwd
187
+
188
+ #Parameter 1 old_name
189
+ old_name = ARGV[0]
190
+ #Parameter 2 new_name
191
+ new_name = ARGV[1]
192
+
193
+ a = Verilog::Rename::File.new(this_dir, old_name, new_name)
194
+ a.rename
195
+ end
@@ -0,0 +1,68 @@
1
+ require 'FileUtils'
2
+
3
+ module Verilog
4
+
5
+ class Rename
6
+ attr_reader :path_files
7
+ attr_reader :old_name, :new_name
8
+ attr_reader :old_file, :new_file
9
+
10
+
11
+ def initialize(old_name, new_name, path_files)
12
+ @old_name = old_name
13
+ @new_name = new_name
14
+ @path_files = path_files
15
+ end
16
+
17
+ def rename
18
+ #Analyse file and rename module
19
+ rename_module
20
+
21
+ #update_refferences
22
+ end
23
+
24
+ def rename_module
25
+
26
+ #find the correct file first
27
+ file_to_rename = @path_files.find_by_module( @old_name )
28
+
29
+ file_to_rename.contents.gsub!(/(^\s*module )#{@old_name}(\s+|;|\()/i, "\\1#{@new_name}\\2")
30
+
31
+ extension = ::File.extname( file_to_rename.filename )
32
+ absolute_original_file_name = file_to_rename.absolute_filename
33
+ original_file_name = file_to_rename.filename
34
+
35
+ #Need to change file name but keep extension
36
+ file_to_rename.filename = @new_name + extension
37
+
38
+ #TODO Possibly check that that module name is not already in use
39
+ #TODO check that the file name is not already used
40
+
41
+ #Save file, will use new name.
42
+ file_to_rename.save
43
+
44
+ ## Delete Old file
45
+ ::File.delete( absolute_original_file_name )
46
+
47
+ ## Update other files including it.
48
+ files_including_it = @path_files.includes_file( original_file_name )
49
+
50
+ files_including_it.each do |file|
51
+ file.contents.gsub!(/(^\s*`include [\'\"])#{ original_file_name }([\'\"])/, "\\1#{ file_to_rename.filename }\\2")
52
+ #This could trigger many writes
53
+ ## Verilog File needs a modified flag so files can be itereated and modified version can be saved.
54
+ file.save
55
+ end
56
+
57
+ ## Update files instatiating it.
58
+ files_instantiating_it = @path_files.instantiates_module( @old_name )
59
+
60
+ files_instantiating_it.each do |file|
61
+ file.contents.gsub!(/(^\s*)#{ @old_name }(\s+)/,"\\1#{ @new_name }\\2")
62
+ file.save
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+