Skip to content

Unused code samples #1434

Closed
Closed

Description

@Youssef1313

There might be alot of unused code samples that needs to be removed.
Is there an easy way to determine if a specific sample is used or not ?
The samples here are used by dotnet/docs and dotnet/dotnet-api-docs, are there any other repos ?

Activity

  1. Youssef1313 commented on Sep 7, 2019

    @Youssef1313
    MemberAuthor

    I've written a small C# program to determine the unused snippets but want to make sure if it's correct ?
    For now, it only checks for .vb files.

    void Main()
    {
        // Get .vb files in my local fork of samples.
        // Replacing \ with (\\|\/) is used later for regex, so it matches both \ and /
        var vbSamples = Directory.GetFiles(@"D:\dotnet\samples\samples\", "*.vb", SearchOption.AllDirectories)
                        .Select(s => s.Replace(@"D:\dotnet\samples\samples\", "").Replace(@"\", @"(\\|\/)").ToLower()).ToArray();
    
        // Get .md files in my local fork of docs.
        var docsMarkdown = Directory.GetFiles(@"D:\dotnet\docs\docs\", "*.md", SearchOption.AllDirectories).ToArray();
    
        // Get .md files in my local fork of dotnet-api-docs.
        var apiDocsMarkdown = Directory.GetFiles(@"D:\dotnet\dotnet-api-docs\dotnet-api-docs\", "*.md", SearchOption.AllDirectories).ToArray();
    
        // Get .xml files in my local fork of dotnet-api-docs.
        var apiDocsXml = Directory.GetFiles(@"D:\dotnet\dotnet-api-docs\dotnet-api-docs\", "*.xml", SearchOption.AllDirectories).ToArray();
        
        // Iterate on each vbSample to check if it is used or not.
        for (var i = 0; i < vbSamples.Length; i++)
        {
            // Initially, mark it as unused.
            var isUsedSample = false;
    
             // If a reference to the sample is found in any file, mark sample as used and break loop.
            foreach (var mdFile in docsMarkdown)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(File.ReadAllText(mdFile).ToLower(), vbSamples[i]))
                {
                    isUsedSample = true;
                    goto end_checking;
                }
            }
            
            foreach (var mdFile    in apiDocsMarkdown)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(File.ReadAllText(mdFile).ToLower(), vbSamples[i]))
                {
                    isUsedSample = true;
                    goto end_checking;
                }
            }
            
            foreach (var xmlFile in apiDocsXml)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(File.ReadAllText(xmlFile).ToLower(), vbSamples[i]))
                {
                    isUsedSample = true;
                    goto end_checking;
                }
            }
            
            
    end_checking:
            
            if (!isUsedSample) Console.WriteLine(vbSamples[i]);
            // Write the unused samples to the console.
            Console.WriteLine($"{i + 1} / {vbSamples.Length}");
        }
    }

    Can there be any scenario where a sample is used and this code report it as unused ?

  2. mairaw commented on Oct 1, 2019

    @mairaw
    Contributor

    I'll close this one as duplicate of dotnet/docs#14825. I think @rpetrusha was also working on a code to identify those.

  3. added
    duplicate
    on Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions