Skip to content
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

Add ExpandingPosition & Release v5.4.0 #85

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v5.3.1] - 2022-09-12
## [v5.4.0] - 2023-10-19
- SourceExpander.Generator: Add ExpandingPosition

## [v5.3.1] - 2023-09-12
### Changed
- SourceExpander.Embedder: Fix `using` in codes expanded by ExpandingSymbol

## [v5.3.0] - 2022-09-12
## [v5.3.0] - 2023-09-12
### Added
- SourceExpander.Embedder: Add ExpandingSymbol config
- Update libraries

## [v5.2.0] - 2022-01-21
## [v5.2.0] - 2023-01-21
### Added
- SourceExpander.Generator: Add IgnoreAssemblies config

Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<PackageProjectUrl>https://github.com/kzrnm/SourceExpander</PackageProjectUrl>
<RepositoryUrl>https://github.com/kzrnm/SourceExpander</RepositoryUrl>
<PackageReleaseNotes>https://github.com/kzrnm/SourceExpander/blob/master/CHANGELOG.md</PackageReleaseNotes>
<Version>5.3.1</Version>
<AssemblyVersion>5.3.1.100</AssemblyVersion>
<Version>5.4.0</Version>
<AssemblyVersion>5.4.0.100</AssemblyVersion>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)key.snk</AssemblyOriginatorKeyFile>
<PackageOutputPath>$(MSBuildThisFileDirectory)bin\Packages\$(Configuration)\</PackageOutputPath>
Expand Down
39 changes: 19 additions & 20 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ README languages:
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**

- [SourceExpander](#sourceexpander)
- [Packages](#packages)
- [SourceExpander(library)](#sourceexpanderlibrary)
- [SourceExpander.Console](#sourceexpanderconsole)
- [SourceExpander.Generator](#sourceexpandergenerator)
- [SourceExpander.Embedder](#sourceexpanderembedder)
- [Status](#status)
- [Getting started](#getting-started)
- [ライブラリ利用者向け](#ライブラリ利用者向け)
- [SourceExpander.Console を使う](#sourceexpanderconsole-を使う)
- [SourceExpander.Generator を使う](#sourceexpandergenerator-を使う)
- [ライブラリ開発者向け](#ライブラリ開発者向け)
- [埋め込みたくない型への対処](#埋め込みたくない型への対処)
- [注釈](#注釈)
- [埋め込まれるデータ](#埋め込まれるデータ)
- [EmbedderVersion](#embedderversion)
- [EmbeddedLanguageVersion](#embeddedlanguageversion)
- [EmbeddedAllowUnsafe](#embeddedallowunsafe)
- [EmbeddedSourceCode](#embeddedsourcecode)
- [EmbeddedSourceCode.GZipBase32768](#embeddedsourcecodegzipbase32768)
- [Packages](#packages)
- [SourceExpander(library)](#sourceexpanderlibrary)
- [SourceExpander.Console](#sourceexpanderconsole)
- [SourceExpander.Generator](#sourceexpandergenerator)
- [SourceExpander.Embedder](#sourceexpanderembedder)
- [Status](#status)
- [Getting started](#getting-started)
- [ライブラリ利用者向け](#%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E5%88%A9%E7%94%A8%E8%80%85%E5%90%91%E3%81%91)
- [SourceExpander.Console を使う](#sourceexpanderconsole-%E3%82%92%E4%BD%BF%E3%81%86)
- [SourceExpander.Generator を使う](#sourceexpandergenerator-%E3%82%92%E4%BD%BF%E3%81%86)
- [ライブラリ開発者向け](#%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E9%96%8B%E7%99%BA%E8%80%85%E5%90%91%E3%81%91)
- [埋め込みたくない型への対処](#%E5%9F%8B%E3%82%81%E8%BE%BC%E3%81%BF%E3%81%9F%E3%81%8F%E3%81%AA%E3%81%84%E5%9E%8B%E3%81%B8%E3%81%AE%E5%AF%BE%E5%87%A6)
- [注釈](#%E6%B3%A8%E9%87%88)
- [埋め込まれるデータ](#%E5%9F%8B%E3%82%81%E8%BE%BC%E3%81%BE%E3%82%8C%E3%82%8B%E3%83%87%E3%83%BC%E3%82%BF)
- [EmbedderVersion](#embedderversion)
- [EmbeddedLanguageVersion](#embeddedlanguageversion)
- [EmbeddedAllowUnsafe](#embeddedallowunsafe)
- [EmbeddedSourceCode](#embeddedsourcecode)
- [EmbeddedSourceCode.GZipBase32768](#embeddedsourcecodegzipbase32768)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ README languages:
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**

- [SourceExpander](#sourceexpander)
- [Packages](#packages)
- [SourceExpander(library)](#sourceexpanderlibrary)
- [SourceExpander.Console](#sourceexpanderconsole)
- [SourceExpander.Generator](#sourceexpandergenerator)
- [SourceExpander.Embedder](#sourceexpanderembedder)
- [Status](#status)
- [Getting started](#getting-started)
- [For library user](#for-library-user)
- [Use SourceExpander.Console](#use-sourceexpanderconsole)
- [Use SourceExpander.Generator](#use-sourceexpandergenerator)
- [For library developer](#for-library-developer)
- [Avoid embedding some type](#avoid-embedding-some-type)
- [Notes](#notes)
- [Embedded data](#embedded-data)
- [EmbedderVersion](#embedderversion)
- [EmbeddedLanguageVersion](#embeddedlanguageversion)
- [EmbeddedAllowUnsafe](#embeddedallowunsafe)
- [EmbeddedSourceCode](#embeddedsourcecode)
- [EmbeddedSourceCode.GZipBase32768](#embeddedsourcecodegzipbase32768)
- [Packages](#packages)
- [SourceExpander(library)](#sourceexpanderlibrary)
- [SourceExpander.Console](#sourceexpanderconsole)
- [SourceExpander.Generator](#sourceexpandergenerator)
- [SourceExpander.Embedder](#sourceexpanderembedder)
- [Status](#status)
- [Getting started](#getting-started)
- [For library user](#for-library-user)
- [Use SourceExpander.Console](#use-sourceexpanderconsole)
- [Use SourceExpander.Generator](#use-sourceexpandergenerator)
- [For library developer](#for-library-developer)
- [Avoid embedding some type](#avoid-embedding-some-type)
- [Notes](#notes)
- [Embedded data](#embedded-data)
- [EmbedderVersion](#embedderversion)
- [EmbeddedLanguageVersion](#embeddedlanguageversion)
- [EmbeddedAllowUnsafe](#embeddedallowunsafe)
- [EmbeddedSourceCode](#embeddedsourcecode)
- [EmbeddedSourceCode.GZipBase32768](#embeddedsourcecodegzipbase32768)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down
4 changes: 3 additions & 1 deletion Source/Sandbox/SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<SourceExpander_Generator_StaticEmbeddingText>// Static Generate &#x9; // Property</SourceExpander_Generator_StaticEmbeddingText>
<SourceExpander_Generator_StaticEmbeddingText>// Static Generate &#x9; // Property</SourceExpander_Generator_StaticEmbeddingText>
<SourceExpander_Generator_ExpandingPosition>AfterUsings</SourceExpander_Generator_ExpandingPosition>
</PropertyGroup>

<ItemGroup>
Expand All @@ -11,6 +12,7 @@
<CompilerVisibleProperty Include="SourceExpander_Generator_MetadataExpandingFile" />
<CompilerVisibleProperty Include="SourceExpander_Generator_IgnoreFilePatternRegex" />
<CompilerVisibleProperty Include="SourceExpander_Generator_StaticEmbeddingText" />
<CompilerVisibleProperty Include="SourceExpander_Generator_ExpandingPosition" />
</ItemGroup>

<ItemGroup>
Expand Down
89 changes: 54 additions & 35 deletions Source/SourceExpander.Generating.Common/CompilationExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,54 +88,73 @@ static IEnumerable<string> TypeNameToNamespaces(string typeName)

cancellationToken.ThrowIfCancellationRequested();


using var sr = new StringReader(newRoot.ToString());
var line = sr.ReadLine();
while (line != null)
switch (Config.ExpandingPosition)
{
sb.AppendLine(line);
line = sr.ReadLine();
case ExpandingPosition.AfterUsings:
Embedded();
Target();
break;
default:
Target();
Embedded();
break;
}

sb.AppendLine("#region Expanded by https://github.com/kzrnm/SourceExpander");
if (!string.IsNullOrEmpty(Config.StaticEmbeddingText))
sb.AppendLine(Config.StaticEmbeddingText);

if (Config.ExpandingByGroup)
return sb.ToString();

void Target()
{
using var sr = new StringReader(newRoot.ToString());
var line = sr.ReadLine();
while (line != null)
{
sb.AppendLine(line);
line = sr.ReadLine();
}
}

void Embedded()
{
var groupedCodes = new Dictionary<string, List<string>>();
foreach (var s in requiedFiles)
sb.AppendLine("#region Expanded by https://github.com/kzrnm/SourceExpander");
if (!string.IsNullOrEmpty(Config.StaticEmbeddingText))
sb.AppendLine(Config.StaticEmbeddingText);

if (Config.ExpandingByGroup)
{
var match = Regex.Match(s.FileName, "^[^>]+");
var assemblyName = match?.Value ?? "<unknown assembly>";
if (!groupedCodes.TryGetValue(assemblyName, out var list))
var groupedCodes = new Dictionary<string, List<string>>();
foreach (var s in requiedFiles)
{
var match = Regex.Match(s.FileName, "^[^>]+");
var assemblyName = match?.Value ?? "<unknown assembly>";
if (!groupedCodes.TryGetValue(assemblyName, out var list))
{
list = groupedCodes[assemblyName] = new List<string>();
}
list.Add(s.CodeBody);
}
foreach (var g in groupedCodes)
{
list = groupedCodes[assemblyName] = new List<string>();
var assemblyName = g.Key;
sb.Append("#region Assembly:").AppendLine(assemblyName);
foreach (var s in g.Value)
sb.AppendLine(s);
sb.Append("#endregion Assembly:").AppendLine(assemblyName);
}
list.Add(s.CodeBody);
}
foreach (var g in groupedCodes)
else
{
var assemblyName = g.Key;
sb.Append("#region Assembly:").AppendLine(assemblyName);
foreach (var s in g.Value)
sb.AppendLine(s);
sb.Append("#endregion Assembly:").AppendLine(assemblyName);
foreach (var s in requiedFiles)
sb.AppendLine(s.CodeBody);
}
}
else
{
foreach (var s in requiedFiles)
sb.AppendLine(s.CodeBody);
}
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();

foreach (var ns in importButUnusedNamespaces)
{
sb.Append("namespace ").Append(ns).AppendLine("{}");
foreach (var ns in importButUnusedNamespaces)
{
sb.Append("namespace ").Append(ns).AppendLine("{}");
}
sb.AppendLine("#endregion Expanded by https://github.com/kzrnm/SourceExpander");
}
sb.AppendLine("#endregion Expanded by https://github.com/kzrnm/SourceExpander");
return sb.ToString();
}

public SourceFileInfo[] ResolveDependency(SyntaxTree origTree, CancellationToken cancellationToken)
Expand Down
26 changes: 23 additions & 3 deletions Source/SourceExpander.Generating.Common/ExpandConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ namespace SourceExpander
/// <param name="StaticEmbeddingText">static text that be added to source code.</param>
/// <param name="MetadataExpandingFile"> file path whose source code is written to metadata</param>
/// <param name="ExpandingByGroup">if true, generator write `#region &lt;AssemblyName&gt;`.</param>
/// <param name="ExpandingPosition">Position of expanded source</param>
public partial record ExpandConfig(
bool Enabled,
ImmutableArray<string> MatchFilePatterns,
ImmutableArray<Regex> IgnoreFilePatterns,
ImmutableArray<string> IgnoreAssemblies,
string? StaticEmbeddingText,
string? MetadataExpandingFile,
bool ExpandingByGroup)
bool ExpandingByGroup,
ExpandingPosition ExpandingPosition)
{
/// <summary>
/// constructor
Expand All @@ -35,7 +37,8 @@ public ExpandConfig(
IEnumerable<Regex>? ignoreFilePatterns = null,
string? staticEmbeddingText = null,
string? metadataExpandingFile = null,
bool? expandingByGroup = null) :
bool? expandingByGroup = null,
ExpandingPosition expandingPosition = ExpandingPosition.EndOfFile) :
this(
Enabled: enabled,
MatchFilePatterns: matchFilePatterns is null
Expand All @@ -49,7 +52,8 @@ public ExpandConfig(
: ImmutableArray.Create(ignoreAssemblies),
StaticEmbeddingText: staticEmbeddingText,
MetadataExpandingFile: metadataExpandingFile,
ExpandingByGroup: expandingByGroup ?? false
ExpandingByGroup: expandingByGroup ?? false,
ExpandingPosition: expandingPosition
)
{ }

Expand All @@ -61,4 +65,20 @@ public bool IsMatch(string filePath)
|| MatchFilePatterns.Any(p => filePath.IndexOf(p, StringComparison.OrdinalIgnoreCase) >= 0))
&& IgnoreFilePatterns.All(regex => !regex.IsMatch(filePath));
}

/// <summary>
/// Position of expanded source
/// </summary>
public enum ExpandingPosition
{
/// <summary>
/// Expanding at EOF
/// </summary>
EndOfFile,

/// <summary>
/// Expanding after usings
/// </summary>
AfterUsings,
}
}
7 changes: 7 additions & 0 deletions Source/SourceExpander.Generator/ExpandConfig.Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static ExpandConfig Parse(string? sourceText, AnalyzerConfigOptions analy
data.StaticEmbeddingText = v;
if (analyzerConfigOptions.TryGetValue(header + "ExpandingByGroup", out v) && !string.IsNullOrWhiteSpace(v))
data.ExpandingByGroup = !StringComparer.OrdinalIgnoreCase.Equals(v, "false");
if (analyzerConfigOptions.TryGetValue(header + "ExpandingPosition", out v) && !string.IsNullOrWhiteSpace(v))
data.ExpandingPosition = v;
}
return data.ToImmutable();
}
Expand All @@ -60,6 +62,10 @@ private class ExpandConfigData
public string? StaticEmbeddingText { set; get; }
[DataMember(Name = "expanding-by-group")]
public bool? ExpandingByGroup { set; get; }
[DataMember(Name = "expanding-position")]
public string? ExpandingPosition { set; get; }
private ExpandingPosition ParsedExpandingPosition
=> Enum.TryParse(ExpandingPosition, true, out ExpandingPosition r) ? r : SourceExpander.ExpandingPosition.EndOfFile;

public ExpandConfig ToImmutable() => new(
enabled: this.Enabled ?? true,
Expand All @@ -68,6 +74,7 @@ private class ExpandConfigData
ignoreFilePatterns: this.IgnoreFilePatternRegex?.Select(s => new Regex(s)) ?? Array.Empty<Regex>(),
staticEmbeddingText: this.StaticEmbeddingText,
metadataExpandingFile: MetadataExpandingFile,
expandingPosition: ParsedExpandingPosition,
expandingByGroup: ExpandingByGroup);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<CompilerVisibleProperty Include="SourceExpander_Generator_MatchFilePattern" />
<CompilerVisibleProperty Include="SourceExpander_Generator_MetadataExpandingFile" />
<CompilerVisibleProperty Include="SourceExpander_Generator_IgnoreFilePatternRegex" />
<CompilerVisibleProperty Include="SourceExpander_Generator_StaticEmbeddingText" />
</ItemGroup>
<CompilerVisibleProperty Include="SourceExpander_Generator_StaticEmbeddingText" />
<CompilerVisibleProperty Include="SourceExpander_Generator_ExpandingPosition" />
</ItemGroup>
</Project>
Loading
Loading