Skip to content

Commit

Permalink
everything is broken currently, but moving computers :)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmcyber committed Oct 22, 2023
1 parent b6cca56 commit a4005c9
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 169 deletions.
File renamed without changes.
22 changes: 22 additions & 0 deletions ImportPSSconfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Management.Automation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using PSSimpleConfig.Utilities;

namespace PSSimpleConfig.Cmdlets;

[Cmdlet(VerbsData.Import, "PSSConfig")]
public class ImportPSSConfig : PSCmdlet
{
[Parameter(Mandatory = true)]
[Alias("Project")]
public string Name { get; set; }

protected override void ProcessRecord()
{
JObject config = PSSimpleConfig.ImportConfig(Name);
SessionState.PSVariable.Set("PSSimpleConfig", JsonConversion.ToPSOutput(config));
WriteObject(SessionState.PSVariable.Get("PSSimpleConfig"));
}
}
12 changes: 12 additions & 0 deletions Output/PSSimpleConfig/PSSimpleConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ModuleCfgRoot": "C:\\Users\\u55398\\Documents\\git\\PSSimpleConfig\\Output\\PSSimpleConfig",
"ModuleCfgFile": "C:\\Users\\u55398\\Documents\\git\\PSSimpleConfig\\Output\\PSSimpleConfig\\PSSimpleConfig.json",
"ProjectCfgRoot": "C:\\ProgramData\\PSSimpleConfig\\Projects",
"Projects": [
{
"id": "6a2c1dd2-c06f-4427-a072-2e8fe230ff0a",
"name": "TestProject",
"root": "C:\\Users\\u55398\\Documents\\git\\PSSimpleConfig\\Output\\PSSimpleConfig\\TestProject"
}
]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 2 additions & 11 deletions src/Cmdlets/ImportPSSconfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@

namespace PSSimpleConfig.Cmdlets;

[Cmdlet(VerbsData.Import, "PSSConfig")]
public class ImportPSSConfig : PSCmdlet
[Cmdlet(VerbsData.Import, "PSSConfig", SupportsShouldProcess = false, ConfirmImpact = ConfirmImpact.None)]
public class ImportPSSConfig
{
[Parameter(Mandatory = true)]
[Alias("Project")]
public string Name { get; set; }

protected override void ProcessRecord()
{
JObject config = PSSimpleConfig.ImportConfig(Name);
SessionState.PSVariable.Set("PSSimpleConfig", JsonConversion.ToPSOutput(config));
WriteObject(SessionState.PSVariable.Get("PSSimpleConfig"));
}
}
126 changes: 29 additions & 97 deletions src/Cmdlets/NewPSSConfig.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using System;
using System.IO;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Management.Automation;
using Newtonsoft.Json;
using System.ServiceModel.Security.Tokens;
using Newtonsoft.Json.Linq;
using PSSimpleConfig.Utilities;

namespace PSSimpleConfig.Cmdlets;

[Cmdlet(VerbsCommon.New, "PSSConfig", SupportsShouldProcess = false, ConfirmImpact = ConfirmImpact.None)]
public class NewPSSConfig : PSCmdlet
{
[Parameter(Mandatory = false, Position = 1)]
[Parameter(Mandatory = true, Position = 1)]
[ValidateNotNullOrEmpty()]
[Alias("Project")]
public string? Name { get; set; }

Expand All @@ -23,110 +20,45 @@ public class NewPSSConfig : PSCmdlet
[Parameter(Mandatory = false)]
public SwitchParameter Force { get; set; }

[Parameter(Mandatory = false)]
public System.IO.DirectoryInfo? Path { get; set; }

[Parameter(Mandatory = false)]
public SwitchParameter FromPSModule { get; set; }

protected override void BeginProcessing()
{

PSSC.InitializeModule(this);
}
protected override void ProcessRecord()
{
PSSimpleConfig.SetScope(Scope);
WriteDebug($"PSSimpleConfig.Scope: {PSSimpleConfig.Scope}");
WriteDebug($"PSSimpleConfig.Root: {PSSimpleConfig.Root}");
WriteDebug($"PSSimpleConfig.ProjectRoot: {PSSimpleConfig.ProjectRoot}");
WriteDebug($"PSSCCfgRoot: {PSSC.Instance.ModuleData["PSSCCfgRoot"]}");
WriteDebug($"PSSCCfgFile: {PSSC.Instance.ModuleData["PSSCCfgFile"]}");
WriteDebug($"ProjectRoot: {PSSC.Instance.ModuleData["ProjectRoot"]}");

// Need to check if there's already a project with the same name.

// Create a PowerShell instance for retrieving module information
using PowerShell ps = PowerShell.Create();
Guid projectGuid = Guid.NewGuid();

try
bool v = Path == null;
#pragma warning disable CS8618 // Nullability warning. We're checking nulls above.
string projectPath = v ? System.IO.Path.Combine(PSSC.Instance.ModuleData["ProjectRoot"].ToString(), Name) : Path.FullName;
#pragma warning restore CS8618 // Nullability warning.

// Check if the project directory exists. If it does, check if we're forcing overwrite.
if (System.IO.Directory.Exists(projectPath))
{
// Create the root (PSSimpleConfig) directory if it doesn't exist
if (!Directory.Exists(PSSimpleConfig.Root))
{
WriteDebug($"PSSimpleConfig root directory not found. Creating directory: {PSSimpleConfig.Root}");
WriteVerbose($"Creating PSSimpleConfig Root directory: {PSSimpleConfig.Root}");
Directory.CreateDirectory(PSSimpleConfig.Root);
}
// Create the projects directory if it doesn't exist
// root/projects
if (!Directory.Exists(PSSimpleConfig.ProjectRoot))
if (Force)
{
WriteDebug($"PSSimpleConfig projects directory not found. Creating directory: {PSSimpleConfig.ProjectRoot}");
WriteVerbose($"Creating project directory: {PSSimpleConfig.ProjectRoot}");
Directory.CreateDirectory(PSSimpleConfig.ProjectRoot);
WriteWarning($"Project directory {projectPath} already exists. Forcing overwrite.");
System.IO.Directory.Delete(projectPath, true);
System.IO.Directory.CreateDirectory(projectPath);
}
// Create the project directory if it doesn't exist
// root/projects/{Name}
if (!string.IsNullOrEmpty(Name))
else
{
string projectFolder = Path.Combine(PSSimpleConfig.ProjectRoot, Name);
bool _createProject = false;
if (!Directory.Exists(projectFolder))
{
_createProject = true;
}
else
{
if (Force == false)
{
WriteWarning($"Project {Name} already exists. Use -Force to overwrite.");
}
else
{
WriteDebug($"Project {Name} exists and -Force is set. Deleting existing project directory: {projectFolder}");
WriteVerbose($"Deleting existing project directory: {projectFolder}");
Directory.Delete(projectFolder, true);
_createProject = true;
}
}
if (_createProject)
{
WriteVerbose($"Creating project directory: {projectFolder}");
Directory.CreateDirectory(projectFolder);

PSModuleInfo _PSSCMod = this.MyInvocation.MyCommand.Module;

// Create a Dictionary to hold the initial config.json values.
Dictionary<string, object> _configDict = new Dictionary<string, object>();
Dictionary<string, object> _psscInfo = new Dictionary<string, object>
{
// Add the PSSimpleConfig module information to the _psscInfo Dictionary
{ "Version", _PSSCMod.Version.ToString() },
{ "ProjectName", Name },
{ "ConfigScope", Scope }
};
_configDict.Add("PSSC", _psscInfo);

// Use Get-Module to retrive some basic information about
// the module and add it to the configObject.
if (FromPSModule)
{
ps.AddCommand("Get-Module").AddParameter("Name", Name).AddParameter("ListAvailable");
Collection<PSObject> module = ps.Invoke();

_configDict.Add("Version", module[0].Properties["Version"].Value.ToString());
_configDict.Add("ModuleRoot", module[0].Properties["ModuleBase"].Value);
}

// Serialize Dictionary to JSON
string configJson = JsonConvert.SerializeObject(_configDict, Formatting.Indented);

File.WriteAllText(Path.Combine(projectFolder, "config.json"), configJson.ToString());
//WriteObject(_configDict);
PSVariableIntrinsics _sessionState = SessionState.PSVariable;
ps.AddCommand("Set-Variable").AddParameter("Name", "PSSC").AddParameter("Scope", "Script").AddParameter("Value", _configDict);
ps.Invoke();
WriteObject(_configDict);

}
WriteError(new ErrorRecord(new System.IO.IOException($"Project directory {projectPath} already exists. Use -Force to overwrite."), "ProjectExists", ErrorCategory.ResourceExists, projectPath));
return;
}
}
catch (Exception e)
{
ErrorRecord errorRecord = new(e, $"Could not create directory structure for {Name}.", ErrorCategory.InvalidOperation, null);
ThrowTerminatingError(errorRecord);
}
}
}
28 changes: 28 additions & 0 deletions src/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace PSSimpleConfig;

public class Project
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Root { get; set; }

public Project(string name, string root)
{
Id = Guid.NewGuid();
Name = name;
Root = root;
}

public Project(Guid id, string name, string root)
{
Id = id;
Name = name;
Root = root;
}

public override string ToString()
{
return $"{Name} ({Id})";
}

}
Loading

0 comments on commit a4005c9

Please sign in to comment.