Skip to content

Commit

Permalink
'-o' command now defaults to overwrite input file instead of printing…
Browse files Browse the repository at this point in the history
… result to std-out
  • Loading branch information
ChrisDeadman committed May 14, 2016
1 parent 5cb9597 commit 9f958ce
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 50 deletions.
2 changes: 1 addition & 1 deletion KSPPartRemover.Tests/Integration/MunMkICraftTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void CanRemoveBoostersAttachedToNoseConeFromCraftFile ()
File.WriteAllText (tempFileName, inputCraftText);

// when
var returnCode = Program.Main ("remove-parts", "--part", "noseCone_4294253786", "-i", tempFileName, "-o", tempFileName, "--silent");
var returnCode = Program.Main ("remove-parts", "--part", "noseCone_4294253786", "-i", tempFileName, "--silent");

// then
Assert.That (returnCode, Is.EqualTo (0));
Expand Down
47 changes: 24 additions & 23 deletions KSPPartRemover.Tests/Integration/ProgramTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace KSPPartRemover.Tests.Integration
{
public class ProgramTest
{
private static readonly String TestFilePath = "test.sfs";
private static readonly StringBuilder StdOutput = new StringBuilder ();
private static readonly StringWriter StdOutputWriter = new StringWriter (StdOutput);

Expand Down Expand Up @@ -38,8 +39,8 @@ public void PrintsInfoHeaderIfSilentSwitchIsNotOn ()
var inputText = KspObjToString (inputCrafts);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("list-crafts", "-i", "input.txt");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("list-crafts", "-i", TestFilePath);

// then
Assert.That (StdOutput.ToString (), Is.StringStarting ("KSPPartRemover v"));
Expand Down Expand Up @@ -108,11 +109,11 @@ public void CanRemovePartByIdAndOutputResult ()
var expectedResult = KspObjToString (expectedCraft);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("remove-parts", "-p", "0", "-i", "input.txt", "-s");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("remove-parts", "-p", "0", "-i", TestFilePath, "-s");

// then
Assert.That (StdOutput.ToString (), Is.EqualTo (expectedResult));
Assert.That (File.ReadAllText (TestFilePath), Is.EqualTo (expectedResult));
Assert.That (returnCode, Is.EqualTo (0));
}

Expand All @@ -134,11 +135,11 @@ public void CanRemovePartByNameAndOutputResult ()
var expectedResult = KspObjToString (expectedCraft);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("remove-parts", "-p", "fuelTank", "-i", "input.txt", "-s");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("remove-parts", "-p", "fuelTank", "-i", TestFilePath, "-s");

// then
Assert.That (StdOutput.ToString (), Is.EqualTo (expectedResult));
Assert.That (File.ReadAllText (TestFilePath), Is.EqualTo (expectedResult));
Assert.That (returnCode, Is.EqualTo (0));
}

Expand Down Expand Up @@ -176,11 +177,11 @@ public void CanRemovePartsOfMultipleCraftsAndOutputResult ()
var expectedResult = KspObjToString (expectedCrafts);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("remove-parts", "-p", "fuelTank", "-c", "!craft2", "-i", "input.txt", "-s");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("remove-parts", "-p", "fuelTank", "-c", "!craft2", "-i", TestFilePath, "-o", TestFilePath, "-s");

// then
Assert.That (StdOutput.ToString (), Is.EqualTo (expectedResult));
Assert.That (File.ReadAllText (TestFilePath), Is.EqualTo (expectedResult));
Assert.That (returnCode, Is.EqualTo (0));
}

Expand All @@ -198,8 +199,8 @@ public void CanPrintCraftList ()
var expectedResult = "someCraft" + Environment.NewLine + "anotherCraft" + Environment.NewLine;

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("list-crafts", "-c", ".*Craft", "-i", "input.txt");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("list-crafts", "-c", ".*Craft", "-i", TestFilePath);

// then
Assert.That (StdOutput.ToString (), Is.StringEnding (expectedResult));
Expand Down Expand Up @@ -232,8 +233,8 @@ public void CanPrintPartList ()
"\t[1]fuelTank" + Environment.NewLine;

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("list-parts", "-p", "[s,f].*", "-c", ".*Craft", "-i", "input.txt");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("list-parts", "-p", "[s,f].*", "-c", ".*Craft", "-i", TestFilePath);

// then
Assert.That (StdOutput.ToString (), Is.StringEnding (expectedResult));
Expand Down Expand Up @@ -274,8 +275,8 @@ public void CanPrintPartDependencies ()
"\t\t[1]fuelTank2[attN(bottom)]" + Environment.NewLine;

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("list-partdeps", "-p", ".*uelTank.*", "-c", ".*Craft", "-i", "input.txt");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("list-partdeps", "-p", ".*uelTank.*", "-c", ".*Craft", "-i", TestFilePath);

// then
Assert.That (StdOutput.ToString (), Is.StringEnding (expectedResult));
Expand All @@ -294,8 +295,8 @@ public void PrintsAndReturnsErrorIfPartIdToRemoveIsNotFound ()
var inputText = KspObjToString (inputCraft);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("remove-parts", "-p", "2", "-i", "input.txt", "-s");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("remove-parts", "-p", "2", "-i", TestFilePath, "-s");

// then
Assert.That (StdOutput.ToString (), Is.EqualTo ("ERROR: No parts removed\n"));
Expand All @@ -314,8 +315,8 @@ public void PrintsAndReturnsErrorIfPartNameToRemoveIsNotFound ()
var inputText = KspObjToString (inputCraft);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("remove-parts", "-p", "nonExistingPart", "-i", "input.txt", "-s");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("remove-parts", "-p", "nonExistingPart", "-i", TestFilePath, "-s");

// then
Assert.That (StdOutput.ToString (), Is.EqualTo ("ERROR: No parts removed\n"));
Expand All @@ -333,8 +334,8 @@ public void PrintsAndReturnsErrorIfNoCraftWithMatchingCraftNameIsFound ()
var inputText = KspObjToString (inputCrafts);

// when
File.WriteAllText ("input.txt", inputText);
var returnCode = Program.Main ("remove-parts", "-p", "somePart", "--craft", "nonExistingCraft", "-i", "input.txt", "-s");
File.WriteAllText (TestFilePath, inputText);
var returnCode = Program.Main ("remove-parts", "-p", "somePart", "--craft", "nonExistingCraft", "-i", TestFilePath, "-s");

// then
Assert.That (StdOutput.ToString (), Is.EqualTo ("No craft matching 'nonExistingCraft' found, aborting" + Environment.NewLine));
Expand Down
2 changes: 1 addition & 1 deletion KSPPartRemover.Tests/Integration/SaveFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void CanRemoveEnginesFromAllBowserCrafts ()
File.WriteAllText (tempFileName, inputCraftText);

// when
var returnCode = Program.Main ("remove-parts", "--part", "liquidEngine.*", "--craft", "Bowser.*", "-i", tempFileName, "-o", tempFileName, "--silent");
var returnCode = Program.Main ("remove-parts", "--part", "liquidEngine.*", "--craft", "Bowser.*", "-i", tempFileName, "--silent");

// then
Assert.That (returnCode, Is.EqualTo (0));
Expand Down
2 changes: 1 addition & 1 deletion KSPPartRemover.Tests/Integration/SpaceStationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SpaceStationTest
File.WriteAllText (tempFileName, inputCraftText);

// when
var returnCode = Program.Main ("remove-parts", "--part", "ladder1", "-i", tempFileName, "-o", tempFileName, "--silent");
var returnCode = Program.Main ("remove-parts", "--part", "ladder1", "-i", tempFileName, "--silent");

// then
Assert.That (returnCode, Is.EqualTo (0));
Expand Down
4 changes: 2 additions & 2 deletions KSPPartRemover/Command/Help.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public int Execute ()
var sb = new StringBuilder ();
sb.Append ("usage: ");
sb.Append (Path.GetFileName (assemblyName.CodeBase));
sb.AppendLine (" <command> [<args>] -i <input-file> [-o <output-file>]");
sb.AppendLine (" <command> [<switches>] -i <input-file>");
sb.AppendLine ();
sb.AppendLine ("Commands:");
sb.AppendLine ();
Expand All @@ -44,7 +44,7 @@ public int Execute ()
sb.AppendLine ();
sb.AppendLine ("\t[Optional]");
sb.AppendLine ("\t -o <path>");
sb.AppendLine ("\t\t specifies the output file (prints to stdout if not specified)");
sb.AppendLine ("\t\t specifies the output file (modifies input file if not specified)");
sb.AppendLine ();
sb.AppendLine ("\t[Optional]");
sb.AppendLine ("\t -c, --craft <name-pattern>");
Expand Down
6 changes: 4 additions & 2 deletions KSPPartRemover/Command/ListCrafts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ public int Execute (Parameters parameters)
{
var allCrafts = CraftLoader.LoadFromFile (parameters.InputFilePath);
ui.DisplayUserMessage ($"Searching for crafts matching '{parameters.CraftFilter}'...");
var filteredCrafts = parameters.CraftFilter.Apply (allCrafts, craft => craft.Name);
var filteredCrafts = parameters.CraftFilter.Apply (allCrafts, craft => craft.Name).ToList ();

ui.DisplayCraftList (filteredCrafts);
if (filteredCrafts.Count > 0) {
ui.DisplayCraftList (filteredCrafts);
}

return 0;
}
Expand Down
6 changes: 4 additions & 2 deletions KSPPartRemover/Command/ListPartDeps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public int Execute (Parameters parameters)
ui.DisplayUserMessage ($"Searching for parts with dependencies to '{parameters.PartFilter}'...");
var partDependencies = filteredCrafts.ToDictionary (craft => craft, craft => FindPartDependencies (craft, parameters.PartFilter));

ui.DisplayPartDependencyList (partDependencies);
if (partDependencies.Any (entry => entry.Value.Count > 0)) {
ui.DisplayPartDependencyList (partDependencies);
}

return 0;
}

private IReadOnlyDictionary<KspPartObject, List<KspPartLinkProperty>> FindPartDependencies (KspCraftObject craft, RegexFilter filter)
private Dictionary<KspPartObject, List<KspPartLinkProperty>> FindPartDependencies (KspCraftObject craft, RegexFilter filter)
{
ui.DisplayUserMessage ($"Entering craft '{craft.Name}'...");
var partLookup = new PartLookup (craft);
Expand Down
4 changes: 3 additions & 1 deletion KSPPartRemover/Command/ListParts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public int Execute (Parameters parameters)
ui.DisplayUserMessage ($"Searching for parts matching '{parameters.PartFilter}'...");
var filteredParts = filteredCrafts.ToDictionary (craft => craft, craft => FindParts (craft, parameters.PartFilter));

ui.DisplayPartList (filteredParts);
if (filteredParts.Any (entry => entry.Value.Count > 0)) {
ui.DisplayPartList (filteredParts);
}

return 0;
}
Expand Down
10 changes: 6 additions & 4 deletions KSPPartRemover/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public static int Main (params String[] args)
{
var parameters = new Parameters () {
CraftFilter = new RegexFilter (""),
PartFilter = new RegexFilter (""),
OutputTextWriter = Console.Out
PartFilter = new RegexFilter ("")
};
var ui = new ProgramUI (parameters);

Expand Down Expand Up @@ -48,8 +47,8 @@ public static int Main (params String[] args)

var commandLineParser = new CommandLineParser ()
.RequiredArgument (0, parseCommand)
.RequiredSwitchArg<String> ("-i", fileName => parameters.InputText = ReadInputFile (fileName))
.OptionalSwitchArg<String> ("-o", fileName => parameters.OutputTextWriter = OpenOutputFile (fileName))
.RequiredSwitchArg<String> ("-i", filePath => parameters.InputFilePath = filePath)
.OptionalSwitchArg<String> ("-o", filePath => parameters.OutputFilePath = filePath)
.OptionalSwitchArg<String> ("-c", pattern => parameters.CraftFilter = new RegexFilter (pattern))
.OptionalSwitchArg<String> ("--craft", pattern => parameters.CraftFilter = new RegexFilter (pattern))
.OptionalSwitchArg<String> ("-p", pattern => parameters.PartFilter = new RegexFilter (pattern))
Expand All @@ -60,6 +59,9 @@ public static int Main (params String[] args)

try {
commandLineParser.Parse (args);
if (parameters.OutputFilePath == null) {
parameters.OutputFilePath = parameters.InputFilePath;
}

if (errors.Count > 0) {
printInfoHeader.Execute ();
Expand Down
6 changes: 3 additions & 3 deletions KSPPartRemover/ProgramUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public ProgramUI (Parameters parameters)
this.parameters = parameters;
}

public void DisplayCraftList (IEnumerable<KspCraftObject> crafts)
public void DisplayCraftList (List<KspCraftObject> crafts)
{
PrintList ("Crafts", crafts.Select (CraftObjectToString));
}

public void DisplayPartList (IReadOnlyDictionary<KspCraftObject, List<KspPartObject>> craftParts)
public void DisplayPartList (Dictionary<KspCraftObject, List<KspPartObject>> craftParts)
{
var listEntries = new List<String> ();

Expand All @@ -34,7 +34,7 @@ public void DisplayPartList (IReadOnlyDictionary<KspCraftObject, List<KspPartObj
PrintList ("Parts", listEntries);
}

public void DisplayPartDependencyList (IReadOnlyDictionary<KspCraftObject, IReadOnlyDictionary<KspPartObject, List<KspPartLinkProperty>>> craftPartLinks)
public void DisplayPartDependencyList (Dictionary<KspCraftObject, Dictionary<KspPartObject, List<KspPartLinkProperty>>> craftPartLinks)
{
var listEntries = new List<String> ();

Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Removes parts from Kerbal Space Program crafts
KSPPartRemover v0.3.0.0
Compatible with KSP version: 1.1
usage: KSPPartRemover.exe <command> [<args>] -i <input-file> [-o <output-file>]
usage: KSPPartRemover.exe <command> [<switches>] -i <input-file>
Commands:
Expand All @@ -41,13 +41,13 @@ Switches:
[Optional]
-o <path>
specifies the output file (prints to stdout if not specified)
specifies the output file (modifies input file if not specified)
[Optional]
-c, --craft <name-pattern>
apply craft filter (applies to all crafts if not specified)
'!' in front of the regex performs inverse matching
example for name pattern: --craft '!^Asteroid'
example for name pattern: --craft '^Asteroid'
example for inverse matching: --craft '!Mün'
[Optional]
Expand All @@ -65,13 +65,12 @@ Switches:

## General notes

If no output file is specified, standard output (console) is used.
That way, if used in combination with --silent, it can be used programmatically should you desire.

This tool has been tested with the following types of files so far :
* .craft files generated by KSP 1.1
* .sfs files generated by KSP 1.1
* .txt craft files generated by the DarkMultiplayer Server (a pretty old version, quite a while ago)
* If no output file is specified, the input file is modified.
* This tool supports a **--silent** switch and can be used programmatically should you desire.
* It has been tested with the following types of files so far :
* .craft files generated by KSP 1.1
* .sfs files generated by KSP 1.1
* .txt craft files generated by the DarkMultiplayer Server (a pretty old version, quite a while ago)

Release notes
=======================
Expand Down

0 comments on commit 9f958ce

Please sign in to comment.