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 ;
26using System . Collections . Generic ;
37using System . Linq ;
48using System . Threading ;
711using CommandDotNet . Attributes ;
812using Generator . Shared . Template ;
913using Generator . Shared . Transformation ;
14+ using Newtonsoft . Json ;
1015using NLog ;
1116
1217namespace 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}
0 commit comments