Example #1
0
        /// <summary>
        /// Ensures the consistency of the MultipleFilesOptionsBase command line options related to
        /// the location of the output file, and adjusts the options for ease of use.
        /// </summary>
        /// <param name="options">
        /// A <see cref="MultipleFilesOptionsBase"/> object containing the relevant options.
        /// </param>
        /// <returns>
        /// true if the options are internally consistent; otherwise false.
        /// </returns>
        /// <remarks>
        /// At this time, this method does not actually do any validation. Unlike the case of
        /// SingleFileOptionsBase, where you have to specify exactly one of --inline and
        /// --output-file, it is _not_ necessary to specify --output-folder-path if --inline is
        /// absent, because by default each transformed file is written to the path containing
        /// corresponding input file.
        ///
        /// However, similarly to the case of SingleFileOptionsBase, we _do_ want to set --force
        /// whenever --inline is true, because there's no reason to force the user to type
        /// "--force" when they've already said that they want to overwrite the input file
        /// (see https://github.com/microsoft/sarif-sdk/issues/1642).
        ///
        /// So we introduce this method for three reasons:
        /// 1) For symmetry with the SingleFileOptionsBase,
        /// 2) To DRY out the logic for making --inline and --force consistent, and
        /// 3) To leave an obvious place to put output file option consistency logic if it's
        ///    needed in future.
        /// </remarks>
        public static bool ValidateOutputOptions(this MultipleFilesOptionsBase options)
        {
            bool valid = true;

            if (options.Inline)
            {
                options.Force = true;
            }

            return(valid);
        }
        internal string GetOutputFilePath(string inputFilePath, MultipleFilesOptionsBase absoluteUriOptions)
        {
            if (absoluteUriOptions.Inline)
            {
                return(inputFilePath);
            }

            return(!string.IsNullOrEmpty(absoluteUriOptions.OutputDirectoryPath) ?
                   Path.GetFullPath(absoluteUriOptions.OutputDirectoryPath) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(inputFilePath) + $"-{ProcessingName}.sarif" :
                   Path.GetDirectoryName(inputFilePath) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(inputFilePath) + $"-{ProcessingName}.sarif");
        }
        /// <summary>
        /// Ensures the consistency of the MultipleFilesOptionsBase command line options related to
        /// the location of the output file, and adjusts the options for ease of use.
        /// </summary>
        /// <param name="options">
        /// A <see cref="MultipleFilesOptionsBase"/> object containing the relevant options.
        /// </param>
        /// <returns>
        /// true if the options are internally consistent; otherwise false.
        /// </returns>
        /// <remarks>
        /// At this time, this method does not actually do any validation. Unlike the case of
        /// SingleFileOptionsBase, where you have to specify exactly one of --inline and
        /// --output-file, it is _not_ necessary to specify --output-folder-path if --inline is
        /// absent, because by default each transformed file is written to the path containing
        /// corresponding input file.
        ///
        /// However, similarly to the case of SingleFileOptionsBase, we _do_ want to set --force
        /// whenever --inline is true, because there's no reason to force the user to type
        /// "--force" when they've already said that they want to overwrite the input file
        /// (see https://github.com/microsoft/sarif-sdk/issues/1642).
        ///
        /// So we introduce this method for three reasons:
        /// 1) For symmetry with the SingleFileOptionsBase,
        /// 2) To DRY out the logic for making --inline and --force consistent, and
        /// 3) To leave an obvious place to put output file option consistency logic if it's
        ///    needed in future.
        /// </remarks>
        public static bool ValidateOutputOptions(this MultipleFilesOptionsBase options)
        {
            bool valid = true;

            //  TODO:  Is it correct to modify options in a "validate" method?
            //  #2267 https://github.com/microsoft/sarif-sdk/issues/2267
            if (options.Inline)
            {
                options.Force = true;
            }

            return(valid);
        }
        protected bool ValidateOptions(MultipleFilesOptionsBase options, IEnumerable <FileProcessingData> filesData)
        {
            bool valid = true;

            valid &= options.ValidateOutputOptions();

            valid &= DriverUtilities.ReportWhetherOutputFilesCanBeCreated(filesData.Select(f => f.OutputFilePath), options.Force, FileSystem);

            if (!options.Inline)
            {
                FileSystem.DirectoryCreateDirectory(options.OutputDirectoryPath);
            }

            return(valid);
        }
        protected IEnumerable <FileProcessingData> GetFilesToProcess(MultipleFilesOptionsBase options)
        {
            // Get files names first, as we may write more sarif logs to the same directory as we rebase them.

            HashSet <string> inputFilePaths = CreateTargetsSet(options.TargetFileSpecifiers, options.Recurse, FileSystem);

            foreach (string inputFilePath in inputFilePaths)
            {
                yield return(new FileProcessingData
                {
                    InputFilePath = inputFilePath,
                    OutputFilePath = GetOutputFilePath(inputFilePath, options),
                    SarifLog = ReadSarifFile <SarifLog>(FileSystem, inputFilePath)
                });
            }
        }