-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RevEng: Canonicalizing output paths #3959
Changes from 1 commit
a1f6e50
7e7a205
c698808
515cc95
ac135ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,8 +52,11 @@ public virtual Task<ReverseEngineerFiles> GenerateAsync( | |
configuration.CheckValidity(); | ||
|
||
var metadataModel = GetMetadataModel(configuration); | ||
|
||
var outputPaths = ConstructCanonicalizedPaths(configuration.ProjectPath, configuration.OutputPath); | ||
|
||
var @namespace = ConstructNamespace(configuration.ProjectRootNamespace, | ||
configuration.ProjectPath, configuration.OutputPath); | ||
outputPaths.CanonicalizedRelativeOutputPath); | ||
|
||
var customConfiguration = _configurationFactory | ||
.CreateCustomConfiguration( | ||
|
@@ -67,16 +70,12 @@ public virtual Task<ReverseEngineerFiles> GenerateAsync( | |
? modelConfiguration.ClassName() | ||
: customConfiguration.ContextClassName; | ||
|
||
var outputPath = string.IsNullOrEmpty(configuration.OutputPath) | ||
? configuration.ProjectPath | ||
: (Path.IsPathRooted(configuration.OutputPath) | ||
? configuration.OutputPath | ||
: Path.Combine(configuration.ProjectPath, configuration.OutputPath)); | ||
|
||
CheckOutputFiles(outputPath, dbContextClassName, metadataModel, configuration.OverwriteFiles); | ||
CheckOutputFiles(outputPaths.CanonicalizedFullOutputPath, | ||
dbContextClassName, metadataModel, configuration.OverwriteFiles); | ||
|
||
return CodeWriter.WriteCodeAsync( | ||
modelConfiguration, outputPath, dbContextClassName, cancellationToken); | ||
modelConfiguration, outputPaths.CanonicalizedFullOutputPath, | ||
dbContextClassName, cancellationToken); | ||
} | ||
|
||
public virtual IModel GetMetadataModel([NotNull] ReverseEngineeringConfiguration configuration) | ||
|
@@ -134,18 +133,41 @@ public virtual void CheckOutputFiles( | |
private static char[] directorySeparatorChars = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }; | ||
|
||
public static string ConstructNamespace( | ||
[NotNull] string rootNamespace, [NotNull] string projectPath, [CanBeNull] string outputPath) | ||
[NotNull] string rootNamespace, [CanBeNull] string canonicalizedRelativeOutputPath) | ||
{ | ||
Check.NotEmpty(rootNamespace, nameof(rootNamespace)); | ||
Check.NotEmpty(projectPath, nameof(projectPath)); | ||
|
||
if (string.IsNullOrEmpty(outputPath) | ||
|| Path.IsPathRooted(outputPath)) | ||
if (string.IsNullOrEmpty(canonicalizedRelativeOutputPath)) | ||
{ | ||
// outputPath is empty or is not relative - so just use root namespace | ||
// canonicalized output path is outside of or is the same | ||
// as the project dir - so just use root namespace | ||
return rootNamespace; | ||
} | ||
|
||
var @namespace = rootNamespace; | ||
foreach (var pathPart in canonicalizedRelativeOutputPath | ||
.Split(directorySeparatorChars, StringSplitOptions.RemoveEmptyEntries)) | ||
{ | ||
@namespace += "." + CSharpUtilities.Instance.GenerateCSharpIdentifier(pathPart, null); | ||
} | ||
|
||
return @namespace; | ||
} | ||
|
||
/// <summary> | ||
/// Construct canonicalized paths from the project path and output path. | ||
/// </summary> | ||
/// <param name="projectPath"> path to the project, must not be empty, can be absolute or relative </param> | ||
/// <param name="outputPath"> path to output directory, can be null or empty, can be absolute or relative (to the project path) </param> | ||
/// <returns> | ||
/// a <see cref="CanonicalizedOutputPaths"> object containing the canonicalized full output path | ||
/// and the canonicalized relative output path | ||
/// </returns> | ||
public static CanonicalizedOutputPaths ConstructCanonicalizedPaths( | ||
[NotNull] string projectPath, [CanBeNull] string outputPath) | ||
{ | ||
Check.NotEmpty(projectPath, nameof(projectPath)); | ||
|
||
// strip off any directory separator chars at end of project path | ||
for (var projectPathLastChar = projectPath.Last(); | ||
directorySeparatorChars.Contains(projectPathLastChar); | ||
|
@@ -154,24 +176,40 @@ public static string ConstructNamespace( | |
projectPath = projectPath.Substring(0, projectPath.Length - 1); | ||
} | ||
|
||
var canonicalizedProjectPath = Path.GetFullPath(projectPath); | ||
var canonicalizedOutputPath = Path.GetFullPath(Path.Combine(projectPath, outputPath)); | ||
if (!canonicalizedOutputPath.StartsWith(canonicalizedProjectPath)) | ||
{ | ||
// canonicalizedOutputPath is outside of project - so just use root namespace | ||
return rootNamespace; | ||
} | ||
var canonicalizedFullProjectPath = Path.GetFullPath(projectPath); | ||
var canonicalizedFullOutputPath = | ||
string.IsNullOrEmpty(outputPath) | ||
? canonicalizedFullProjectPath | ||
: Path.IsPathRooted(outputPath) | ||
? Path.GetFullPath(outputPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checked and you're right. Will fix. |
||
: Path.GetFullPath(Path.Combine(projectPath, outputPath)); | ||
|
||
var canonicalizedRelativeOutputPath = | ||
canonicalizedFullOutputPath == canonicalizedFullProjectPath | ||
? string.Empty | ||
: canonicalizedFullOutputPath.StartsWith(canonicalizedFullProjectPath) | ||
? canonicalizedFullOutputPath | ||
.Substring(canonicalizedFullProjectPath.Count() + 1) | ||
: null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not completely sure if this is the same, but this looks almost the same as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried |
||
|
||
return new CanonicalizedOutputPaths( | ||
canonicalizedFullOutputPath, canonicalizedRelativeOutputPath); | ||
} | ||
|
||
var @namespace = rootNamespace; | ||
var canonicalizedRelativePath = canonicalizedOutputPath | ||
.Substring(canonicalizedProjectPath.Count() + 1); | ||
foreach (var pathPart in canonicalizedRelativePath | ||
.Split(directorySeparatorChars, StringSplitOptions.RemoveEmptyEntries)) | ||
public class CanonicalizedOutputPaths | ||
{ | ||
public CanonicalizedOutputPaths( | ||
[NotNull] string canonicalizedFullOutputPath, | ||
[CanBeNull] string canonicalizedRelativeOutputPath) | ||
{ | ||
@namespace += "." + CSharpUtilities.Instance.GenerateCSharpIdentifier(pathPart, null); | ||
Check.NotEmpty(canonicalizedFullOutputPath, nameof(canonicalizedFullOutputPath)); | ||
|
||
CanonicalizedFullOutputPath = canonicalizedFullOutputPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why repeat the words of the name of the class in each property? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change them. |
||
CanonicalizedRelativeOutputPath = canonicalizedRelativeOutputPath; | ||
} | ||
|
||
return @namespace; | ||
public virtual string CanonicalizedFullOutputPath { get; } | ||
public virtual string CanonicalizedRelativeOutputPath { get; } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this check intentionally removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope - well spotted. Will put back in.