Class: Spawnpoint::Synchronizer

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

Constant Summary collapse

BACKUP_DIR =
".spwn_sync_backup"

Instance Method Summary collapse

Instance Method Details

#rollback(argv) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/spawnpoint/synchronizer.rb', line 150

def rollback(argv)
  target = parse_rollback_args(argv)
  unless target
    puts "Usage: spwn rollback --into <game-folder>"
    return 1
  end

  target = Pathname.new(target).expand_path
  backup = target.join(BACKUP_DIR)
  unless backup.directory?
    puts "Oops: there is no lesson sync to roll back in #{target}."
    return 1
  end

  backup.join("created_paths").readlines.each do |line|
    target.join(line.strip).delete if !line.strip.empty? && target.join(line.strip).file?
  end
  backup.join("files").find do |saved|
    next if saved.directory? || saved == backup.join("files")
    rel = saved.relative_path_from(backup.join("files"))
    dest = target.join(rel)
    FileUtils.mkdir_p(dest.parent)
    FileUtils.cp(saved, dest)
  end

  marker = target.join(".spwn_synced_paths")
  if backup.join("marker").file?
    FileUtils.cp(backup.join("marker"), marker)
  else
    marker.delete if marker.file?
  end

  FileUtils.rm_rf(backup)
  puts "Rolled back the most recent lesson sync."
  0
end

#run(argv) ⇒ Object



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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/spawnpoint/synchronizer.rb', line 28

def run(argv)
  lesson_folder, into_folder, force = parse_args(argv)

  unless lesson_folder
    puts "Usage:"
    puts "  spwn sync <lesson-folder> --into <game-folder>"
    puts "  spwn sync <lesson-folder> --into <game-folder> --force"
    puts ""
    puts "Example:"
    puts "  spwn sync 04-collectibles/starter --into ~/DragonRuby/mygame"
    puts ""
    puts "The first time a lesson copies a file that already exists in your"
    puts "game folder, spwn asks before replacing it. After you accept a"
    puts "file, later lessons replace that same file automatically."
    return 1
  end

  unless into_folder
    puts "Oops: tell spwn where to copy the lesson with --into."
    puts "Example: spwn sync 04-collectibles/starter --into ~/DragonRuby/mygame"
    return 1
  end

  source = Pathname.new(lesson_folder).expand_path
  target = Pathname.new(into_folder).expand_path

  unless source.directory?
    puts "Oops: #{source} is not a folder."
    return 1
  end

  unless target.directory?
    puts "Oops: #{target} is not a folder yet."
    return 1
  end

  has_assets = source.join("assets").directory?

  if has_assets
    puts "This lesson includes an assets/ folder."
    puts "Assets may look different from the previous lesson."
    puts "If you already have sprites from an older lesson, you will be asked before each one is replaced."
    puts ""
  end

  marker = target.join(".spwn_synced_paths")
  backup = target.join(BACKUP_DIR)
  FileUtils.rm_rf(backup)
  backup.join("files").mkpath
  backup.join("created_paths").write("")
  if marker.file?
    FileUtils.cp(marker, backup.join("marker"))
  else
    backup.join("no_marker").write("")
  end

  known = if marker.file?
    marker.readlines.map(&:strip).reject(&:empty?).to_set
  else
    Set.new
  end

  already_existed = Set.new
  target.find.each do |child|
    next unless child.file?
    already_existed << child.relative_path_from(target).to_s
  end

  copied = 0
  upgraded = 0
  skipped = 0
  new_known = Set.new

  source.find.to_a.each do |child|
    next unless child.file?

    rel = child.relative_path_from(source)
    rel_s = rel.to_s
    dest = target.join(rel)

    if dest.exist?
      if force || known.include?(rel_s)
        backup_existing(backup, dest, rel_s)
        copy_file(child, dest)
        upgraded += 1
        new_known << rel_s
      elsif already_existed.include?(rel_s)
        if agree?("Copy #{rel} from this lesson?")
          backup_existing(backup, dest, rel_s)
          copy_file(child, dest)
          upgraded += 1
          new_known << rel_s
        else
          skipped += 1
        end
      end
    else
      copy_file(child, dest)
      File.open(backup.join("created_paths"), "a") { |fh| fh.puts(rel_s) }
      copied += 1
      new_known << rel_s
    end
  end

  merged = known | new_known
  if merged.any?
    marker.open("w") do |fh|
      merged.to_a.sort.each do |name|
        fh.puts(name)
      end
    end
  end

  puts ""
  puts "Done."
  puts "New:       #{copied}"
  puts "Updated:   #{upgraded}"
  puts "Skipped:   #{skipped}"

  0
end