Class: GDSync::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(src, dest_dir, option) ⇒ Sync


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sync.rb', line 16

def initialize(src, dest_dir, option)
  @googledrive_session = nil
  @googledrive_fs = nil
  @local_fs = nil

  @googledrive_config_path = _prepare_config_file

  @src = src.map { |_| _.encode(::Encoding::UTF_8) }
  @dest = dest_dir.encode(::Encoding::UTF_8)
  @option = option
end

Instance Method Details

#dryrun_fsObject


35
36
37
38
39
40
# File 'lib/sync.rb', line 35

def dryrun_fs
  if @dryrun_fs.nil?
    @dryrun_fs = DryRunFileSystem.new
  end
  @dryrun_fs
end

#googledrive_fsObject


42
43
44
45
46
47
# File 'lib/sync.rb', line 42

def googledrive_fs
  if @googledrive_fs.nil?
    @googledrive_fs = GoogleDriveFileSystem.new(googledrive_session)
  end
  @googledrive_fs
end

#googledrive_sessionObject


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sync.rb', line 49

def googledrive_session
  if @googledrive_session.nil?
    if Gem.win_platform?
      # "OpenSSL::X509::DEFAULT_CERT_FILE" may point to invalid location,
      # typically depending on who build the RubyInstaller. (ex. "C:/Users/(someone)/Projects/knap-build/...")
      # So we have to set correct *.pem file path. Fortunately, 'google-api-client' provides valid 'cacerts.pem' file.
      cert_path = ::File.join(::Gem.loaded_specs['google-api-client'].full_gem_path, 'lib', 'cacerts.pem')
      ENV['SSL_CERT_FILE'] = cert_path
    end
    @googledrive_session = ::GoogleDrive.saved_session(@googledrive_config_path)
  end
  @googledrive_session
end

#local_fsObject


28
29
30
31
32
33
# File 'lib/sync.rb', line 28

def local_fs
  if @local_fs.nil?
    @local_fs = LocalFileSystem.new
  end
  @local_fs
end

#runObject


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sync.rb', line 63

def run
  @src.each { |_|
    src = _lookup_file_or_dir(_)

    if src.nil?
      raise "file or directory '#{_}' not found"
    else
      if src.is_dir?
        if @option.recursive? || @option.dirs?
          dest = _lookup_dir(@dest)
          
          if !dest.nil? && !src.path.end_with?('/')
            sub = _lookup_dir(::File.join(@dest, src.title))
            if sub.nil? && !@option.existing?
              sub = _create_new_dir(src.title, dest)
              raise "cannot create directory '#{::File.join(@dest, src.title)}'" if sub.nil?
            end
            dest = sub
          end

          if dest.nil?
            raise "cannot find dest directory" unless @option.existing?
          else
            _transfer_directory_contents_recursive(src, dest) unless @option.dirs? && !src.path.end_with?('/')
          end
        else
          @option.log_skip(src)
        end
      else
        dest = _lookup_dir(@dest)
        if dest.nil?
          raise "cannot find dest directory"
        else
          dest_existing_file = dest.fs.find(::File.join(dest.path, src.title))
          _transfer_file(src, dest, dest_existing_file) 
        end
      end
    end
  }
end