#!/usr/bin/env ruby # frozen_string_literal: true require 'fileutils' require 'securerandom' require 'digest/sha2' # below are the methods needed for atome creation and run def test_path(project_name, arg_name, path) if project_name != 'run' && project_name != 'guard' && !project_name.nil? && project_name != 'port:' && project_name != 'server' && project_name != 'build' # the line below remove the optional parameters from the project name project_name = project_name.sub(ARGV[ARGV.find_index(arg_name) + 1], '') "#{path}/#{project_name}" end path end def found_path(arg_name) project_name = ARGV[ARGV.find_index(arg_name) + 1] path = `pwd`.chomp if ARGV[ARGV.find_index(arg_name) + 1] == 'production' app_builder_helpers = File.join(File.dirname(__FILE__), '../app_builder_helpers') `cd #{app_builder_helpers};rake minimizer[#{path}]` end path = `pwd`.chomp test_path(project_name, arg_name, path) end def delete_all(dir, user_application = nil) Dir.foreach(dir) do |e| next if %w[. ..].include? e full_name = dir + File::Separator + e if FileTest.directory?(full_name) delete_all(full_name, user_application) if user_application && dir != 'application' else File.delete(full_name) end end Dir.delete(dir) if user_application && dir != 'application' end def build_app(path, production) # now run the rake task to build the needed libraries (atome, renderers, etc...) app_builder_helpers = File.join(File.dirname(__FILE__), '../app_builder_helpers') # now run the rake task to compile user code to javascript `cd #{app_builder_helpers};rake build_user_code[#{path},#{production}]` end def content_to_add(coded_id) <<~STR class Atome def self.aui "#{coded_id}" end end STR end def app_creation(content_to_add, current_path, project_name) added_content = content_to_add file_location = "#{current_path}/#{project_name}/aui.rb" File.new file_location, 'w' File.open(file_location, 'w') do |f| f.puts added_content end end def test_app_creation(project_name, current_path, force) new_app_creation = false unless force && File.directory?(project_name) new_app_creation = true Dir.mkdir project_name # now lets create the application identity uuid = SecureRandom.uuid sha = Digest::SHA256.hexdigest(uuid) coded_id = sha.gsub('-', '_') app_creation(content_to_add(coded_id), current_path, project_name) end new_app_creation end def build_atome_resources(current_path, project_name, production) gem_assets_location = File.join(File.dirname(__FILE__), '../vendor/assets/') app_builder_helpers = File.join(File.dirname(__FILE__), '../app_builder_helpers') target_template_location = "#{current_path}/#{project_name}" Dir.entries(gem_assets_location).select do |entry| if File.join(entry) && !%w[. ..].include?(entry) entry = "#{File.dirname(__FILE__)}/../vendor/assets/#{entry}" FileUtils.cp_r entry, target_template_location end end # now run the rake task to build the needed libraries (atome, renderers, etc...) `cd #{app_builder_helpers};rake system_builder[#{current_path}/#{project_name},#{production}]` end def create_application(project_name, force, production) # print "directory already exist, use force to overwrite" current_path = `pwd`.chomp new_app_creation = test_app_creation(project_name, current_path, force) build_atome_resources(current_path, project_name, production) return unless new_app_creation # now adding name to the app index_html = "#{current_path}/#{project_name}/src/index.html" index_html_content = File.open(index_html).read index_html_content = index_html_content.gsub('