diff options
Diffstat (limited to 'new/lib/crys')
-rw-r--r-- | new/lib/crys/batch_builder.rb | 36 | ||||
-rw-r--r-- | new/lib/crys/builder.rb | 47 | ||||
-rw-r--r-- | new/lib/crys/deployer.rb | 38 | ||||
-rw-r--r-- | new/lib/crys/image_processor.rb | 12 | ||||
-rw-r--r-- | new/lib/crys/pages_db_manager.rb | 53 | ||||
-rw-r--r-- | new/lib/crys/processors/common_parts.rb | 18 | ||||
-rw-r--r-- | new/lib/crys/processors/html_processor.rb | 31 | ||||
-rw-r--r-- | new/lib/crys/processors/mixins/common_functions.rb (renamed from new/lib/crys/processors/common_functions.rb) | 13 | ||||
-rw-r--r-- | new/lib/crys/processors/mixins/common_parts.rb | 18 | ||||
-rw-r--r-- | new/lib/crys/processors/processed_page.rb | 8 | ||||
-rw-r--r-- | new/lib/crys/processors/rss_processor.rb | 19 | ||||
-rw-r--r-- | new/lib/crys/processors/yaml_batch_processor.rb | 45 | ||||
-rw-r--r-- | new/lib/crys/server.rb | 16 |
13 files changed, 243 insertions, 111 deletions
diff --git a/new/lib/crys/batch_builder.rb b/new/lib/crys/batch_builder.rb index 61fdcf22..d8f1d7b6 100644 --- a/new/lib/crys/batch_builder.rb +++ b/new/lib/crys/batch_builder.rb @@ -1,34 +1,30 @@ +# frozen_string_literal: true + module Crys class BatchBuilder - def db_path - output_dir = "#{File.dirname(File.dirname(File.dirname(__FILE__)))}/db/pages.yaml" - end - - def output_dir - "#{File.dirname(File.dirname(__FILE__))}/output" - end - - def image_dir - "#{File.dirname(File.dirname(File.expand_path(__FILE__)))}/assets/images" - end + def initialize(pages_db_manager:, output_dir:, image_dir:, builder_class:) + @pages_db_manager = pages_db_manager + @output_dir = output_dir + @image_dir = image_dir + @builder_class = builder_class - def builder_class - Crys::Builder end - def db - Crys::PagesDbManager.new - end - def build - db.pages.each do |page| + pages_db_manager.pages.each do |page| builder_class.new( - file_path: page.file_path, + file_path: page.source, output_dir: output_dir, image_dir: image_dir, + pages_db_manager: pages_db_manager ).build - puts "processed: " + page.filename + puts "processed: #{page.filename}" end end + + private + + attr_reader :pages_db_manager, :output_dir, :image_dir, :builder_class + end end diff --git a/new/lib/crys/builder.rb b/new/lib/crys/builder.rb index b8135c77..123935a6 100644 --- a/new/lib/crys/builder.rb +++ b/new/lib/crys/builder.rb @@ -1,39 +1,46 @@ +# frozen_string_literal: true + module Crys class Builder - def initialize(file_path:, output_dir:, image_dir:) + def initialize(file_path:, output_dir:, image_dir:, pages_db_manager:) @file_path = file_path @output_dir = output_dir - @processor = processor_class.new(file_path: file_path, image_dir: image_dir) + @image_dir = image_dir + @pages_db_manager = pages_db_manager end - def build + def build + @processor = processor_class.new(file_path: file_path, image_dir: image_dir) + process_content process_assets - - upsert_pages_db end private - attr_reader :file_path, :output_dir, :processor, :db + attr_reader :file_path, :output_dir, :processor, :db, :image_dir, :pages_db_manager + - def pages_db_manager - Crys::PagesDbManager.new - end - def process_assets processor.assets.each do |asset| - output_path = output_dir + "/" + asset.relative_path - File.open(output_path, 'w') { |file| file.write(asset.processed_asset) } + output_path = "#{output_dir}/#{asset.relative_path}" + File.write(output_path, asset.processed_asset) end end def process_content - content = processor.parsed_content - filename = processor.filename - output_path = output_dir + "/" + filename + processor.parsed_pages.each do |page| + path = output_dir + "/" + File.dirname(page.filename) + FileUtils.mkdir_p(path) + + content = page.content + filename = page.filename + output_path = "#{output_dir}/#{filename}" + pages_db_manager.add_page(page) + File.write(output_path, content) + end - File.open(output_path, 'w') { |file| file.write(content) } + pages_db_manager.save end def processor_class @@ -42,8 +49,10 @@ module Crys html_processor when /xml.rb$/ rss_processor + when /yaml$/ + yaml_batch_processor else - raise StandardError.new("No processor for #{file_path}") + raise StandardError, "No processor for #{file_path}" end end @@ -55,8 +64,8 @@ module Crys Crys::RssProcessor end - def upsert_pages_db - pages_db_manager.add_page(processor) + def yaml_batch_processor + Crys::YamlBatchProcessor end end end diff --git a/new/lib/crys/deployer.rb b/new/lib/crys/deployer.rb new file mode 100644 index 00000000..501ea046 --- /dev/null +++ b/new/lib/crys/deployer.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'rsync' + +module Crys + class Deployer + DEST_PREFIX = '/usr/local/sites/' + + def initialize(dest:, source:) + @dest = dest + @source = source + + Rsync.configure do |config| + config.host = 'mms@10.0.7.0' + end + end + + def run + puts "source: #{source}" + full_dest = DEST_PREFIX + dest + puts "dest: #{full_dest}" + + Rsync.run(source, full_dest, ['-rtz']) do |result| + if result.success? + result.changes.each do |change| + puts "#{change.filename} (#{change.summary})" + end + else + puts result.error + end + end + end + + private + + attr_reader :dest, :source + end +end diff --git a/new/lib/crys/image_processor.rb b/new/lib/crys/image_processor.rb index b7e2fd67..306c0769 100644 --- a/new/lib/crys/image_processor.rb +++ b/new/lib/crys/image_processor.rb @@ -1,26 +1,24 @@ +# frozen_string_literal: true + module Crys class ImageProcessor def initialize(path:, filename:, width: :auto) @path = path @filename = filename @file = File.read(path) + @width = width end def relative_path filename end - def filename - filename - end - def processed_asset file.to_s end - private + private - attr_reader :path, :filename, :file - + attr_reader :path, :filename, :file end end diff --git a/new/lib/crys/pages_db_manager.rb b/new/lib/crys/pages_db_manager.rb index 6a5e71c9..d8da7e70 100644 --- a/new/lib/crys/pages_db_manager.rb +++ b/new/lib/crys/pages_db_manager.rb @@ -1,30 +1,47 @@ +# frozen_string_literal: true + module Crys class PagesDbManager PAGES_KEY = :pages - PAGE = Struct.new(:filename, :file_path, :last_update, :in_rss, keyword_init: true) + PAGE = Struct.new( + :filename, + :source, + + :last_updated_at, + :created_at, + + :in_rss, + :in_all, - def db_path + keyword_init: true) + + + def initialize(db_path:) + @db_path = db_path end - def add_page(processor) - @processor = processor - hash = Digest::MD5.hexdigest(processor.file_path) + + def add_page(page) + @page = page + hash = Digest::MD5.hexdigest(page.source) known_page = known_page(hash) upsert_db(hash, known_page) + end + + def save save_db end def pages - db.fetch(:pages).map do | _, data| + db.fetch(:pages).map do |_, data| PAGE.new(data) end end private - attr_reader :hash, :processor - + attr_reader :hash, :page, :db_path def db @db ||= YAML.load_file( @@ -34,25 +51,19 @@ module Crys end def upsert_db(hash, known_page) - if known_page - record = PAGE.new(known_page) - else - record = PAGE.new( - filename: processor.filename, - file_path: processor.file_path, - in_rss: true - ) - end + record = PAGE.new(known_page || { filename: page.filename, + source: page.source, + created_at: Time.now, + in_rss: true, + in_all: true}) - record.last_update = Time.now + record.last_updated_at = page.last_updated_at @db[PAGES_KEY][hash] = record.to_h end def save_db - File.open(db_path, 'w') do |f| - f.write db.to_yaml - end + File.write(db_path, db.to_yaml) end def known_page(hash) diff --git a/new/lib/crys/processors/common_parts.rb b/new/lib/crys/processors/common_parts.rb deleted file mode 100644 index 4e1dae56..00000000 --- a/new/lib/crys/processors/common_parts.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Crys - module CommonParts - def webbutton(file:, url:, alt:) - full_path = image_dir + "/buttons/" + file - - image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: 88) - assets << image - - code = ERB.new <<-EOF -<a href="<%=url%>"><img src="<%=image.relative_path%>" width="88" height="31" class="webbutton" alt="<%=alt%>"></a> -EOF - - code.result(binding) - - end - - end -end diff --git a/new/lib/crys/processors/html_processor.rb b/new/lib/crys/processors/html_processor.rb index bcc9768b..eccc748b 100644 --- a/new/lib/crys/processors/html_processor.rb +++ b/new/lib/crys/processors/html_processor.rb @@ -1,26 +1,37 @@ -require 'erb' -require_relative "common_functions" -require_relative "common_parts" +# frozen_string_literal: true + +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' module Crys class HtmlProcessor - include CommonFunctions include CommonParts - + def initialize(file_path:, image_dir:) @file_path = file_path @image_dir = image_dir @assets = [] end - def parsed_content - html_file = ERB.new(File.read(file_path)) - html_file.result(local_binding) + def parsed_pages + [ + ProcessedPage.new( + filename: filename, + content: html_content, + source: file_path, + last_updated_at: Time.now + ) + ] end def filename - file_path.scan(/.*\/(.*)\.erb/).flatten.first + file_path.scan(%r{.*/(.*)\.erb}).flatten.first + end + + def html_content + html_file = ERB.new(File.read(file_path)) + html_file.result(local_binding) end attr_reader :assets, :file_path @@ -31,7 +42,7 @@ module Crys attr_writer :assets def local_binding - local_binding ||= binding.clone + @local_binding ||= binding.clone end end end diff --git a/new/lib/crys/processors/common_functions.rb b/new/lib/crys/processors/mixins/common_functions.rb index f33c7681..52ce65ac 100644 --- a/new/lib/crys/processors/common_functions.rb +++ b/new/lib/crys/processors/mixins/common_functions.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + require 'rss' module Crys module CommonFunctions def project_root - "#{File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))))}" + File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))))).to_s end - def process_image(file:, width: :auto) - full_path = image_dir + "/" + file + def process_image(file:, width: :auto) + full_path = "#{image_dir}/#{file}" image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: width) assets << image @@ -19,10 +21,13 @@ module Crys if name path = case name when :main - "/public/index.xml" + '/public/index.xml' end rss = File.read(project_root + path) + else + puts url + end RSS::Parser.parse(rss) diff --git a/new/lib/crys/processors/mixins/common_parts.rb b/new/lib/crys/processors/mixins/common_parts.rb new file mode 100644 index 00000000..a6c2dc16 --- /dev/null +++ b/new/lib/crys/processors/mixins/common_parts.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module Crys + module CommonParts + def webbutton(file:, url:, alt:) + full_path = "#{image_dir}/buttons/#{file}" + + image = ::Crys::ImageProcessor.new(path: full_path, filename: file, width: 88) + assets << image + + code = ERB.new <<~EOF + <a href="<%=url%>"><img src="<%=image.relative_path%>" width="88" height="31" class="webbutton" alt="<%=alt%>"></a> + EOF + + code.result(binding) + end + end +end diff --git a/new/lib/crys/processors/processed_page.rb b/new/lib/crys/processors/processed_page.rb new file mode 100644 index 00000000..8627c10e --- /dev/null +++ b/new/lib/crys/processors/processed_page.rb @@ -0,0 +1,8 @@ +module Crys + ProcessedPage = Struct.new( + :filename, + :content, + :source, + :last_updated_at + ) +end diff --git a/new/lib/crys/processors/rss_processor.rb b/new/lib/crys/processors/rss_processor.rb index bce48ad6..8254bf4a 100644 --- a/new/lib/crys/processors/rss_processor.rb +++ b/new/lib/crys/processors/rss_processor.rb @@ -1,5 +1,7 @@ -require_relative "common_functions" -require_relative "common_parts" +# frozen_string_literal: true + +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' module Crys class RssProcessor @@ -8,10 +10,18 @@ module Crys rss_file = File.read(file_path) instance_eval(rss_file) @assets = [] + @image_dir = image_dir # just for api consistency end - def parsed_content - rss.content + def parsed_pages + [ + ProcessedPage.new( + filename: rss.filename, + content: rss.content, + source: file_path, + last_updated_at: Time.now + ) + ] end def filename @@ -23,6 +33,5 @@ module Crys private attr_reader :rss - end end diff --git a/new/lib/crys/processors/yaml_batch_processor.rb b/new/lib/crys/processors/yaml_batch_processor.rb new file mode 100644 index 00000000..ef0257b8 --- /dev/null +++ b/new/lib/crys/processors/yaml_batch_processor.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "yaml" + +require_relative 'mixins/common_functions' +require_relative 'mixins/common_parts' + +module Crys + class YamlBatchProcessor + def initialize(file_path:, image_dir:) + @file_path = file_path + rss_file = File.read(file_path) + + @assets = [] + @image_dir = image_dir # just for api consistency + end + + def parsed_pages + pages.map do |page| + ProcessedPage.new( + filename: page[:filename], + content: "all your base are belong to us", + source: file_path + "#" + page[:uid].to_s , + last_updated_at: page[:last_updated_at] + ) + end + end + + def filename + rss.filename + end + + attr_reader :assets, :file_path + + private + + def pages + @pages ||= YAML.load_file(file_path, + permitted_classes: [Time, Symbol] + ) + end + + attr_reader :rss + end +end diff --git a/new/lib/crys/server.rb b/new/lib/crys/server.rb index aa0c19c3..cc43b396 100644 --- a/new/lib/crys/server.rb +++ b/new/lib/crys/server.rb @@ -1,4 +1,6 @@ -require 'webrick' +# frozen_string_literal: true + +require 'webrick' module Crys class Server def initialize(root:) @@ -6,16 +8,16 @@ module Crys end def start - server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root - trap 'INT' do server.shutdown end + server = WEBrick::HTTPServer.new Port: 8000, DocumentRoot: root + trap 'INT' do + server.shutdown + end server.start end - private - - attr_reader :root + private + attr_reader :root end end - |