Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Gen liquid templates #16

Merged
merged 80 commits into from
May 30, 2017
Merged
Changes from 1 commit
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
59574e6
refact gyro to generate json and create liquid script to generate mod…
StevenWatremez Apr 12, 2017
2852062
change spec for better visualisation
StevenWatremez Apr 12, 2017
c794367
manage templates and file system
StevenWatremez Apr 12, 2017
0fe2bd6
integrate liquid generator inside gyro
StevenWatremez Apr 13, 2017
4f766c0
manage comments
StevenWatremez Apr 13, 2017
c6df48d
improve loop to write non empty output
StevenWatremez Apr 13, 2017
460eff2
add --param option for gyro cli
StevenWatremez Apr 13, 2017
fde6cd2
Manage default template with -t
StevenWatremez Apr 13, 2017
024fdf2
improve spec tests with array
StevenWatremez Apr 13, 2017
9806371
add rspec for liquid generation
StevenWatremez Apr 14, 2017
7ada300
add realm test for liquid template.
StevenWatremez Apr 15, 2017
6aadca7
manage fixtures with 2 spaces for indentations
StevenWatremez Apr 16, 2017
ed46bac
improve gyro cli and error messages
StevenWatremez Apr 17, 2017
6704dd8
manage json_key_path for attributes/relationships/enums
StevenWatremez Apr 17, 2017
7d89bf0
Revert "manage json_key_path for attributes/relationships/enums"
StevenWatremez Apr 17, 2017
9383bca
add object mapper and Decodable template
StevenWatremez Apr 17, 2017
b85d512
improve gyro --template command line treatment
StevenWatremez Apr 18, 2017
3f970ae
manage swift3 templates with Digiposte+ project
StevenWatremez Apr 18, 2017
3f39574
update object mapper template to match with existing generation
StevenWatremez Apr 18, 2017
f15e927
re add android command line
StevenWatremez Apr 18, 2017
9e692c9
fix bad behaviour for android generation and clean code inside gyro b…
StevenWatremez Apr 18, 2017
4dd1c32
update fixtures for liquid TU
StevenWatremez Apr 20, 2017
b688c89
remove unused code
StevenWatremez Apr 20, 2017
148500e
rename templates
StevenWatremez Apr 20, 2017
a7e84eb
fix bad condition for json key
StevenWatremez Apr 20, 2017
6c5104e
remove useless type interpolation
StevenWatremez Apr 20, 2017
97ba058
add wip java template
StevenWatremez Apr 26, 2017
58c4fe0
finalize java template
StevenWatremez Apr 26, 2017
e19bdf7
improve android template. wip.
StevenWatremez Apr 28, 2017
dedab03
manage gemspec and gemfile
StevenWatremez May 2, 2017
4cca27b
fix gemspec error
StevenWatremez May 2, 2017
7cd5ff1
Fixed gem dependencies
May 2, 2017
b865016
Add patch for whitespace support in Liquid 3 :)
AliSoftware May 2, 2017
5f38367
Fix the algorithm searching for templates in the data/templates dir
AliSoftware May 2, 2017
bee9d04
Generate a clean error when the output dir does not exist (instead of…
AliSoftware May 2, 2017
02fb999
[rubocop] Replace License Block-Comments with standard comments
AliSoftware May 2, 2017
31e2d0b
[rubocop] Additional style fixes
AliSoftware May 2, 2017
c1d565b
[Rubocop] Fix alias_method
AliSoftware May 2, 2017
1d28c7f
[Rubocop] Style/AlignHash rule + Style/BracesAroundHashParameters
AliSoftware May 2, 2017
5d3b970
[rubocop] Use `delete` or `tr` instead of `gsub` ; + lines too long
AliSoftware May 2, 2017
7ff2939
[rubocop] And/Or style
AliSoftware May 2, 2017
b3759a3
[rubocop] case indentation
AliSoftware May 2, 2017
65d49ee
[Rubocop] Style/ColonMethodCall
AliSoftware May 2, 2017
5dd6ceb
[Rubocop] Style/ConditionalAssignment
AliSoftware May 2, 2017
5e2b8af
[Rubocop] Style/DotPosition
AliSoftware May 2, 2017
ebe1040
[Rubocop] Style/EmptyLineBetweenDefs + Style/EmptyLines
AliSoftware May 2, 2017
aa8a8e2
[Rubocop] Style: Empty Lines
AliSoftware May 2, 2017
268cf60
[Rubocop] Style/EmptyLiteral
AliSoftware May 2, 2017
1adf222
[rubocop] Indentation
AliSoftware May 2, 2017
4a33e86
[rubocop] freeze constants
AliSoftware May 2, 2017
c3e6e12
[rubocop] avoid semicolons
AliSoftware May 2, 2017
002be8b
[rubocop] Fix spaces
AliSoftware May 2, 2017
235e45f
Remove $LOAD_PATH.unshft(lib) in spec_helper
AliSoftware May 2, 2017
829fd91
[Rubocop] Style/StringLiterals
AliSoftware May 2, 2017
ced3d50
[Rubocop] Style/TrailingBlankLines + Style/TrailingWhitespace
AliSoftware May 2, 2017
c65c9ec
[rubocop] Unneeded %q & Proc/block
AliSoftware May 2, 2017
28e674e
[rubocop] Naming + Array literals
AliSoftware May 2, 2017
60784e5
[Rubocop] Replace unless…else with if…else inverted logic
AliSoftware May 2, 2017
16b440c
Fix "has_number_attribute?" for readability
AliSoftware May 2, 2017
75c51e4
[rubocop] Use `next` when appropriate instead of big `if` blocks
AliSoftware May 2, 2017
2c88cfe
[Rubocop] Useless return
AliSoftware May 2, 2017
f6b2afb
Methods & Modules Documentation
AliSoftware May 2, 2017
6ca48e0
Clean-up Code Style using the `rubocop` tool
AliSoftware May 2, 2017
6d66999
manage templates to match with whitespace support workaround
StevenWatremez May 7, 2017
8ca157a
manage and test androi templates with SmartAssistan
StevenWatremez May 7, 2017
aae2052
move filters inside a specific file
StevenWatremez May 7, 2017
7dafa9b
refact initializer methods for LiquidGen
StevenWatremez May 7, 2017
786946a
improve documentation definition inside gemspec
StevenWatremez May 7, 2017
1e8551b
move templates from data to lib folder
StevenWatremez May 7, 2017
ad38c6b
refact methods for LiquidGen file
StevenWatremez May 7, 2017
c5d2e8d
delete android cli
StevenWatremez May 7, 2017
8da65ed
remove require for android gen and remove unsuse cli options
StevenWatremez May 7, 2017
b23952d
improve android templates
StevenWatremez May 14, 2017
94c225e
implement code review feedbacks
StevenWatremez May 14, 2017
a15b405
manage fixtures
StevenWatremez May 23, 2017
d701d87
Fix Android UnitTests
TomTom-Fr May 29, 2017
0563733
Add “\v” and liquid patch
TomTom-Fr May 29, 2017
738c5ad
Add second ignore attribut for TU
TomTom-Fr May 29, 2017
9b01178
Rename primitive with isPrimitives
TomTom-Fr May 30, 2017
aff6099
Use “|” separator (previous : “,”)
TomTom-Fr May 30, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 50 additions & 40 deletions lib/gyro/liquidgen/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,81 +24,56 @@ module Liquidgen
#
class Generator

attr_accessor :root_template, :filename_template, :params, :output_dir
attr_accessor :enum_filename_template, :enum_filename_template_path, :enum_template_path, :enum_template
attr_accessor :params, :output_dir, :template_dir

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

@params = params
@output_dir = output_dir

# Parse object template name
root_template_path = template_dir + 'root.liquid'
Gyro::Error.exit_with_error('Bad template directory content ! Your template need to include root.liquid file') unless root_template_path.exist?
root_template_string = root_template_path.read
@root_template = Liquid::Template.parse(root_template_string)
@template_dir = template_dir

# Define Template path for Liquid file system to use Include Tag
Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_dir)
# Parse object template
filename_template_path = (template_dir + 'filename.liquid')
Gyro::Error.exit_with_error('Bad template directory content ! Your template need to include filename.liquid file') unless filename_template_path.exist?
filename_template_string = filename_template_path.readlines.first
@filename_template = Liquid::Template.parse(filename_template_string)

# Parse enum template
@enum_template_path = (template_dir + 'enum.liquid')

enum_template_string = enum_template_path.read
@enum_template = Liquid::Template.parse(enum_template_string)

# Parse enum template name
@enum_filename_template_path = (template_dir + 'enum_filename.liquid').exist? ? (template_dir + 'enum_filename.liquid') : (template_dir + 'filename.liquid')
enum_filename_template_string = enum_filename_template_path.readlines.first
@enum_filename_template = Liquid::Template.parse(enum_filename_template_string)
end

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

def generate(xcdatamodel)
private ################################################################

def generate_root_template(xcdatamodel)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather rename that generate_entities_files or generate_entities_from_root_template, as it generates files from templates, it doesn't generate the root template itself.

xcdatamodel.to_h['entities'].each do |entity|
entity_context = { 'params' => @params, 'entity' => entity }
# Rendering template using entity and params context
output = @root_template.render(entity_context, filters: [CustomFilters])
.gsub(/^ +$/, '')
output = root_template_rendering(entity_context)
# Don't generate empty output
next if output.delete("\n").empty?

filename_context = { 'params' => @params, 'name' => entity['name'] }
# Rendering filename template using entity name and params context
filename = @filename_template.render(filename_context).chomp
filename = filename_rendering(filename_context)
Gyro::Log.success("#{filename} is created !")
# Write model object
Gyro.write_file_with_name(@output_dir, filename, output)
# Generate model object enums
generate_enums(entity['attributes'])
end

Gyro::Log.success('Model objects are generated !')
end

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


def generate_enums(attributes)
enums = []
attributes.each do |attribute|
enum_type = attribute['enum_type']
next unless !enums.include?(enum_type) && !enum_type.empty?
enums.push(enum_type)
next unless @enum_template_path.exist?

enum_context = { 'params' => @params, 'attribute' => attribute }
# Rendering enum template using attribute and params context
output = @enum_template.render(enum_context, filters: [CustomFilters])
.gsub(/^ +$/, '')
output = enum_template_rendering(enum_context)
# Don't generate empty output
next if output.delete("\n").empty?

Expand All @@ -107,14 +82,49 @@ def generate_enums(attributes)
end

def generate_enum(enum_name, output)

Gyro::Error.exit_with_error('Bad template directory content ! Your template need to have enum_filename.liquid or filename.liquid file !') unless @enum_filename_template_path.exist?
# Rendering enum filename template using enum name and params context
enum_filename_context = { 'params' => @params, 'name' => enum_name }
enum_filename = @enum_filename_template.render(enum_filename_context).chomp

enum_filename = enum_filename_template_rendering(enum_filename_context)
Gyro.write_file_with_name(@output_dir, enum_filename, output)
end

def root_template_rendering(context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename that render_root_template(context)?

# Parse object template
root_template_path = @template_dir + 'root.liquid'
Gyro::Error.exit_with_error('Bad template directory content ! Your template need to include root.liquid file') unless root_template_path.exist?
root_template_string = root_template_path.read
root_template = Liquid::Template.parse(root_template_string)
root_template.render(context, filters: [CustomFilters])
.gsub(/^ +$/, '')
end

def filename_rendering(context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename that render_filename(context)?

# Parse object template name
filename_template_path = (@template_dir + 'filename.liquid')
Gyro::Error.exit_with_error('Bad template directory content ! Your template need to include filename.liquid file') unless filename_template_path.exist?
filename_template_string = filename_template_path.readlines.first
filename_template = Liquid::Template.parse(filename_template_string)
filename_template.render(context).chomp
end

def enum_template_rendering(context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename that render_enum(context)?

# Parse enum template
enum_template_path = (@template_dir + 'enum.liquid')
Gyro::Error.exit_with_error('Bad template directory content ! Your template need to have enum.liquid file !') unless enum_template_path.exist?
enum_template_string = enum_template_path.read
enum_template = Liquid::Template.parse(enum_template_string)
enum_template.render(context, filters: [CustomFilters])
.gsub(/^ +$/, '')
end

def enum_filename_template_rendering(context)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename that render_enum_filename?

# Parse enum template name
enum_filename_template_path = (@template_dir + 'enum_filename.liquid').exist? ? (@template_dir + 'enum_filename.liquid') : (@template_dir + 'filename.liquid')
Gyro::Error.exit_with_error('Bad template directory content ! Your template need to have enum_filename.liquid or filename.liquid file !') unless enum_filename_template_path.exist?
enum_filename_template_string = enum_filename_template_path.readlines.first
enum_filename_template = Liquid::Template.parse(enum_filename_template_string)
enum_filename_template.render(context).chomp
end
end
end
end