Skip to content

Instantly share code, notes, and snippets.

@jenya239
Created October 7, 2019 11:53
Show Gist options
  • Save jenya239/b0939212b4ac2d91e2744d093ff51a79 to your computer and use it in GitHub Desktop.
Save jenya239/b0939212b4ac2d91e2744d093ff51a79 to your computer and use it in GitHub Desktop.
require 'awesome_print'
require 'pathname'
require 'net/ssh'
require 'net/scp'
class SFile
attr_reader :sym, :base, :root, :path
def initialize( sync, parent, path_str )
@root =parent ? parent .root : self
@sync =sync
@parent =parent
@path =Pathname .new( path_str )
@is_dir =@path .directory?
@ext =@path .extname
@base =@path .basename( @ext )
@sym =@base .to_s .to_sym
if @is_dir
@children ={}
@path .children .each do |child_path|
file =SFile .new( @sync, self, child_path )
@children[ file .sym ] =file
end
end
@exclude =false
end
def to_s( indent ='' )
res ="#{ indent }#{ @sym }\n"
if @is_dir
@children .each do |base, child|
next if child .exclude?
res += child .to_s( indent + "\t" )
end
end
res
end
def []( base )
@children[ base .to_sym ]
end
def exclude?; @exclude end
def exclude!; @exclude =true end
def exclude( child_base )
child_base =child_base .to_sym
@children[ child_base ] .exclude! if @children .key?( child_base ) end
def method_missing(name, *args, &block)
sym =name .to_sym
@children[ sym ] if @children .key?( sym )
end
def rel_path
# ap @root .path
# ap @path
return '' if self ==root
res =@path .relative_path_from( @root .path )
# ap res .directory?
# ap res .to_path
res .to_path
end
def target_path;
tp =@sync .target_path
res =File .join( tp .to_path, root .base .to_path, rel_path)
end
def upload!
print rel_path + "\n"
if @is_dir
@sync .ssh .exec! "mkdir -p '#{ target_path }'"
@children .each do |base, child|
next if child .exclude?
child .upload!
end
else
@sync .ssh .scp .upload!( @path .to_s, target_path )
end
end
end
class Sync
attr_reader :ssh, :target_path
def initialize
@target_path =Pathname .new '/home/user/work/ftg/space'
@roots_paths ={ backend: '../backend', frontend: '../frontend' }
#ap @roots_paths
@root =SFile .new( self, nil, @roots_paths[ :backend ] )
@root .data .exclude!
@root .config .exclude!
@root .exclude '.convert'
# print @root
# print "\n"
# print "\n"
#paths =Dir[ File .join( @roots_paths[ :backend ], '**/*' ) ]
#@files =paths .map{ |path_str| SFile .new( self, path_str ) }
Net::SSH .start( 'rfx' ) do |ssh|
@ssh =ssh
#ap ssh .exec!( 'pwd' )
@root .upload!
ssh .exec! 'pm2 restart gen'
#ssh .scp .upload! 'sync.rb', '/home/user/work'
end
end
end
Sync .new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment