Building Content Project on the fly doesn't copy when Item is set to "none" - msbuild

So I m building a tool that creates a ContentProject on the fly and then Builds it (so that it outputs Xnb's)
At the moment the problem is that files that are not to be compiled should be copied into the output directory if marked with CopyToOutputDirectory = 'always' or 'PreserveNewest' . If we were looking at the .contentProj that section for a file that shouldn't be built but should be copied would look like this
<ItemGroup>
<None Include="MyFile.file">
<Name>level1</Name>
<Importer>XmlImporter</Importer>
<Processor>PassThroughProcessor</Processor>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
However, I m building the conntent project on the fly so I need the following code to create the project and add the items
var projectPath = Path.Combine(buildDirectory, "content.contentproj");
_projectRootElement = ProjectRootElement.Create(projectPath);
_projectRootElement.AddImport("$(MSBuildExtensionsPath)\\Microsoft\\XNA Game Studio\\v4.0\\Microsoft.Xna.GameStudio.ContentPipeline.targets");
_contentProject = new Project(_projectRootElement);
_contentProject.SetProperty("XnaPlatform", "Windows");
_contentProject.SetProperty("XnaProfile", "HiDef");
_contentProject.SetProperty("XnaFrameworkVersion", "v4.0");
_contentProject.SetProperty("Configuration", "Debug");
_contentProject.SetProperty("OutputPath", _outputDirectory);
// Register any custom importers or processors.
foreach (string pipelineAssembly in PipelineAssemblies)
{
_contentProject.AddItem("Reference", pipelineAssembly);
}
// Hook up our custom error logger.
_errorLogger = new ErrorLogger();
_buildParameters = new BuildParameters(ProjectCollection.GlobalProjectCollection)
{
Loggers = new ILogger[] { _errorLogger, }
};
//.... removed code that is not required the following is code that adds each item to the project. In case of items that shoulndt compile I m using None (as in xml from project above)
var itemType = compile ? "Compile" : "None";
var items = _contentProject.AddItem(itemType, filename);
var item = items.SingleOrDefault(x => x.EvaluatedInclude.Equals(filename, StringComparison.InvariantCultureIgnoreCase));
item.SetMetadataValue("Link", Path.GetFileName(filename));
item.SetMetadataValue("Name", Path.GetFileNameWithoutExtension(filename));
if (!compile)
item.SetMetadataValue("CopyToOutputDirectory", "Always");
Finally the build code
BuildManager.DefaultBuildManager.BeginBuild(_buildParameters);
var request = new BuildRequestData(_contentProject.CreateProjectInstance(), new string[0]);
var submission = BuildManager.DefaultBuildManager.PendBuildRequest(request);
var execute = Task.Factory.StartNew(() => submission.ExecuteAsync(null, null), cancellationTokenSource.Token);
var endBuild = execute.ContinueWith(ant => BuildManager.DefaultBuildManager.EndBuild());
endBuild.Wait();
In BuildRequest the empty array takes parameters that are targets... I ve been going through many different targets that do different things, from building but not really outputing the files to copy dependency dlls into the main folder but not what I need
Cheers

Related

How to add custom roslyn analyzer from locally placed DLL?

I have created a Roslyn Analyzer project which generates a nuget package and DLL of it. I want to use that DLL in a standalone code analysis project. How can i do that? For example i have following code:
MSBuildLocator.RegisterDefaults();
var filePath = #"C:\Users\user\repos\ConsoleApp\ConsoleApp.sln";
var msbws = MSBuildWorkspace.Create();
var soln = await msbws.OpenSolutionAsync(filePath);
var errors = new List<Diagnostic>();
foreach (var proj in soln.Projects)
{
var analyzer = //Here i want to load analyzer from DLL present locally.
var compilation = await proj.GetCompilationAsync();
var compWithAnalyzer = compilation.WithAnalyzers(analyzer.GetAnalyzersForAllLanguages());
var res = compWithAnalyzer.GetAllDiagnosticsAsync().Result;
errors.AddRange(res.Where(r => r.Severity == DiagnosticSeverity.Error).ToList());
}
I have tried following
var analyzer = new AnalyzerFileReference("Path to DLL", new AnalyzerAssemblyLoader());
But here AnalyzerAssemblyLoader shows error as it is inaccessible to to its protection level (class is internal).
Kindly suggest me if we can do this.
the .WithAnalyzers() option will allow you to pass an instance of an analyzer. If you're referencing the DLL locally, you can just create the analyzer like you would any other object and pass it to the compilation.
var analyzer = new MyAnalyzer();
var compilation = await proj.GetCompilationAsync();
var compWithAnalyzer = compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(analyzer));
If you're not referencing the assembly, but want to load it at runtime, you can use the usual System.Reflection based methods to get an instance of the analyzer:
var assembly = Assembly.LoadFrom(#"<path to assembly>.dll");
var analyzers = assembly.GetTypes()
.Where(t => t.GetCustomAttribute<DiagnosticAnalyzerAttribute>() is object)
.Select(t => (DiagnosticAnalyzer) Activator.CreateInstance(t))
.ToArray();
compWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzers));

Exception when listing created sub-directory in documents directory

I am using the official API to list a directory I had created for some purpose and have pushed few files into it. Now I want to list them out.
Here's how I am creating first
final Directory extDir = await getApplicationDocumentsDirectory();
// final String dirPath = '${extDir.path}/Movies/flutter_test';
final String dirPath = '${extDir.path}/mydir';
await new Directory(dirPath).create(recursive: true);
Here's what I am doing to read in a different page.
final Directory extDir = await getApplicationDocumentsDirectory();
// final String dirPath = '${extDir.path}/Pictures/flutter_test';
final String dirPath = '${extDir.path}/mydir';
final String thumbDirPath = '$dirPath/thumbs';
final Directory imgDir = Directory.fromUri(Uri.file(dirPath));
dirExists = imgDir.existsSync();
fileCount = 0;
if(dirExists) {
print("my dir exists");
// thumbDir.list(recursive: false, followLinks: false)
fileCount = await imgDir.list(recursive: false).length;
print('mydir images count $fileCount');
if(fileCount > 1) { // we think one is always the directory itself?
try {
imgDir.list(recursive: false, followLinks: false)
.listen((FileSystemEntity entity) {
The code breaks here giving the following exception message
type '() => Null' is not a subtype of type '(Object) => FutureOr<dynamic>'
I have to also tell you this doesn't happen when I am listing the external storage directory. Am I doing something wrong while creating my directory?
EDIT
I have now upgraded flutter to 0.7.3 on my Mac, and have a new problem altogether. The application won't run at all.
Okay, I found a solution myself. Apparently tinkering with the large provider API has given me different way to list the items, which actually told me I was trying to run listeners twice.
fileCount = await imgDir.list(recursive: false).length;
and
imgDir.list(recursive: false, followLinks: false)
.listen((FileSystemEntity entity) {
from original code cannot be written one after the other, as the first one finishes the listener. This I found out by altering my own code and listing directories this way instead.
Stream<FileSystemEntity> files = imgDir.list(recursive: false, followLinks: false);
will add as many events as many files according to API.. and then create a regular generic list
List<FileSystemEntity> entities = await files.toList();
and then you perform usual iteration using iterator or for() loop like
for(FileSystemEntity entity in entities) {
... // every file now is available

Is it possible to read MSI properties from MSI files in chain before detect phase in bootstrapper?

In "Bundle.wxs" I have a "chain" with "MsiPackages" which contain "InstallConditions". In order for the user to decide which packages he/she wants installed/upgraded I would like to display properties found in them.
For instance, I want to read the property "ProductName" and "ProductVersion" in the "Property" table of every MSI in the chain and display it to the user next to a checkbox for every MSI in the chain. The checkbox is wired to the burn variable used in "InstallConditions".
But the problem is, it doesn't seem like I have access to the MSI files before the "Apply" step. They are not extracted from the Bootstrapper Application EXE before this step. So, my question is, Is there a way to load these values programatically in order to display them to the user before the Apply step? I could use variables and populate them myself with the values but this information is already in the MSI so this seems inefficient.
Is there a way to do this? Thanks for the help.
<Bundle>
<Variable Name="InstallProduct1" Type="string" Value="true" />
<Variable Name="ProductName1" Type="string" Value="My Product 1"/> <!-- Better way? -->
<Variable Name="ProductVersion1" Type="version" Value="1.2.3.4"/> <!-- Better way? -->
<Chain>
<MsiPackage SourceFile="my_product_1.msi"
InstallCondition="InstallProduct1">
</MsiPackage>
</Chain>
</Bundle>
WiX does generate a BootstrapperApplicationData.xml file which includes a lot of the information used to build the exe and is included in the files available at runtime. You can parse that file at runtime in order to access that metadata. Since the file, along with all of our assemblies and .msi files, are placed in a randomly-name temp folder, we can’t know ahead of time where the file will live, so we must use our assembly’s path to find it. You can then parse the XML to get the metadata.
I have a blog post with additional details here: https://www.wrightfully.com/part-3-of-writing-your-own-net-based-installer-with-wix-context-data/
In my case, I use the tag instead of '', so have some of the info available to me may not be there for you, so your experience may vary. I would suggest running a makeshift installer in debug mode and setting a breakpoint to inspect the contents of the XML in order to get a full list of what’s available.
Here’s an example of how I get data from the file in my ManagedBootstrapperApplication (c#). Note: in this example, my domain objects are MBAPrereqPackage, BundlePackage and PackageFeature, each of which take an XML node object in their constructor and further parse the data into the object’s properties.
const XNamespace ManifestNamespace = ( XNamespace) “http://schemas.microsoft.com/wix/2010/BootstrapperApplicationData” ;
public void Initialize()
{
//
// parse the ApplicationData to find included packages and features
//
var bundleManifestData = this.ApplicationData;
var bundleDisplayName = bundleManifestData
.Element(ManifestNamespace + “WixBundleProperties“ )
.Attribute( “DisplayName“)
.Value;
var mbaPrereqs = bundleManifestData.Descendants(ManifestNamespace + “WixMbaPrereqInformation“)
.Select(x => new MBAPrereqPackage(x))
.ToList();
//
//exclude the MBA prereq packages, such as the .Net 4 installer
//
var pkgs = bundleManifestData.Descendants(ManifestNamespace + “WixPackageProperties“)
.Select(x => new BundlePackage(x))
.Where(pkg => !mbaPrereqs.Any(preReq => preReq.PackageId == pkg.Id));
//
// Add the packages to a collection of BundlePackages
//
BundlePackages.AddRange(pkgs);
//
// check for features and associate them with their parent packages
//
var featureNodes = bundleManifestData.Descendants(ManifestNamespace + “WixPackageFeatureInfo“);
foreach ( var featureNode in featureNodes)
{
var feature = new PackageFeature(featureNode);
var parentPkg = BundlePackages.First(pkg => pkg.Id == feature.PackageId);
parentPkg.AllFeatures.Add(feature);
feature.Package = parentPkg;
}
}
///
/// Fetch BootstrapperApplicationData.xml and parse into XDocument.
///
public XElement ApplicationData
{
get
{
var workingFolder = Path.GetDirectoryName(this.GetType().Assembly.Location);
var bootstrapperDataFilePath = Path.Combine(workingFolder, “BootstrapperApplicationData.xml”);
using (var reader = new StreamReader(bootstrapperDataFilePath))
{
var xml = reader.ReadToEnd();
var xDoc = XDocument.Parse(xml);
return xDoc.Element(ManifestNamespace + “BootstrapperApplicationData“);
}
}
}

Generate HBM files using mapping by code

I'm using NHibernate mapping by code and I'm creating the session factory in this way:
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
const bool executeScript = false;
var configuration = new Configuration();
configuration.DataBaseIntegration(c =>
{
c.Dialect<MsSql2005Dialect>();
c.ConnectionString =
ConfigurationManager.ConnectionStrings["ShopConnectionString"]
.ConnectionString;
c.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
});
configuration.AddMapping(domainMapping);
_sessionFactory = configuration.BuildSessionFactory();
I need to get the corresponding HBM files.
How can I achieve that?
Two ways:-
//This will write all the XML into the bin/mappings folder
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();
be careful of this method above as your asp.net app will recycle as changes are detected in your bin folder, another way is:-
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
//you could add a breakpoint here!
var mappingXml = mapping.AsString();
Use the AsString() extension method:
domainMapping.AsString()
It will give you the xml which you can save into a file. You can call that method e.g. before you build the SessionFactory.

ms Build engine find existing item or remove duplicate items

I am currently working on an application that generates C# code files and adds them to an existing project. To edit the project I'm using Microsoft.Build.BuildEngine, and loading the exisiting .csproj file into the Project class.
csproj = new Project(new Engine(),"3.5");
csproj.Load("/myproject.csproj");
After wich I am able to add refeneces and compile items as I need. However I do not know if the files or references are already in the .csproj file, so currently they get added multiple times, which I do not want.
for example:
<Reference Include="System" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Core" />
Does anyone know of a way I can check if a builditem exists before I add it to the project? Or of a way to remove duplicated builditems after I've added them?
You could iterate through the EvaluatedItems of your Project Object
This sample code demonstrates how to check builditem existence before adding it :
// Create a new Engine object.
Engine engine = new Engine(Environment.CurrentDirectory);
// Create a new Project object.
Project csproj = new Project(new Engine(),"3.5");
csproj.Load("/myproject.csproj");
BuildItemGroup referenceItemGroup;
var currentReferences = new HashSet<string>();
// Iterate through each ItemGroup in the Project.
foreach (BuildItemGroup ig in csproj.ItemGroups)
{
// Iterate through each Item in the ItemGroup.
foreach (BuildItem item in ig)
{
if (item.Name == "Reference")
{
currentReferences.Add(item.Include);
referenceItemGroup = ig;
}
}
}
// Add only non existing reference
if (!currentReferences.Contains("NewReferenceToAdd"))
{
if (referenceItemGroup != null)
{
referenceItemGroup.AddNewItem("Reference", "IncludeValue");
}
}
And this one how to remove duplicates item
// Create a new Engine object.
Engine engine = new Engine(Environment.CurrentDirectory);
// Create a new Project object.
Project csproj = new Project(new Engine(),"3.5");
csproj.Load("/myproject.csproj");
var currentReferences = new HashSet<string>();
// Iterate through each ItemGroup in the Project.
foreach (BuildItemGroup ig in csproj.ItemGroups)
{
var itemsToRemove = new List<BuildItem>;
// Iterate through each Item in the ItemGroup.
foreach (BuildItem item in ig)
{
if (item.Name == "Reference")
{
if (currentReferences.Contains(item.Include))
{
itemsToRemove.Add(item);
}
else
{
currentReferences.Add(item.Include);
}
}
}
// Remove duplicate items
foreach (BuildItem itemToRemove in itemsToRemove)
{
ig.RemoveItem(itemToRemove);
}
}