Example #1
0
            public async Task Sync(SyncArguments arguments)
            {
                var session = configuration.StartSession();

                var schemaText = string.Empty;
                var schemaName = arguments.Name;

                try
                {
                    schemaText = await File.ReadAllTextAsync(arguments.File);
                }
                catch (IOException)
                {
                    throw new CLIException("Cannot read schema file.");
                }

                if (string.IsNullOrWhiteSpace(schemaName))
                {
                    try
                    {
                        var sourceSchema = SchemaWithRefs <SchemaDto> .Parse(schemaText);

                        schemaName = sourceSchema.Schema.Name;
                    }
                    catch (JsonException ex)
                    {
                        throw new CLIException($"Cannot deserialize schema: {ex.Message}");
                    }
                }

                if (string.IsNullOrWhiteSpace(schemaName))
                {
                    throw new CLIException("Schema name cannot be empty.");
                }

                SchemaDto targetSchema;

                try
                {
                    targetSchema = await session.Schemas.GetSchemaAsync(session.App, schemaName);
                }
                catch
                {
                    targetSchema = null;
                }

                if (targetSchema == null)
                {
                    var request = SchemaWithRefs <CreateSchemaDto> .Parse(schemaText);

                    if (!arguments.NoRefFix && request.ReferencedSchemas.Any())
                    {
                        var allSchemas = await session.Schemas.GetSchemasAsync(session.App);

                        request.AdjustReferences(allSchemas.Items);
                    }

                    request.Schema.Name = schemaName;

                    await session.Schemas.PostSchemaAsync(session.App, request.Schema);

                    log.WriteLine("> Created schema because it does not exists in the target system.");
                }
                else
                {
                    var request = SchemaWithRefs <SynchronizeSchemaDto> .Parse(schemaText);

                    if (!arguments.NoRefFix && request.ReferencedSchemas.Any())
                    {
                        var allSchemas = await session.Schemas.GetSchemasAsync(session.App);

                        request.AdjustReferences(allSchemas.Items);
                    }

                    request.Schema.NoFieldDeletion   = arguments.NoFieldDeletion;
                    request.Schema.NoFieldRecreation = arguments.NoFieldRecreation;

                    await session.Schemas.PutSchemaSyncAsync(session.App, schemaName, request.Schema);

                    log.WriteLine("> Synchronized schema");
                }
            }
Example #2
0
            public async Task Sync(SyncArguments arguments)
            {
                var(app, service) = Configuration.GetClient();

                var schemasClient = service.CreateSchemasClient();

                var schemaText = (string)null;
                var schemaName = arguments.Name;

                var targetSchema = (SchemaDetailsDto)null;

                try
                {
                    schemaText = File.ReadAllText(arguments.File);
                }
                catch (IOException)
                {
                    throw new SquidexException("Cannot read schema file.");
                }

                if (string.IsNullOrWhiteSpace(schemaName))
                {
                    try
                    {
                        var sourceSchema = SchemaWithRefs <SchemaDetailsDto> .Parse(schemaText);

                        schemaName = sourceSchema.Schema.Name;
                    }
                    catch (JsonException ex)
                    {
                        throw new SquidexException($"Cannot deserialize schema: {ex.Message}");
                    }
                }

                if (string.IsNullOrWhiteSpace(schemaName))
                {
                    throw new SquidexException("Schema name cannot be empty.");
                }

                try
                {
                    targetSchema = await schemasClient.GetSchemaAsync(app, schemaName);
                }
                catch
                {
                    targetSchema = null;
                }

                if (targetSchema == null)
                {
                    var request = SchemaWithRefs <CreateSchemaDto> .Parse(schemaText);

                    if (!arguments.NoRefFix && request.ReferencedSchemas.Any())
                    {
                        var allSchemas = await schemasClient.GetSchemasAsync(app);

                        request.AdjustReferences(allSchemas);
                    }

                    request.Schema.Name = schemaName;

                    await schemasClient.PostSchemaAsync(app, request.Schema);

                    Console.WriteLine("> Created schema because it does not exists in the target system.");
                }
                else
                {
                    var request = SchemaWithRefs <SynchronizeSchemaDto> .Parse(schemaText);

                    if (!arguments.NoRefFix && request.ReferencedSchemas.Any())
                    {
                        var allSchemas = await schemasClient.GetSchemasAsync(app);

                        request.AdjustReferences(allSchemas);
                    }

                    request.Schema.NoFieldDeletion   = arguments.NoFieldDeletion;
                    request.Schema.NoFieldRecreation = arguments.NoFieldRecreation;

                    await schemasClient.PutSchemaSyncAsync(app, schemaName, request.Schema);

                    Console.WriteLine("> Synchronized schema");
                }
            }