Class: Verilog::Rename

Inherits:
Object
  • Object
show all
Defined in:
lib/verilog/rename.rb,
lib/verilog/rename-backup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_name, new_name, path_files) ⇒ Rename

Returns a new instance of Rename.



11
12
13
14
15
# File 'lib/verilog/rename.rb', line 11

def initialize(old_name, new_name, path_files) 
  @old_name   = old_name
  @new_name   = new_name
  @path_files = path_files
end

Instance Attribute Details

#new_fileObject (readonly)

Returns the value of attribute new_file.



8
9
10
# File 'lib/verilog/rename.rb', line 8

def new_file
  @new_file
end

#new_nameObject (readonly)

Returns the value of attribute new_name.



7
8
9
# File 'lib/verilog/rename.rb', line 7

def new_name
  @new_name
end

#old_fileObject (readonly)

Returns the value of attribute old_file.



8
9
10
# File 'lib/verilog/rename.rb', line 8

def old_file
  @old_file
end

#old_nameObject (readonly)

Returns the value of attribute old_name.



7
8
9
# File 'lib/verilog/rename.rb', line 7

def old_name
  @old_name
end

#path_filesObject (readonly)

Returns the value of attribute path_files.



6
7
8
# File 'lib/verilog/rename.rb', line 6

def path_files
  @path_files
end

Instance Method Details

#find_rename_verilog_fileObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/verilog/rename-backup.rb', line 96

def find_rename_verilog_file
  old_file, ext = find_module_file( @old_name )

  if old_file 
    new_file = @new_name + ext
    puts "Moving #{old_file} to #{new_file}"
    FileUtils.move( old_file, new_file ) 
  else
    #Old_filename does not exists
    # This could be second tim running script.
    # Look for newfile incase already moved.
    new_file, ext = find_module_file( @new_name )
  end

  return [old_file, new_file]
end

#renameObject



17
18
19
20
21
22
# File 'lib/verilog/rename.rb', line 17

def rename
  #Analyse file and rename module
  rename_module

  #update_refferences
end

#rename_moduleObject



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
# File 'lib/verilog/rename.rb', line 24

def rename_module

  #find the correct file first
  file_to_rename     = @path_files.find_by_module( @old_name )

  file_to_rename.contents.gsub!(/(^\s*module )#{@old_name}(\s+|;|\()/i, "\\1#{@new_name}\\2")
  
  extension                   = ::File.extname( file_to_rename.filename )
  absolute_original_file_name = file_to_rename.absolute_filename
  original_file_name          = file_to_rename.filename

  #Need to change file name but keep extension
  file_to_rename.filename = @new_name + extension

  #TODO Possibly check that that module name is not already in use
  #TODO check that the file name is not already used

  #Save file, will use new name.
  file_to_rename.save

  ## Delete Old file
  ::File.delete( absolute_original_file_name )

  ## Update other files including it.
  files_including_it = @path_files.includes_file( original_file_name )

  files_including_it.each do |file|
      file.contents.gsub!(/(^\s*`include [\'\"])#{ original_file_name }([\'\"])/, "\\1#{ file_to_rename.filename }\\2")
      #This could trigger many writes
      ## Verilog File needs a modified flag so files can be itereated and modified version can be saved.
      file.save
  end 

  ## Update files instatiating it.
  files_instantiating_it = @path_files.instantiates_module( @old_name )

  files_instantiating_it.each do |file|
      file.contents.gsub!(/(^\s*)#{ @old_name }(\s+)/,"\\1#{ @new_name }\\2")
      file.save
  end 

end

#update_refferencesObject



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
# File 'lib/verilog/rename-backup.rb', line 26

def update_refferences
  files = Dir.glob('*.*')
  #puts "File list searching for insatances of #{@old_name}"
  files.each do |file|
    next if File.directory?( file )
    contents = Verilog::FileReader.get_file_as_string( file )

    #Update Instatiations
    updated_instance_b = contents.gsub!(
      /(^\s*)#{@old_name}(\s\w+\s*\()/i, "\\1#{@new_name}\\2")
      if updated_instance_b  
        puts "Updating instantiation of #{@old_name} to #{@new_name} in #{file.to_s}"
      end

      #Update inculde
      ## want filename of file being moved
      updated_include_b = contents.gsub!(/(^\s*`include [\'\"])#{@old_file}([\'\"])/i, "\\1#{@new_file}\\2")
      if updated_include_b  
        puts "Updating include of #{@old_file} to #{@new_file} in #{file.to_s}"
      end


      #Update lines in *.do runscripts
      updated_run_b = contents.gsub!(/(\s+)#{@old_file}(\s+)/, "\\1#{@new_file}\\2")
      if updated_instance_b  
        puts "Updating call to #{@old_file} to #{@new_file} in #{file.to_s}"
      end

      #Update File
      if updated_instance_b or updated_include_b or updated_run_b
        write_file_to_string( file, contents )
      end


  end

end