Skip to content

Commit

Permalink
Add a packaging pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
BGMP committed Jan 14, 2022
1 parent 2698a98 commit 775e085
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Rake
.rakeTasks

# JetBrains
.idea

# Vim generated files
*~
*.swp

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Re-Volt Paintkits
*.psd
*.pdn
*.xcf

# Packaging
*.zip
packages.json
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source 'https://rubygems.org'

ruby '2.7.3'

gem 'digest', '~> 3.1'
gem 'rake', '~> 13.0', '>= 13.0.6'
gem 'rubyzip', '~> 2.3', '>= 2.3.2'
20 changes: 20 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
GEM
remote: https://rubygems.org/
specs:
digest (3.1.0)
rake (13.0.6)
rubyzip (2.3.2)

PLATFORMS
x64-mingw32

DEPENDENCIES
digest (~> 3.1)
rake (~> 13.0, >= 13.0.6)
rubyzip (~> 2.3, >= 2.3.2)

RUBY VERSION
ruby 2.7.3p183

BUNDLED WITH
2.2.21
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rva_tracks/
===

Re-Volt America's Tracks Pack.
106 changes: 106 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

require 'zip'
require 'digest'
require_relative 'data'

task default: %i[package version]

task :package do
zipfile_name = "#{RVATracks::NAME}.zip"
puts "Packaging into #{zipfile_name} ..."

exclude = %w[. .. Gemfile Gemfile.lock Rakefile .git .gitignore .idea data.rb README.md]
zf = ZipFileGenerator.new('.', zipfile_name, exclude)
zf.write

puts "Packaging complete! (#{bytes_to_mb(File.size(zipfile_name))} MB)."
puts 'OK.'
end

task version: [:package] do
checksum = nil
checksum = Digest::SHA256.hexdigest File.read "#{RVATracks::NAME}.zip" if File.exist?("#{RVATracks::NAME}.zip")

contents = [
'{',
"\t\"rva_tracks\":",
"\t{",
"\t\t\"description\": \"#{RVATracks::DESCRIPTION}\",",
"\t\t\"version\": #{RVATracks::VERSION},",
"\t\t\"checksum\": \"#{checksum}\",",
"\t\t\"url\": \"#{RVATracks::URL}\"",
"\t}",
'}'
]

File.open('packages.json', 'w+') do |f|
contents.each { |line| f.puts(line) }
end
end

def bytes_to_mb(bytes)
format_number((bytes.to_f / 2**20).round(2))
end

def format_number(number)
whole, decimal = number.to_s.split('.')
num_groups = whole.chars.to_a.reverse.each_slice(3)
whole_with_commas = num_groups.map(&:join).join(',').reverse
[whole_with_commas, decimal].compact.join('.')
end

# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
# directory_to_zip = "/tmp/input"
# output_file = "/tmp/out.zip"
# zf = ZipFileGenerator.new(directory_to_zip, output_file, exclude)
# zf.write()
class ZipFileGenerator
# Initialize with the directory to zip and the location of the output archive.
def initialize(input_dir, output_file, exclude = [])
@input_dir = input_dir
@output_file = output_file
@exclude = exclude
end

# Zip the input directory.
def write
packaged = Dir.entries(@input_dir).select { |p| File.extname(p) == '.zip' }
entries = Dir.entries(@input_dir) - %w[. ..].concat(@exclude).concat(packaged)

::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
write_entries entries, '', zipfile
end
end

private

# A helper method to make the recursion work.
def write_entries(entries, path, zipfile)
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
puts zipfile_path
if File.directory? disk_file_path
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
else
put_into_archive(disk_file_path, zipfile, zipfile_path)
end
end
end

def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
zipfile.mkdir zipfile_path
subdir = Dir.entries(disk_file_path) - %w[. ..]
write_entries subdir, zipfile_path, zipfile
end

def put_into_archive(disk_file_path, zipfile, zipfile_path)
zipfile.add(zipfile_path, disk_file_path)
end
end
8 changes: 8 additions & 0 deletions data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module RVATracks
NAME = 'rva_tracks'
DESCRIPTION = "Re-Volt America's Tracks Pack"
VERSION = '1.0000'
URL = 'https://distribute.revolt-america.com/rva/rva_tracks.zip'
end

0 comments on commit 775e085

Please sign in to comment.