Last active
October 27, 2019 15:45
-
-
Save Xeinaemm/6c491584e39e0b6b9269fdc659ffb537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#addin "Cake.FileHelpers&version=3.2.0" | |
#addin "Cake.Powershell&version=0.4.8" | |
#addin "Cake.IIS&version=0.4.2" | |
#addin "Microsoft.Win32.Registry&version=4.0.0.0" | |
#addin "System.Reflection.TypeExtensions&version=4.1.0.0" | |
void CreateIISWebsites( | |
string root = "", | |
string glob = "**/*.csproj", | |
bool aspNetCore = false, | |
bool inProcess = true) | |
{ | |
var env = Argument<string>("Environment", "development"); | |
var websitePath = $"C:\\iis\\{env}"; | |
var websiteName = Argument<string>("WebsiteName", "DefaultIISWebsite"); | |
var appPoolName = Argument<string>("ApplicationPoolName", "DefaultIISWebsite"); | |
if(!aspNetCore) | |
{ | |
DefaultIISSetup(websiteName, appPoolName, websitePath, env); | |
} | |
if(aspNetCore && inProcess) | |
{ | |
InProcessSetup(websiteName, appPoolName, websitePath, env); | |
} | |
if(aspNetCore && !inProcess) | |
{ | |
OutOfProcessSetup(websiteName, appPoolName, websitePath, env); | |
} | |
} | |
void DefaultIISSetup( | |
string websiteName, | |
string appPoolName, | |
string websitePath, | |
string env, | |
string root = "", | |
string glob = "**/*.csproj") | |
{ | |
var paths = GetFiles(root + glob, | |
new GlobberSettings | |
{ | |
Predicate = x => | |
!FileExists($"{x.Path.FullPath}/appsettings.json") && | |
FileExists($"{x.Path.FullPath}/Web.config") | |
}); | |
if (paths.Any()) | |
{ | |
CreateDirectory(websitePath); | |
CreatePoolAndWebsite(websiteName, $"{appPoolName}Pool", websitePath); | |
CreateVirtualDirectory(websiteName, env, websitePath); | |
foreach(var path in paths) | |
{ | |
CreateApplicationSite(websiteName, $"{appPoolName}Pool", env, path); | |
} | |
} | |
} | |
void InProcessSetup( | |
string websiteName, | |
string appPoolName, | |
string websitePath, | |
string env, | |
string root = "", | |
string glob = "**/*.csproj") | |
{ | |
var paths = GetFiles(root + glob, | |
new GlobberSettings | |
{ | |
Predicate = x => | |
FileExists($"{x.Path.FullPath}/appsettings.json") | |
}); | |
if (paths.Any()) | |
{ | |
var port = 81; | |
foreach(var path in paths) | |
{ | |
var projectName = path | |
.GetDirectory() | |
.GetDirectoryName() | |
.Replace(".", string.Empty); | |
CreateDirectory(websitePath); | |
CreatePoolAndWebsite( | |
$"{websiteName}{projectName}", | |
$"{appPoolName}{projectName}Pool", | |
websitePath, "", | |
port++); | |
CreateVirtualDirectory( | |
$"{websiteName}{projectName}", | |
env, | |
websitePath); | |
CreateApplicationSite( | |
$"{websiteName}{projectName}", | |
$"{appPoolName}{projectName}Pool", | |
env, | |
path); | |
} | |
} | |
} | |
void OutOfProcessSetup( | |
string websiteName, | |
string appPoolName, | |
string websitePath, | |
string env, | |
string root = "", | |
string glob = "**/*.csproj") | |
{ | |
var paths = GetFiles(root + glob, | |
new GlobberSettings | |
{ | |
Predicate = x => | |
FileExists($"{x.Path.FullPath}/appsettings.json") | |
}); | |
if (paths.Any()) | |
{ | |
CreateDirectory(websitePath); | |
CreatePoolAndWebsite( | |
$"{websiteName}Core", | |
$"{appPoolName}CorePool", | |
websitePath, | |
"", | |
81); | |
CreateVirtualDirectory( | |
$"{websiteName}Core", | |
env, | |
websitePath); | |
foreach(var path in paths) | |
{ | |
CreateApplicationSite( | |
$"{websiteName}Core", | |
$"{appPoolName}CorePool", | |
env, | |
path); | |
} | |
} | |
} | |
void CreateDirectory(string websitePath) | |
{ | |
StartPowershellScript("New-Item", args => | |
{ | |
args.Append("ItemType", "Directory"); | |
args.Append("Path", websitePath); | |
args.Append("-Force"); | |
}); | |
} | |
void CreatePoolAndWebsite( | |
string websiteName, | |
string appPoolName, | |
string websitePath, | |
string managedRuntimeVersion = "v4.0", | |
int port = 80) | |
{ | |
var appPoolSettings = new ApplicationPoolSettings | |
{ | |
Name = appPoolName, | |
ManagedRuntimeVersion = managedRuntimeVersion | |
}; | |
CreatePool(appPoolSettings); | |
CreateWebsite(new WebsiteSettings | |
{ | |
ApplicationPool = appPoolSettings, | |
Binding = IISBindings.Http | |
.SetHostName("localhost") | |
.SetIpAddress("*") | |
.SetPort(port), | |
Name = websiteName, | |
PhysicalDirectory = websitePath | |
}); | |
} | |
void CreateApplicationSite( | |
string websiteName, | |
string appPool, | |
string env, | |
FilePath path) | |
{ | |
var siteApplication = new ApplicationSettings | |
{ | |
SiteName = websiteName, | |
ApplicationPath = $"/{env}/{path.GetDirectory().GetDirectoryName()}", | |
ApplicationPool = appPool, | |
VirtualDirectory = "/", | |
PhysicalDirectory = path.GetDirectory().FullPath | |
}; | |
if(SiteApplicationExists(siteApplication)) | |
{ | |
RemoveSiteApplication(siteApplication); | |
AddSiteApplication(siteApplication); | |
} else | |
{ | |
AddSiteApplication(siteApplication); | |
} | |
} | |
void CreateVirtualDirectory( | |
string websiteName, | |
string env, | |
string physicalDirectory) | |
{ | |
var virtualDirectory = new VirtualDirectorySettings | |
{ | |
SiteName = websiteName, | |
PhysicalDirectory = physicalDirectory, | |
ApplicationPath = "/", | |
Path = $"/{env}" | |
}; | |
if(SiteVirtualDirectoryExists("", virtualDirectory)) | |
{ | |
RemoveSiteVirtualDirectory(virtualDirectory); | |
AddSiteVirtualDirectory(virtualDirectory); | |
} else | |
{ | |
AddSiteVirtualDirectory(virtualDirectory); | |
} | |
} | |
Task("CreateIISWebsites").Does(() => | |
{ | |
CreateIISWebsites(); //ASP.NET | |
CreateIISWebsites("", "**/*.csproj", true); //ASP.NET Core InProcess | |
//CreateIISWebsites("", "**/*.csproj", true, false); //ASP.NET Core OutOfProcess | |
}); | |
RunTarget(Argument<string>("Target", "")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment