2023-10-07 08:21:48 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-06-22 04:43:31 +02:00
|
|
|
##
|
|
|
|
# The {Utils Utils} module provides methods that are
|
2024-06-22 04:55:16 +02:00
|
|
|
# available at build time. ERB templates, the Rules file
|
|
|
|
# and Rakefile.rb all have access to the methods of this
|
|
|
|
# module
|
2024-06-22 04:43:31 +02:00
|
|
|
module Utils
|
2024-05-25 05:53:15 +02:00
|
|
|
require "test-cmd"
|
2024-06-22 04:43:31 +02:00
|
|
|
require_relative "utils/t"
|
|
|
|
require_relative "utils/inline"
|
|
|
|
require_relative "utils/erb"
|
2023-10-07 08:21:48 +02:00
|
|
|
|
2024-06-22 07:01:18 +02:00
|
|
|
##
|
|
|
|
# Generic error
|
|
|
|
Error = Class.new(RuntimeError) unless defined?(Error)
|
|
|
|
|
2024-06-22 00:04:17 +02:00
|
|
|
##
|
|
|
|
# @return [Ryo::Object]
|
|
|
|
# Returns common directory paths as a Ryo object
|
|
|
|
def dirs
|
|
|
|
@dirs ||= Ryo(
|
|
|
|
root:,
|
2024-06-22 00:09:54 +02:00
|
|
|
build: File.join(root, nanoc.output_dir),
|
2024-06-22 00:04:17 +02:00
|
|
|
content: File.join(root, nanoc.data_sources[0].content_dir)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# @return [Ryo::Object]
|
|
|
|
# Returns the contents of nanoc.yaml as a Ryo object
|
|
|
|
def nanoc
|
|
|
|
@nanoc ||= Ryo.from_yaml(path: File.join(root, "nanoc.yaml"))
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns an absolute path to the root directory of the website
|
|
|
|
def root
|
|
|
|
@root ||= File.realpath(File.join(__dir__, "..", "..", "."))
|
2024-05-01 16:51:18 +02:00
|
|
|
end
|
|
|
|
|
2024-06-22 00:04:17 +02:00
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns the website version
|
|
|
|
def version
|
2024-08-22 18:28:04 +02:00
|
|
|
@version ||= begin
|
|
|
|
path = File.join(dirs.root, "share", "al-quran.reflectslight.io", "VERSION")
|
|
|
|
File.read(path).gsub(/[^\d.]/, "")
|
|
|
|
end
|
2024-05-25 05:53:15 +02:00
|
|
|
end
|
|
|
|
|
2024-06-22 00:04:17 +02:00
|
|
|
##
|
2024-06-22 07:01:18 +02:00
|
|
|
# @raise [Utils::Error]
|
|
|
|
# When git fails or is unavailable
|
2024-06-22 00:04:17 +02:00
|
|
|
# @return [String]
|
|
|
|
# Returns the most recent git commit hash
|
|
|
|
def commit
|
2024-06-22 06:16:30 +02:00
|
|
|
@commit ||= begin
|
2024-06-22 07:01:18 +02:00
|
|
|
hash = nil
|
|
|
|
cmd("git", "rev-parse", "HEAD")
|
|
|
|
.success { hash = _1.stdout.strip }
|
2024-06-22 07:24:49 +02:00
|
|
|
.failure { error!("git exited unsuccessfully in method Utils#commit") }
|
2024-06-22 07:01:18 +02:00
|
|
|
hash
|
2024-06-22 06:16:30 +02:00
|
|
|
end
|
2024-05-26 03:27:28 +02:00
|
|
|
end
|
|
|
|
|
2024-06-02 08:07:45 +02:00
|
|
|
##
|
|
|
|
# The URL for an audio file is resolved
|
|
|
|
# by joining `nanoc.audio.base_url` and
|
|
|
|
# `/<surahid>/<ayahid>.mp3`.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# Returns the base url for audio requests.
|
|
|
|
# The default (https://al-quran.reflectslight.io/audio/alafasy)
|
|
|
|
# works out of the box.
|
|
|
|
def audio_base_url
|
|
|
|
nanoc.audio.base_url
|
2024-05-26 03:27:28 +02:00
|
|
|
end
|
|
|
|
|
2024-06-02 08:07:45 +02:00
|
|
|
##
|
|
|
|
# @return [String]
|
|
|
|
# Returns the base URL for use with opengraph,
|
|
|
|
# <link> tags, /sitemap.xml, etc. The default is
|
|
|
|
# https://al-quran.reflectslight.io.
|
|
|
|
def base_url
|
|
|
|
nanoc.server.base_url
|
|
|
|
end
|
|
|
|
|
2023-10-07 08:21:48 +02:00
|
|
|
include T
|
|
|
|
include Inline
|
|
|
|
include ERB
|
2024-06-22 07:24:49 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def error!(m)
|
|
|
|
raise Error, m, []
|
|
|
|
end
|
2023-10-07 08:21:48 +02:00
|
|
|
end
|
2024-06-22 04:55:16 +02:00
|
|
|
use_helper(Utils) if respond_to?(:use_helper)
|