none
Check certain files are EmbeddedResource RRS feed

  • Question

  • Hello

    I need to have a Pre-Build event that checks if all .sql files in a .csproj are included with BuildAction type of 'EmbeddedResource'. If any .sql file is not set as 'EmbeddedResource' a Buid error should be shown in output.

    Is there any command for this? Should I implement a custom command line tool to call before build?


    Jalalx


    • Edited by Jalal K Sunday, June 9, 2019 1:30 PM
    Sunday, June 9, 2019 1:29 PM

All replies

  • Hi Jalal K,

    Welcome to MSDN forum.

    According to your requirement, I could not find related to command line to check certain file. So I write a script to implement it. First I check the .csproj file to find the "EmbeddedResource" items, then I find the files in the specified folder, finally compare them.

    Please follow the below steps to have a try:

    #1 Create a console application

    using System;
    using System.Linq;
    using Microsoft.Build.BuildEngine;
    using System.IO;
    
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Project project = new Project();
                project.Load(Project path);
                
                var embeddedResources = from grp in project.ItemGroups.Cast<BuildItemGroup>()
                                        from item in grp.Cast<BuildItem>()
                                        where item.Name == "EmbeddedResource"
                                         select item;
    
                string[] files = Directory.GetFiles(folder path, "*.resx");
                string[] filename = new string[3] ;
                int i = 0,j = 0;
    
                
                foreach (string file in files)
                {
                    string pathname = Path.GetFileName(file);
    
                    filename[i] = pathname;
                    //Console.WriteLine(filename[i]);
                    i++;
    
                }
    
                foreach (BuildItem item in embeddedResources)
                {
                    int len = filename.Length;
                    string itemname = item.Include;
                    if (filename[j] == itemname)
                    {
                        j++;
                        break;
                    }
                    else
                        throw new ArgumentNullException("the name is different");
                    
                }
    
                Console.ReadLine();
            }
        }
    }

    #2 open the properties of the project >> select the "Build Event" tag >> type the path of the console app(.exe)


    #3 Build your project, and the exception message will show in output window

    Any feedback will be expected.

    Best Regards,

    Dylan



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com



    Monday, June 10, 2019 11:14 AM
  • The hard part is finding "all .sql files in a .csproj". If it's OK to instead check all SQL files within the project directory, then you can do it with a target like this:

      <Target Name="CheckSqlFiles" BeforeTargets="Build">
        <ItemGroup>
          <BadSqlFile Include="**\*.sql" Exclude="@(EmbeddedResource)" />
        </ItemGroup>
        <!-- First report each file as a warning so that the build keeps going until
             we have reported them all.  Then report an error that aborts the build.  -->
        <Warning File="%(Identity)" Text="The SQL file '%(BadSqlFile.Identity)' is not included with BuildAction type of 'EmbeddedResource'." />
        <Error Text="Found @(BadSqlFile->Count()) SQL files that are not included with BuildAction type of 'EmbeddedResource'." Condition="'@(BadSqlFile)' != ''" />
      </Target>
    

    Otherwise, I suppose you could replace the Include="**\*.sql" with something like Include="@(AdditionalFiles);@(ApplicationDefinition);@(CodeAnalysisDictionary);@(Compile);@(Content);@(DesignData);@(DesignDataWithDesignTimeCreatableTypes);@(EntityDeploy);@(None);@(Page);@(Resource);@(SplashScreen);@(XamlAppDef)", to cover the item types that users are most likely to select by accident.

    Wednesday, June 12, 2019 5:07 PM
  • Hi Jalal,
    Sorry for bothering you.
    Any update for this issue? If the reply from ranta below is helpful for you, please mark it as answer, it will be beneficial to other community members which has the similar issue. If the issue persists, please feel free to let us know.

    Best Regards

    Lance


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 5, 2019 3:43 AM