Skip to content

Commit 9e620de

Browse files
phenry20CodingMadeEasy
authored andcommitted
feat(vars): Merge pull request #1 from Cosmic9Studios/feature/varinvar
feat(vars): Added nested variables
2 parents a99f01b + 3119be1 commit 9e620de

File tree

13 files changed

+235
-108
lines changed

13 files changed

+235
-108
lines changed

.github/workflows/build.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Build and Push
2+
on:
3+
push:
4+
branches:
5+
- '*'
6+
- '*/*'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-16.04
11+
steps:
12+
- uses: actions/checkout@v1
13+
- name: Setup dotnet
14+
uses: actions/setup-dotnet@v1
15+
with:
16+
dotnet-version: 3.0.100
17+
18+
- name: Build
19+
run: dotnet build
20+
21+
push:
22+
runs-on: ubuntu-16.04
23+
if: github.ref == 'refs/heads/master'
24+
steps:
25+
- uses: actions/checkout@v1
26+
- name: Setup dotnet
27+
uses: actions/setup-dotnet@v1
28+
with:
29+
dotnet-version: 3.0.100
30+
31+
- name: Semantic Release
32+
uses: codfish/semantic-release-action@master
33+
id: semantic
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Pack and Push
38+
if: steps.semantic.outputs.new-release-published == 'true'
39+
run: |
40+
dotnet pack -p:PackageVersion=$RELEASE_VERSION -o ./
41+
dotnet nuget push ./*.nupkg -k $NUGET_KEY -s https://api.nuget.org/v3/index.json
42+
env:
43+
NUGET_KEY: ${{ secrets.NUGET_KEY }}
44+

.releaserc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"plugins": [
3+
'@semantic-release/commit-analyzer',
4+
'@semantic-release/release-notes-generator',
5+
'@semantic-release/github'
6+
]
7+
}

.vscode/launch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Samples/VinV",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build-vinv-sample",
12+
"program": "${workspaceFolder}/Samples/VariableInVariable/bin/Debug/netcoreapp2.0/VariableInVariable.dll",
13+
"args": [],
14+
"cwd": "${workspaceFolder}",
15+
"console": "internalConsole",
16+
"stopAtEntry": false,
17+
"internalConsoleOptions": "openOnSessionStart",
18+
"env": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
{
23+
"name": ".NET Core Attach",
24+
"type": "coreclr",
25+
"request": "attach",
26+
"processId": "${command:pickProcess}"
27+
}
28+
]
29+
}

.vscode/tasks.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build-vinv-sample",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/Samples/VariableInVariable/VariableInVariable.csproj"
11+
],
12+
"problemMatcher": "$msCompile"
13+
}
14+
]
15+
}

C9S.Configuration.Variables.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netstandard2.0</TargetFramework>
4+
<PackageId>C9S.Configuration.Variables</PackageId>
5+
<Authors>Cosmic9Studios</Authors>
6+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
13+
</ItemGroup>
14+
</Project>

C9S.Extensions.Configuration.sln

Lines changed: 0 additions & 34 deletions
This file was deleted.

C9S.Extensions.Configuration/C9S.Extensions.Configuration.csproj

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using System.Text.RegularExpressions;
54
using Microsoft.Extensions.Configuration;
65

7-
namespace C9S.Extensions.Configuration
6+
namespace C9S.Configuration.Variables
87
{
98
public static class ConfigurationRootExtensions
109
{
@@ -27,7 +26,9 @@ private static void GetAllConfigurationSections(IEnumerable<IConfigurationSectio
2726

2827
private static void VariableResolver(IConfiguration Configuration, string open, string close)
2928
{
30-
var variableRegex = new Regex($@"({open})(?!.*{open})(?<var>[^{close}]+)\{close}");
29+
var open_esc = Regex.Escape(open);
30+
var close_esc = Regex.Escape(close);
31+
var variableRegex = new Regex($@"({open_esc})(?!.*{open_esc})(?<var>[^{close_esc}]+)\{close_esc}");
3132
while (configSections.Any())
3233
{
3334
var currentSection = configSections.First();
@@ -58,7 +59,7 @@ private static void VariableResolver(IConfiguration Configuration, string open,
5859
section = section.GetSection(sec);
5960
}
6061

61-
currentSection.Value = currentSection.Value.Replace($"{{{{{variable}}}}}", section.Value ?? section[key]);
62+
currentSection.Value = currentSection.Value.Replace($"{open}{variable}{close}", section.Value ?? section[key]);
6263
configSections.Add(currentSection);
6364
}
6465
}

README.md

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,115 @@ Becomes
4141

4242
After running
4343

44+
Startup.cs
45+
46+
```cs
47+
public void ConfigureServices(IServiceCollection services)
48+
{
49+
...
50+
51+
Configuration.ResolveVariables();
52+
53+
...
54+
}
55+
```
56+
57+
ResolveVariables will take any variable path and resolve it. It even works across .json files. As long as the data is stored inside the ConfigurationRoot, it is resolvable
58+
59+
## Variable Identification
60+
61+
In the ResolveVariables function you can declare what a variable is (default `{{variable}}`).
62+
63+
Example:
64+
65+
Startup.cs
66+
4467
```cs
45-
IConfigurationRoot configuration = ...
46-
configuration.ResolveVariables();
68+
public void ConfigureServices(IServiceCollection services)
69+
{
70+
...
71+
72+
Configuration.ResolveVariables("${", "}");
73+
74+
...
75+
}
76+
```
77+
78+
appsettitngs.json
79+
80+
```json
81+
{
82+
"General":
83+
{
84+
"HttpListenPort": "3000",
85+
"HttpsListenPort": "3001",
86+
"WebUrl": "https://localhost:${General.HttpsListenPort}"
87+
},
88+
"Pages":
89+
{
90+
"AuthorizeAddress": "${General.WebUrl}/api/beta/authorize",
91+
"DashboardAddress": "${General.WebUrl}/dashboard"
92+
}
93+
}
94+
```
95+
96+
Now anything inside the `${variable}` is considered a variable.
97+
98+
## Nested variables (variables inside variables)
99+
100+
If the need arises you can also resolve variables within other variables
101+
102+
Example:
103+
104+
Assuming `ASPNETCORE_ENVIRONMENT = Development`
105+
106+
appsettings.json
107+
108+
```json
109+
{
110+
"App":
111+
{
112+
"Development": {
113+
"ClientId": "FooId"
114+
}
115+
},
116+
"Auth":
117+
{
118+
"ClientID": "${App.${ASPNETCORE_ENVIRONMENT}.ClientId}"
119+
}
120+
}
121+
```
122+
123+
Will become
124+
125+
```json
126+
{
127+
"App":
128+
{
129+
"Development": {
130+
"ClientId": "FooId"
131+
}
132+
},
133+
"Auth":
134+
{
135+
"ClientID": "${App.Development.ClientId}"
136+
}
137+
}
47138
```
48139

49-
ResolveVariables will take any variable path and resolve it. It even works across .json files. As long as the data is stored inside the ConfigurationRoot, it is resolvable
140+
Which will resolve to
141+
142+
```json
143+
{
144+
"App":
145+
{
146+
"Development": {
147+
"ClientId": "FooId"
148+
}
149+
},
150+
"Auth":
151+
{
152+
"ClientID": "FooId"
153+
}
154+
}
155+
```

Samples/Samples.sln

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)