Skip to content

Commit

Permalink
Moved classes to IO folder (#510)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Petrochuk (from Dev Box) <anpetroc@microsoft.com>
  • Loading branch information
petrochuk and anpetroc committed Jan 21, 2024
1 parent f0610c6 commit 452fb0d
Show file tree
Hide file tree
Showing 34 changed files with 159 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/PAModel/CanvasDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using Microsoft.AppMagic.Authoring.Persistence;
using Microsoft.PowerPlatform.Formulas.Tools.ControlTemplates;
using Microsoft.PowerPlatform.Formulas.Tools.EditorState;
using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.Schemas;
using Microsoft.PowerPlatform.Formulas.Tools.Schemas.PcfControl;
using Microsoft.PowerPlatform.Formulas.Tools.SourceTransforms;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using System.IO;
using System.Linq;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Diagnostics;
using System.Linq;

namespace Microsoft.PowerPlatform.Formulas.Tools.Utility;
namespace Microsoft.PowerPlatform.Formulas.Tools.IO;

/// <summary>
/// This represents a path to a control in a control tree
Expand Down
117 changes: 117 additions & 0 deletions src/PAModel/IO/DirectoryReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.IO;
using System.Linq;
using System.Text.Json;
using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.Yaml;

namespace Microsoft.PowerPlatform.Formulas.Tools.IO;

/// <summary>
/// Abstraction over file system.
/// Helps organize full path, relative paths
/// </summary>
internal class DirectoryReader
{
private readonly string _directory;

public DirectoryReader(string directory)
{
_directory = directory;
}

// A file that can be read.
public class Entry
{
private readonly string _fullpath;
public FileKind Kind;
internal string _relativeName;

public Entry(string fullPath)
{
_fullpath = fullPath;
}

public SourceLocation SourceSpan => SourceLocation.FromFile(_fullpath);

// FileEntry is the same structure we get back from a Zip file.
public FileEntry ToFileEntry()
{
// Some paths mistakenly start with DirectorySepChar in the msapp,
// We replaced it with `_/` when writing, remove that now.
if (_relativeName.StartsWith(FileEntry.FilenameLeadingUnderscore.ToString()))
_relativeName = _relativeName.TrimStart(FileEntry.FilenameLeadingUnderscore);
return new FileEntry
{
Name = FilePath.FromPlatformPath(_relativeName),
RawBytes = File.ReadAllBytes(_fullpath)
};
}

public T ToObject<T>()
{
if (Utilities.IsYamlFile(_fullpath))
{
using (var textReader = new StreamReader(_fullpath))
{
var obj = YamlPocoSerializer.Read<T>(textReader);
return obj;
}
}
else
{
var str = File.ReadAllText(_fullpath);
return JsonSerializer.Deserialize<T>(str, Utilities._jsonOpts);
}
}

public string GetContents()
{
return File.ReadAllText(_fullpath);
}
}

// Returns file entries.
public Entry[] EnumerateFiles(string subdir, string pattern = "*", bool searchSubdirectories = true)
{
var root = Path.Combine(_directory, subdir);

if (!Directory.Exists(root))
{
return new Entry[0];
}

var fullPaths = Directory.EnumerateFiles(root, pattern, searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

var entries = from fullPath in fullPaths
let relativePath = Utilities.GetRelativePath(root, fullPath)
select new Entry(fullPath)
{
_relativeName = relativePath,
Kind = FileEntry.TriageKind(FilePath.FromPlatformPath(relativePath))
};

return entries.ToArray();
}

// Returns subdirectories.
public DirectoryReader[] EnumerateDirectories(string subdir, string pattern = "*", bool searchSubdirectories = false)
{
var root = Path.Combine(_directory, subdir);

if (!Directory.Exists(root))
{
return new DirectoryReader[0];
}

var fullPaths = Directory.EnumerateDirectories(root, pattern, searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

var entries = from fullPath in fullPaths
let relativePath = Utilities.GetRelativePath(root, fullPath)
select new DirectoryReader(fullPath);

return entries.ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using Microsoft.PowerPlatform.Formulas.Tools.Yaml;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Xml.Linq;
using Microsoft.PowerPlatform.Formulas.Tools.Yaml;

namespace Microsoft.PowerPlatform.Formulas.Tools;
namespace Microsoft.PowerPlatform.Formulas.Tools.IO;

// Abstraction over file system.
// Helps organize full path, relative paths
internal class DirectoryWriter
/// <summary>
/// Abstraction over file system.
/// Helps organize full path, relative paths
/// </summary>
public class DirectoryWriter
{
private readonly string _directory;

Expand Down Expand Up @@ -167,108 +167,3 @@ private bool ValidateSafeToDelete(ErrorContainer errors)
return true;
}
}

// Abstraction over file system.
// Helps organize full path, relative paths
internal class DirectoryReader
{
private readonly string _directory;

public DirectoryReader(string directory)
{
_directory = directory;
}

// A file that can be read.
public class Entry
{
private readonly string _fullpath;
public FileKind Kind;
internal string _relativeName;

public Entry(string fullPath)
{
_fullpath = fullPath;
}

public SourceLocation SourceSpan => SourceLocation.FromFile(_fullpath);

// FileEntry is the same structure we get back from a Zip file.
public FileEntry ToFileEntry()
{
// Some paths mistakenly start with DirectorySepChar in the msapp,
// We replaced it with `_/` when writing, remove that now.
if (_relativeName.StartsWith(FileEntry.FilenameLeadingUnderscore.ToString()))
_relativeName = _relativeName.TrimStart(FileEntry.FilenameLeadingUnderscore);
return new FileEntry
{
Name = FilePath.FromPlatformPath(_relativeName),
RawBytes = File.ReadAllBytes(_fullpath)
};
}

public T ToObject<T>()
{
if (Utilities.IsYamlFile(_fullpath))
{
using (var textReader = new StreamReader(_fullpath))
{
var obj = YamlPocoSerializer.Read<T>(textReader);
return obj;
}
}
else
{
var str = File.ReadAllText(_fullpath);
return JsonSerializer.Deserialize<T>(str, Utilities._jsonOpts);
}
}

public string GetContents()
{
return File.ReadAllText(_fullpath);
}
}

// Returns file entries.
public Entry[] EnumerateFiles(string subdir, string pattern = "*", bool searchSubdirectories = true)
{
var root = Path.Combine(_directory, subdir);

if (!Directory.Exists(root))
{
return new Entry[0];
}

var fullPaths = Directory.EnumerateFiles(root, pattern, searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

var entries = from fullPath in fullPaths
let relativePath = Utilities.GetRelativePath(root, fullPath)
select new Entry(fullPath)
{
_relativeName = relativePath,
Kind = FileEntry.TriageKind(FilePath.FromPlatformPath(relativePath))
};

return entries.ToArray();
}

// Returns subdirectories.
public DirectoryReader[] EnumerateDirectories(string subdir, string pattern = "*", bool searchSubdirectories = false)
{
var root = Path.Combine(_directory, subdir);

if (!Directory.Exists(root))
{
return new DirectoryReader[0];
}

var fullPaths = Directory.EnumerateDirectories(root, pattern, searchSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

var entries = from fullPath in fullPaths
let relativePath = Utilities.GetRelativePath(root, fullPath)
select new DirectoryReader(fullPath);

return entries.ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using System.IO;
using System.Linq;

namespace Microsoft.PowerPlatform.Formulas.Tools.Utility;
namespace Microsoft.PowerPlatform.Formulas.Tools.IO;

[DebuggerDisplay("{ToPlatformPath()}")]
internal class FilePath
public class FilePath
{
public const int MaxFileNameLength = 60;
private const string yamlExtension = ".fx.yaml";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.IO;

namespace Microsoft.PowerPlatform.Formulas.Tools;
namespace Microsoft.PowerPlatform.Formulas.Tools.IO;

/// <summary>
/// Return a full path for a temporary file, and delete it at Dispose.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.PowerPlatform.Formulas.Tools.Utility;
namespace Microsoft.PowerPlatform.Formulas.Tools.IR;

internal interface ICloneable<T>
{
Expand Down
1 change: 0 additions & 1 deletion src/PAModel/IR/IRNode.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using System.Diagnostics;
using System.Linq;

Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/ControlDiffVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.EditorState;
using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using System.Linq;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool;
Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/Deltas/AddControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.EditorState;
using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;

Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/Deltas/ChangeComponentFunction.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.Schemas;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using System.Linq;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;
Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/Deltas/ChangeProperty.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.IR;
using Microsoft.PowerPlatform.Formulas.Tools.Schemas;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using System.Linq;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;
Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/Deltas/RemoveControl.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using Microsoft.PowerPlatform.Formulas.Tools.IO;
using System.Linq;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;
Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/Deltas/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using Microsoft.PowerPlatform.Formulas.Tools.Schemas;
using System.Linq;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;
using System.Diagnostics.Contracts;
using Microsoft.PowerPlatform.Formulas.Tools.IO;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;

Expand Down
2 changes: 1 addition & 1 deletion src/PAModel/MergeTool/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.AppMagic.Authoring.Persistence;
using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;
using Microsoft.PowerPlatform.Formulas.Tools.Utility;

namespace Microsoft.PowerPlatform.Formulas.Tools.MergeTool;

Expand Down
1 change: 1 addition & 0 deletions src/PAModel/MsAppTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerPlatform.Formulas.Tools.IO;
using Microsoft.PowerPlatform.Formulas.Tools.MergeTool;
using Microsoft.PowerPlatform.Formulas.Tools.MergeTool.Deltas;
using System.IO;
Expand Down
Loading

0 comments on commit 452fb0d

Please sign in to comment.