Example #1
0
        /// <summary>
        ///   Determines a collection of <see cref="LicenseHeaderContentInput" /> instance to be processed, based on a
        ///   <see cref="ProjectItem" /> and its child items.
        /// </summary>
        /// <param name="item">
        ///   The <see cref="ProjectItem" /> to be used as starting point when looking for child items whose
        ///   license headers should also be updated.
        /// </param>
        /// <param name="headers">
        ///   The parsed headers based on a license header definition file. Keys are file extensions, values
        ///   represent the header, one line per array-element.
        /// </param>
        /// <param name="countSubLicenseHeaders">
        ///   Number of license header definition files found in child
        ///   <see cref="ProjectItem" />s
        /// </param>
        /// <param name="fileOpenedStatus">
        ///   Provides information on which files (full path, dictionary key) are currently opened
        ///   (values)
        /// </param>
        /// <param name="searchForLicenseHeaders">
        ///   Determines whether child <see cref="ProjectItem" />s should also be searched for
        ///   license header definition files and its corresponding header definitions. If <see langword="false" />, only the
        ///   license header definitions represented by <paramref name="headers" /> are used, even for all child items.
        /// </param>
        /// <returns>
        ///   Returns a <see cref="ICollection{T}" /> whose generic type parameter is
        ///   <see cref="LicenseHeaderContentInput" /> that encompasses the input for a <see cref="LicenseHeaderReplacer" />
        ///   instance based on the given parameters.
        /// </returns>
        public static ICollection <LicenseHeaderContentInput> GetFilesToProcess(
            ProjectItem item,
            IDictionary <string, string[]> headers,
            out int countSubLicenseHeaders,
            out IDictionary <string, bool> fileOpenedStatus,
            bool searchForLicenseHeaders = true)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            fileOpenedStatus = new Dictionary <string, bool>();
            var files = new List <LicenseHeaderContentInput>();

            countSubLicenseHeaders = 0;

            if (item == null)
            {
                return(files);
            }

            if (item.FileCount == 1 && File.Exists(item.FileNames[1]))
            {
                var content = item.GetContent(out var wasAlreadyOpen, LicenseHeadersPackage.Instance);
                if (content != null)
                {
                    files.Add(new LicenseHeaderContentInput(content, item.FileNames[1], headers, item.GetAdditionalProperties()));
                    fileOpenedStatus[item.FileNames[1]] = wasAlreadyOpen;
                }
            }


            if (item.ProjectItems == null)
            {
                return(files);
            }

            var childHeaders = headers;

            if (searchForLicenseHeaders)
            {
                childHeaders = LicenseHeaderFinder.SearchItemsDirectlyGetHeaderDefinition(item.ProjectItems);
                if (childHeaders != null)
                {
                    countSubLicenseHeaders++;
                }
                else
                {
                    childHeaders = headers;
                }
            }

            foreach (ProjectItem child in item.ProjectItems)
            {
                var subFiles = GetFilesToProcess(child, childHeaders, out var subLicenseHeaders, out var subFileOpenedStatus, searchForLicenseHeaders);

                files.AddRange(subFiles);
                foreach (var status in subFileOpenedStatus)
                {
                    fileOpenedStatus[status.Key] = status.Value;
                }

                countSubLicenseHeaders += subLicenseHeaders;
            }

            return(files);
        }
Example #2
0
        /// <summary>
        ///   Adds the license header text for the specified extension to the given project item.
        /// </summary>
        /// <param name="item">Specifies the project item in which the license header text is to be inserted.</param>
        /// <param name="extension">Specifies the extension of the language whose content should be added to the project item.</param>
        /// <param name="calledByUser">Specifies whether this method was called explicitly by the user or by the program.</param>
        /// <returns></returns>
        public static async Task AddLicenseHeaderToItemAsync(
            this ProjectItem item,
            ILicenseHeaderExtension extension,
            bool calledByUser)
        {
            if (item == null || ProjectItemInspection.IsLicenseHeader(item))
            {
                return;
            }

            await extension.JoinableTaskFactory.SwitchToMainThreadAsync();

            var headers = LicenseHeaderFinder.GetHeaderDefinitionForItem(item);

            if (headers != null)
            {
                var content = item.GetContent(out var wasAlreadyOpen, extension);
                if (content == null)
                {
                    return;
                }

                var result = await extension.LicenseHeaderReplacer.RemoveOrReplaceHeader(
                    new LicenseHeaderContentInput (content, item.FileNames[1], headers, item.GetAdditionalProperties()));

                await CoreHelpers.HandleResultAsync(result, extension, wasAlreadyOpen, calledByUser);

                return;
            }

            if (calledByUser && LicenseHeaderDefinitionFileHelper.ShowQuestionForAddingLicenseHeaderFile(item.ContainingProject, extension.DefaultLicenseHeaderPageModel))
            {
                await AddLicenseHeaderToItemAsync(item, extension, true);
            }
        }