Exemplo n.º 1
0
        /// <summary>Initializes a new instance of the <see cref="CSharpFileTemplateModel" /> class.</summary>
        /// <param name="clientTypes">The client types.</param>
        /// <param name="dtoTypes">The DTO types.</param>
        /// <param name="outputType">Type of the output.</param>
        /// <param name="document">The Swagger document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="generator">The client generator base.</param>
        /// <param name="resolver">The resolver.</param>
        public CSharpFileTemplateModel(
            IEnumerable <CodeArtifact> clientTypes,
            IEnumerable <CodeArtifact> dtoTypes,
            ClientGeneratorOutputType outputType,
            OpenApiDocument document,
            CSharpGeneratorBaseSettings settings,
            CSharpGeneratorBase generator,
            CSharpTypeResolver resolver)
        {
            _outputType = outputType;
            _document   = document;
            _generator  = generator;
            _settings   = settings;
            _resolver   = resolver;
            _clientCode = clientTypes.Concatenate();

            Classes   = dtoTypes.Concatenate();
            SourceSha = _settings.ChecksumCacheEnabled ? _document.GetChecksum() : "";
        }
Exemplo n.º 2
0
        /// <summary>Initializes a new instance of the <see cref="TypeScriptFileTemplateModel" /> class.</summary>
        /// <param name="clientTypes">The client types.</param>
        /// <param name="dtoTypes">The DTO types.</param>
        /// <param name="document">The Swagger document.</param>
        /// <param name="extensionCode">The extension code.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="resolver">The resolver.</param>
        public TypeScriptFileTemplateModel(
            IEnumerable <CodeArtifact> clientTypes,
            IEnumerable <CodeArtifact> dtoTypes,
            OpenApiDocument document,
            TypeScriptExtensionCode extensionCode,
            TypeScriptClientGeneratorSettings settings,
            TypeScriptTypeResolver resolver)
        {
            _document      = document;
            _extensionCode = extensionCode;
            _settings      = settings;
            _resolver      = resolver;

            _clientCode  = clientTypes.OrderByBaseDependency().Concatenate();
            _clientTypes = clientTypes;

            Types = dtoTypes.OrderByBaseDependency().Concatenate();
            ExtensionCodeBottom = GenerateExtensionCodeAfter();
            Framework           = new TypeScriptFrameworkModel(settings);
            SourceSha           = _settings.ChecksumCacheEnabled ? _document.GetChecksum() : "";
        }
Exemplo n.º 3
0
        private bool OutputRequiresRefresh(string outputFilePath)
        {
            if (!BaseSettings.ChecksumCacheEnabled || string.IsNullOrWhiteSpace(outputFilePath) || !File.Exists(outputFilePath))
            {
                // no point in checking
                return(true);
            }

            var sourceDocumentChecksum = _document.GetChecksum();

            if (string.IsNullOrWhiteSpace(sourceDocumentChecksum))
            {
                return(true);
            }

            // if we can match both source checksum and toolchain, we can presume file hasn't been changed
            var          checksumMatches         = false;
            var          toolchainVersionMatches = false;
            const string checksumMarker          = "SourceSHA: ";
            const string toolchainVersionMarker  = "toolchain v";

            try
            {
                using (var reader = new StreamReader(new FileStream(outputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    // read multiple lines as we can have headers before version info
                    var    counter = 10;
                    string line;
                    while ((line = reader.ReadLine()) != null && counter > 0)
                    {
                        counter--;
                        var checksumStartIndex = line.IndexOf(checksumMarker);
                        if (checksumStartIndex > -1)
                        {
                            var startIndex    = checksumStartIndex + checksumMarker.Length;
                            var fileSourceSha = line.Substring(startIndex).Trim();
                            checksumMatches = fileSourceSha == sourceDocumentChecksum;
                        }

                        var toolchainStartIndex = line.IndexOf(toolchainVersionMarker);
                        if (toolchainStartIndex > -1)
                        {
                            var startIndex           = toolchainStartIndex + toolchainVersionMarker.Length;
                            var length               = line.IndexOf(" ", startIndex) - startIndex;
                            var fileToolchainVersion = line.Substring(startIndex, length).Trim();
                            toolchainVersionMatches = fileToolchainVersion == OpenApiDocument.ToolchainVersion;
                        }

                        if (checksumMatches && toolchainVersionMatches)
                        {
                            // we are good to go with cached version, no need to refresh
                            return(false);
                        }
                    }
                }
            }
            catch
            {
                // no-go
            }

            return(true);
        }