Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for overriding the quotes content on deployment #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
tools/

# Visual Studio 2015 cache/options directory
.vs/
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# Random Quotes
Fun random quotes web app. App is built using Microsoft's ASP.NET core framework and it's very simple.

## Build

There is a Cake script for building the Random Quotes application in the root folder.

The Octo .NET Core CLI extension must be installed prior to running the build, using the command line

```shell
dotnet tool install Octopus.DotNet.Cli --tool-path .\tools\
```

Running locally will place the resulting Zip package into a folder called `LocalPackages`, as a sibling to the repo's folder.
8 changes: 8 additions & 0 deletions RandomQuotes/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ public class AppSettings
{
public string AppVersion { get; set; }
public string EnvironmentName { get; set; }
public string TenantName { get; set; }
public QuoteRecord[] CustomQuotes { get; set; }
}

public class QuoteRecord
{
public string Quote { get; set; }
public string Author { get; set; }
}
}
20 changes: 16 additions & 4 deletions RandomQuotes/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using RandomQuotes.Models;

namespace RandomQuotes
Expand Down Expand Up @@ -53,10 +54,21 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

var quoteFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot{Path.DirectorySeparatorChar}data{Path.DirectorySeparatorChar}quotes.txt");
Quote.Quotes = File.Exists(quoteFilePath) ? File.ReadAllLines(quoteFilePath).Select(System.Net.WebUtility.HtmlDecode).ToList() : new List<string>();
var authorFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot{Path.DirectorySeparatorChar}data{Path.DirectorySeparatorChar}authors.txt");
Quote.Authors = File.Exists(authorFilePath) ? File.ReadAllLines(authorFilePath).Select(System.Net.WebUtility.HtmlDecode).ToList() : new List<string>();
var service = app.ApplicationServices.GetService(typeof(IOptions<AppSettings>));
var settings = ((IOptions<AppSettings>)service).Value;

if (settings.CustomQuotes != null && settings.CustomQuotes.Any())
{
Quote.Quotes = settings.CustomQuotes.Select(x => x.Quote).ToList();
Quote.Authors = settings.CustomQuotes.Select(x => x.Author).ToList();
}
else
{
var quoteFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot{Path.DirectorySeparatorChar}data{Path.DirectorySeparatorChar}quotes.txt");
Quote.Quotes = File.Exists(quoteFilePath) ? File.ReadAllLines(quoteFilePath).Select(System.Net.WebUtility.HtmlDecode).ToList() : new List<string>();
var authorFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"wwwroot{Path.DirectorySeparatorChar}data{Path.DirectorySeparatorChar}authors.txt");
Quote.Authors = File.Exists(authorFilePath) ? File.ReadAllLines(authorFilePath).Select(System.Net.WebUtility.HtmlDecode).ToList() : new List<string>();
}
}
}
}
13 changes: 10 additions & 3 deletions RandomQuotes/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
@using Microsoft.Extensions.Options;
@inject IOptions<AppSettings> Settings
@{
var title = "Random Quotes";
if (!string.IsNullOrWhiteSpace(Settings.Value.TenantName))
{
title += $" - {Settings.Value.TenantName}";
}
}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>@ViewData["Title"] - Random Quotes</title>
<title>@title</title>

<environment include="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css"/>
Expand All @@ -30,7 +37,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Random Quotes</a>
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">@title</a>
</div>
</div>
</nav>
Expand All @@ -39,7 +46,7 @@
@RenderBody()
<hr/>
<footer>
<p>&copy; 2018 - Random Quotes | Version @Settings.Value.AppVersion running in @Settings.Value.EnvironmentName</p>
<p>&copy; 2018 - @title | Version @Settings.Value.AppVersion running in @Settings.Value.EnvironmentName</p>
</footer>
</div>

Expand Down
3 changes: 2 additions & 1 deletion RandomQuotes/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"AppSettings": {
"AppVersion": "0.0.0",
"EnvironmentName": "DEV"
"EnvironmentName": "DEV",
"CustomQuotes": []
}
}
118 changes: 118 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#tool "nuget:?package=GitVersion.CommandLine&prerelease"

using Path = System.IO.Path;
using IO = System.IO;
using Cake.Common.Tools;

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var octopusServer = Argument("octopusServer", "");
var octopusApikey = Argument("octopusApiKey", "");

var isLocalBuild = string.IsNullOrWhiteSpace(octopusServer);

var packageId = "RandomQuotes";

var publishDir = "./publish";
var artifactsDir = "./artifacts";
var localPackagesDir = "../LocalPackages";

var gitVersionInfo = GitVersion(new GitVersionSettings {
OutputType = GitVersionOutput.Json
});

var nugetVersion = gitVersionInfo.NuGetVersion;

Setup(context =>
{
if(BuildSystem.IsRunningOnTeamCity)
BuildSystem.TeamCity.SetBuildNumber(gitVersionInfo.NuGetVersion);

Information("Building v{0}", nugetVersion);
});

Teardown(context =>
{
Information("Finished running tasks.");
});

Task("__Default")
.IsDependentOn("__Clean")
.IsDependentOn("__Restore")
.IsDependentOn("__Build")
.IsDependentOn("__Publish")
.IsDependentOn("__Pack")
.IsDependentOn("__Push");

Task("__Clean")
.Does(() =>
{
CleanDirectory(artifactsDir);
CleanDirectory(publishDir);
CleanDirectories("./**/bin");
CleanDirectories("./**/obj");
});

Task("__Restore")
.Does(() => DotNetCoreRestore(".", new DotNetCoreRestoreSettings
{
ArgumentCustomization = args => args.Append($"/p:Version={nugetVersion}")
})
);

Task("__Build")
.Does(() =>
{
DotNetCoreBuild(".", new DotNetCoreBuildSettings
{
Configuration = configuration,
ArgumentCustomization = args => args.Append($"/p:Version={nugetVersion}")
});
});

Task("__Publish")
.Does(() =>
{
DotNetCorePublish(".", new DotNetCorePublishSettings
{
Framework = "netcoreapp2.0",
Configuration = configuration,
OutputDirectory = publishDir,
ArgumentCustomization = args => args.Append($"--no-build")
});
});

Task("__Pack")
.Does(() => {

OctoPack(packageId, new OctopusPackSettings {
BasePath = publishDir,
Format = OctopusPackFormat.Zip,
Version = nugetVersion,
OutFolder = artifactsDir,
ToolPath = IsRunningOnUnix() ? Context.Tools.Resolve("dotnet-octo") : Context.Tools.Resolve("dotnet-octo.exe")
});
});

Task("__Push")
.Does(() => {

var packageFile = $"{artifactsDir}\\{packageId}.{nugetVersion}.zip";

if (!isLocalBuild)
{
OctoPush(octopusServer, octopusApikey, packageFile, new OctopusPushSettings {
ToolPath = IsRunningOnUnix() ? Context.Tools.Resolve("dotnet-octo") : Context.Tools.Resolve("dotnet-octo.exe")
});
}
else
{
CreateDirectory(localPackagesDir);
CopyFileToDirectory(packageFile, localPackagesDir);
}
});

Task("Default")
.IsDependentOn("__Default");

RunTarget(target);
24 changes: 24 additions & 0 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@ECHO OFF
REM see http://joshua.poehls.me/powershell-batch-file-wrapper/

SET SCRIPTNAME=%~d0%~p0%~n0.ps1
SET ARGS=%*
IF [%ARGS%] NEQ [] GOTO ESCAPE_ARGS

:POWERSHELL
PowerShell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Unrestricted -Command "& { $ErrorActionPreference = 'Stop'; & '%SCRIPTNAME%' @args; EXIT $LASTEXITCODE }" %ARGS%
EXIT /B %ERRORLEVEL%

:ESCAPE_ARGS
SET ARGS=%ARGS:"=\"%
SET ARGS=%ARGS:`=``%
SET ARGS=%ARGS:'=`'%
SET ARGS=%ARGS:$=`$%
SET ARGS=%ARGS:{=`}%
SET ARGS=%ARGS:}=`}%
SET ARGS=%ARGS:(=`(%
SET ARGS=%ARGS:)=`)%
SET ARGS=%ARGS:,=`,%
SET ARGS=%ARGS:^%=%

GOTO POWERSHELL
Loading