using Microsoft.Build.Framework;
using System;
using System.IO;
using System.Linq;
namespace Packaging.Targets
{
///
/// Provides extension methods for the interface.
///
public static class TaskItemExtensions
{
///
/// Gets a value indicating whether this item is copied to the publish directory or not.
///
///
/// The item for which to determine whether it is copied or not.
///
///
/// if the file is copied over; otherwise, .
///
public static bool IsPublished(this ITaskItem item)
{
if (item == null)
{
return false;
}
if (!item.MetadataNames.OfType().Contains("CopyToPublishDirectory"))
{
return false;
}
var copyToPublishDirectoryValue = item.GetMetadata("CopyToPublishDirectory");
CopyToDirectoryValue copyToPublishDirectory;
if (!Enum.TryParse(copyToPublishDirectoryValue, out copyToPublishDirectory))
{
return false;
}
return copyToPublishDirectory != CopyToDirectoryValue.DoNotCopy;
}
///
/// Gets the path to where the file is published.
///
///
/// The item for which to determine the publish path.
///
///
/// The path to where the file is published.
///
public static string GetPublishedPath(this ITaskItem item)
{
if (item == null)
{
return null;
}
var link = item.GetMetadata("Link");
if (!string.IsNullOrEmpty(link))
{
return link.Replace("\\", "/");
}
var relativeDirectory = item.GetMetadata("RelativeDir");
var filename = item.GetMetadata("FileName");
var extension = item.GetMetadata("Extension");
return Path.Combine(relativeDirectory, $"{filename}{extension}").Replace("\\", "/");
}
///
/// Gets the path of the file in the Linux filesystem.
///
///
/// The item for which to get the Linux path.
///
///
/// The path to the file on the Linux filesystem.
///
public static string GetLinuxPath(this ITaskItem item)
{
return TryGetValue(item, "LinuxPath");
}
///
/// Gets the file mode of the file in the Linux filesystem.
///
///
/// The item for which to get the file mode.
///
///
/// The file mode of the file on the Linux file system.
///
public static string GetLinuxFileMode(this ITaskItem item)
{
return TryGetValue(item, "LinuxFileMode");
}
///
/// Gets the Linux owner of the file.
///
///
/// The item for which to get the file owner.
///
///
/// The owner of the file.
///
public static string GetOwner(this ITaskItem item)
{
return TryGetValue(item, "Owner", "root");
}
///
/// Gets the Linux group of the file.
///
///
/// The item for which to get the file group.
///
///
/// The group of the file.
///
public static string GetGroup(this ITaskItem item)
{
return TryGetValue(item, "Group", "root");
}
///
/// Gets the version of the RPM dependency.
///
///
/// The task item which represents the RPM dependency.
///
///
/// The version of the dependency.
///
public static string GetVersion(this ITaskItem item)
{
return TryGetValue(item, "Version", null);
}
///
/// Gets a value indicating whether the item should be removed when the
/// program is removed.
///
///
/// The item to inspect.
///
///
/// if the file should be removed; otherwise,
/// .
///
public static bool GetRemoveOnUninstall(this ITaskItem item)
{
var valueString = TryGetValue(item, "RemoveOnUninstall", "false");
bool value;
if (!bool.TryParse(valueString, out value))
{
return false;
}
return value;
}
private static string TryGetValue(ITaskItem item, string name, string @default = null)
{
if (item == null)
{
return @default;
}
if (!item.MetadataNames.OfType().Contains(name))
{
return @default;
}
var linuxPath = item.GetMetadata(name);
return linuxPath;
}
}
}