public class FtpDeployer : IDeployer
{
public const string DeployerKey = "ftp";
private readonly string _hostname;
private readonly int _port;
private readonly string _username;
private readonly string _password;
private readonly string _folder;
public FtpDeployer(Dictionary<string, string> parameters)
{
_hostname = parameters["Hostname"];
_username = parameters["Username"];
_password = parameters["Password"];
_folder = parameters["Folder"];
_port = 21;
int.TryParse(parameters["Port"], out _port);
}
public virtual Task<XStaticResult> DeployWholeSite(string folderPath)
{
return TaskHelper.FromResultOf(() =>
{
return Deploy(folderPath);
});
}
public virtual XStaticResult Deploy(string folderPath)
{
try
{
// Do the upload
}
catch (Exception e)
{
return XStaticResult.Error("Error deploying the site using FTP.", e);
}
return XStaticResult.Success("Site deployed using FTP.");
}
}
public class FtpDeployerDefinition : IDeployerDefinition
{
public string Id => FtpDeployer.DeployerKey;
public string Name => "FTP";
public string Help => "The remote FTP files will be mirrored to match the generated site.";
public IEnumerable<string> Fields => new[]
{
"Hostname",
"Username",
"Password",
"Folder",
"Port"
};
}
public class FtpAutoInstaller : IDeployerAutoInstaller
{
public IDeployerDefinition Definition => new FtpDeployerDefinition();
public Func<Dictionary<string, string>, IDeployer> Constructor => (x) => new FtpDeployer(x);
}
The second way to ensure that your custom deployers appear in xStatic is to manually add your deployer to the DeployServiceBuilder. While you can do this directly in the startup class, the recommended way is to create an extension method as shown below.
public static IDeployServiceBuilder AddFtpDeployer(this IDeployServiceBuilder builder)
{
builder.AddDeployer(new FtpDeployerDefinition(), (x) => new FtpDeployer(x));
return builder;
}
This can then be added in the startup class like below
var builder = services.AddXStatic();
builder.DeployServiceBuilder.AddFtpDeployer();
// You'll need to manually register more stuff too...
builder.Build();
I build websites based on the Sitecore and Umbraco CMS platforms. I have over 10 years of C# and JavaScript experience. I've also released a few open source projects and like to take photos.
Every component I've built for this site I've open sourced and released as the free UmbracoFolio package. This can be installed and used for free from the packages section within Umbraco.
© Sam Mullins 2024