Edit the DefineConstants in csproj file

删除回忆录丶 提交于 2020-01-06 17:58:27

问题


I have csproj with the following tag:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>Bin\Debug</OutputPath>
    <DefineConstants>TRACE;DEBUG;SILVERLIGHT;WINDOWS_PHONE;SUPPORTS_ICLOUD</DefineConstants>
    <NoStdLib>true</NoStdLib>
    <NoConfig>true</NoConfig>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

Here if you see that i have added a macro called SUPPORTS_ICLOUD.

I have .txt file in that i have the following :

#define SUPPORTS_IN_APP_PURCHASE 

#define BUTTON_THEME    
#define SUPPORTS_TITLE 

#define HAS_HTML_IMAGE_CONTENT 
#define TINT_COLOR 
#define NAVIGATIONTITLE_TEXT_COLOR 

#define SUPPORTS_ICLOUD 

As you can see in my text file i have many macros defined, now instead of passsing each macro in DefineConstants can i pass it altogether as a file or a string so that it will take all those ? Is this possible or is there any other way ?

EDIT

This is my csproj file:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>10.0.20506</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{0D446418-B7CD-4624-91F4-F3E382F8DD23}</ProjectGuid>
    <ProjectTypeGuids>{C089C8C0-30E0-4E22-80C0-CE093F111A43};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>POCChild3</RootNamespace>
    <AssemblyName>POCChild3</AssemblyName>
    <TargetFrameworkIdentifier>WindowsPhone</TargetFrameworkIdentifier>
    <TargetFrameworkVersion>v8.0</TargetFrameworkVersion>
    <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
    <SilverlightApplication>true</SilverlightApplication>
    <SupportedCultures>
    </SupportedCultures>
    <XapOutputs>true</XapOutputs>
    <GenerateSilverlightManifest>true</GenerateSilverlightManifest>
    <XapFilename>POCChild3_$(Configuration)_$(Platform).xap</XapFilename>
    <SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate>
    <SilverlightAppEntry>POCChild3.App</SilverlightAppEntry>
    <ValidateXaml>true</ValidateXaml>
    <MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
    <ThrowErrorsInValidation>true</ThrowErrorsInValidation>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>Bin\Release\</OutputPath>
    <DefineConstants>DEBUG;TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
    <NoStdLib>true</NoStdLib>
    <NoConfig>true</NoConfig>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>Bin\Release</OutputPath>
    <DefineConstants>TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
    <NoStdLib>true</NoStdLib>
    <NoConfig>true</NoConfig>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup>
    <MacroFile>Macros.txt</MacroFile>
  </PropertyGroup>
  <Target Name="Build">
    <ReadLinesFromFile
         File="$(MacroFile)" >
      <Output
          TaskParameter="Lines"
          ItemName="MacrosFromFile"/>
    </ReadLinesFromFile>

    <CreateProperty
        Value="@(MacrosFromFile->Replace('#define ', ''))">
      <Output
          TaskParameter="Value"
          PropertyName="FileDefineConstants" />
    </CreateProperty>

    <CreateProperty
        Value="$(DefineConstants);$(FileDefineConstants)">
      <Output
          TaskParameter="Value"
          PropertyName="DefineConstants" />
    </CreateProperty>

    <Message Text="Const >> $(DefineConstants)" />
  </Target>

In my MainPage.cs file;

#if SUPPORTS_ICLOUD 
            AppName.Text = "ABCD";
            Debug.WriteLine("App type is app with main screen");

#endif

回答1:


You can use ReadLinesFromFile, the CreateProperty and the item.Replace function to update your property by using the batching capabilities of the msbuild engine:

  <!-- property defintion -->
  <PropertyGroup>
     <MacroFile>def.txt</MacroFile> <!-- the filename with your #defines -->
  </PropertyGroup>

  <!-- other stuff in your build file -->

  <!-- import common targets near the end of your build file -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

  <!-- override targets (make sure this is AFTER the import elements) -->
  <!-- BeforeBuild is pre-dfined target which can be overriden -->
  <Target Name="BeforeBuild">
       <!-- Open the #define file and read every line in items named
            MacrosFromFile 
        -->
       <ReadLinesFromFile
            File="$(MacroFile)" >
            <Output
                TaskParameter="Lines"
                ItemName="MacrosFromFile"/>
        </ReadLinesFromFile>

        <!-- Create a new property called FileDefineConstants combining 
             every Item from MacrosFromFile   
             using the built-in replace statement to get 
             rid of the #define instruction
         -->
        <CreateProperty
            Value="@(MacrosFromFile->Replace('#define ', ''))">
            <Output
                TaskParameter="Value"
                PropertyName="FileDefineConstants" />
        </CreateProperty>

        <!-- re-create the orignal DefineConstants combining the current value
             and the value from FileDefineConstants -->
        <CreateProperty
            Value="$(DefineConstants);$(FileDefineConstants)">
            <Output
                TaskParameter="Value"
                PropertyName="DefineConstants" />
        </CreateProperty>

        <Message Text="Const >> $(DefineConstants)" />
  </Target>


来源:https://stackoverflow.com/questions/22112684/edit-the-defineconstants-in-csproj-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!