How to get property value of a project file using msbuild - visual-studio

Using MSBuild I include a solution>
<ItemGroup>
<ProjectToBuild Include="$(SVNLocalPath)\$(SolutionName)"> </ProjectToBuild>
</ItemGroup>
I need to include all the *.csproj file from the solution with the condition of the proj file contain or define a property; for example if x.csproj contain a defined property "TestProjectType" would like to include the project into my itemGroup
something like this
<Target Name = "TestProperties">
<Message Text="TestProperties"/>
<ItemGroup>
<AllProj Include="$(SVNLocalPath)\*.csproj"/>
<AllTestProj Include="%(AllProj.Identity)" Condition="%(AllProj.ProjectTypeGuids)=={3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"/>
</ItemGroup>
<Message Text="#(AllTestProj )"/>
</Target>
Thanks

You can achieve that through custom task.
A simple sample to check a property (test) in all projects exclude current project of current solution:
<UsingTask TaskName="GetPropertyTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup>
<ProjectFile ParameterType="System.String" Required="true" />
<BuildOutput ParameterType="System.String[]" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml"/>
<Reference Include="Microsoft.Build"/>
<Using Namespace="Microsoft.Build" />
<Using Namespace="Microsoft.Build.Evaluation" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var properties = new Dictionary<string, string>
{
{ "Configuration", "$(Configuration)" },
{ "Platform", "$(Platform)" }
};
//Log.LogMessage(MessageImportance.High, "customLog");
// Log.LogMessage(MessageImportance.High, ProjectFile);
var collection = new ProjectCollection(properties);
var project = collection.LoadProject(ProjectFile);
ProjectProperty pp = project.Properties.Where(p => p.Name == "MyCustomProperty").FirstOrDefault();
string customValue = pp==null?"empty":pp.EvaluatedValue;
BuildOutput = new String[] { customValue };
]]></Code>
</Task>
</UsingTask>
<Target Name="AfterBuild">
<GetPropertyTask ProjectFile="%(ProjectToScan.FullPath)">
<Output ItemName="ProjectToScanOutput" TaskParameter="BuildOutput"/>
</GetPropertyTask>
<Message Text="ClassLibrary1" Importance="high" Condition="'%(ProjectToScanOutput.Identity)' == 'test'" />
</Target>
More information, please refer to this article.

Related

Change base path on VS publish

I have a blazorWASM project. While developing, I need index.html to have it's base path set to <base href="/" />, but when I use the built-in publish to file, the output is set to land directly into my PHP project which acts as a host that serves WASM static files, and needs this base path: <base href="/wwwroot/" />.
Is there a way to have them automatically switched so I do not keep forgetting to do so? Alternatively, how do I configure the project so that it will work while I debug it on IIS with the wwwroot base path?
You need to add a build task to your CSPROJ Blazor WASM.
Important note: you need Newtonsoft 13.0.1, System.Text.Json isn't supported.
<UsingTask TaskName="ReplaceBaseHRef" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<AppSettingsfile ParameterType="System.String" Required="true" />
<BaseHRefAttribute ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="$(NugetPackageRoot)\newtonsoft.json\13.0.1\lib\netstandard2.0\Newtonsoft.Json.dll" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Newtonsoft.Json" />
<Using Namespace="Newtonsoft.Json.Linq" />
<Code Type="Fragment" Language="C#">
<![CDATA[
var inputFile = File.ReadAllText(InputFilename);
var appsetting = File.ReadAllText(AppSettingsfile);
JObject appsettings = JObject.Parse(appsetting);
var baseHRef = appsettings[BaseHRefAttribute].Value<string>();
if (!string.IsNullOrEmpty(baseHRef)) {
Log.LogMessage( MessageImportance.High, baseHRef );
var outputFile = InputFilename;
var matchExpression = "\\<base\\ href=\\\"(.*)\\\"\\ \\/\\>";
var newBaseHRef = $"<base href=\"{baseHRef}\" />";
File.WriteAllText(
outputFile,
Regex.Replace(inputFile, matchExpression, newBaseHRef)
);
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="ReplaceBaseHRef" AfterTargets="Publish">
<ReplaceBaseHRef InputFilename="$(PublishDir)wwwroot\index.html" AppSettingsfile="$(PublishDir)wwwroot\appsettings.Production.json" BaseHRefAttribute="BaseHRef" />
</Target>
Now take your appsettings.Production.json and add the configuration setting as:
{
"BaseHRef": "/client/",
...
}
now launch the Publish (in folder, IIS or on Azure).
Your index.html will contain <base href="/client/" />.

MSBuild: How to access a property value set by a Target during the Post Build event in Visual Studio

I have a PostBuild event which invokes a batch file and I need to pass in a particular parameter into the batch file. This parameter is populated through another task which is invoked through a Target configured to run before the PostBuildEvent.
I can see that it gets successfully displayed when displayed using the element as part of the section.
But $(TargetFrameworkToolsFolderPath) under the PostBuildEvent has an "" value. Is there a way to access this custom property in the post build event?
Example:
<UsingTask TaskName="GetTargetFrameworkToolsFolderName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<SDKFolderPath ParameterType="System.String" Required="true" />
<TargetFrameworkVersionStr ParameterType="System.String" Required="true" />
<TargetFrameworkToolsFolder ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
TargetFrameworkToolsFolder = SDKFolderPath + "\\" + "bin\\NETFX " + TargetFrameworkVersionStr.Substring(1) + " Tools\\";
</Code>
</Task>
</UsingTask>
<Target Name="FindTargetFrameworkToolsFolderPath" BeforeTargets="PostBuildEvent">
<GetFrameworkSdkPath>
<Output TaskParameter="Path" PropertyName="SdkPath" />
</GetFrameworkSdkPath>
<GetTargetFrameworkToolsFolderName SDKFolderPath="$(SdkPath)" TargetFrameworkVersionStr="$(TargetFrameworkVersion)">
<Output PropertyName="TargetFrameworkToolsFolderPath" TaskParameter="TargetFrameworkToolsFolder"/>
</GetTargetFrameworkToolsFolderName>
<Message Text="$(TargetFrameworkToolsFolderPath)" Importance="normal" /> --> Displayed correctly here
</Target>
<PropertyGroup>
<PostBuildEvent>
call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.
</PostBuildEvent>
</PropertyGroup>
But $(TargetFrameworkToolsFolderPath) under the PostBuildEvent has an
"" value. Is there a way to access this custom property in the post
build event?
In fact, <PostBuildEvent> is a property and MSBuild reads all the properties first and then executes all targets.
If you put these below outside the target which defines the property TargetFrameworkToolsFolderPath, these below will always execute first, as expected, the values of TargetFrameworkToolsFolderPath will be empty.
To avoid it, you should put the PostBuildEvent and TargetFrameworkToolsFolderPath properties in the same target and make sure the target is executed early enough, such as, run after PrepareForBuild target.
<PropertyGroup>
<PostBuildEvent>
call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.
</PostBuildEvent>
</PropertyGroup>
Solution
Try this below:
<UsingTask TaskName="GetTargetFrameworkToolsFolderName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<SDKFolderPath ParameterType="System.String" Required="true" />
<TargetFrameworkVersionStr ParameterType="System.String" Required="true" />
<TargetFrameworkToolsFolder ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
TargetFrameworkToolsFolder = SDKFolderPath + "\\" + "bin\\NETFX " + TargetFrameworkVersionStr.Substring(1) + " Tools\\";
</Code>
</Task>
</UsingTask>
<Target Name="MyFindTargetFrameworkToolsFolderPath" AfterTargets="PrepareForBuild">
<GetFrameworkSdkPath>
<Output TaskParameter="Path" PropertyName="SdkPath" />
</GetFrameworkSdkPath>
<GetTargetFrameworkToolsFolderName SDKFolderPath="$(SdkPath)" TargetFrameworkVersionStr="$(TargetFrameworkVersion)">
<Output PropertyName="TargetFrameworkToolsFolderPath" TaskParameter="TargetFrameworkToolsFolder" />
</GetTargetFrameworkToolsFolderName>
<PropertyGroup>
<PostBuildEvent> call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.</PostBuildEvent>
</PropertyGroup>
<Message Text="$(TargetFrameworkToolsFolderPath)" Importance="normal" />
</Target>
Hope it could help you.

How to setup two separate webpack instances on a site?

I have a requirement at work to have two separate webpack instances which will essentially handle two different "sites" on the same domain.
Here is my project structure:
App1/
App2/
webpack.config.js
App2-webpack.config.js
Basically, I want to be able to be able to have all routes that start with /App2 use the App2 webpack config file and all other routes be handled by the other webpack file. Both App1 and App2 folders are completely separate instances of react, react router and redux.
I'm using the "ASP.NET Core Web Application" template from visual studio (https://github.com/aspnet/JavaScriptServices) if that helps. I've modified the csproj file and added some more commands for the second webpack bundle:
<Target Name="RunWebpackDebug" AfterTargets="AfterBuild" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="npm install" />
<Exec Command="webpack --config App2-webpack.config.vendor.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.js" />
</Target>
However I'm getting the error:
The command "webpack --config App2-webpack.config.vendor.js" exited with code 9009.
If I run the offending command in command prompt, I get a more specific error message:
Cannot find module './App2/wwwroot/dist/vendor-manifest.json' at ...
My guess is the fact that I'm trying to use the wwwroot folder inside my App2 folder (to keep the two apps separate) doesn't work (usually the wwwroot folder is in the root folder).
Also, is the intended functionality even possible? Thanks
EDIT: here is my webpack file for the second app:
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
const merge = require('webpack-merge');
const CopyWebPackPlugin = require('copy-webpack-plugin');
const CompressionPlugin = require("compression-webpack-plugin");
module.exports = (env) => {
const isDevBuild = !(env && env.prod);
// Configuration in common to both client-side and server-side bundles
const sharedConfig = () => ({
stats: { modules: false },
resolve: { extensions: ['.js', '.jsx', '.ts', '.tsx'] },
output: {
filename: '[name].js',
publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{
test: /\.ts$/,
enforce: 'pre',
loader: 'tslint-loader',
options: {
tsConfigFile: 'tsconfig.json'
}
},
{ test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' }
]
},
plugins: [new CheckerPlugin(),
new CopyWebPackPlugin([{ from: 'static' }])
]
});
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig(), {
entry: { 'main-client': './ClientApp/boot-client.tsx' },
module: {
rules: [
{ test: /\.less$/, exclude: /node_modules/, use: ExtractTextPlugin.extract(['css-loader', 'less-loader']) },
{ test: /\.css$/, exclude: /node_modules/, use: ExtractTextPlugin.extract({ use: 'css-loader' }) },
{ test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|eot|ttf)$/, exclude: /node_modules/, use: 'url-loader?limit=25000' },
]
},
output: {
path: path.join(__dirname, clientBundleOutputDir)
},
plugins: [
new ExtractTextPlugin('site.css'),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig(), {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot-server.tsx' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
],
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
const externalConfig = merge(sharedConfig(), {
entry: { 'external-shell-actions': './ClientApp/actions/externalShellActions' },
output: {
path: path.resolve(__dirname, './dist'),
filename: 'external-shell-actions.js',
library: 'externalShellActions',
libraryTarget: 'umd'
}
});
return [clientBundleConfig, serverBundleConfig, externalConfig];
};
And here is my csproj file:
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
<TypeScriptToolsVersion>2.3</TypeScriptToolsVersion>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>portable</DebugType>
<DebugSymbols>True</DebugSymbols>
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\Debug\net462\</OutputPath>
<DefineConstants>TRACE;DEBUG;NET462</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<DebugType>portable</DebugType>
<DebugSymbols>True</DebugSymbols>
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ProductionRelease|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='ProductionRelease|x86'">
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AspNetCore.Antiforgery.Aes" Version="1.0.4" />
<PackageReference Include="AutoMapper" Version="6.1.1" />
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="NETStandard.Library" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<!-- Files not to show in IDE -->
<None Remove="App.POS.View.React.csproj.vspscc" />
<None Remove="App2\Routes.tsx" />
<None Remove="yarn.lock" />
<Compile Remove="ClientApp\components\assets\**" />
<Compile Remove="ClientApp\dist\**" />
<Compile Remove="node_modules\postcss-zindex\postcss-unique-selectors\**" />
<Compile Remove="wwwroot\dist\**" />
<!-- Files not to publish (note that the 'dist' subfolders are re-added below) -->
<Content Remove="ClientApp\**" />
<Content Remove="node_modules\postcss-zindex\postcss-unique-selectors\**" />
<Content Remove="wwwroot\dist\**" />
<EmbeddedResource Remove="ClientApp\components\assets\**" />
<EmbeddedResource Remove="ClientApp\dist\**" />
<EmbeddedResource Remove="node_modules\postcss-zindex\postcss-unique-selectors\**" />
<EmbeddedResource Remove="wwwroot\dist\**" />
<None Remove="ClientApp\components\assets\**" />
<None Remove="ClientApp\dist\**" />
<None Remove="node_modules\postcss-zindex\postcss-unique-selectors\**" />
<None Remove="wwwroot\dist\**" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\PublishProfiles\FolderProfile.pubxml.user" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<None Update="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="static\images\" />
</ItemGroup>
<ItemGroup>
<TypeScriptCompile Include="ClientApp\actions\01.ts" />
<TypeScriptCompile Include="ClientApp\actions\0.ts" />
<TypeScriptCompile Include="ClientApp\components\1.tsx" />
<TypeScriptCompile Include="ClientApp\components\2.tsx" />
<TypeScriptCompile Include="ClientApp\components\3.tsx" />
<TypeScriptCompile Include="ClientApp\components\4.tsx" />
<TypeScriptCompile Include="ClientApp\components\5.tsx" />
<TypeScriptCompile Include="ClientApp\components\common\6.tsx" />
<TypeScriptCompile Include="ClientApp\components\common\7.tsx" />
<TypeScriptCompile Include="ClientApp\components\8.tsx" />
<TypeScriptCompile Include="ClientApp\components\9.tsx" />
<TypeScriptCompile Include="ClientApp\reducers\10.ts" />
<TypeScriptCompile Include="App2\actions\2.ts" />
<TypeScriptCompile Include="App2\components\Layout.tsx" />
<TypeScriptCompile Include="App2\components\1\1.tsx" />
<TypeScriptCompile Include="App2\components\1\2.tsx" />
<TypeScriptCompile Include="App2\containers\1.tsx" />
<TypeScriptCompile Include="App2\routes.tsx" />
</ItemGroup>
<Target Name="RunWebpackDebug" AfterTargets="AfterBuild" Condition="'$(Configuration)' == 'Debug'">
<Exec Command="npm install" />
<Exec Command="node_modules\.bin\webpack --config App2-webpack.config.vendor.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.js" />
<Exec Command="node_modules\.bin\webpack --config webpack.config.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.js" />
</Target>
<Target Name="RunWebpackRelease" AfterTargets="AfterBuild" Condition="'$(Configuration)' == 'Release'">
<Exec Command="npm install" />
<Exec Command="node_modules\.bin\webpack --config App2-webpack.config.vendor.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.vendor.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.production.js --env.prod" />
</Target>
<Target Name="RunWebpackProductionRelease" AfterTargets="AfterBuild" Condition="'$(Configuration)' == 'ProductionRelease'">
<Exec Command="npm install" />
<Exec Command="node_modules\.bin\webpack --config App2-webpack.config.vendor.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.vendor.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.production.js --env.prod" />
</Target>
<Target Name="RunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec Command="npm install" />
<Exec Command="node_modules\.bin\webpack --config App2-webpack.config.vendor.js" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.vendor.production.js --env.prod" />
<Exec Command="node node_modules/webpack/bin/webpack.js --config App2-webpack.config.production.js --env.prod" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="wwwroot\dist\**; ClientApp\dist\**; App2\dist\**" />
<ResolvedFileToPublish Include="#(DistFiles->'%(FullPath)')" Exclude="#(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
<ProjectExtensions><VisualStudio><UserProperties package_1json__JSONSchema="http://json.schemastore.org/schema-catalog" /></VisualStudio></ProjectExtensions>
</Project>

UnitTask error trying to use Microsoft.Build.Evaluation

have the following code inside my CSProj file:
<UsingTask TaskName="HelloWorld" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<Task>
<!--Microsoft.Build.dll-->
<Using Namespace="Microsoft.Build.Evaluation" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var p = new Project("$(MSBuildProjectFullPath)");
]]>
</Code>
</Task>
</UsingTask>
in my AfterBuild I call it like this:
<Target Name="AfterBuild">
<HelloWorld />
</Target>
The error I am getting is:
error CS0246: The type or namespace name 'Project' could not be found
(are you missing a using directive or an assembly reference?)
Referring to var p = new Project
Include Microsoft.Build reference in the task can fix that issue. (The Microsoft.Build.dll reference need to be added to your project firstly)
<Task>
<Reference Include="Microsoft.Build"/>
<Using Namespace="Microsoft.Build.Evaluation" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var p = new Project("$(MSBuildProjectFullPath)");
]]>
</Code>
</Task>

XPath expression for parsing WiX Processing-Instructions as MsBuild properties

I have the following wix include VersionFile.wxi
<?xml version="1.0" encoding="utf-8"?>
<Include>
<?define ProductVersionMajor = "1" ?>
<?define ProductVersionMinor = "00" ?>
<?define ProductName= "MyProduct" ?>
<?define UpgradeCode = "myUpgradeCode" ?>
</Include>
Now I want to get e.g. the ProductVersionMajor as "1" or ProductName "MyProduct" (without quotes) using XmlPeek and a XPath query. With following code
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Test">
<XmlPeek XmlInputPath="VersionFile.wxi"
Query="//processing-instruction('define')[starts-with(., "ProductVersionMajor =")]">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
<XmlPeek XmlInputPath="VersionFile.wxi"
Query="//processing-instruction('define')[starts-with(., "ProductVersionMajor=")]">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
<Message Text="#(Peeked)"/>
</Target>
</Project>
I already got it down to
<?define ProductVersionMajor = "1" ?>
But goal would be
1
Any help how to tweak the XPath query highly appreciated. Also it'll be great to have a placeholder "ProductVersionMajor*=" instead using XmlPeek twice.
<XmlPeek XmlInputPath="ProductVersion.wxi"
Query="substring-before(substring-after(//processing-instruction("define")[starts-with(., "ProductVersionMajor=")],"),")">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
unfortunately only produces an
error MSB3734: XPath Query "substring-before(substring-after(//processing-instruction("define")[starts-with(., "ProductVersionMajor=")],"),")" cannot be loaded. 'substring-before(substring-after(//processing-instruction("define")[starts-with(., "ProductVersionMajor=")],"),")' has an invalid token.
Assume that XmlPeek needs some more custom XPath syntax possibly?
Yes. Tried it as well. Now also tried
Query="substring-before(substring-after(//processing-instruction('define')[starts-with(., 'ProductVersionMajor =')],&apos;"&apos;),&apos;"&apos;) ">
Also no success. Error is
error MSB4018: The "XmlPeek" task failed unexpectedly.\r
error MSB4018: System.Xml.XPath.XPathException: Expression must evaluate to a node-set.\r
error MSB4018: at System.Xml.XPath.XPathNavigator.Select(XPathExpression expr)\r
error MSB4018: at Microsoft.Build.Tasks.XmlPeek.Execute()\r
error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()\r
error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, Task
LoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)
Form the xpath point of view the following should do:
Query='substring-before(
substring-after(
//processing-instruction("define")[starts-with(., "ProductVersionMajor =")]
,
&apos;"&apos;
)
,
&apos;"&apos;
)'
I solved this problem with this approach. Hope it helps:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="GetWixDefine"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<File ParameterType="System.String" Required="true" Output="false" />
<DefineName ParameterType="System.String" Required="true" Output="false" />
<Value ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Using Namespace="System" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="System.Xml" />
<Code Type="Fragment" Language="cs">
<![CDATA[
this.Value = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.File);
string selector = string.Format("//processing-instruction('define')[starts-with(., '{0}')]", this.DefineName);
XmlNode defineNode = xmlDoc.SelectSingleNode(selector);
if (defineNode == null)
throw new Exception("define not found");
string regex = string.Format("{0}[ ]*=[ ]*\"(?<value>.*)\"", DefineName);
Match match = Regex.Match(defineNode.InnerText, regex);
if (!match.Success)
throw new Exception("cannot match correctly");
this.Value = match.Groups["value"].Value;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="BeforeBuild">
<GetWixDefine File="VersionFile.wxi" DefineName="ProductVersion">
<Output TaskParameter="Value" ItemName="ProductVersionValue"/>
</GetWixDefine>
<Message Importance="High" Text="ProductVersion: #(ProductVersionValue)"/>
</Target>
</Project>

Resources