Example #1
0
        /// <summary>
        /// Gets the contract source file, if one does not exist it will create the partial class file.
        /// </summary>
        /// <param name="source">The visual studio source file to search.</param>
        /// <param name="sourceClass">The target class that contains the contract.</param>
        /// <returns>The target source code or null if it could not be created.</returns>
        public static async Task <CsSource> GetContractSourceFileAsync(VsCSharpSource source, CsClass sourceClass)
        {
            if (source == null)
            {
                return(null);
            }
            if (sourceClass == null)
            {
                return(null);
            }

            var contractFile = GetContractFilePath(sourceClass);

            CsSource result = null;

            if (contractFile.hasFile)
            {
                result = await source.GetCsSourceDocumentFromParent(contractFile.filePath);
            }
            else
            {
                var manager            = source.SourceCode.LoadNamespaceManager(sourceClass.Namespace);
                var partialClassSource = CSharpSourceGenerationCommon.GeneratePartialClass(sourceClass, manager);

                if (string.IsNullOrEmpty(partialClassSource))
                {
                    throw new CodeFactoryException($"Could not generated partial class definition for class {sourceClass.Name}");
                }

                result = await source.AddCSharpCodeFileToParentAsync(partialClassSource,
                                                                     Path.GetFileName(contractFile.filePath));
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Gets the contracts main class file. This will force a model reload.
        /// </summary>
        /// <param name="source">Visual studio source file to start from.</param>
        /// <param name="sourceClass">The target class to check for.</param>
        /// <returns>The source file or null if it could not before loaded. </returns>
        public static async Task <CsSource> GetContractBaseFileAsync(VsCSharpSource source, CsClass sourceClass)
        {
            if (source == null)
            {
                return(null);
            }
            if (sourceClass == null)
            {
                return(null);
            }

            var contractFile = GetContractFilePath(sourceClass);

            var baseFilePath = contractFile.filePath.Replace(WPFConstants.DefaultContractSuffix, ".cs");

            return(await source.GetCsSourceDocumentFromParent(baseFilePath));
        }