Skip to content

Commit

Permalink
Fix cross-device file renaming issue using File.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhvath28 committed Sep 5, 2024
1 parent efad356 commit 7e29956
Showing 1 changed file with 69 additions and 63 deletions.
132 changes: 69 additions & 63 deletions src/worker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,81 +14,87 @@ module Crystal2Nix
def run
temp_file_path = File.tempfile("shards").path

File.open temp_file_path, "w+" do |file|
file.puts %({)
ShardLock.from_yaml(File.read(@lock_file)).shards.each do |key, value|
begin
repo = Repo.new value
rescue ex : Exception
log_message "Error processing repository '#{key}': #{ex.message}. Please check the repository details and try again."
next
end

sha256 = ""

case repo.type
when :git
args = [
"--url", repo.url,
"--rev", repo.rev,
]
begin
File.open temp_file_path, "w+" do |file|
file.puts %({)
ShardLock.from_yaml(File.read(@lock_file)).shards.each do |key, value|
begin
Process.run("nix-prefetch-git", args: args) do |process|
process.error.each_line { |e| STDERR.puts e }
json_output = process.output.gets_to_end
sha256 = GitPrefetchJSON.from_json(json_output).sha256
end
repo = Repo.new value
rescue ex : Exception
log_message "Error running nix-prefetch-git for '#{key}': #{ex.message}.
Try running the command manually to troubleshoot:
nix-prefetch-git #{args.join " "}
Ensure that the git repository is accessible and try again."
log_message "Error processing repository '#{key}': #{ex.message}. Please check the repository details and try again."
next
end

when :hg
args = [
repo.url,
repo.rev,
]
begin
Process.run("nix-prefetch-hg", args: args) do |process|
process.error.each_line { |e| STDERR.puts e }
output = process.output.gets_to_end
sha256 = output.strip.split("\n").first
sha256 = ""

case repo.type
when :git
args = [
"--url", repo.url,
"--rev", repo.rev,
]
begin
Process.run("nix-prefetch-git", args: args) do |process|
process.error.each_line { |e| STDERR.puts e }
json_output = process.output.gets_to_end
sha256 = GitPrefetchJSON.from_json(json_output).sha256
end
rescue ex : Exception
log_message "Error running nix-prefetch-git for '#{key}': #{ex.message}.
Try running the command manually to troubleshoot:
nix-prefetch-git #{args.join " "}
Ensure that the git repository is accessible and try again."
next
end
rescue ex : Exception
log_message "Error running nix-prefetch-hg for '#{key}': #{ex.message}.
Try running the command manually to troubleshoot:
nix-prefetch-hg #{args.join " "}
Ensure that the Mercurial repository is accessible and try again."
next

when :hg
args = [
repo.url,
repo.rev,
]
begin
Process.run("nix-prefetch-hg", args: args) do |process|
process.error.each_line { |e| STDERR.puts e }
output = process.output.gets_to_end
sha256 = output.strip.split("\n").first
end
rescue ex : Exception
log_message "Error running nix-prefetch-hg for '#{key}': #{ex.message}.
Try running the command manually to troubleshoot:
nix-prefetch-hg #{args.join " "}
Ensure that the Mercurial repository is accessible and try again."
next
end

else
log_message "Unsupported repository type for '#{key}': #{repo.type}. Currently supported types are: git, hg."
break
end

else
log_message "Unsupported repository type for '#{key}': #{repo.type}. Currently supported types are: git, hg."
break
file.puts %( #{key} = {)
file.puts %( url = "#{repo.url}";)
file.puts %( rev = "#{repo.rev}";)
file.puts %( sha256 = "#{sha256}";)
file.puts %( };)
end

file.puts %( #{key} = {)
file.puts %( url = "#{repo.url}";)
file.puts %( rev = "#{repo.rev}";)
file.puts %( sha256 = "#{sha256}";)
file.puts %( };)
file.puts %(})
end
file.puts %(})
end

if @errors.any?
File.delete(temp_file_path)
STDERR.puts "\nSummary of errors encountered:"
@errors.each { |error| STDERR.puts " - #{error}" }
STDERR.puts "\nProcess not completed due to the above errors. Please review and resolve them before re-running."
rescue ex : Exception
log_message "Error with temporary file operations: #{ex.message}"
exit 1
ensure
if @errors.any?
File.delete(temp_file_path)
STDERR.puts "\nSummary of errors encountered:"
@errors.each { |error| STDERR.puts " - #{error}" }
STDERR.puts "\nProcess not completed due to the above errors. Please review and resolve them before re-running."
exit 1
else
File.copy(temp_file_path, SHARDS_NIX)
File.delete(temp_file_path)
puts "Processing completed successfully."
end
end

File.rename(temp_file_path, SHARDS_NIX)
puts "Processing completed successfully."
end
end
end

0 comments on commit 7e29956

Please sign in to comment.