<commit_before># Copyright 2016 - Niji
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'gyro/xcdatamodel/parser'
require 'gyro/liquidgen/whitespace_patch'
require 'gyro/liquidgen/filters'
require 'liquid'
require 'pathname'

module Gyro
  module Liquidgen
    # Generates arbitrary output from the input datamodel, using a Liquid template provided by the user
    #
    class Generator

    attr_accessor :params, :output_dir

      # PUBLIC METHODS #######################################################
      def initialize(template_dir, output_dir, params)
        Gyro::Log.title('Generating Model')

        @params = params
        @output_dir = output_dir

        # Define Template path for Liquid file system to use Include Tag
        Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_dir)

        @entity_template = Generator.load_template(template_dir + 'entity.liquid')
        @entity_filename_template = Generator.load_template(template_dir + 'entity_filename.liquid')
        @enum_template = Generator.load_template(template_dir + 'enum.liquid')
        @enum_filename_template = Generator.load_template((template_dir + 'enum_filename.liquid').exist? ? (template_dir + 'enum_filename.liquid') : (template_dir + 'filename.liquid'))
      end

      def generate(xcdatamodel)
        generate_entities(xcdatamodel)
        Gyro::Log.success('Model objects are generated !')
      end

      private ################################################################

      def self.load_template(template_path)
        Gyro::Error.exit_with_error('Bad template directory content ! Your template need to ' + template_path.to_s + ' file') unless template_path.exist?
        template_path_string = template_path.read
        #filename_template_string = filename_template_path.readlines.first - filename.liquid
        Liquid::Template.parse(template_path_string)
      end

      def generate_entities(xcdatamodel) 
        xcdatamodel.to_h['entities'].each do |entity|
          entity_context = { 'params' => @params, 'entity' => entity }
          # Rendering template using entity and params context
          output = render_entity(entity_context)
          # Don't generate empty output
          next if output.delete("\n").empty?

          filename_context = { 'params' => @params, 'name' => entity['name'] }<commit_msg>Log error if filename template contains return line<commit_after># Copyright 2016 - Niji
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'gyro/xcdatamodel/parser'
require 'gyro/liquidgen/whitespace_patch'
require 'gyro/liquidgen/filters'
require 'liquid'
require 'pathname'

module Gyro
  module Liquidgen
    # Generates arbitrary output from the input datamodel, using a Liquid template provided by the user
    #
    class Generator

    attr_accessor :params, :output_dir

      # PUBLIC METHODS #######################################################
      def initialize(template_dir, output_dir, params)
        Gyro::Log.title('Generating Model')

        @params = params
        @output_dir = output_dir

        # Define Template path for Liquid file system to use Include Tag
        Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_dir)

        @entity_template = Generator.load_template(template_dir + 'entity.liquid', false)
        @entity_filename_template = Generator.load_template(template_dir + 'entity_filename.liquid', true)
        @enum_template = Generator.load_template(template_dir + 'enum.liquid', false)
        @enum_filename_template = Generator.load_template((template_dir + 'enum_filename.liquid').exist? ? (template_dir + 'enum_filename.liquid') : (template_dir + 'filename.liquid'), true)
      end

      def generate(xcdatamodel)
        generate_entities(xcdatamodel)
        Gyro::Log.success('Model objects are generated !')
      end

      private ################################################################

      def self.load_template(template_path, preventReturnLine)
        Gyro::Error.exit_with_error('Bad template directory content ! Your template need to ' + template_path.to_s + ' file') unless template_path.exist?
        template_path_string = template_path.read
        Gyro::Log.error('The given template ' + template_path.to_s + ' contains return line(s). This can lead to side effets.') unless !(preventReturnLine && template_path_string.include?("\n"))
        Liquid::Template.parse(template_path_string)
      end

      def generate_entities(xcdatamodel) 
        xcdatamodel.to_h['entities'].each do |entity|
          entity_context = { 'params' => @params, 'entity' => entity }
          # Rendering template using entity and params context
          output = render_entity(entity_context)
          # Don't generate empty output
          next if output.delete("\n").empty?

          filename_context = { 'params' => @params, 'name' => entity['name'] }
{'fraction_non_alphanumeric': 0.12586585571887143, 'fraction_numerical': 0.0027031593174522725, 'mean_word_length': 3.809098294069862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2207195', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1550, 'n_words': 463}