Skip to content

Commit

Permalink
Chore: Use singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
pusewicz committed Feb 17, 2022
1 parent 4e5d3be commit e00dc3c
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 96 deletions.
69 changes: 33 additions & 36 deletions lib/statique.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,58 @@
require "front_matter_parser"
require "hashie"
require "pathname"
require "rack"
require "singleton"
require "tty-logger"

::FrontMatterParser::SyntaxParser::Builder = FrontMatterParser::SyntaxParser::MultiLineComment["=begin", "=end"]

$LOAD_PATH.unshift(File.expand_path("..", __FILE__))

require "statique/version"
require "statique/cli"
require "statique/mode"
require "statique/discover"
require "statique/document"
require "statique/configuration"
require "statique/paginator"

class Statique
class Error < StandardError; end
include Singleton
extend Forwardable

class << self
extend Forwardable
class Error < StandardError; end

def_delegators :configuration, :root_url
autoload :Configuration, "statique/configuration"
autoload :Discover, "statique/discover"
autoload :Document, "statique/document"
autoload :Mode, "statique/mode"
autoload :Paginator, "statique/paginator"
autoload :VERSION, "statique/version"

def configuration
@configuration ||= Configuration.new
end
def_delegators :configuration, :root_url

def discover
@discover ||= Discover.new(configuration.paths.content)
end
attr_reader :configuration, :discover, :mode, :pwd, :build_queue

def mode
@mode ||= Mode.new
end
def initialize
@mode = Mode.new
@configuration = Configuration.new
@discover = Discover.new(configuration.paths.content, self)
@pwd = Pathname.pwd.freeze
end

def pwd
@pwd ||= Pathname.pwd.freeze
end
def statique
self.class.instance
end

def version
VERSION
end
def version
VERSION
end

def ui
@ui ||= TTY::Logger.new(output: $stdout) do |config|
config.level = :debug if ENV["DEBUG"] == "true"
end
end
def ui
self.class.ui
end

def url(document_or_path)
File.join(configuration.root_url, document_or_path.is_a?(Document) ? document_or_path.path : document_or_path)
def self.ui
@ui ||= TTY::Logger.new(output: $stdout) do |config|
config.level = :debug if ENV["DEBUG"] == "true"
end
end

def build_queue
@build_queue ||= Queue.new
end
def url(document_or_path)
File.join(configuration.root_url, document_or_path.is_a?(Document) ? document_or_path.path : document_or_path)
end
end
46 changes: 25 additions & 21 deletions lib/statique/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,54 @@ class Statique
class App < Roda
extend Forwardable

def_delegators :Statique, :url, :root_url
@statique = Statique.instance

opts[:root] = Statique.configuration.paths.pwd
def_delegators :@statique, :url, :root_url, :statique

opts[:root] = @statique.configuration.paths.pwd

plugin :environments
plugin :static_routing
plugin :render, views: Statique.configuration.paths.content.basename, engine: "slim", allowed_paths: [Statique.configuration.paths.content.basename, Statique.configuration.paths.layouts.basename]
plugin :partials, views: Statique.configuration.paths.layouts.basename
plugin :render, views: @statique.configuration.paths.content.basename, engine: "slim", allowed_paths: [@statique.configuration.paths.content.basename, @statique.configuration.paths.layouts.basename]
plugin :partials, views: @statique.configuration.paths.layouts.basename

if Statique.mode.server? && Statique.configuration.paths.public.exist?
plugin :public, root: Statique.configuration.paths.public.basename
if @statique.mode.server? && @statique.configuration.paths.public.exist?
plugin :public, root: @statique.configuration.paths.public.basename
end

if Statique.configuration.paths.assets.exist?
css_files = Statique.configuration.paths.assets.join("css").glob("*.{css,scss}")
js_files = Statique.configuration.paths.assets.join("js").glob("*.js")
plugin :assets, css: css_files.map { _1.basename.to_s }, js: js_files.map { _1.basename.to_s }, public: Statique.configuration.paths.destination, precompiled: Statique.configuration.paths.destination.join("assets/manifest.json"), relative_paths: true
if @statique.configuration.paths.assets.exist?
css_files = @statique.configuration.paths.assets.join("css").glob("*.{css,scss}")
js_files = @statique.configuration.paths.assets.join("js").glob("*.js")
plugin :assets, css: css_files.map { _1.basename.to_s }, js: js_files.map { _1.basename.to_s }, public: @statique.configuration.paths.destination, precompiled: @statique.configuration.paths.destination.join("assets/manifest.json"), relative_paths: true
plugin :assets_preloading

Statique.mode.build do
@statique.mode.build do
compiled = compile_assets
Statique.ui.info "Compiling assets", css: compiled["css"], js: compiled["js"]
@statique.ui.info "Compiling assets", css: compiled["css"], js: compiled["js"]
end
end

route do |r|
if Statique.mode.server?
r.public if Statique.configuration.paths.public.exist?
r.assets if Statique.configuration.paths.assets.exist?
@statique = Statique.instance
if @statique.mode.server?
r.public if @statique.configuration.paths.public.exist?
r.assets if @statique.configuration.paths.assets.exist?
end

path, page = r.env["REQUEST_PATH"].split("/page/")
path, page = r.env["PATH_INFO"].split("/page/")

document = Statique.discover.documents.find { _1.path == path }
document = @statique.discover.documents.find { _1.path == path }

r.on(proc { document }) do
locals = {
documents: Statique.discover.documents,
collections: Statique.discover.collections,
document: document
collections: @statique.discover.collections,
document: document,
documents: @statique.discover.documents,
statique: @statique
}

if document.meta.paginates
paginator = Paginator.new(Statique.discover.collections[document.meta.paginates].to_a, document.path, page)
paginator = Paginator.new(@statique.discover.collections[document.meta.paginates].to_a, document.path, page, @statique)
locals[document.meta.paginates.to_sym] = paginator.documents
locals[:paginator] = paginator
end
Expand Down
14 changes: 10 additions & 4 deletions lib/statique/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def init

desc "server", "Start Statique server"
def server
Statique.mode.server!
Server.new.run
statique.mode.server!
Server.new(statique).run
end

desc "build", "Build Statique site"
def build
Statique.mode.build!
Build.new(options.dup).run
statique.mode.build!
Build.new(options.dup, statique).run
end

desc "version", "Prints the statique's version information"
Expand All @@ -50,5 +50,11 @@ def version
end

map aliases_for("version")

private

def statique
Statique.instance
end
end
end
23 changes: 12 additions & 11 deletions lib/statique/cli/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ class CLI
class Build
prepend MemoWise

def initialize(options)
def initialize(options, statique)
Thread.abort_on_exception = true
@queue = Statique.build_queue
@statique = statique
@queue = Queue.new
end

def run
require "statique/app"
time = Benchmark.realtime do
create_directory(Statique.configuration.paths.destination)
create_directory(@statique.configuration.paths.destination)
copy_public_assets
build_pages
end

Statique.ui.success "Done!", time: time
@statique.ui.success "Done!", time: time
end

private

def build_pages
Statique.discover.documents.each do |document|
@statique.discover.documents.each do |document|
@queue << document.path

if (pages = document.pagination_pages)
Expand All @@ -42,12 +43,12 @@ def build_pages
path = @queue.pop
response = mock_request.get(path)
if response.successful?
destination = Statique.configuration.paths.destination.join(File.extname(path).empty? ? "./#{path}/index.html" : "./#{path}")
Statique.ui.info "Building page", path: path
destination = @statique.configuration.paths.destination.join(File.extname(path).empty? ? "./#{path}/index.html" : "./#{path}")
@statique.ui.info "Building page", path: path
create_directory(destination.dirname)
File.write(destination, response.body)
else
Statique.ui.error "Error building page", path: document.path, status: response.status
@statique.ui.error "Error building page", path: path, status: response.status
end
end
end
Expand All @@ -61,9 +62,9 @@ def mock_request
end

def copy_public_assets
assets = Statique.configuration.paths.public.glob("**/*.*")
Statique.ui.info "Copying public assets", assets:
FileUtils.cp_r(assets, Statique.configuration.paths.destination)
assets = @statique.configuration.paths.public.glob("**/*.*")
@statique.ui.info "Copying public assets", assets:
FileUtils.cp_r(assets, @statique.configuration.paths.destination)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/statique/cli/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def run
create_directory(@dest_dir)

write_file(@dest_dir.join("public/robots.txt"), "User-agent: *\nAllow: /")
write_file(@dest_dir.join("assets/css/app.scss"), %(@import url("https://cdn.jsdelivr.net/npm/water.css@2/out/water.css");))
write_file(@dest_dir.join("assets/css/app.css"), %(@import url("https://cdn.jsdelivr.net/npm/water.css@2/out/water.css");))
write_file(@dest_dir.join("assets/js/app.js"), %(console.log("Hello from Statique!")))

copy_template("index.md", @dest_dir.join("content"))
Expand Down
9 changes: 5 additions & 4 deletions lib/statique/cli/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ def log(type, msg, attrs = {})
end
end

def initialize(port: 3000)
def initialize(statique, port: 3000)
@port = port
@statique = statique
end

def run
require "statique/app"
Statique.ui.info "Starting server", port: @port
@statique.ui.info "Starting server", port: @port

logger = LoggerWrapper.new

Expand All @@ -42,7 +43,7 @@ def run
end

def stop
Statique.ui.info "Stopping server"
@statique.ui.info "Stopping server"
Rack::Handler::WEBrick.shutdown
end

Expand All @@ -52,7 +53,7 @@ def initialize(type)
end

def puts(message)
Statique.ui.public_send(@type, message)
@statique.ui.public_send(@type, message)
end

def write(message)
Expand Down
19 changes: 10 additions & 9 deletions lib/statique/discover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ class Discover

GLOB = "**/*.{#{SUPPORTED_EXTENSIONS.join(",")}}"

def initialize(root)
def initialize(root, statique)
@root = root
@documents = []
@collections = Hashie::Mash.new { |hash, key| hash[key] = Set.new }
@statique = statique

discover_files!
discover!

Statique.mode.build do
@statique.mode.build do
@files.freeze
@documents.freeze
@collections.freeze
end

watch_for_changes if Statique.mode.server?
watch_for_changes if @statique.mode.server?
end

private

def discover_files!
@files = @root.glob(GLOB)
ensure
Statique.ui.debug "Discovered files", count: @files.size
@statique.ui.debug "Discovered files", count: @files.size
end

def discover!
Expand All @@ -46,7 +47,7 @@ def discover!
end

def process(file)
document = Document.new(file)
document = Document.new(file, @statique)

documents << document

Expand All @@ -58,20 +59,20 @@ def process(file)
def watch_for_changes
require "filewatcher"

@filewatcher = Filewatcher.new([Statique.configuration.paths.content, Statique.configuration.paths.layouts])
@filewatcher = Filewatcher.new([@statique.configuration.paths.content, @statique.configuration.paths.layouts])
@filewatcher_thread = Thread.new(@filewatcher) do |watcher|
watcher.watch do |file, event|
Statique.ui.debug "File change event", file: file, event: event
@statique.ui.debug "File change event", file: file, event: event
discover_files!
path = Pathname.new(file)
remove_file!(path)
process(path) unless event == :deleted
end
end
Statique.ui.debug "Started file watcher", filewatcher: @filewatcher, thread: @filewatcher_thread
@statique.ui.debug "Started file watcher", filewatcher: @filewatcher, thread: @filewatcher_thread

at_exit do
Statique.ui.debug "Closing file watcher", thread: @filewatcher_thread
@statique.ui.debug "Closing file watcher", thread: @filewatcher_thread
@filewatcher.stop
@filewatcher.finalize
@filewatcher_thread.join
Expand Down
Loading

0 comments on commit e00dc3c

Please sign in to comment.