#!/usr/bin/env ruby # frozen_string_literal: true require 'fileutils' require 'securerandom' require 'digest/sha2' require 'pathname' require 'opal' require 'parser' require 'uglifier' require 'rubygems' require 'gems' # utils def modify_and_copy_file(source_path, destination_path, destination_dir) file_content = File.read(source_path) cleaned_content = file_content.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') modified_code = cleaned_content.gsub(/require\s+(['"])(.*?)\1/) do |match| quote = $1 relative_path = $2 if relative_path.start_with?('./') relative_path_copy = "#{relative_path}" relative_path_copy.slice!(0, 2) destination_dir_copy = "#{destination_dir}" destination_dir_copy.slice!(0, 2) "require #{quote}#{relative_path_copy}#{quote}" else match end end File.write(destination_path, modified_code) end def process_directories(source_dir, destination_dir) Dir.entries(source_dir).each do |entry| next if entry == '.' || entry == '..' source_path = File.join(source_dir, entry) destination_path = File.join(destination_dir, entry) if File.directory?(source_path) Dir.mkdir(destination_path) unless Dir.exist?(destination_path) process_directories(source_path, destination_path) elsif File.file?(source_path) modify_and_copy_file(source_path, destination_path, destination_dir) end end end # builders def build_aui(destination, project_name) path = "#{destination}/#{project_name}/src/utilities/" FileUtils.mkdir_p(path) unless Dir.exist?(path) uuid = SecureRandom.uuid sha = Digest::SHA256.hexdigest(uuid) coded_id = sha.gsub('-', '_') aui_file = <<~STR class Atome def self.aui "#{coded_id}" end end STR File.new("#{path}aui.rb", 'w') File.open("#{path}aui.rb", 'w') do |f| f.puts aui_file end # now building opal version js_directory = "#{destination}/#{project_name}/src/js" opal_js = "#{js_directory}/aui.js" FileUtils.mkdir_p(js_directory) File.new opal_js, 'w' # we add platform_specific script to the Opal framework opal_content = Opal::Builder.build("#{path}aui.rb").to_s File.open(opal_js, 'w') do |f| f.puts opal_content end end def build_host_mode(destination, project_name, mode) path = "#{destination}/#{project_name}/src/utilities/" FileUtils.mkdir_p(path) unless Dir.exist?(path) host_type = <<~STR class Atome def self.host "#{mode}" end end STR File.new("#{path}host_mode.rb", 'w') File.open("#{path}host_mode.rb", 'w') do |f| f.puts host_type end # now building opal version js_directory = "#{destination}/#{project_name}/src/js" opal_js = "#{js_directory}/host_mode.js" File.new opal_js, 'w' # we add platform_specific script to the Opal framework opal_content = Opal::Builder.build("#{path}host_mode.rb").to_s File.open(opal_js, 'w') do |f| f.puts opal_content end end # opal compilation def build_opal_application(_source, destination, project_name) # first we need to remplace all './require ' with require so opal can parse the path Dir.mkdir("#{destination}/#{project_name}/tmp") unless Dir.exist?("#{destination}/#{project_name}/tmp") unless Dir.exist?("#{destination}/#{project_name}/tmp/application") Dir.mkdir("#{destination}/#{project_name}/tmp/application") end process_directories("#{destination}/#{project_name}/application", "#{destination}/#{project_name}/tmp/application") opal_compiler_content = <