internal void Save(bool cleanIntermediateFiles)
{
// clear out the nuspec files node.
_nuSpec.files = null;
var temporaryFiles = new List<string>();
var files = _nuSpec.Add("files");
// default xamlUi
var xamlText = _defaultUIProperties;
if (xamlText.Is()) {
var targetFilename = @"default-propertiesui.xml";
var xamlPath = Path.Combine(Directory, targetFilename);
xamlPath.TryHardToDelete();
File.WriteAllText(xamlPath, xamlText);
temporaryFiles.Add(xamlPath);
AddFileToNuSpec(xamlPath, @"\build\native\{0}".format(targetFilename));
GetTargetsProject("native").Xml.AddItemGroup().AddItem("PropertyPageSchema", @"$(MSBuildThisFileDirectory)\{0}".format(targetFilename));
}
// generated xaml
var xaml = GenerateSettingsXaml();
if (xaml != null) {
var targetFilename = @"{0}-propertiesui-{1}.xml".format(_pkgName, Guid.NewGuid());
var xamlPath = Path.Combine(Directory, targetFilename);
xamlPath.TryHardToDelete();
Event<Verbose>.Raise("NugetPackage.Save", "Saving xaml file [{0}].", xamlPath);
xaml.Save(xamlPath);
temporaryFiles.Add(xamlPath);
AddFileToNuSpec(xamlPath, @"\build\native\{0}".format(targetFilename));
GetTargetsProject("native").Xml.AddItemGroup().AddItem("PropertyPageSchema", @"$(MSBuildThisFileDirectory)\{0}".format(targetFilename));
}
foreach (var framework in _props.Keys) {
var prj = _props[framework];
if(prj.Xml.Children.Count > 0) {
prj.FullPath.TryHardToDelete();
if (prj.Save()) {
temporaryFiles.Add(prj.FullPath);
AddFileToNuSpec(prj.FullPath, @"\build\{0}\{1}".format(framework, prj.Filename));
}
}
}
foreach(var framework in _targets.Keys) {
var prj = _targets[framework];
if(prj.Xml.Children.Count > 0) {
prj.FullPath.TryHardToDelete();
if (prj.Save()) {
temporaryFiles.Add(prj.FullPath);
AddFileToNuSpec(prj.FullPath, @"\build\{0}\{1}".format(framework, prj.Filename));
}
}
}
// save the /build/configurations.autopkg file
var configurationsFilename = @"configurations.autopkg";
var cfgPath = Path.Combine(Directory, configurationsFilename );
cfgPath.TryHardToDelete();
SaveConfigurationFile(cfgPath);
temporaryFiles.Add(cfgPath);
AddFileToNuSpec(cfgPath, @"\build\{0}".format(configurationsFilename));
var publisherInfoFilename = @"publisher-info.txt";
var pifPath = Path.Combine(Directory, publisherInfoFilename);
pifPath.TryHardToDelete();
SavePifFile(pifPath);
temporaryFiles.Add(pifPath);
AddFileToNuSpec(pifPath, @"\build\{0}".format(publisherInfoFilename));
Event<Verbose>.Raise("NugetPackage.Save", "Saving nuget spec file to [{0}].", FullPath);
foreach(var src in _files.Keys) {
AddFileToNuSpec(_files[src], src );
}
string tags = _nuSpec.metadata.tags;
tags = tags.Replace(",", " ");
if (tags.IndexOf("nativepackage") == -1) {
tags = tags + " nativepackage";
}
_nuSpec.metadata.tags = tags;
switch (PkgRole) {
// do any last minute stuff here.
case "default":
// add a dependency to the redist package.
var redistPkg = _packageScript.GetNugetPackage("redist");
if (redistPkg != null && !redistPkg._files.Values.IsNullOrEmpty()) {
// add the dependency to the list
var node = _nuSpec.metadata.dependencies.Add("dependency");
node.Attributes.id = redistPkg._pkgName;
node.Attributes.version = (string)_nuSpec.metadata.version;
;
var targets = GetTargetsProject("native");
targets.BeforeSave += () => {
ProjectPropertyGroupElement ppge = null;
foreach (var p in Pivots.Values) {
if (!p.IsBuiltIn) {
ppge = targets.AddPropertyInitializer("{0}-{1}".format(p.Name, redistPkg.SafeName), "", "$({0}-{1})".format(p.Name, SafeName), ppge);
}
}
};
}
break;
}
_nuSpec.Save(FullPath);
temporaryFiles.Add(FullPath);
if (PkgRole == "default" || !_files.Values.IsNullOrEmpty()) {
// don't save the package if it has no files in it.
NuPack(FullPath);
}
if (cleanIntermediateFiles) {
temporaryFiles.ForEach( FilesystemExtensions.TryHardToDelete );
}
}