Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@
* File: \Program.cs
* Created: Friday, 18th July 2025 7:00:41 pm
* -----
* Last Modified: Saturday, 19th July 2025 12:42:46 am
* Last Modified: Sunday, 20th July 2025 3:33:56 am
* Modified By: tutosrive ([email protected])
* -----
*/

using InitVenv.src.App.models;
using InitVenv.src.App.Utils;

namespace InitVenv
{
class App
{
public static void Main(string[] args)
{
string? os = OS.GetOS();
Console.WriteLine(os);
WindowsCommands win1 = WindowsCommands.LoadConfigs<WindowsCommands>("Windows");
string str = win1.ToString();

WindowsCommands readed = Files.ReadJSON<WindowsCommands>("./src/App/configs/commands/Windows.jsonc");
Console.WriteLine(readed.ActivateVenv);
Console.WriteLine(str);
}
}
}
27 changes: 22 additions & 5 deletions src/App/Utils/Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* File: \Files.cs
* Created: Friday, 18th July 2025 7:00:41 pm
* -----
* Last Modified: Saturday, 19th July 2025 12:43:47 am
* Last Modified: Sunday, 20th July 2025 3:30:57 am
* Modified By: tutosrive ([email protected])
* -----
*/
Expand All @@ -19,24 +19,42 @@ namespace InitVenv.src.App.Utils
{
public static class Files
{
/// <summary>
/// Check if any file exists in the OS
/// </summary>
/// <param name="arg">The file to check</param>
/// <returns>A bool that indicates if exists or not</returns>
public static bool Exists(string arg)
{
string fullPath = Paths.ToUniversalPaths(arg);
return Path.Exists(fullPath);
}

/// <summary>
/// Try "read" JSON files and return its content (Needs a class to understand the JSON format)
/// </summary>
/// <typeparam name="T">The "interface" to understand the file format (its keys, values, types...)</typeparam>
/// <param name="pathFile">The path to the file (Absolute path)</param>
/// <returns>If not have errors, return the file content (The interface <strong>T</strong>)</returns>
/// <exception cref="FileNotFoundException">This function check if the file exists</exception>
/// <exception cref="JsonException">When the file contains an invalid format</exception>
public static T ReadJSON<T>(string pathFile)
{
string content;
T? commands;
Console.WriteLine(string.Format("Tipo de \"T\": {0}", typeof(T)));

// Get absolute "Universal path"
pathFile = Paths.AbsoluteUniversalPath(pathFile);

Console.WriteLine($"PathFile => | {pathFile} |");

// Check file exist before read
if (Exists(pathFile))
{
using StreamReader reader = new(pathFile);
try
{
// Try read and "Deserialize" the content
using StreamReader reader = new(pathFile);
content = reader.ReadToEnd();
commands = JsonSerializer.Deserialize<T>(content);

Expand All @@ -54,8 +72,7 @@ public static T ReadJSON<T>(string pathFile)
throw new FileNotFoundException(string.Format("File \"{0}\" not found", pathFile));
}

throw new JsonException("Was not possible deserialize the file");

throw new JsonException(string.Format("Was not possible deserialize the file \"{0}\"", pathFile));
}
}
}
8 changes: 6 additions & 2 deletions src/App/Utils/OS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
* File: \OS.cs
* Created: Friday, 18th July 2025 7:00:41 pm
* -----
* Last Modified: Friday, 18th July 2025 11:31:43 pm
* Last Modified: Sunday, 20th July 2025 12:48:34 am
* Modified By: tutosrive ([email protected])
* -----
*/

namespace InitVenv.src.App.Utils
{
public static class OS
public class OS
{
/// <summary>
/// Get the Operating System "type" (e.g. Windows, Linux, MacOS)
/// </summary>
/// <returns>If the OS is supported (Windows, Linux, MacOS) return its name, else null</returns>
public static string? GetOS()
{
string? os = null;
Expand Down
19 changes: 17 additions & 2 deletions src/App/Utils/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* File: \Paths.cs
* Created: Friday, 18th July 2025 7:00:41 pm
* -----
* Last Modified: Friday, 18th July 2025 11:30:56 pm
* Last Modified: Sunday, 20th July 2025 1:36:00 am
* Modified By: tutosrive ([email protected])
* -----
*/
Expand All @@ -17,10 +17,25 @@ namespace InitVenv.src.App.Utils
{
public static class Paths
{

/// <summary>
/// Format any <strong>path</strong> so it can be used on any OS (On Windows, the paths using backslash)
/// </summary>
/// <param name="path">The "<strong>path</strong>" to format</param>
/// <returns>A string with the formated <strong>path</strong></returns>
public static string ToUniversalPaths(string path)
{
return path.Replace("\\", "/");
}

public static string AbsoluteUniversalPath(string path)
{
try
{
return ToUniversalPaths(Path.GetFullPath(path));
}
catch (Exception) { throw; }

throw new Exception("The absolute path could not be determined");
}
}
}
66 changes: 66 additions & 0 deletions src/App/models/os.commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* models - Initialize a virtual environment automatically
* Copyright 2025 - 2025 Dev2Forge
* Licence: GPL-3
* More information: https://github.com/Dev2Forge/Init-Venv/blob/main/LICENSE
* Author: tutosrive ([email protected])
*
* File: \os.commands.cs
* Created: Sunday, 20th July 2025 12:32:02 am
* -----
* Last Modified: Sunday, 20th July 2025 3:32:49 am
* Modified By: tutosrive ([email protected])
* -----
*/

using InitVenv.src.App.Utils;

namespace InitVenv.src.App.models
{
public class IOsCommands()
{
/// <summary>
/// On windows, command to show the "pip"
/// </summary>
public required string PipPaths { get; set; }
/// <summary>
/// Find out the route of the "python"
/// </summary>
public required string PythonPaths { get; set; }
/// <summary>
/// Command to create new "venv"
/// </summary>
public required string CreateVenv { get; set; }
/// <summary>
/// Is used to activate the venv (usally is ".\venv\Scripts\activate" on Windows)
/// </summary>
public required string ActivateVenv { get; set; }
/// <summary>
/// Usually is "pip -r requirements.txt"
/// </summary>
public required string RequirementsInstall { get; set; }

/// <summary>
/// Load the configs for the OS
/// </summary>
/// <typeparam name="T">The class Type</typeparam>
/// <returns></returns>
public static T LoadConfigs<T>(string OSName) where T : IOsCommands
{
T configs = Files.ReadJSON<T>($"./src/App/configs/commands/{OSName}.jsonc");
return configs;
}

public override string ToString()
{
string response = @$"OS Commands:
Pip Paths: {this.PipPaths}
Python Paths: {this.PythonPaths}
Create Venv: {this.CreateVenv}
Activate Venv: {this.ActivateVenv}
Requirements Install: {this.RequirementsInstall}";
return response;

}
}
}
11 changes: 4 additions & 7 deletions src/App/models/windows.commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
* File: \windows.commands.cs
* Created: Friday, 18th July 2025 11:51:38 pm
* -----
* Last Modified: Saturday, 19th July 2025 12:01:55 am
* Last Modified: Sunday, 20th July 2025 3:23:51 am
* Modified By: tutosrive ([email protected])
* -----
*/

using InitVenv.src.App.Utils;

namespace InitVenv.src.App.models
{
public class WindowsCommands
public class WindowsCommands : IOsCommands
{
public required string PipPaths { get; set; }
public required string PythonPaths { get; set; }
public required string CreateVenv { get; set; }
public required string ActivateVenv { get; set; }
public required string RequirementsInstall { get; set; }
}
}