require 'yaml' require 'fileutils' def copy_to_documentation(file_path,relative_path_document) FileUtils.mkpath("#{relative_path_document}") system("cp #{file_path} #{relative_path_document}") end def open_file_and_find_lines_to(file_path,lines_to_scan) text = File.open(file_path).read positions_lines = [] line_num = 0 i = 0 text.gsub!(/\r\n?/, "\n") text.each_line do |line| line_num += 1 line_match = lines_to_scan[i] next unless line_match if line.match(line_match) positions_lines[i] = {:line_num => line_num, :line_match => line_match} i += 1 end end return positions_lines end def add_comment_to_top(file_path_documentation,comments) open(file_path_documentation, 'r+') do |f| line = 0 while (line-=1) > 0 f.readline end pos = f.pos rest = f.read f.seek(pos) comments = comments.split("&") comments.each do |line| f.write(line+"\n") end f.write(rest) end end def add_documentation_to_line(file_path_documentation,line,new_lines_lines) new_lines = 0 open(file_path_documentation, 'r+') do |f| line_num = line[:line_num]-1 while (line_num-=1) > 0 # read up to the line you want to write after f.readline end pos = f.pos # save your position in the file rest = f.read # save the rest of the file f.seek(pos) # go back to the old position documentation = line[:documentation].split("//") documentation.each do |line| new_lines += 1 f.write(line+"\n") # write new data end f.write(rest) # write rest of file end return new_lines + new_lines_lines end def init_documentation(root) system("rm -R #{root}/documentation/") documents = YAML.load_file("#{root}/config/documentation_files.yml") documents.each do |document| begin document = YAML.load_file("#{root}/"+document["file"]+".yml") relative_path_document = document["relative_path"] files = document["files"] @absolute_path = "#{root}/"+relative_path_document.to_s files.each do |documentation_file| documentation_file = documentation_file["file"] file_path = @absolute_path + documentation_file["path"]+".rb" copy_to_documentation(file_path,"#{root}/"+"documentation"+relative_path_document) file_path_documentation = "#{root}/"+"documentation"+relative_path_document+documentation_file["path"]+".rb" #Agregar comentarios al inicio del archivo comments = documentation_file["comments"] #Agregar documentation YARN body = documentation_file["body"] lines_to_scan = body.keys positions_lines = open_file_and_find_lines_to(file_path,lines_to_scan) new_lines = 0 positions_lines.each_with_index do |line,index| line[:documentation] = body[line[:line_match]]["documentation"] line[:line_num] += new_lines new_lines = add_documentation_to_line(file_path_documentation,line,new_lines) end add_comment_to_top(file_path_documentation,comments) puts "Terminado archivo: "+file_path_documentation.to_s end rescue Exception => e puts e next end end end