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
|