Skip to content

Commit a831b4c

Browse files
authored
Merge pull request #21 from taori/dev/more-methods-for-ci
Dev/more methods for ci
2 parents 3963007 + 9ef978d commit a831b4c

File tree

9 files changed

+227
-55
lines changed

9 files changed

+227
-55
lines changed

src/.editorconfig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# All files
44
[*]
55
indent_style = tab
6-
dotnet_diagnostic.xUnit2015.severity = silent
6+
dotnet_diagnostic.xUnit2015.severity = none
77
dotnet_diagnostic.NU1701.severity = silent
88

99
# Xml files
@@ -28,3 +28,15 @@ dotnet_naming_style.end_in_async.word_separator =
2828

2929
# CS0067: Das Ereignis "CloseOnEscapeBehavior.CanExecuteChanged" wird nie verwendet.
3030
dotnet_diagnostic.CS0067.severity = silent
31+
32+
# SA1200: Using directives should be placed correctly
33+
dotnet_diagnostic.SA1200.severity = suggestion
34+
35+
# SA1027: Use tabs correctly
36+
dotnet_diagnostic.SA1027.severity = silent
37+
38+
# SA1600: Elements should be documented
39+
dotnet_diagnostic.SA1600.severity = suggestion
40+
41+
# SA1633: File should have header
42+
dotnet_diagnostic.SA1633.severity = none
Lines changed: 155 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System;
1+
// Copyright (c) 2020 Andreas Müller
2+
// This file is a part of Amusoft.VisualStudio.TemplateGenerator and is licensed under Apache 2.0
3+
// See https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator/blob/master/LICENSE for details
4+
5+
using System;
26
using System.Collections.Generic;
37
using System.Linq;
48
using System.Threading;
@@ -7,29 +11,34 @@
711
using CommandDotNet.Attributes;
812
using Generator.Shared.Template;
913
using Generator.Shared.Transformation;
14+
using Newtonsoft.Json;
1015
using NLog;
1116

1217
namespace Generator.Client.CommandLine
1318
{
19+
[ApplicationMetadata(
20+
Name = "amusoft.vs.tg")]
1421
public class ConsoleApplication
1522
{
1623
private static readonly ILogger Log = LogManager.GetLogger(nameof(ConsoleApplication));
1724

1825
[ApplicationMetadata(
19-
Description = "Tries to build a template according the the specified configuration",
20-
Syntax = "build [configName] [option: -s pathToStorage]"
21-
)]
26+
Description = "Tries to build a template according the the specified configuration.",
27+
Syntax = "build [configName] [option: -s pathToStorage]")]
2228
public async Task<int> Build(
2329
[Argument(
24-
Name = "config",
25-
Description = "Name of configuration within configuration store")]
30+
Description = "id or name of the configuration to build.")]
2631
string configurationId,
27-
[Option(ShortName = "s")]
28-
string storage = null
29-
)
32+
[Option(
33+
Description = "Path to storage.",
34+
ShortName = "s",
35+
LongName = "storage")]
36+
string storage = null)
3037
{
3138
if (!ConsoleUtils.TryGetManager(storage, out var manager))
39+
{
3240
return 3;
41+
}
3342

3443
Log.Info($"Trying to build [{configurationId}].");
3544

@@ -49,7 +58,9 @@ public async Task<int> Build(
4958
try
5059
{
5160
Log.Info($"Executing build tool.");
52-
var result = await rewriteTool.ExecuteAsync(CancellationToken.None, new Progress<string>(message => Console.WriteLine(message)));
61+
var result = await rewriteTool.ExecuteAsync(
62+
CancellationToken.None,
63+
new Progress<string>(message => Console.WriteLine(message)));
5364
return result ? 0 : 4;
5465
}
5566
catch (Exception e)
@@ -60,42 +71,28 @@ public async Task<int> Build(
6071
}
6172

6273
[SubCommand]
63-
[ApplicationMetadata(Description = "Entry point for obtaining information")]
64-
public class Get
65-
{
66-
[ApplicationMetadata(Description = "Retrieves a list of all configurations contained in the storage.")]
67-
public async Task<int> Configurations(
68-
[Option(ShortName = "s")]
69-
string storage = null)
70-
{
71-
if (!ConsoleUtils.TryGetManager(storage, out var manager))
72-
return 1;
73-
var configurations = await manager.LoadStorageContentAsync();
74-
for (var index = 0; index < configurations.Length; index++)
75-
{
76-
var configuration = configurations[index];
77-
Console.WriteLine($"(#{index+1:00}): {configuration.ConfigurationName}");
78-
}
79-
80-
return 0;
81-
}
82-
}
83-
84-
[SubCommand]
85-
[ApplicationMetadata(Description = "Entry point for modifying configurations")]
74+
[ApplicationMetadata(Description = "Entry point for configurations.")]
8675
public class Configuration
8776
{
8877
[ApplicationMetadata(Description = "Renames the configuration if it can be found through the given id.")]
8978
public async Task<int> Rename(
90-
[Argument(Description = "id of configuration which can be the position of the configuration, its guid, or the configuration name")]
91-
string id,
92-
[Argument(Description = "new name of the configuration")]
79+
[Argument(
80+
Description = "id of configuration which can be the position of the configuration, its guid, or the configuration name")]
81+
string id,
82+
[Argument(
83+
Description = "new name of the configuration")]
9384
string newName,
94-
[Option(ShortName = "s")]
85+
[Option(
86+
Description = "Path to storage.",
87+
ShortName = "s",
88+
LongName = "storage")]
9589
string storage = null)
9690
{
9791
if (!ConsoleUtils.TryGetManager(storage, out var manager))
92+
{
9893
return 1;
94+
}
95+
9996
try
10097
{
10198
var configuration = await manager.GetConfigurationByIdAsync(id);
@@ -108,6 +105,127 @@ public async Task<int> Rename(
108105
return 2;
109106
}
110107
}
108+
109+
[ApplicationMetadata(Description = "Saves the local configuration to the specified location.")]
110+
public async Task<int> Export(
111+
[Argument(
112+
Description = "export path")]
113+
string newPath)
114+
{
115+
if (!ConsoleUtils.TryGetManager(null, out var manager))
116+
{
117+
Log.Error($"Failed to obtain local storage manager.");
118+
return 1;
119+
}
120+
121+
try
122+
{
123+
var configurations = await manager.LoadStorageContentAsync();
124+
if (!await manager.SaveConfigurationsAsync(configurations, newPath))
125+
{
126+
Log.Error($"Failed to save configurations to path [{newPath}].");
127+
return 3;
128+
}
129+
130+
return 0;
131+
}
132+
catch (Exception e)
133+
{
134+
Log.Error(e.Message);
135+
return 2;
136+
}
137+
}
138+
139+
[SubCommand]
140+
[ApplicationMetadata(Description = "Get actions.")]
141+
public class Get
142+
{
143+
[ApplicationMetadata(Description = "Retrieves a list of all configurations contained in the storage.")]
144+
public async Task<int> All(
145+
[Option(
146+
Description = "Path to storage.",
147+
ShortName = "s",
148+
LongName = "storage")]
149+
string storage = null)
150+
{
151+
if (!ConsoleUtils.TryGetManager(storage, out var manager))
152+
{
153+
return 1;
154+
}
155+
156+
var configurations = await manager.LoadStorageContentAsync();
157+
for (var index = 0; index < configurations.Length; index++)
158+
{
159+
var configuration = configurations[index];
160+
Console.WriteLine($"(#{index + 1:00}): {configuration.ConfigurationName}");
161+
}
162+
163+
return 0;
164+
}
165+
166+
[ApplicationMetadata(Description = "Gets the json of a configuration identified by the given ID")]
167+
public async Task<int> Json(
168+
[Argument(
169+
Description = "id of configuration which can be the position of the configuration, its guid, or the configuration name")]
170+
string id,
171+
[Option(
172+
Description = "Path to storage.",
173+
ShortName = "s",
174+
LongName = "storage")]
175+
string storage = null)
176+
{
177+
if (!ConsoleUtils.TryGetManager(storage, out var manager))
178+
{
179+
return 1;
180+
}
181+
182+
try
183+
{
184+
var configuration = await manager.GetConfigurationByIdAsync(id);
185+
var serialized = JsonConvert.SerializeObject(configuration, Formatting.Indented);
186+
Console.WriteLine(serialized);
187+
return 0;
188+
}
189+
catch (Exception e)
190+
{
191+
Log.Error(e.Message);
192+
return 2;
193+
}
194+
}
195+
}
196+
197+
[SubCommand]
198+
[ApplicationMetadata(Description = "Import actions.")]
199+
public class Import
200+
{
201+
[ApplicationMetadata(Description = "Updates the local storage from a given path.")]
202+
public async Task<int> FromFile(
203+
[Argument(
204+
Description = "Path to storage file.")]
205+
string storagePath)
206+
{
207+
if (!ConsoleUtils.TryGetManager(null, out var currentManager))
208+
{
209+
Log.Error("Failed to obtain local manager instance.");
210+
return 1;
211+
}
212+
213+
if (!ConsoleUtils.TryGetManager(storagePath, out var remoteManager))
214+
{
215+
Log.Error($"Failed to obtain remote manager instance using path [{storagePath}].");
216+
return 2;
217+
}
218+
219+
var remoteContents = await remoteManager.LoadStorageContentAsync();
220+
if (!await currentManager.SaveConfigurationsAsync(remoteContents))
221+
{
222+
Log.Error($"Failed to update local manager instance.");
223+
return 3;
224+
}
225+
226+
return 0;
227+
}
228+
}
111229
}
112230
}
113231
}

src/Generator.Client.CommandLine/ConsoleUtils.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System;
1+
// Copyright 2020 Andreas Müller
2+
// This file is a part of Amusoft.VisualStudio.TemplateGenerator and is licensed under Apache 2.0
3+
// See https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator/blob/master/LICENSE for details
4+
5+
using System;
6+
using System.IO;
27
using Generator.Shared.Template;
38
using NLog;
49

@@ -12,7 +17,16 @@ public static bool TryGetManager(string storage, out ConfigurationManager config
1217
{
1318
try
1419
{
15-
configurationManager = storage == null ? ConfigurationManager.Default() : ConfigurationManager.FromPath(storage);
20+
if (!string.IsNullOrEmpty(storage) && !File.Exists(storage))
21+
{
22+
Log.Error($"File [{storage}] does not exist.");
23+
configurationManager = null;
24+
return false;
25+
}
26+
27+
configurationManager = storage == null
28+
? ConfigurationManager.Default()
29+
: ConfigurationManager.FromPath(storage);
1630
return true;
1731
}
1832
catch (Exception e)

src/Generator.Client.CommandLine/Dependencies/FileDialogService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ namespace Generator.Client.CommandLine.Dependencies
66
public class FileDialogService : IFileDialogService
77
{
88
/// <inheritdoc />
9-
public bool OpenFileDialog(out string path, string filter, bool @readonly = false, bool multiSelect = false,
9+
public bool OpenFileDialog(
10+
out string path,
11+
string filter,
12+
bool @readonly = false,
13+
bool multiSelect = false,
1014
bool checkFileExists = false)
1115
{
1216
throw new System.NotImplementedException();

src/Generator.Client.CommandLine/Dependencies/ServiceLocatorInitializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-

1+
// Copyright 2020 Andreas Müller
2+
// This file is a part of Amusoft.VisualStudio.TemplateGenerator and is licensed under Apache 2.0
3+
// See https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator/blob/master/LICENSE for details
4+
25
using Generator.Shared.DependencyInjection;
36
using Microsoft.Extensions.DependencyInjection;
47

src/Generator.Client.CommandLine/Dependencies/UIService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-

1+
// Copyright 2020 Andreas Müller
2+
// This file is a part of Amusoft.VisualStudio.TemplateGenerator and is licensed under Apache 2.0
3+
// See https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator/blob/master/LICENSE for details
4+
25
using System;
36
using System.Threading.Tasks;
47
using Generator.Shared.DependencyInjection;

src/Generator.Client.CommandLine/Generator.Client.CommandLine.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
<Company />
1010
<Product>Amusoft.VisualStudio.TemplateGenerator.CommandLine</Product>
1111
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
12-
<ToolCommandName>amusoft-vs-templategenerator</ToolCommandName>
12+
<ToolCommandName>amusoft.vs.tg</ToolCommandName>
1313
<Copyright>2020</Copyright>
1414
<RepositoryType>git</RepositoryType>
1515
<PackageTags>visual studio template generator</PackageTags>
1616
<RepositoryUrl>https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator.git</RepositoryUrl>
1717
<PackageProjectUrl>https://github.com/taori/Amusoft.VisualStudio.TemplateGenerator.git</PackageProjectUrl>
18-
<VersionPrefix>1.1.0</VersionPrefix>
18+
<VersionPrefix>2.0.0</VersionPrefix>
1919
</PropertyGroup>
2020

2121
<ItemGroup>
@@ -29,9 +29,13 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="CommandDotNet" Version="2.7.5" />
32+
<PackageReference Include="CommandDotNet" Version="2.8.2" />
3333
<PackageReference Include="JetBrains.Annotations" Version="2018.2.1" />
3434
<PackageReference Include="NLog" Version="4.6.8" />
35+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
36+
<PrivateAssets>all</PrivateAssets>
37+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
38+
</PackageReference>
3539
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
3640
</ItemGroup>
3741

src/Generator.Client.CommandLine/Program.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Generator.Client.CommandLine
1313
{
14-
class Program
14+
public class Program
1515
{
16-
static int Main(string[] args)
16+
private static int Main(string[] args)
1717
{
1818
try
1919
{
@@ -28,13 +28,16 @@ static int Main(string[] args)
2828
Console.WriteLine("Waiting for user input.");
2929
input = Console.ReadLine();
3030
if (string.IsNullOrEmpty(input))
31+
{
3132
return 0;
33+
}
3234

3335
Console.Clear();
34-
var userArgs = input.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);
36+
var userArgs = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
3537
var code = RunApplication(userArgs);
3638
Console.WriteLine(code);
37-
} while (input != "exit");
39+
}
40+
while (input != "exit");
3841

3942
return 0;
4043
}

0 commit comments

Comments
 (0)