Skip to content

Commit a3e0c86

Browse files
committed
Adjust single sample to pull in sample assets using relative paths
1 parent 02a352f commit a3e0c86

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

common/CommunityToolkit.Labs.Shared/Renderers/ToolkitSampleRenderer.xaml.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,19 @@ private async Task LoadData()
197197

198198
/// <summary>
199199
/// Compute path to a code file bundled in the app using type information.
200-
/// Assumes file path in project matches namespace.
200+
/// Assumes path to file within the included assembly folder matches the namespace.
201201
/// </summary>
202202
private static string GetRelativePathToFileWithoutExtension(Type type)
203203
{
204+
// MSBuild uses wildcard to find the files, and the wildcards decide where they end up
205+
// Single experiments use relative paths, the allExperiment head uses absolute paths that grab from all experiments
206+
// The discrepency is accounted for manually.
207+
// Logic here is the exact same that MSBuild uses to find and include the files we need.
208+
var assemblyName = typeof(ToolkitSampleRenderer).Assembly.GetName().Name;
209+
var isAllExperimentHead = assemblyName.StartsWith("CommunityToolkit.Labs.", StringComparison.OrdinalIgnoreCase);
210+
var isProjectTemplateHead = assemblyName.StartsWith("ProjectTemplate");
211+
var isSingleExperimentHead = !isAllExperimentHead && !isProjectTemplateHead;
212+
204213
var simpleAssemblyName = type.Assembly.GetName().Name;
205214
var typeNamespace = type.Namespace;
206215

@@ -213,13 +222,16 @@ private static string GetRelativePathToFileWithoutExtension(Type type)
213222
var sampleName = simpleAssemblyName.Replace(".Sample", "");
214223

215224
var folderPath = typeNamespace.Replace(simpleAssemblyName, "").Trim('.').Replace('.', '/');
216-
217225
if (folderPath.Length != 0)
218-
{
219226
folderPath += "/";
220-
}
221227

222-
return $"SourceAssets/{sampleName}/samples/{simpleAssemblyName}/{folderPath}{type.Name}";
228+
if (isSingleExperimentHead || isProjectTemplateHead)
229+
return $"SourceAssets/{simpleAssemblyName}/{folderPath}{type.Name}";
230+
231+
if (isAllExperimentHead)
232+
return $"SourceAssets/{sampleName}/samples/{simpleAssemblyName}/{folderPath}{type.Name}";
233+
234+
throw new InvalidOperationException("Unable to determine if running in a single or all experiment solution.");
223235
}
224236
}
225237
}

common/Labs.Head.props

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
<Analyzer Include="$(RepositoryDirectory)common\CommunityToolkit.Labs.Core.SourceGenerators\bin\$(Configuration)\netstandard2.0\CommunityToolkit.Labs.Core.SourceGenerators.dll" />
2323
</ItemGroup>
2424

25-
<ItemGroup Condition="$(MSBuildProjectName.Contains('ProjectTemplate')) == 'false'">
25+
<PropertyGroup>
26+
<IsAllExperimentHead Condition="$(MSBuildProjectName.StartsWith('CommunityToolkit.Labs.')) == 'true'">true</IsAllExperimentHead>
27+
<IsProjectTemplateHead Condition="$(MSBuildProjectName.StartsWith('ProjectTemplate')) == 'true'">true</IsProjectTemplateHead>
28+
<IsSingleExperimentHead Condition="$(IsAllExperimentHead) == 'false' AND $(IsProjectTemplateHead) == 'false'">true</IsSingleExperimentHead>
29+
</PropertyGroup>
30+
31+
<ItemGroup Condition="$(IsAllExperimentHead) == 'true'">
2632
<!-- These are also included in Labs.Samples.props, but added here to workaround https://github.com/unoplatform/uno/issues/2502 -->
2733
<Content Include="$(RepositoryDirectory)labs\**\samples\*.Sample\**\*.md" Exclude="$(RepositoryDirectory)**\**\samples\*.Sample\obj\**\*.md;$(RepositoryDirectory)**\**\samples\*.Sample\bin\**\*.md;$(RepositoryDirectory)\**\SourceAssets\**\*.md" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>
2834
<Content Include="$(RepositoryDirectory)labs\**\samples\*.Sample\**\*.xaml" Exclude="$(RepositoryDirectory)**\**\samples\*.Sample\obj\**\*.xaml;$(RepositoryDirectory)**\**\samples\*.Sample\bin\**\*.xaml;$(RepositoryDirectory)\**\SourceAssets\**\*.xaml" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>
@@ -34,7 +40,19 @@
3440
<AdditionalFiles Include="$(RepositoryDirectory)labs\**\samples\*.Sample\**\*.md" Exclude="$(RepositoryDirectory)**\**\samples\**\obj\**\*.md;$(RepositoryDirectory)**\**\samples\**\bin\**\*.md"/>
3541
</ItemGroup>
3642

37-
<ItemGroup Condition="$(MSBuildProjectName.Contains('ProjectTemplate')) == 'true'">
43+
<ItemGroup Condition="$(IsSingleExperimentHead) == 'true'">
44+
<!-- These are also included in Labs.Samples.props, but added here to workaround https://github.com/unoplatform/uno/issues/2502 -->
45+
<Content Include="..\*.Sample\**\*.md" Exclude="..\*.Sample\obj\**\*.md;..\*.Sample\bin\**\*.md;..\**\SourceAssets\**\*.md" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>
46+
<Content Include="..\*.Sample\**\*.xaml" Exclude="..\*.Sample\obj\**\*.xaml;..\*.Sample\bin\**\*.xaml;..\**\SourceAssets\**\*.xaml" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>
47+
48+
<!-- Link/.dat is a workaround for https://github.com/unoplatform/uno/issues/8649 -->
49+
<Content Include="..\*.Sample\**\*.cs" Exclude="..\*.Sample\obj\**\*.cs;..\*.Sample\bin\**\*.cs" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension).dat" />
50+
51+
<!-- Include markdown files from all samples so the head can access them in the source generator -->
52+
<AdditionalFiles Include="..\*.Sample\**\*.md" Exclude="..\**\obj\**\*.md;..\**\bin\**\*.md"/>
53+
</ItemGroup>
54+
55+
<ItemGroup Condition="$(IsProjectTemplateHead) == 'true'">
3856
<!-- These are also included in Labs.Samples.props, but added here to workaround https://github.com/unoplatform/uno/issues/2502 -->
3957
<Content Include="$(RepositoryDirectory)template\**\samples\*.Sample\**\*.md" Exclude="$(RepositoryDirectory)**\**\samples\*.Sample\obj\**\*.md;$(RepositoryDirectory)**\**\samples\*.Sample\bin\**\*.md;$(RepositoryDirectory)\**\SourceAssets\**\*.md" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>
4058
<Content Include="$(RepositoryDirectory)template\**\samples\*.Sample\**\*.xaml" Exclude="$(RepositoryDirectory)**\**\samples\*.Sample\obj\**\*.xaml;$(RepositoryDirectory)**\**\samples\*.Sample\bin\**\*.xaml;$(RepositoryDirectory)\**\SourceAssets\**\*.xaml" Link="SourceAssets/%(RecursiveDir)%(FileName)%(Extension)"/>

0 commit comments

Comments
 (0)