Last active
July 9, 2017 17:31
-
-
Save jenya239/6ebb726bf0a85de34640 to your computer and use it in GitHub Desktop.
change watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'diffy' | |
require 'awesome_print' | |
#require 'pry' | |
module J | |
class Change | |
@file | |
@diff | |
attr_reader :file, :diff | |
def initialize file, old_content, new_content | |
@file = file | |
@diff = Diffy::Diff.new old_content, new_content | |
end | |
def to_s | |
"#{ @file.name }\n#{ @diff.to_s(:text) }" | |
end | |
end | |
class Include | |
@whole | |
@token | |
attr_reader :whole, :token | |
def initialize whole, token | |
@whole = whole; @token =token | |
end | |
def to_s | |
"#{ @whole }: #{ @token }" | |
end | |
def filename | |
"app/#{ @token }.iced" #.gsub( /^app\//, '' ) | |
end | |
end | |
class File | |
@name | |
@ctime | |
@content | |
@jincludes | |
@included | |
@included_in | |
attr_reader :name, :ctime, :jincludes, :included_in | |
def update | |
@ctime = ::File.ctime @name | |
old_content = @content ? @content : '' | |
@content = IO.read @name if ::File.file? @name | |
find_includes | |
Change.new self, old_content, @content | |
end | |
def initialize path | |
#binding.pry | |
@name = path | |
@jincludes = [] | |
@included = [] | |
update | |
end | |
def to_s | |
"#{ @name }:#{ @ctime.to_i } ( #{ content[ 0 .. 20 ] } )" | |
end | |
def content | |
if ::File.file? @name | |
#@content.size > 20 ? @content[0..20] : @content | |
@content | |
else | |
'' | |
end | |
end | |
def content_size | |
content.size | |
end | |
def locate_included all_files | |
@included = [] | |
@jincludes.each do | ji | | |
@included << all_files[ ji.filename ] if all_files.key? ji.filename | |
end | |
end | |
def locate_included_in all_files | |
@included_in = [] | |
all_files.each do | path, jf | | |
@included_in << jf if jf.included_files.include? self | |
end | |
end | |
def included_files | |
@included | |
end | |
def included_content | |
res = content | |
@jincludes.each do |ji| | |
file = find_file_by_token ji | |
res = res.gsub( ji.whole, file.included_content ) if file | |
end | |
res | |
end | |
private | |
def find_includes | |
@jincludes = [] | |
arr = content.scan /\b(jinclude\b\s([\w\!\?\|\/\\\.\_\-\+\=\@\%\#]+))/im #ToDo lookahead for "\ " | |
arr.each do | el | | |
@jincludes << J::Include.new( el[ 0 ], el[ 1 ] ) | |
end | |
end | |
def find_file_by_token incl #todo учитывать отступы | |
@included.each do | jf | | |
return jf if jf.name == incl.filename | |
end | |
end | |
end | |
class Files | |
@files | |
@paths | |
attr_reader :files, :paths | |
def initialize | |
@paths = [] | |
@files = {} | |
end | |
def add path, file | |
@paths << path | |
@files[ path ] = file | |
end | |
def get path | |
@files[ path ] | |
end | |
def to_s | |
@files.reduce( '' ) do | res, ( path, file ) | | |
"#{ res }#{ path }: #{ file.content_size }\n" | |
end | |
end | |
def submitted? path | |
@paths.include? path | |
end | |
def update_included changed_file | |
old_included = changed_file.included_files | |
changed_file.locate_included @files | |
( old_included - changed_file.included_files ).each do | jf | | |
jf.included_in.delete changed_file | |
end | |
( changed_file.included_files - old_included ).each do | jf | | |
jf.included_in << changed_file | |
end | |
end | |
end | |
end | |
def watch | |
files = {} | |
changes = [] | |
jfiles = J::Files.new | |
Dir[ 'app/**/*.iced' ].each do |path| | |
files[ path ] = file = J::File.new path | |
jfiles.add path, file | |
end | |
jfiles.files.each do | path, jf | | |
jf.locate_included( jfiles.files ) | |
end | |
jfiles.files.each do | path, jf | | |
jf.locate_included_in( jfiles.files ) | |
print "#{ jf.name }: " | |
jf.included_in.each{ |f| print "#{ f.name }, " } | |
print "\n" | |
end | |
#print jfiles | |
#return | |
while changes.empty? do | |
sleep 0.01 | |
existing_files = [] | |
Dir[ 'app/**/*.iced' ].each do |path| | |
existing_files << path | |
#if files.key? path | |
if jfiles.submitted? path | |
file = jfiles.get path #files[ path ] | |
if File.ctime( path ) != file.ctime | |
changes << file.update | |
jfiles.update_included file | |
end | |
else | |
#files[ path ] = | |
jfiles.add path, ( file = J::File.new path ) | |
changes << ( J::Change.new file, '', file.content ) | |
jfiles.update_included file | |
end | |
end | |
#removed = files.keys - existing_files | |
removed = jfiles.paths - existing_files | |
removed.each do |rf| | |
changes << rf | |
end | |
end | |
changes.each do |ch| | |
#for | |
`iced -c --no-header -I inline #{ ch.file.name }` | |
print "#{ ch.file.name }\n" | |
ch.file.included_in.each{ |f| print "#{ f }\n" } | |
end | |
end | |
33.times do watch end | |
#watch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'diffy' | |
require 'awesome_print' | |
#require 'pry' | |
# вставка содержимых | |
# мы знаем какие в каких | |
# у каждого вхождения ставим флаг processed или не processed | |
# ходим по всем файлам пока все не processed | |
# если файл processed, то вставляем его во все содержащие и помечаем соотв. вхожд. как processed | |
# если все вхожденя в файле processed, то помечаем его как processed | |
module J | |
class Entry | |
@container | |
@processed | |
attr_reader :container, :included | |
def initialize container | |
@container = container; @processed = false; | |
end | |
def set_processed | |
@processed = true | |
end | |
def included_files | |
@container.included_files | |
end | |
def to_s | |
@container.name + '+' + @processed.to_s | |
end | |
end | |
class FileEntries | |
@file | |
@entries | |
@processed_content | |
attr_reader :file, :entries | |
def initialize file | |
@file = file | |
@entries = file.included_files.map{ |f| J::Entry.new( f ) } | |
@processed_content = file.content | |
end | |
def to_s | |
@file.name + ': ' + @entries.join( ', ' ) + "\n" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment