Skip to content

Commit

Permalink
Merge pull request #197 from atifaziz/consolidate-schemes-search
Browse files Browse the repository at this point in the history
Consolidate schemes path search code
  • Loading branch information
miniksa authored Jun 6, 2018
2 parents 11a65ef + aa20e89 commit da53ff9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 69 deletions.
35 changes: 2 additions & 33 deletions tools/ColorTool/ColorTool/IniSchemeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using static ColorTool.ConsoleAPI;
Expand Down Expand Up @@ -70,41 +71,9 @@ static uint ParseColor(string arg)
}
}

// TODO: Abstract the locating of a scheme into a function the implementation can call into
// Both parsers duplicate the searching, they should just pass in their extension and
// a callback for initally validating the file
static string FindIniScheme(string schemeName)
{
string exeDir = System.IO.Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).FullName;
string filename = schemeName + ".ini";
string exeSchemes = exeDir + "/schemes/";
string cwd = "./";
string cwdSchemes = "./schemes/";
// Search order, for argument "name", where 'exe' is the dir of the exe.
// 1. ./name
// 2. ./name.ini
// 3. ./schemes/name
// 4. ./schemes/name.ini
// 5. exe/schemes/name
// 6. exe/schemes/name.ini
// 7. name (as an absolute path)
string[] paths = {
cwd + schemeName,
cwd + filename,
cwdSchemes + schemeName,
cwdSchemes + filename,
exeSchemes + schemeName,
exeSchemes + filename,
schemeName,
};
foreach (string path in paths)
{
if (File.Exists(path))
{
return path;
}
}
return null;
return Scheme.GetSearchPaths(schemeName, ".ini").FirstOrDefault(File.Exists);
}

public ColorScheme ParseScheme(string schemeName, bool reportErrors = true)
Expand Down
41 changes: 41 additions & 0 deletions tools/ColorTool/ColorTool/Scheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the terms described in the LICENSE file in the root of this project.
//

using System.Collections.Generic;
using System.IO;

namespace ColorTool
{
static class Scheme
{
public static IEnumerable<string> GetSearchPaths(string schemeName, string extension)
{
// Search order, for argument "name", where 'exe' is the dir of the exe.
// 1. ./name
// 2. ./name.ext
// 3. ./schemes/name
// 4. ./schemes/name.ext
// 5. exe/schemes/name
// 6. exe/schemes/name.ext
// 7. name (as an absolute path)

string cwd = "./";
yield return cwd + schemeName;

string filename = schemeName + extension;
yield return cwd + filename;

string cwdSchemes = "./schemes/";
yield return cwdSchemes + schemeName;
yield return cwdSchemes + filename;

string exeDir = Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).FullName;
string exeSchemes = exeDir + "/schemes/";
yield return exeSchemes + schemeName;
yield return exeSchemes + filename;
yield return schemeName;
}
}
}
44 changes: 8 additions & 36 deletions tools/ColorTool/ColorTool/XmlSchemeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Xml;
Expand Down Expand Up @@ -81,49 +82,20 @@ static bool parseRgbFromXml(XmlNode components, ref uint rgb)
static XmlDocument loadXmlScheme(string schemeName)
{
XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object
string exeDir = System.IO.Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).FullName;
bool found = false;
string filename = schemeName + ".itermcolors";
string exeSchemes = exeDir + "/schemes/";
string cwd = "./";
string cwdSchemes = "./schemes/";
// Search order, for argument "name", where 'exe' is the dir of the exe.
// 1. ./name
// 2. ./name.itermcolors
// 3. ./schemes/name
// 4. ./schemes/name.itermcolors
// 5. exe/schemes/name
// 6. exe/schemes/name.itermcolors
// 7. name (as an absolute path)
string[] paths = {
cwd + schemeName,
cwd + filename,
cwdSchemes + schemeName,
cwdSchemes + filename,
exeSchemes + schemeName,
exeSchemes + filename,
schemeName,
};
foreach (string path in paths)
foreach (string path in Scheme.GetSearchPaths(schemeName, ".itermcolors")
.Where(File.Exists))
{
try
{
xmlDoc.Load(path);
found = true;
break;
}
catch (Exception /*e*/)
{
// We can either fail to find the file,
// or fail to parse the XML here.
return xmlDoc;
}
catch (XmlException /*e*/) { /* failed to parse */ }
catch (IOException /*e*/) { /* failed to find */ }
catch (UnauthorizedAccessException /*e*/) { /* unauthorized */ }
}

if (!found)
{
return null;
}
return xmlDoc;
return null;
}


Expand Down

0 comments on commit da53ff9

Please sign in to comment.