-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
78 changed files
with
1,186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
|
||
using Newtonsoft.Json; | ||
using ScriptCaster.Services; | ||
|
||
namespace ScriptCaster.app | ||
{ | ||
/* | ||
Cast shall read the template, the local variable file and the global variable file | ||
variable files are json dictionarie <string,string>, every occurence of the key shall be replace by the value | ||
some default key also exist: | ||
%DATE% : DateTime.Now | ||
%DATEUTC% : DateTime.UtcNow | ||
%NEWGUID% | ||
Nice to have : | ||
Logical variable : use reflexion to execute a C# function define in other variable script | ||
Bash variable : set variable as the result of a bash command | ||
*/ | ||
public static class Cast | ||
{ | ||
static int depth = 0; | ||
|
||
public static void LaunchCast() { | ||
var variables = JsonConvert.DeserializeObject<Dictionary<string, string>>( | ||
File.ReadAllText(Context.Instance.GlobalVariablePath)); | ||
|
||
variables?.AddRangeOverride(JsonConvert.DeserializeObject<Dictionary<string, string>>( | ||
File.ReadAllText(Context.Instance.TemplateVariablePath))); | ||
|
||
CreateFolders(variables); | ||
var filesInTemplate = GetAllFiles(); | ||
|
||
foreach(var tFilePath in filesInTemplate) { | ||
|
||
var rFilePath = tFilePath | ||
.Replace(Context.Instance.TemplatePath, Context.Instance.LocalPath) | ||
.Replace("\n", ""); | ||
|
||
if(File.Exists(rFilePath)) { | ||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
Console.WriteLine($"{rFilePath} already exist. Ignored. Soon(~ish) force option will be add."); | ||
Console.ResetColor(); | ||
continue; | ||
} | ||
|
||
var fileContent = File.ReadAllText(tFilePath); | ||
|
||
if(fileContent.Length > 0) { | ||
Console.WriteLine("There is something in file content"); | ||
} else { | ||
Console.WriteLine("File content empty"); | ||
} | ||
|
||
|
||
for(int i = 0; i < Context.Instance.Recursivity; i++) { | ||
foreach(var kv in variables) { | ||
rFilePath = rFilePath.Replace(kv.Key, kv.Value); | ||
fileContent = fileContent.Replace(kv.Key, kv.Value); | ||
} | ||
} | ||
|
||
if(fileContent.Length > 0) { | ||
Console.WriteLine("There is something in file content after mapping"); | ||
} else { | ||
Console.WriteLine("File content empty after mapping"); | ||
} | ||
|
||
using (var stream = File.CreateText(rFilePath)){ | ||
stream.Write(fileContent); | ||
stream.Close(); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
private static void CreateFolders(Dictionary<string, string> variables) { | ||
var foldersInTemplate = GetAllFolders(); | ||
|
||
foreach(var tFolder in foldersInTemplate) { | ||
var rFolder = tFolder | ||
.Replace(Context.Instance.TemplatePath, Context.Instance.LocalPath) | ||
.Replace("\n", ""); | ||
|
||
for(int i = 0; i < Context.Instance.Recursivity; i++) { | ||
foreach(var kv in variables) { | ||
rFolder = rFolder.Replace(kv.Key, kv.Value); | ||
} | ||
} | ||
|
||
Directory.CreateDirectory(rFolder); | ||
} | ||
} | ||
|
||
private static string[] GetAllFolders() { | ||
return GetFoldersFromParent(Context.Instance.TemplatePath); | ||
} | ||
|
||
private static string[] GetFoldersFromParent(string directory) { | ||
var list = new List<string>(); | ||
var foldersInDir = Process.GetAllFilesAndFolders(directory); | ||
|
||
foreach(var folderName in foldersInDir) { | ||
if(Directory.Exists($"{directory}/{folderName}")) { | ||
list.Add($"{directory}/{folderName}"); | ||
list.AddRange(GetFoldersFromParent($"{directory}/{folderName}")); | ||
} | ||
} | ||
|
||
return list.ToArray(); | ||
} | ||
|
||
//There is a way to merge GetAllFolder and GetAllFile | ||
//It would be a lot better optimization wize | ||
//But also harder to read | ||
|
||
private static string[] GetAllFiles() { | ||
return GetFilesFromParent(Context.Instance.TemplatePath); | ||
} | ||
|
||
private static string[] GetFilesFromParent(string directory) { | ||
var list = new List<string>(); | ||
var filesInDir = Process.GetAllFilesAndFolders(directory); | ||
|
||
foreach(var fileName in filesInDir) { | ||
if(!Directory.Exists($"{directory}/{fileName}")) { | ||
list.Add($"{directory}/{fileName}"); | ||
} else { | ||
list.AddRange(GetFilesFromParent($"{directory}/{fileName}")); | ||
} | ||
} | ||
|
||
return list.ToArray(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
|
||
namespace ScriptCaster.app | ||
{ | ||
|
||
public class Context | ||
{ | ||
|
||
#region Singleton | ||
private static Context instance = null; | ||
|
||
private Context() | ||
{ | ||
} | ||
|
||
public static Context Instance | ||
{ | ||
get | ||
{ | ||
if (instance == null) | ||
{ | ||
instance = new Context(); | ||
} | ||
return instance; | ||
} | ||
} | ||
#endregion | ||
|
||
|
||
public string TemplateName { get; private set; } | ||
public string TemplatePath { get; private set; } | ||
public string TemplateVariablePath { get; private set; } | ||
public string LocalPath { get; private set; } | ||
public string GlobalVariablePath { get; private set; } | ||
public int Recursivity {get; private set; } | ||
|
||
//TODO: the template path shall be a config in ~/.config/ScriptCatser/config | ||
//TODO: Recursivity shall be a config or an option | ||
public void InitContext(string templateName) { | ||
TemplateName = templateName; | ||
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
TemplatePath = $"{home}/Templates/{templateName}"; | ||
TemplateVariablePath = $"{TemplatePath}/.variables.json"; | ||
|
||
LocalPath = ScriptCaster.Services.Process.GetPwd(); | ||
|
||
GlobalVariablePath = $"{home}/Templates/.variables.json"; | ||
|
||
Recursivity = 3; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ScriptCaster.app | ||
{ | ||
public static class CollectionExtention | ||
{ | ||
public static void AddRangeOverride<TKey, TValue>(this IDictionary<TKey, TValue> dic, IDictionary<TKey, TValue> dicToAdd) | ||
{ | ||
dicToAdd.ForEach(x => dic[x.Key] = x.Value); | ||
} | ||
|
||
public static void AddRangeNewOnly<TKey, TValue>(this IDictionary<TKey, TValue> dic, IDictionary<TKey, TValue> dicToAdd) | ||
{ | ||
dicToAdd.ForEach(x => { if (!dic.ContainsKey(x.Key)) dic.Add(x.Key, x.Value); }); | ||
} | ||
|
||
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> dic, IDictionary<TKey, TValue> dicToAdd) | ||
{ | ||
dicToAdd.ForEach(x => dic.Add(x.Key, x.Value)); | ||
} | ||
|
||
public static bool ContainsKeys<TKey, TValue>(this IDictionary<TKey, TValue> dic, IEnumerable<TKey> keys) | ||
{ | ||
bool result = false; | ||
keys.ForEachOrBreak((x) => { result = dic.ContainsKey(x); return result; }); | ||
return result; | ||
} | ||
|
||
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) | ||
{ | ||
foreach (var item in source) | ||
action(item); | ||
} | ||
|
||
public static void ForEachOrBreak<T>(this IEnumerable<T> source, Func<T, bool> func) | ||
{ | ||
foreach (var item in source) | ||
{ | ||
bool result = func(item); | ||
if (result) break; | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
| ||
using ScriptCaster.app; | ||
|
||
class Program { | ||
|
||
static void Main(string[] args) { | ||
Console.WriteLine("Welcome into Script Caster :)"); | ||
|
||
if(args.Length < 1) { | ||
Console.WriteLine(" You need to tell me which template I shall use :( "); | ||
return; | ||
} | ||
|
||
var templateName = args[0]; | ||
Context.Instance.InitContext(templateName); | ||
|
||
Cast.LaunchCast(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ScriptCaster.services\ScriptCaster.services.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
namespace ScriptCaster.app | ||
{ | ||
|
||
/* | ||
* Set variable class shall alow me to edit the .variables.json files | ||
* Depending on the option, either editing the global variables or the variable of the template | ||
*/ | ||
public class SetVariable | ||
{ | ||
|
||
} | ||
} |
Binary file not shown.
Binary file not shown.
52 changes: 52 additions & 0 deletions
52
ScriptCaster.app/bin/Debug/net6.0/ScriptCaster.app.deps.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"runtimeTarget": { | ||
"name": ".NETCoreApp,Version=v6.0", | ||
"signature": "" | ||
}, | ||
"compilationOptions": {}, | ||
"targets": { | ||
".NETCoreApp,Version=v6.0": { | ||
"ScriptCaster.app/1.0.0": { | ||
"dependencies": { | ||
"Newtonsoft.Json": "13.0.1", | ||
"ScriptCaster.services": "1.0.0" | ||
}, | ||
"runtime": { | ||
"ScriptCaster.app.dll": {} | ||
} | ||
}, | ||
"Newtonsoft.Json/13.0.1": { | ||
"runtime": { | ||
"lib/netstandard2.0/Newtonsoft.Json.dll": { | ||
"assemblyVersion": "13.0.0.0", | ||
"fileVersion": "13.0.1.25517" | ||
} | ||
} | ||
}, | ||
"ScriptCaster.services/1.0.0": { | ||
"runtime": { | ||
"ScriptCaster.services.dll": {} | ||
} | ||
} | ||
} | ||
}, | ||
"libraries": { | ||
"ScriptCaster.app/1.0.0": { | ||
"type": "project", | ||
"serviceable": false, | ||
"sha512": "" | ||
}, | ||
"Newtonsoft.Json/13.0.1": { | ||
"type": "package", | ||
"serviceable": true, | ||
"sha512": "sha512-ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A==", | ||
"path": "newtonsoft.json/13.0.1", | ||
"hashPath": "newtonsoft.json.13.0.1.nupkg.sha512" | ||
}, | ||
"ScriptCaster.services/1.0.0": { | ||
"type": "project", | ||
"serviceable": false, | ||
"sha512": "" | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions
9
ScriptCaster.app/bin/Debug/net6.0/ScriptCaster.app.runtimeconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"runtimeOptions": { | ||
"tfm": "net6.0", | ||
"framework": { | ||
"name": "Microsoft.NETCore.App", | ||
"version": "6.0.0" | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.