Skip to content

Commit

Permalink
Prepare multitarget setup (#126)
Browse files Browse the repository at this point in the history
* prepare multitarget seupt

* remove forgottent line

* revert argument parser

* adjust readme

* fix SkipLast function

* adjust * from link + typo fix

Co-authored-by: Martin Ševčík <[email protected]>
  • Loading branch information
Simply007 and Sevitas authored Jan 13, 2022
1 parent a1b0278 commit f165949
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.x
dotnet-version: |
5.0.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
33 changes: 26 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,46 @@ on:
types: [published]

jobs:
build:
test:
name: Build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
5.0.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore --configuration Release /p:ContinuousIntegrationBuild=true
- name: Test
run: dotnet test --no-build --verbosity normal --configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Codecov
uses: codecov/codecov-action@v1

release:
name: Build and release
needs: test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.x
dotnet-version: |
5.0.x
6.0.x
- name: Extract version from tag
id: get_version
uses: battila7/get-version-action@v2
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore --configuration Release /p:ContinuousIntegrationBuild=true /p:Version="${{ steps.get_version.outputs.version-without-v }}"
- name: Test
run: dotnet test --no-build --verbosity normal --configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
- name: Codecov
uses: codecov/codecov-action@v1
- name: Pack
run: dotnet pack --no-build --verbosity normal --configuration Release --output ./artifacts /p:PackageVersion="${{ steps.get_version.outputs.version-without-v }}"
- name: Build self-contained apps for Windows, Linux, and macOS # Needs to run after dotnet-pack because it performs another build
Expand All @@ -45,4 +64,4 @@ jobs:
with:
args: ./artifacts
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67 changes: 40 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
[![Build & Test](https://github.com/Kentico/kontent-generators-net/actions/workflows/integrate.yml/badge.svg)](https://github.com/Kentico/kontent-generators-net/actions/workflows/integrate.yml)
[![codecov](https://codecov.io/gh/Kentico/kontent-generators-net/branch/master/graph/badge.svg?token=9LvfJ7m8gT)](https://codecov.io/gh/Kentico/kontent-generators-net)
[![NuGet](https://img.shields.io/nuget/v/Kentico.Kontent.ModelGenerator.svg)](https://www.nuget.org/packages/Kentico.Kontent.ModelGenerator)
[![NuGet](https://img.shields.io/nuget/dt/Kentico.Kontent.ModelGenerator.svg)](https://www.nuget.org/packages/Kentico.Kontent.ModelGenerator)
[![Stack Overflow](https://img.shields.io/badge/Stack%20Overflow-ASK%20NOW-FE7A16.svg?logo=stackoverflow&logoColor=white)](https://stackoverflow.com/tags/kentico-kontent)
[![Discord](https://img.shields.io/discord/821885171984891914?color=%237289DA&label=Kontent%20Discord&logo=discord)](https://discord.gg/SKCxwPtevJ)

| Packages | Version | Downloads | Compatibility | Documentation |
| ------------------------------ | :---------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------: | :-------------------: |
| Kentico.Kontent.ModelGenerator | [![NuGet](https://img.shields.io/nuget/vpre/Kentico.Kontent.ModelGenerator.svg)](https://www.nuget.org/packages/Kentico.Kontent.ModelGenerator) | [![NuGet](https://img.shields.io/nuget/dt/Kentico.Kontent.ModelGenerator.svg)](https://www.nuget.org/packages/Kentico.Kontent.ModelGenerator) | [`net6.0`](https://dotnet.microsoft.com/download/dotnet/6.0) [`netstandard2.0`](https://docs.microsoft.com/en-us/dotnet/standard/net-standard)\* | [📖 Wiki](../../wiki) |

> \* We highly recommend targeting [`net6.0`](https://dotnet.microsoft.com/download/dotnet/6.0) in your projects. [`netstandard2.0`](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) is supported to allow older projects to iteratively upgrade.
# Kontent model generator utility for .NET

This utility generates strongly-typed (POCO) models based on [Content Types](https://docs.kontent.ai/tutorials/set-up-projects/define-content-models/creating-and-deleting-content-types) in a [Kontent by Kentico](https://kontent.ai) project. You can choose one of the following:

- [Generate models compatible with the Kontent Delivery SDK for .NET](#how-to-use-for-delivery-sdk)
- [Generate models compatible with the Kontent Management SDK for .NET](#how-to-use-for-management-sdk).
- [Generate models compatible with the Kontent Management SDK for .NET](#how-to-use-for-management-sdk).

## How to use for [Delivery SDK](https://github.com/Kentico/kontent-delivery-sdk-net)

To fully understand all benefits of this approach, please read the [documentation](https://github.com/Kentico/kontent-delivery-sdk-net/wiki/Working-with-strongly-typed-models#customizing-the-strong-type-binding-logic).

### .NET Tool

The recommended way of obtaining this tool is installing it as a [.NET Tool](https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools). You can install it as a global tool or per project as a local tool.

**Global Tool**
- `dotnet tool install -g Kentico.Kontent.ModelGenerator`

- `dotnet tool install -g Kentico.Kontent.ModelGenerator`
- `KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]`

**Local Tool**
Expand All @@ -27,6 +36,7 @@ The recommended way of obtaining this tool is installing it as a [.NET Tool](htt
- `dotnet tool run KontentModelGenerator --projectid "<projectid>" [--namespace "<custom-namespace>"] [--outputdir "<output-directory>"] [--withtypeprovider <True|False>] [--structuredmodel <True|False>] [--filenamesuffix "<suffix>"]`

### Standalone apps for Windows 🗔, Linux 🐧, macOS 🍎

[Self-contained apps](https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained) are an ideal choice for machines without any version of .NET installed.

Latest release: [Download](https://github.com/Kentico/kontent-generators-net/releases/latest)
Expand All @@ -37,24 +47,27 @@ To learn how to generate executables for your favorite target platform, follow t

### Parameters

| Short key | Long key | Required | Default value | Description |
| --------------------- |:---------:|:---------:|:--------------:|:-----------:|
| `-p` | `--projectid` | True | `null` | A GUID that can be found in [Kontent](https://app.kontent.ai) -> API keys -> Project ID |
| `-n` | `--namespace` | False | `KenticoKontentModels` | A name of the [C# namespace](https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx) |
| `-o` | `--outputdir` | False | `\.` | An output folder path |
| `-g` | `--generatepartials` | False | `true` | Generates partial classes for customization. Partial classes are the best practice for customization so the recommended value is `true`. |
| `-t` | `--withtypeprovider` | False | `true` | Indicates whether the `CustomTypeProvider` class should be generated (see [Customizing the strong-type binding logic](https://github.com/Kentico/kontent-delivery-sdk-net/wiki/Working-with-Strongly-Typed-Models#customizing-the-strong-type-binding-logic) for more info) |
| `-s` | `--structuredmodel` | False | `false` | Generates `IRichTextContent` instead of `string` for rich-text elements. This enables utilizing [structured rich-text rendering](https://github.com/Kentico/delivery-sdk-net/wiki/Structured-Rich-text-rendering) |
| `-f` | `--filenamesuffix` | False | `null` | Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs) |
| `-b` | `--baseclass` | False | `null` | If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes |
| Short key | Long key | Required | Default value | Description |
| --------- | :------------------: | :------: | :--------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| `-p` | `--projectid` | True | `null` | A GUID that can be found in [Kontent](https://app.kontent.ai) -> API keys -> Project ID |
| `-n` | `--namespace` | False | `KenticoKontentModels` | A name of the [C# namespace](https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx) |
| `-o` | `--outputdir` | False | `\.` | An output folder path |
| `-g` | `--generatepartials` | False | `true` | Generates partial classes for customization. Partial classes are the best practice for customization so the recommended value is `true`. |
| `-t` | `--withtypeprovider` | False | `true` | Indicates whether the `CustomTypeProvider` class should be generated (see [Customizing the strong-type binding logic](https://github.com/Kentico/kontent-delivery-sdk-net/wiki/Working-with-Strongly-Typed-Models#customizing-the-strong-type-binding-logic) for more info) |
| `-s` | `--structuredmodel` | False | `false` | Generates `IRichTextContent` instead of `string` for rich-text elements. This enables utilizing [structured rich-text rendering](https://github.com/Kentico/delivery-sdk-net/wiki/Structured-Rich-text-rendering) |
| `-f` | `--filenamesuffix` | False | `null` | Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs) |
| `-b` | `--baseclass` | False | `null` | If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes |

### CLI Syntax

Short keys such as `-s true` are interchangable with the long keys `--structuredmodel true`. Other possible syntax is `-s=true` or `--structuredmodel=true`. Parameter values are case-insensitive, so you can use both `-s=true` and `-s=True`. To see all aspects of the syntax, see the [MS docs](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.commandlineconfigurationextensions.addcommandline).

### Config file

These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.

### Advanced configuration (Preview API, Secure API)

There are two ways of configuring advanced Delivery SDK options (such as secure API access, preview API access, and [others](https://github.com/Kentico/kontent-delivery-sdk-net/blob/master/Kentico.Kontent.Delivery/Configuration/DeliveryOptions.cs)):

1. Command-line arguments `--DeliveryOptions:UseSecureAccess true` ([syntax](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.commandlineconfigurationextensions.addcommandline))
Expand Down Expand Up @@ -107,8 +120,8 @@ public partial class Home
{
// Allowed Content Types == "Article"
public IEnumerable<Article> Articles => LinkedContentItems.OfType<Article>();
// Allowed Content Types == "Article" && Limit number of items == 1

// Allowed Content Types == "Article" && Limit number of items == 1
public Article Article => LinkedContentItems.OfType<Article>().FirstOrDefault();
}
```
Expand All @@ -123,18 +136,17 @@ KontentModelGenerator.exe --projectid "<projectid>" --contentmanagementapi true

### Parameters

| Short key | Long key | Required | Default value | Description |
| --------------------- |:---------:|:---------:|:--------------:|:-----------:|
| `-p` | `--projectid` | True | `null` | A GUID that can be found in [Kontent](https://app.kontent.ai) -> API keys -> Project ID |
| `-c` | `--contentmanagementapi` | True | `false` | Indicates that models should be generated for [Content Management SDK](https://github.com/Kentico/content-management-sdk-net) |
| `-n` | `--namespace` | False | `KenticoKontentModels` | A name of the [C# namespace](https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx) |
| `-o` | `--outputdir` | False | `\.` | An output folder path |
| `-f` | `--filenamesuffix` | False | `null` | Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs) |
| `-b` | `--baseclass` | False | `null` | If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes |
| Short key | Long key | Required | Default value | Description |
| --------- | :----------------------: | :------: | :--------------------: | :------------------------------------------------------------------------------------------------------------------------------------: |
| `-p` | `--projectid` | True | `null` | A GUID that can be found in [Kontent](https://app.kontent.ai) -> API keys -> Project ID |
| `-c` | `--contentmanagementapi` | True | `false` | Indicates that models should be generated for [Content Management SDK](https://github.com/Kentico/content-management-sdk-net) |
| `-n` | `--namespace` | False | `KenticoKontentModels` | A name of the [C# namespace](https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx) |
| `-o` | `--outputdir` | False | `\.` | An output folder path |
| `-f` | `--filenamesuffix` | False | `null` | Adds a suffix to generated filenames (e.g., News.cs becomes News.Generated.cs) |
| `-b` | `--baseclass` | False | `null` | If provided, a base class type will be created and all generated classes will derive from that base class via partial extender classes |

These parameters can also be set via the appSettings.json file located in the same directory as the executable file. Command-line parameters always take precedence.


### Example output

```csharp
Expand Down Expand Up @@ -163,14 +175,15 @@ namespace KenticoKontentModels
```

## Feedback & Contributing

Check out the [contributing](https://github.com/Kentico/kontent-generators-net/blob/master/CONTRIBUTING.md) page to see the best places to file issues, start discussions and begin contributing.

### Wall of Fame

We would like to express our thanks to the following people who contributed and made the project possible:

- [Dražen Janjiček](https://github.com/djanjicek) - [EXLRT](http://www.exlrt.com/)
- [Dražen Janjiček](https://github.com/djanjicek) - [EXLRT](http://www.exlrt.com/)
- [Kashif Jamal Soofi](https://github.com/kashifsoofi)
- [Casey Brown](https://github.com/MajorGrits)

Would you like to become a hero too? Pick an [issue](https://github.com/Kentico/kontent-generators-net/issues) and send us a pull request!

9 changes: 7 additions & 2 deletions self-contained.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# Publish self-contained apps for Windows, Linux, and macOS

$runtimes = "win-x64", "linux-x64", "osx-x64"
$targets = "net5.0", "net6.0"

Foreach($r in $runtimes)
{
dotnet publish .\src\Kentico.Kontent.ModelGenerator\ -c Release --runtime $r -o (".\artifacts\" + $r) -p:PublishSingleFile=true -p:PublishTrimmed=true
Compress-Archive (".\artifacts\" + $r + "\*") (".\artifacts\KontentModelGenerator-" + $r + ".zip")
Foreach($t in $targets)
{
dotnet publish .\src\Kentico.Kontent.ModelGenerator\ -c Release --runtime $r --framework $t --self-contained -o (".\artifacts\" + $r + "\" + $t) -p:PublishSingleFile=true -p:PublishTrimmed=true
Compress-Archive (".\artifacts\" + $r + "\" + $t + "\*") (".\artifacts\KontentModelGenerator-" + $r + "-" + $t + ".zip")
Remove-Item (".\artifacts\" + $r + "\" + $t) -Recurse
}
Remove-Item (".\artifacts\" + $r) -Recurse
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ namespace Kentico.Kontent.ModelGenerator.Core.Generators.Class
{
public class ManagementClassCodeGenerator : ClassCodeGenerator
{
private static readonly string KontentElementIdAttributeName = new string(nameof(KontentElementIdAttribute).SkipLast(9).ToArray());
private static readonly string KontentElementIdAttributeName = new string
(
nameof(KontentElementIdAttribute)
.Substring(0, nameof(KontentElementIdAttribute).Length - "Attribute".Length)
.ToArray()
);

public ManagementClassCodeGenerator(ClassDefinition classDefinition, string classFilename, string @namespace = DefaultNamespace)
: base(classDefinition, classFilename, @namespace)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Authors>Kentico Software</Authors>
<Product>Kontent by Kentico</Product>
<PackageIconUrl>https://github.com/Kentico/Home/blob/master/images/kk-logo-nuget.png?raw=true</PackageIconUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<AssemblyName>KontentModelGenerator</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Kentico.Kontent.ModelGenerator</PackageId>
Expand Down
Loading

0 comments on commit f165949

Please sign in to comment.