Skip to content

Commit 4e54dba

Browse files
authored
Merge pull request #10 from MORYX-Industry/feature/add-plain-resource
Add the feature of adding simple, single resources
2 parents dfcd9db + 966a574 commit 4e54dba

File tree

12 files changed

+154
-16
lines changed

12 files changed

+154
-16
lines changed

src/Moryx.Cli.Commands/Add.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static CommandResult Products(AddOptions options)
1717
return AddThing(options, (settings) => AddProducts.Exec(settings, options.Name!.ToCleanList()));
1818
}
1919

20+
public static CommandResult Resources(AddOptions options)
21+
{
22+
return AddThing(options, (settings) => AddResources.Exec(settings, options.Name!.ToCleanList()));
23+
}
24+
2025
public static CommandResult Module(AddOptions options)
2126
{
2227
return AddThing(options, (settings) => AddModule.Exec(settings, options.Name!));

src/Moryx.Cli.Commands/AddModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static CommandResult Exec(TemplateSettings settings, string moduleName)
1616
SolutionName = settings.AppName,
1717
ThingName = moduleName,
1818
Thing = "module",
19-
ThingPlaceholder = Template.Template.ModulePlaceholder,
19+
ThingPlaceholders = [Template.Template.ModulePlaceholder],
2020
},
2121
filenames.Module(),
2222
s => AddProjectsToSolution(settings, s)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Moryx.Cli.Commands.Extensions;
2+
using Moryx.Cli.Template;
3+
using Moryx.Cli.Template.Models;
4+
5+
namespace Moryx.Cli.Commands
6+
{
7+
public static class AddResources
8+
{
9+
public static CommandResult Exec(TemplateSettings settings, IEnumerable<string> resources)
10+
{
11+
return CommandBase.Exec(settings, (filenames)
12+
=> resources.Select(resource => AddThing.Exec(
13+
settings,
14+
new AddConfig
15+
{
16+
SolutionName = settings.AppName,
17+
ThingName = $"{resource}Resource",
18+
Thing = "resource",
19+
ThingPlaceholders =
20+
[
21+
Template.Template.ResourcePlaceholder,
22+
Template.Template.ResourcePlaceholder2
23+
],
24+
},
25+
filenames.Resource()
26+
))
27+
.ToArray()
28+
.Flatten());
29+
}
30+
}
31+
}

src/Moryx.Cli.Commands/AddStep.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static CommandResult Exec(TemplateSettings settings, string step)
1515
SolutionName = settings.AppName,
1616
ThingName = step,
1717
Thing = "step",
18-
ThingPlaceholder = Template.Template.StepPlaceholder,
18+
ThingPlaceholders = [Template.Template.StepPlaceholder],
1919
},
2020
filenames.Step()
2121
));

src/Moryx.Cli.Commands/AddSteps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static CommandResult Exec(TemplateSettings settings, IEnumerable<string>
1616
SolutionName = settings.AppName,
1717
ThingName = step,
1818
Thing = "step",
19-
ThingPlaceholder = Template.Template.StepPlaceholder,
19+
ThingPlaceholders = [Template.Template.StepPlaceholder],
2020
},
2121
filenames.Step()
2222
))

src/Moryx.Cli.Commands/AddThing.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Moryx.Cli.Template;
1+
using Moryx.Cli.Commands.Extensions;
2+
using Moryx.Cli.Template;
23
using Moryx.Cli.Template.Models;
34

45
namespace Moryx.Cli.Commands
@@ -18,15 +19,16 @@ public static CommandResult Exec(TemplateSettings settings, AddConfig config, Li
1819
var files = Template.Template.WriteFilesToDisk(
1920
dictionary,
2021
settings,
21-
s => s.Replace(config.ThingPlaceholder, config.ThingName).Replace(Template.Template.AppPlaceholder, config.SolutionName));
22+
s => s.Replace(config.ThingPlaceholders, config.ThingName).Replace(Template.Template.AppPlaceholder, config.SolutionName));
23+
24+
var replaceHolders = config.ThingPlaceholders
25+
.ToDictionary(s => s, s => config.ThingName);
26+
replaceHolders.Add(Template.Template.AppPlaceholder, config.SolutionName);
2227

2328
Template.Template.ReplacePlaceHoldersInsideFiles(
2429
files,
25-
new Dictionary<string, string>
26-
{
27-
{ Template.Template.AppPlaceholder, config.SolutionName},
28-
{ config.ThingPlaceholder, config.ThingName },
29-
});
30+
replaceHolders);
31+
3032
onAddedFiles?.Invoke(files);
3133
}
3234
catch (Exception ex)
@@ -59,6 +61,6 @@ public class AddConfig
5961
/// <summary>
6062
/// *Thing*s placeholder
6163
/// </summary>
62-
public string ThingPlaceholder { get; set; }
64+
public IEnumerable<string> ThingPlaceholders { get; set; }
6365
}
6466
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Moryx.Cli.Commands.Options;
2+
using Moryx.Cli.Template.Models;
3+
4+
namespace Moryx.Cli.Commands.Extensions
5+
{
6+
public static class StringExtensions
7+
{
8+
public static string Replace(this string s, IEnumerable<string> oldStrings, string newString)
9+
{
10+
foreach (var oldString in oldStrings)
11+
{
12+
s = s.Replace(oldString, newString);
13+
}
14+
return s;
15+
}
16+
}
17+
}

src/Moryx.Cli.Template/Template.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static class Template
1010
public const string ProductPlaceholder = "MyProduct";
1111
public const string ModulePlaceholder = "MyModule";
1212
public const string StepPlaceholder = "Some";
13+
public const string ResourcePlaceholder = "SomeResource";
14+
public const string ResourcePlaceholder2 = "MyResource";
1315

1416
public static List<string> GetCleanedResourceNames(TemplateSettings settings)
1517
{
@@ -33,6 +35,7 @@ public static List<string> BareProjectFiles(this List<string> list)
3335
.WithoutSetupTrigger()
3436
.WithoutCellSelector()
3537
.WithoutModule()
38+
.WithoutResource()
3639
;
3740

3841
public static List<string> WithoutProduct(this List<string> list)
@@ -65,6 +68,12 @@ public static List<string> WithoutModule(this List<string> list)
6568
return list.Except(list.Module()).ToList();
6669
}
6770

71+
public static List<string> WithoutResource(this List<string> list)
72+
{
73+
return list.Except(list.Resource()).ToList();
74+
}
75+
76+
6877
public static List<string> Product(this List<string> list)
6978
{
7079
var whitelist = new List<string>(){
@@ -124,6 +133,17 @@ public static List<string> Recipe(this List<string> list)
124133
.ToList();
125134
}
126135

136+
public static List<string> Resource(this List<string> list)
137+
{
138+
var whitelist = list
139+
.Where(e => e.Contains("ISomeResource") || e.Contains("MyResource"))
140+
.ToList();
141+
142+
return list
143+
.Intersect(whitelist, new ListComparer())
144+
.ToList();
145+
}
146+
127147
public static List<string> SetupTrigger(this List<string> list)
128148
{
129149
var whitelist = new List<string>(){

src/Moryx.Cli/CommandLine/Add.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,41 @@ public override int Execute([NotNull] CommandContext context, [NotNull] AddModul
9696
.ProcessResult();
9797
}
9898
}
99+
100+
[Description("Adds resources to your MORYX solution.")]
101+
internal class AddResources : Command<AddResources.AddResourcesSettings>
102+
{
103+
internal class AddResourcesSettings : AddSettings
104+
{
105+
[Description("Name of the module to be added")]
106+
[CommandArgument(0, "<NAME>")]
107+
public string? Name { get; set; }
108+
109+
[Description("A Git repository url that will be used for the project template.")]
110+
[CommandOption("-t|--template-url")]
111+
public string? Template { get; set; }
112+
113+
[Description("Branch to use with the template repository.")]
114+
[CommandOption("-b|--branch")]
115+
public string? Branch { get; set; }
116+
117+
[Description("Update the template repository.")]
118+
[CommandOption("--pull"), DefaultValue(false)]
119+
public bool Pull { get; set; }
120+
}
121+
122+
public override int Execute([NotNull] CommandContext context, [NotNull] AddResourcesSettings settings)
123+
{
124+
var options = new AddOptions
125+
{
126+
Name = settings.Name,
127+
Branch = settings.Branch,
128+
Template = settings.Template,
129+
Pull = settings.Pull
130+
};
131+
132+
return Commands.Add.Resources(options)
133+
.ProcessResult();
134+
}
135+
}
99136
}

src/Moryx.Cli/CommandLine/CommandLineSetup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static CommandApp Setup(this CommandApp app)
1717
{
1818
add.AddCommand<AddProduct>("product")
1919
.WithExample(new[] { "add", "product", "<NAME>" });
20+
add.AddCommand<AddResources>("resource")
21+
.WithExample(new[] { "add", "resource", "<NAME>" });
2022
add.AddCommand<AddStep>("step")
2123
.WithExample(new[] { "add", "step", "<NAME>" });
2224
add.AddCommand<AddModule>("module")

0 commit comments

Comments
 (0)