Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
[Bb]in/
[Oo]bj/

.vs/

# mstest test results
TestResults

Expand Down Expand Up @@ -106,3 +108,4 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
App_Data/
62 changes: 62 additions & 0 deletions Our.Umbraco.Community.PowerShellModule/ConsoleApplicationBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Linq;
using Umbraco.Core;

namespace Our.Umbraco.Community.PowerShellModule
{
/// <summary>
/// Extends the UmbracoApplicationBase, which is needed to start the application with out own BootManager.
/// </summary>
public class ConsoleApplicationBase : UmbracoApplicationBase
{
public string BaseDirectory { get; private set; }
public string DataDirectory { get; private set; }

protected override IBootManager GetBootManager()
{
var binDirectory = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, "bin"));
BaseDirectory = ResolveBasePath(binDirectory);
DataDirectory = Path.Combine(BaseDirectory, "app_data");
var appDomainConfigPath = new DirectoryInfo(Path.Combine(BaseDirectory, "config"));

//Copy config files to AppDomain's base directory
if (binDirectory.FullName.Equals(BaseDirectory) == false &&
appDomainConfigPath.Exists == false)
{
appDomainConfigPath.Create();
var baseConfigPath = new DirectoryInfo(Path.Combine(BaseDirectory, "config"));
var sourceFiles = baseConfigPath.GetFiles("*.config", SearchOption.TopDirectoryOnly);
foreach (var sourceFile in sourceFiles)
{
sourceFile.CopyTo(sourceFile.FullName.Replace(baseConfigPath.FullName, appDomainConfigPath.FullName), true);
}
}

AppDomain.CurrentDomain.SetData("DataDirectory", DataDirectory);

return new ConsoleBootManager(this, BaseDirectory);
}

public void Start(object sender, EventArgs e)
{
base.Application_Start(sender, e);
}

private string ResolveBasePath(DirectoryInfo currentFolder)
{
var folders = currentFolder.GetDirectories();
if (folders.Any(x => x.Name.Equals("app_data", StringComparison.OrdinalIgnoreCase)) &&
folders.Any(x => x.Name.Equals("config", StringComparison.OrdinalIgnoreCase)))
{
return currentFolder.FullName;
}

if (currentFolder.Parent == null)
throw new Exception("Base directory containing an 'App_Data' and 'Config' folder was not found."+
" These folders are required to run this console application as it relies on the normal umbraco configuration files.");

return ResolveBasePath(currentFolder.Parent);
}
}
}
39 changes: 39 additions & 0 deletions Our.Umbraco.Community.PowerShellModule/ConsoleBootManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Umbraco.Core;
using umbraco.editorControls;
using umbraco.interfaces;

namespace Our.Umbraco.Community.PowerShellModule
{
/// <summary>
/// Extends the CoreBootManager for use in this Console app.
/// </summary>
public class ConsoleBootManager : CoreBootManager
{
public ConsoleBootManager(UmbracoApplicationBase umbracoApplication, string baseDirectory)
: base(umbracoApplication)
{
//This is only here to ensure references to the assemblies needed for the DataTypesResolver
//otherwise they won't be loaded into the AppDomain.
var interfacesAssemblyName = typeof(IDataType).Assembly.FullName;
var editorControlsAssemblyName = typeof(uploadField).Assembly.FullName;

base.InitializeApplicationRootPath(baseDirectory);
}

/// <summary>
/// Can be used to initialize our own Application Events
/// </summary>
protected override void InitializeApplicationEventsResolver()
{
base.InitializeApplicationEventsResolver();
}

/// <summary>
/// Can be used to add custom resolvers or overwrite existing resolvers once they are made public
/// </summary>
protected override void InitializeResolvers()
{
base.InitializeResolvers();
}
}
}
37 changes: 37 additions & 0 deletions Our.Umbraco.Community.PowerShellModule/CreateUmbracoCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;

namespace Our.Umbraco.Community.PowerShellModule
{
class UmbracoInstanceContainer
{
internal static UmbracoInstance instance = null;

internal static UmbracoInstance Instance
{
get
{
if (instance == null)
{
instance = new UmbracoInstance();
instance.Start();
}

return instance;
}
}
}

[Cmdlet("Create", "UmbracoInstance")]
public class CreateUmbracoCmdlet : PSCmdlet
{
protected override void ProcessRecord()
{
WriteObject(UmbracoInstanceContainer.Instance);
}
}
}
17 changes: 17 additions & 0 deletions Our.Umbraco.Community.PowerShellModule/GetGreeting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Management.Automation;

namespace Our.Umbraco.Community.PowerShellModule
{
[Cmdlet("Get", "Greeting")]
public class GetGreeting : PSCmdlet
{
static int greets = 0;

[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)] public string Greetee { get; set; } = "Stranger";

protected override void ProcessRecord()
{
WriteObject($"Hi there, {Greetee} #{++greets}!");
}
}
}
Loading