2

Is there a way to dynamically add an embedded resource during the build process? I am currently using Ajax-Minifier to reduce two files down into one. The result works great. However I need to include this file as an embedded resource when the code is running in release mode (currently toggled by a Preprocessor directive of if(!DEBUG)

Here's a simple version of the target command I am using in my MSBUILD

<Target Name="TestMinifiy" Condition=" '$(ConfigurationName)' == 'Release' ">
    <ItemGroup>
      <MyScript Include="..\Test1.js" />
      <MyScript Include="..\Test2.js" />
    </ItemGroup>
    <AjaxMin JsSourceFiles="@(MyScript)" JsCombinedFileName="$(IntermediateOutputPath)\Final.js" />
</Target>

So basically I would like to include Final.js as an embedded resource, I then can reference within the code.

From the C# side this is what I am looking to do:

#if (!DEBUG)
[assembly: WebResource("Final.js", "text/javascript")]
#endif

Any help or ideas would be appreciated.

EDIT:

I've changed the JsCombinedFileName to $(IntermediateOutputPath)\Final.js. So that should create the file. I've then created a separate target to embed this file.

   <Target Name="EmbedResource" Condition=" '$(ConfigurationName)' == 'Release' ">
        <Message Text="Copying into Embedded..." Importance="high" />
        <ItemGroup>
          <EmbeddedResource Include="$(IntermediateOutputPath)\Final.js" >
            <LogicalName>
              Final.js
            </LogicalName>
          </EmbeddedResource>
        </ItemGroup>
      </Target>

However, still no luck.

cgatian
  • 22,047
  • 9
  • 56
  • 76

1 Answers1

1

(Updated to better answer the question.)

I believe that the problem you are running into is that the minification is occurring as part of the build, which means that the generated final.js file is not available within the TestMinify target. The embedding itself can be included within an item group like this.

<Target Name="Build" Condition=" '$(ConfigurationName)' == 'Release' ">
    <!-- other stuff -->

    <ItemGroup>
        <EmbeddedResource Include="final.js" />

        <!-- other stuff -->
    </ItemGroup>
</Target>

To avoid duplication in the main build target, the conditional can also be placed directly in the EmbeddedResource tag like this.

<Target Name="Build">
    <!-- other stuff -->

    <ItemGroup>
        <EmbeddedResource Condition=" '$(Configuration)' == 'Release' " Include="final.js" />

        <!-- other stuff -->
    </ItemGroup>
</Target>

You can then call your TestMinify target before your main Build target.

Alternatively, you can create a prebuild event to generate the file. It would need to call an executable rather than a MSBuild task.

Here is an example of how the prebuild event might look.

<PropertyGroup>
  <PreBuildEvent>if "$(ConfigurationName)" equ "Release" (copy "$(ProjectDir)source.js" "$(ProjectDir)final.js")</PreBuildEvent>
</PropertyGroup>

In this second scenario, the Build target with the EmbeddedResource could be set up in either of the ways mentioned previously.

rileywhite
  • 365
  • 2
  • 12
  • I added how I would like to use the file on the C# side. My Target is currently being ran at the correct time since I am specifying the Target name in the "ResGenDependsOn" group. Could you provide some details on how to get the referenced file "Final" set as a resource? – cgatian Feb 24 '14 at 22:39
  • I updated the answer with what I hope is a better understanding of the question @cgatian :-) – rileywhite Feb 24 '14 at 23:32
  • if you dont mind taking one last look at my update, maybe it will help explain further. – cgatian Feb 25 '14 at 14:06
  • 1
    As strange as it sounds, adding the resource within a Target did not work. Adding the resource outside the target works fine. Feels like a hack though, since the file generation happens in its own target, yet the actual addition of the file happens while embedding other resources. Only difference being a Condition attribute was added. – cgatian Feb 25 '14 at 22:14
  • Interesting finding. I'm glad you were able to find the solution. – rileywhite Feb 25 '14 at 22:19
  • Ill at least +1 it for the effort. – cgatian Feb 26 '14 at 00:54
  • Can someone please post, what actually worked, the final answer? – keerthee Jul 04 '21 at 17:14