From 04d8f94d30e3615e26b7a67e4133b77b8c391dbe Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:52:23 +0100 Subject: [PATCH 1/6] ci: add 1200 line max --- .github/scripts/max-lines.sh | 57 ++++++++++++++++++++++++++++++++++++ .github/workflows/tests.yml | 13 ++++++++ 2 files changed, 70 insertions(+) create mode 100755 .github/scripts/max-lines.sh diff --git a/.github/scripts/max-lines.sh b/.github/scripts/max-lines.sh new file mode 100755 index 000000000..febd2225e --- /dev/null +++ b/.github/scripts/max-lines.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +# Check if all required arguments are provided +if [ "$#" -ne 3 ]; then + echo "Usage: $0 " + exit 1 +fi + +folder_path="$1" +max_length="$2" +file_globs="$3" + +# Function to get relative path +get_relative_path() { + local path="$1" + local base="$(pwd)" + echo "${path#$base/}" +} + +# Initialize a flag to check if any lines exceed the max length +found_lines=0 + +# Convert comma-separated globs to an array +IFS=',' read -ra glob_array <<< "$file_globs" + +# Use find to get all files matching the glob patterns +for glob in "${glob_array[@]}"; do + while IFS= read -r -d '' file; do + # Get the relative path + relative_path=$(get_relative_path "$file") + + # Use awk to process each file + awk -v max="$max_length" -v file="$relative_path" ' + length($0) > max { + print file ":" NR + found = 1 + exit 1 + } + END { + exit found + }' "$file" + + # Check awk's exit status + if [ $? -eq 1 ]; then + found_lines=1 + fi + done < <(find "$folder_path" -type f -name "$glob" -print0) +done + +# Exit with appropriate code +if [ $found_lines -eq 0 ]; then + echo "All lines are within the $max_length character limit." + exit 0 +else + echo "Some lines exceedded the $max_length character limit." + exit 1 +fi \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 37d870a9b..b724bc60b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -101,3 +101,16 @@ jobs: - name: Lint run: composer lint + + max-lines: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Make script executable + run: chmod +x ./.github/scripts/max-lines.sh + + - name: Check max lines + run: ./.github/scripts/max-lines.sh . 1200 "*.twig" \ No newline at end of file From 36701193eb6d306f9e48475e6eee8d0783e26899 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:00:21 +0100 Subject: [PATCH 2/6] chore: refactor longest line --- templates/dotnet/Package/Models/Model.cs.twig | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/templates/dotnet/Package/Models/Model.cs.twig b/templates/dotnet/Package/Models/Model.cs.twig index 00df5c7a9..e7c69f725 100644 --- a/templates/dotnet/Package/Models/Model.cs.twig +++ b/templates/dotnet/Package/Models/Model.cs.twig @@ -39,13 +39,34 @@ namespace {{ spec.title | caseUcfirst }}.Models } public static {{ definition.name | caseUcfirst | overrideIdentifier}} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier}}( - {%~ for property in definition.properties %} - {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {% if property.sub_schema %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList(){% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject>()!){% endif %}{% else %}{% if property.type == 'array' %}((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>(){% else %}{% if property.type == "integer" or property.type == "number" %}{% if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]){% else %}{% if property.type == "boolean" %}({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"]{% else %}map{% if not property.required %}.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null{% else %}["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString(){% endif %}{% endif %}{% endif %}{% endif %}{% endif %}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} - - {%~ endfor %} - {%~ if definition.additionalProperties %} - data: map - {%~ endif %} + {%- for property in definition.properties -%} + {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {%- if property.sub_schema -%} + {%- if property.type == 'array' -%} + ((JArray)map["{{ property.name }}"]).ToObject>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList() + {%- else -%} + {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject>()!) + {%- endif -%} + {%- else -%} + {%- if property.type == 'array' -%} + ((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>() + {%- else -%} + {%- if property.type == "integer" or property.type == "number" -%} + {%- if not property.required -%}map["{{ property.name }}"] == null ? null : {%- endif -%} + Convert.To{%- if property.type == "integer" -%}Int64{%- else -%}Double{%- endif -%}(map["{{ property.name }}"]) + {%- else -%} + {%- if property.type == "boolean" -%} + ({{ property | typeName }}{%- if not property.required -%}?{%- endif -%})map["{{ property.name }}"] + {%- else -%} + map{%- if not property.required -%}.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null{%- else -%}["{{ property.name }}"]{%- if not property.required -%}?{%- endif -%}.ToString() + {%- endif -%} + {%- endif -%} + {%- endif -%} + {%- endif -%} + {%- if not loop.last or (loop.last and definition.additionalProperties) -%},{%- endif -%} + {%- endfor -%} + {%- if definition.additionalProperties -%} + data: map + {%- endif -%} ); public Dictionary ToMap() => new Dictionary() From 36b8533678f05534a162b8591346ace4b98d07f3 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:05:40 +0100 Subject: [PATCH 3/6] fix: dotnet template --- templates/dotnet/Package/Models/Model.cs.twig | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/templates/dotnet/Package/Models/Model.cs.twig b/templates/dotnet/Package/Models/Model.cs.twig index e7c69f725..86d27bc79 100644 --- a/templates/dotnet/Package/Models/Model.cs.twig +++ b/templates/dotnet/Package/Models/Model.cs.twig @@ -38,35 +38,50 @@ namespace {{ spec.title | caseUcfirst }}.Models {%~ endif %} } - public static {{ definition.name | caseUcfirst | overrideIdentifier}} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier}}( - {%- for property in definition.properties -%} - {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: {%- if property.sub_schema -%} - {%- if property.type == 'array' -%} - ((JArray)map["{{ property.name }}"]).ToObject>>().Select(it => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: it)).ToList() - {%- else -%} - {{property.sub_schema | caseUcfirst | overrideIdentifier}}.From(map: ((JObject)map["{{ property.name }}"]).ToObject>()!) - {%- endif -%} - {%- else -%} - {%- if property.type == 'array' -%} + public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}( + {%- for property in definition.properties %} + {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: + {%- if property.sub_schema %} + {%- if property.type == 'array' %} + ((JArray)map["{{ property.name }}"]) + .ToObject>>() + .Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)) + .ToList() + {%- else %} + {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From( + map: ((JObject)map["{{ property.name }}"]) + .ToObject>()! + ) + {%- endif %} + {%- else %} + {%- if property.type == 'array' %} ((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>() - {%- else -%} - {%- if property.type == "integer" or property.type == "number" -%} - {%- if not property.required -%}map["{{ property.name }}"] == null ? null : {%- endif -%} - Convert.To{%- if property.type == "integer" -%}Int64{%- else -%}Double{%- endif -%}(map["{{ property.name }}"]) - {%- else -%} - {%- if property.type == "boolean" -%} - ({{ property | typeName }}{%- if not property.required -%}?{%- endif -%})map["{{ property.name }}"] - {%- else -%} - map{%- if not property.required -%}.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null{%- else -%}["{{ property.name }}"]{%- if not property.required -%}?{%- endif -%}.ToString() - {%- endif -%} - {%- endif -%} - {%- endif -%} - {%- endif -%} - {%- if not loop.last or (loop.last and definition.additionalProperties) -%},{%- endif -%} - {%- endfor -%} - {%- if definition.additionalProperties -%} + {%- else %} + {%- if property.type == "integer" or property.type == "number" %} + {%- if not property.required %} + map["{{ property.name }}"] == null ? null : + {%- endif %} + Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) + {%- else %} + {%- if property.type == "boolean" %} + ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] + {%- else %} + map + {%- if not property.required %} + .TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? + {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null + {%- else %} + ["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString() + {%- endif %} + {%- endif %} + {%- endif %} + {%- endif %} + {%- endif %} + {%- if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} + {%- endfor %} + {%- if definition.additionalProperties %} data: map - {%- endif -%} + {%- endif %} ); public Dictionary ToMap() => new Dictionary() From d842fcb3772cd61501447feb6ab9d043e92dc10c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:53:42 +0100 Subject: [PATCH 4/6] chore: rename script --- .github/scripts/{max-lines.sh => max-line-length.sh} | 0 .github/workflows/tests.yml | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename .github/scripts/{max-lines.sh => max-line-length.sh} (100%) diff --git a/.github/scripts/max-lines.sh b/.github/scripts/max-line-length.sh similarity index 100% rename from .github/scripts/max-lines.sh rename to .github/scripts/max-line-length.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b724bc60b..f9c6e4a9d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -102,7 +102,7 @@ jobs: - name: Lint run: composer lint - max-lines: + max-line-length: runs-on: ubuntu-latest steps: @@ -110,7 +110,7 @@ jobs: uses: actions/checkout@v4 - name: Make script executable - run: chmod +x ./.github/scripts/max-lines.sh + run: chmod +x ./.github/scripts/max-line-length.sh - name: Check max lines - run: ./.github/scripts/max-lines.sh . 1200 "*.twig" \ No newline at end of file + run: ./.github/scripts/max-line-length.sh . 1200 "*.twig" \ No newline at end of file From 09818193ca416e3a379d83285892c338551ab4d2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:52:31 +0100 Subject: [PATCH 5/6] fix: spacing --- templates/dotnet/Package/Models/Model.cs.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/dotnet/Package/Models/Model.cs.twig b/templates/dotnet/Package/Models/Model.cs.twig index 86d27bc79..a3fccf9b6 100644 --- a/templates/dotnet/Package/Models/Model.cs.twig +++ b/templates/dotnet/Package/Models/Model.cs.twig @@ -67,10 +67,10 @@ namespace {{ spec.title | caseUcfirst }}.Models ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] {%- else %} map - {%- if not property.required %} + {%- if not property.required -%} .TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null - {%- else %} + {%- else -%} ["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString() {%- endif %} {%- endif %} From 2d2c93279302acf67db79d93bcd2fc7f278eed73 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:13:39 +0100 Subject: [PATCH 6/6] fix: spacing --- templates/dotnet/Package/Models/Model.cs.twig | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/templates/dotnet/Package/Models/Model.cs.twig b/templates/dotnet/Package/Models/Model.cs.twig index a3fccf9b6..bf330c23c 100644 --- a/templates/dotnet/Package/Models/Model.cs.twig +++ b/templates/dotnet/Package/Models/Model.cs.twig @@ -39,49 +39,42 @@ namespace {{ spec.title | caseUcfirst }}.Models } public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}( - {%- for property in definition.properties %} - {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: - {%- if property.sub_schema %} - {%- if property.type == 'array' %} + {%~ for property in definition.properties %} + {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}: + {%- if property.sub_schema %} + {%~ if property.type == 'array' %} ((JArray)map["{{ property.name }}"]) - .ToObject>>() - .Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)) - .ToList() + .ToObject>>() + .Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)) + .ToList() {%- else %} {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From( map: ((JObject)map["{{ property.name }}"]) - .ToObject>()! + .ToObject>()! ) {%- endif %} - {%- else %} - {%- if property.type == 'array' %} + {%~ else %} + {%~ if property.type == 'array' %} ((JArray)map["{{ property.name }}"]).ToObject<{{ property | typeName }}>() - {%- else %} - {%- if property.type == "integer" or property.type == "number" %} - {%- if not property.required %} - map["{{ property.name }}"] == null ? null : - {%- endif %} - Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) - {%- else %} - {%- if property.type == "boolean" %} + {%~ else %} + {%~ if property.type == "integer" or property.type == "number" %} + {%~ if not property.required %}map["{{ property.name }}"] == null ? null : {% endif %} Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) + {%~ else %} + {%~ if property.type == "boolean" %} ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] - {%- else %} - map - {%- if not property.required -%} - .TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? - {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null - {%- else -%} - ["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString() - {%- endif %} + {%~ else %} + {%~ if not property.required %}map.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null + {%~ else %} map["{{ property.name }}"].ToString(){% endif %} {%- endif %} - {%- endif %} - {%- endif %} - {%- endif %} - {%- if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} - {%- endfor %} + {%~ endif %} + {%~ endif %} + {%~ endif %} + {%- if not loop.last or (loop.last and definition.additionalProperties) %}, + {%~ endif %} + {%~ endfor %} {%- if definition.additionalProperties %} - data: map - {%- endif %} + , data: map + {%- endif ~%} ); public Dictionary ToMap() => new Dictionary()