2021-09-12 10:48:45 +05:30
|
|
|
require "http"
|
|
|
|
require "yaml"
|
2024-02-20 01:45:46 +05:30
|
|
|
require "file_utils"
|
2021-09-12 10:48:45 +05:30
|
|
|
require "digest/sha1"
|
|
|
|
require "option_parser"
|
|
|
|
require "colorize"
|
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
# Represents an "install_instruction" section specified per dependency in `videojs-dependencies.yml`
|
|
|
|
#
|
|
|
|
# This is used to modify the download logic for dependencies that are packaged differently.
|
|
|
|
struct InstallInstruction
|
|
|
|
include YAML::Serializable
|
|
|
|
|
|
|
|
property js_path : String? = nil
|
|
|
|
property css_path : String? = nil
|
|
|
|
property download_as : String? = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Object representing a dependency specified within `videojs-dependencies.yml`
|
|
|
|
class ConfigDependency
|
|
|
|
include YAML::Serializable
|
|
|
|
|
|
|
|
property version : String
|
|
|
|
property shasum : String
|
|
|
|
|
|
|
|
property install_instructions : InstallInstruction? = nil
|
|
|
|
|
|
|
|
# Checks if the current dependency needs to be installed/updated
|
|
|
|
def fetch?(name : String)
|
|
|
|
path = "assets/videojs/#{name}"
|
|
|
|
|
|
|
|
# Check for missing dependency files
|
|
|
|
#
|
|
|
|
# Does the directory exist?
|
|
|
|
# Does the Javascript file exist?
|
|
|
|
# Does the CSS file exist?
|
|
|
|
#
|
|
|
|
# videojs-contrib-quality-levels.js is the only dependency that does not come with a CSS file so
|
|
|
|
# we skip the check there
|
|
|
|
if !Dir.exists?(path)
|
|
|
|
Dir.mkdir(path)
|
|
|
|
return true
|
|
|
|
elsif !(File.exists?("#{path}/#{name}.js") || File.exists?("#{path}/versions.yml"))
|
|
|
|
return true
|
|
|
|
elsif name != "videojs-contrib-quality-levels" && !File.exists?("#{path}/#{name}.css")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check if we need to update the dependency
|
|
|
|
|
|
|
|
versions = File.open("#{path}/versions.yml") do |file|
|
|
|
|
YAML.parse(file).as_h
|
|
|
|
end
|
|
|
|
|
|
|
|
if versions["version"].as_s != self.version || versions["minified"].as_bool != CONFIG.minified
|
|
|
|
# Clear directory
|
|
|
|
{"*.js", "*.css"}.each do |file_types|
|
|
|
|
Dir.glob("#{path}/#{file_types}").each do |file_path|
|
|
|
|
File.delete(file_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Object representing the `videojs-dependencies.yml` file
|
|
|
|
class PlayerDependenciesConfig
|
|
|
|
include YAML::Serializable
|
|
|
|
|
|
|
|
property version : String
|
2024-02-20 03:54:39 +05:30
|
|
|
property registry_url : String
|
2024-02-20 04:00:33 +05:30
|
|
|
property cache_directory : String
|
2024-02-20 03:48:56 +05:30
|
|
|
property dependencies : Hash(YAML::Any, ConfigDependency)
|
|
|
|
|
|
|
|
def get_dependencies_to_fetch
|
|
|
|
return self.dependencies.select { |name, config| config.fetch?(name.as_s) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Runtime Dependency config for easy access to all the variables
|
|
|
|
class Config
|
|
|
|
property minified : Bool
|
|
|
|
property skip_checksum : Bool
|
|
|
|
property clear_cache : Bool
|
|
|
|
|
|
|
|
property dependency_config : PlayerDependenciesConfig
|
|
|
|
|
|
|
|
def initialize(path : String)
|
|
|
|
@minified = false
|
|
|
|
@skip_checksum = false
|
|
|
|
@clear_cache = false
|
2024-02-19 06:19:21 +05:30
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
@dependency_config = PlayerDependenciesConfig.from_yaml(File.read(path))
|
|
|
|
end
|
2024-02-20 04:00:33 +05:30
|
|
|
|
|
|
|
# Less verbose way to access @dependency_config.registry_url
|
|
|
|
def registry_url
|
|
|
|
return @dependency_config.registry_url
|
|
|
|
end
|
|
|
|
|
|
|
|
# Less verbose way to access @dependency_config.cache_directory
|
|
|
|
def cache_directory
|
|
|
|
return @dependency_config.cache_directory
|
|
|
|
end
|
2024-02-20 03:48:56 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Object representing a player dependency
|
|
|
|
class Dependency
|
|
|
|
@config : ConfigDependency
|
2024-02-19 06:19:21 +05:30
|
|
|
|
2024-02-20 04:00:33 +05:30
|
|
|
def initialize(@config : ConfigDependency, @dependency : String)
|
|
|
|
@download_path = "#{CONFIG.cache_directory}/#{@dependency}"
|
2024-02-19 06:19:21 +05:30
|
|
|
@destination_path = "assets/videojs/#{@dependency}"
|
|
|
|
end
|
|
|
|
|
2024-02-20 01:45:46 +05:30
|
|
|
private def validate_checksum(io)
|
2024-02-20 03:48:56 +05:30
|
|
|
if !CONFIG.skip_checksum && Digest::SHA1.hexdigest(io) != @config.shasum
|
2024-02-20 01:45:46 +05:30
|
|
|
raise IO::Error.new("Checksum for '#{@dependency}' failed")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Requests and downloads a specific dependency from NPM
|
|
|
|
#
|
|
|
|
# Validates a cached tarball if it already exists.
|
2024-02-19 06:19:21 +05:30
|
|
|
private def request
|
2024-02-20 01:45:46 +05:30
|
|
|
downloaded_package_path = "#{@download_path}/package.tgz"
|
|
|
|
|
|
|
|
# Create a download directory for the dependency if it does not already exist
|
|
|
|
if Dir.exists?(@download_path)
|
|
|
|
# Validate checksum of existing cached tarball
|
|
|
|
# Fetches a new one when the checksum fails.
|
|
|
|
if File.exists?(downloaded_package_path)
|
|
|
|
begin
|
|
|
|
return self.validate_checksum(File.open(downloaded_package_path))
|
|
|
|
rescue IO::Error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2024-02-19 06:19:21 +05:30
|
|
|
Dir.mkdir(@download_path)
|
2024-02-20 01:45:46 +05:30
|
|
|
end
|
2024-02-19 06:19:21 +05:30
|
|
|
|
2024-02-20 04:00:33 +05:30
|
|
|
HTTP::Client.get("#{CONFIG.registry_url}/#{@dependency}/-/#{@dependency}-#{@config.version}.tgz") do |response|
|
2024-02-20 01:45:46 +05:30
|
|
|
data = response.body_io.gets_to_end
|
|
|
|
File.write(downloaded_package_path, data)
|
|
|
|
self.validate_checksum(data)
|
2024-02-19 06:19:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private def move_file(full_target_path, extension)
|
|
|
|
minified_target_path = sprintf(full_target_path, {"file_extension": ".min.#{extension}"})
|
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
if CONFIG.minified && File.exists?(minified_target_path)
|
2024-02-19 06:19:21 +05:30
|
|
|
target_path = minified_target_path
|
|
|
|
else
|
|
|
|
target_path = sprintf(full_target_path, {"file_extension": ".#{extension}"})
|
|
|
|
end
|
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
if download_as = @config.install_instructions.try &.download_as
|
|
|
|
destination_path = "#{@destination_path}/#{sprintf(download_as, {"file_extension": ".#{extension}"})}"
|
2024-02-19 06:19:21 +05:30
|
|
|
else
|
2024-02-20 01:45:46 +05:30
|
|
|
destination_path = @destination_path
|
2024-02-19 06:19:21 +05:30
|
|
|
end
|
|
|
|
|
2024-02-20 01:45:46 +05:30
|
|
|
FileUtils.cp(target_path, destination_path)
|
2024-02-19 06:19:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private def fetch_path(is_css)
|
|
|
|
if is_css
|
2024-02-20 03:48:56 +05:30
|
|
|
raw_target_path = @config.install_instructions.try &.css_path
|
2024-02-19 06:19:21 +05:30
|
|
|
else
|
2024-02-20 03:48:56 +05:30
|
|
|
raw_target_path = @config.install_instructions.try &.js_path
|
2024-02-19 06:19:21 +05:30
|
|
|
end
|
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
if raw_target_path
|
2024-02-19 06:19:21 +05:30
|
|
|
return "#{@download_path}/package/#{raw_target_path}"
|
|
|
|
else
|
|
|
|
return "#{@download_path}/package/dist/#{@dependency}%{file_extension}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private def move_js_file
|
|
|
|
return self.move_file(self.fetch_path(is_css: false), "js")
|
|
|
|
end
|
|
|
|
|
|
|
|
private def move_css_file
|
|
|
|
path = self.fetch_path(is_css: true)
|
|
|
|
|
|
|
|
if File.exists?(sprintf(path, {"file_extension": ".css"}))
|
|
|
|
return move_file(path, "css")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private def update_versions_yaml
|
|
|
|
File.open("#{@destination_path}/versions.yml", "w") do |io|
|
|
|
|
YAML.build(io) do |builder|
|
|
|
|
builder.mapping do
|
|
|
|
# Versions
|
|
|
|
builder.scalar "version"
|
2024-02-20 03:48:56 +05:30
|
|
|
builder.scalar "#{@config.version}"
|
2024-02-19 06:19:21 +05:30
|
|
|
|
|
|
|
builder.scalar "minified"
|
2024-02-20 03:48:56 +05:30
|
|
|
builder.scalar CONFIG.minified
|
2024-02-19 06:19:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
|
|
|
self.request
|
|
|
|
|
|
|
|
# Crystal's stdlib provides no way of extracting a tarball
|
|
|
|
`tar -vzxf '#{@download_path}/package.tgz' -C '#{@download_path}'`
|
|
|
|
raise "Extraction for #{@dependency} failed" if !$?.success?
|
|
|
|
|
|
|
|
self.move_js_file
|
|
|
|
self.move_css_file
|
|
|
|
|
|
|
|
self.update_versions_yaml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
CONFIG = Config.new("videojs-dependencies.yml")
|
|
|
|
|
2024-02-19 02:19:36 +05:30
|
|
|
# Hacky solution to get separated arguments when called from invidious.cr
|
|
|
|
if ARGV.size == 1
|
|
|
|
parser_args = [] of String
|
|
|
|
ARGV[0].split(",") { |str| parser_args << str.strip }
|
|
|
|
else
|
|
|
|
parser_args = ARGV
|
|
|
|
end
|
|
|
|
|
2021-09-12 10:48:45 +05:30
|
|
|
# Taken from https://crystal-lang.org/api/1.1.1/OptionParser.html
|
2024-02-19 02:19:36 +05:30
|
|
|
OptionParser.parse(parser_args) do |parser|
|
2021-09-12 10:48:45 +05:30
|
|
|
parser.banner = "Usage: Fetch VideoJS dependencies [arguments]"
|
2024-02-20 03:48:56 +05:30
|
|
|
parser.on("-m", "--minified", "Use minified versions of VideoJS dependencies (performance and bandwidth benefit)") { CONFIG.minified = true }
|
|
|
|
parser.on("--skip-checksum", "Skips the checksum validation of downloaded files") { CONFIG.skip_checksum = true }
|
|
|
|
parser.on("--clear-cache", "Clears the cache and re-downloads all dependency files") { CONFIG.clear_cache = true }
|
2021-09-12 10:48:45 +05:30
|
|
|
|
|
|
|
parser.on("-h", "--help", "Show this help") do
|
|
|
|
puts parser
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.invalid_option do |flag|
|
|
|
|
STDERR.puts "ERROR: #{flag} is not a valid option."
|
|
|
|
STDERR.puts parser
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
dependencies_to_install = CONFIG.dependency_config.get_dependencies_to_fetch
|
2021-09-12 10:48:45 +05:30
|
|
|
|
2024-02-20 04:00:33 +05:30
|
|
|
Dir.mkdir(CONFIG.cache_directory) if !Dir.exists? CONFIG.cache_directory
|
2021-10-02 18:37:48 +05:30
|
|
|
channel = Channel(String | Exception).new
|
2021-09-12 10:48:45 +05:30
|
|
|
|
2024-02-20 03:48:56 +05:30
|
|
|
dependencies_to_install.each do |dep_name, dependency_config|
|
2021-09-12 10:48:45 +05:30
|
|
|
spawn do
|
2024-02-20 04:00:33 +05:30
|
|
|
dependency = Dependency.new(dependency_config, dep_name.as_s)
|
2024-02-19 06:19:21 +05:30
|
|
|
dependency.fetch
|
2024-02-20 03:48:56 +05:30
|
|
|
channel.send(dep_name.as_s)
|
2021-10-02 18:37:48 +05:30
|
|
|
rescue ex
|
|
|
|
channel.send(ex)
|
2021-09-12 10:48:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-12 10:53:19 +05:30
|
|
|
if dependencies_to_install.empty?
|
2021-09-12 11:17:51 +05:30
|
|
|
puts "#{"Player".colorize(:blue)} #{"dependencies".colorize(:green)} are satisfied"
|
2021-09-12 10:53:19 +05:30
|
|
|
else
|
2021-09-12 11:17:51 +05:30
|
|
|
puts "#{"Resolving".colorize(:green)} #{"player".colorize(:blue)} dependencies"
|
2021-09-12 10:53:19 +05:30
|
|
|
dependencies_to_install.size.times do
|
|
|
|
result = channel.receive
|
2021-10-02 18:37:48 +05:30
|
|
|
|
|
|
|
if result.is_a? Exception
|
|
|
|
raise result
|
|
|
|
end
|
|
|
|
|
2021-09-12 10:53:19 +05:30
|
|
|
puts "#{"Fetched".colorize(:green)} #{result.colorize(:blue)}"
|
|
|
|
end
|
2021-09-12 10:48:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Cleanup
|
2024-02-20 03:48:56 +05:30
|
|
|
if CONFIG.clear_cache
|
2024-02-20 04:00:33 +05:30
|
|
|
FileUtils.rm_r("#{CONFIG.cache_directory}")
|
2024-02-20 01:45:46 +05:30
|
|
|
end
|