Class: Confinement::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/confinement.rb

Constant Summary collapse

PARCEL_FILES_OUTPUT_REGEX =
/^✨[^\n]+\n\n(.*)Done in(?:.*)\z/m
PARCEL_FILE_OUTPUT_REGEX =
/^(?<page>.*?)\s+(?<size>[0-9\.]+\s*[A-Z]?B)\s+(?<time>[0-9\.]+[a-z]?s)$/

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Compiler

Returns a new instance of Compiler.



633
634
635
636
# File 'lib/confinement.rb', line 633

def initialize(config)
  @config = config
  @logger = config.logger
end

Instance Method Details

#compile_assets(site) ⇒ Object



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/confinement.rb', line 647

def compile_assets(site)
  @logger.info { "compiling assets" }
  create_destination_directory
  asset_files = site.asset_blobs.send(:lookup)
  asset_paths = asset_files.values

  command = [
    "yarn",
    "run",
    "parcel",
    "build",
    "--no-cache",
    "--dist-dir", @config.compiler.output_assets_path.to_s,
    "--public-url", @config.compiler.output_assets_path.basename.to_s,
    *asset_paths.select(&:entrypoint?).map(&:input_path).map(&:to_s)
  ]

  @logger.debug { "running: #{command.join(" ")}" }

  out, status = Open3.capture2(*command)

  if !status.success?
    @logger.fatal { "asset compilation failed" }
    raise "Asset compilation failed"
  end

  matches = PARCEL_FILES_OUTPUT_REGEX.match(out)[1]

  if !matches
    @logger.fatal { "asset compilation ouptut parsing failed" }
    raise "Asset compilation output parsing failed"
  end

  processed_file_paths = matches.split("\n\n")

  processed_file_paths.map do |file|
    output_file, *input_files = file.strip.split(/\n(?:└|├)── /)

    output_path = @config.root.concat(output_file[PARCEL_FILE_OUTPUT_REGEX, 1])

    input_files.each do |input_file|
      input_path = @config.root.concat(input_file[PARCEL_FILE_OUTPUT_REGEX, 1])

      if !asset_files.key?(input_path)
        next
      end

      url_path = output_path.relative_path_from(@config.compiler.output_root_path)
      @logger.debug { "processesd asset: #{input_path}, #{url_path}, #{output_path}" }
      asset_files[input_path].url_path = url_path.to_s
      asset_files[input_path].output_path = output_path
      asset_files[input_path].body = output_path.read
    end
  end

  @logger.info { "finished compiling assets" }
end

#compile_contents(site) ⇒ Object



705
706
707
708
709
710
711
712
713
# File 'lib/confinement.rb', line 705

def compile_contents(site)
  @logger.info { "compiling contents" }
  create_destination_directory
  contents = site.route_identifiers.send(:lookup).values
  contents.each do |content|
    compile_content(site, content)
  end
  @logger.info { "finished compiling contents" }
end

#compile_everything(site) ⇒ Object



638
639
640
641
642
# File 'lib/confinement.rb', line 638

def compile_everything(site)
  # Assets first since it's almost always a dependency of contents
  compile_assets(site)
  compile_contents(site)
end

#partial_compilation_dirty?(before:, after:) ⇒ Boolean

Returns:

  • (Boolean)


715
716
717
718
719
720
721
722
723
724
725
726
727
# File 'lib/confinement.rb', line 715

def partial_compilation_dirty?(before:, after:)
  return true if !before.key?(:asset_blobs)
  return true if !after.key?(:asset_blobs)

  before_assets = before[:asset_blobs].send(:lookup)
  after_assets = after[:asset_blobs].send(:lookup)

  return true if before_assets.keys.sort != after_assets.keys.sort
  return true if before_assets.any? { |k, v| v.input_path != after_assets[k].input_path }
  return true if before_assets.any? { |k, v| v.entrypoint? != after_assets[k].entrypoint? }

  false
end